We don't actually need to restart HttpListener when the user changes the port

Changed Settings to implement INotifyPropertyChanged to allow consumers to potentially listen to individual property changes
This commit is contained in:
TheEvilPenguin committed 2025-06-12 08:49:21 +10:00
1 parent 07229424a4
commit 700e06b898
3 files changed
+35 -9

No files matched your search

+11 -4
View File
@@ -28,6 +28,8 @@ namespace CommunityMapLocation.Api
this.logger = logger;
}
private static string GetPrefix(int port) => $"http://localhost:{port}/";
private readonly Settings settings;
private readonly UnityModManager.ModEntry.ModLogger logger;
@@ -66,7 +68,7 @@ namespace CommunityMapLocation.Api
{
try
{
listener.Prefixes.Add($"http://localhost:{settings.Port}/");
listener.Prefixes.Add(GetPrefix(settings.Port));
listener.Start();
listener.BeginGetContext(HandleConnection, null);
}
@@ -126,10 +128,15 @@ namespace CommunityMapLocation.Api
}
}
internal void Reload()
public void UpdateSettings()
{
Stop();
Start();
var prefix = GetPrefix(settings.Port);
if (listener.Prefixes.Count != 1 || listener.Prefixes.Single() != prefix)
{
listener.Prefixes.Clear();
listener.Prefixes.Add(prefix);
}
}
public void Stop()
+23 -4
View File
@@ -1,15 +1,22 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using UnityEngine;
using UnityModManagerNet;
namespace CommunityMapLocation.Config
{
public class Settings : UnityModManager.ModSettings
public class Settings : UnityModManager.ModSettings, INotifyPropertyChanged
{
public int Port { get; set; } = 9090;
public RequestSourceType RequestSourceType { get; set; } = RequestSourceType.Specific;
public string RequestSource { get; set; } = "https://pyronicampt.github.io";
private int port = 9090;
public int Port { get => port; set => Set(ref port, value); }
private RequestSourceType requestSourceType = RequestSourceType.Specific;
public RequestSourceType RequestSourceType { get => requestSourceType; set => Set(ref requestSourceType, value); }
private string requestSource = "https://pyronicampt.github.io";
public string RequestSource { get => requestSource; set => Set(ref requestSource, value); }
public void Draw()
{
@@ -30,5 +37,17 @@ namespace CommunityMapLocation.Config
GUILayout.EndVertical();
}
public event PropertyChangedEventHandler PropertyChanged;
// Todo: store and hold back property changes until Settings is saved to prevent rapidly reconfiguring server on each keypress?
private void Set<T>(ref T field, T value, [CallerMemberName] string propertyName = null)
{
if (!EqualityComparer<T>.Default.Equals(field, value))
{
field = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
}
}
+1 -1
View File
@@ -47,7 +47,7 @@ namespace CommunityMapLocation
{
Settings.Save(modEntry);
apiServer.Reload();
apiServer.UpdateSettings();
}
}
}