diff --git a/CommunityMapLocation/Api/ApiServer.cs b/CommunityMapLocation/Api/ApiServer.cs index eabf2b3..154138b 100644 --- a/CommunityMapLocation/Api/ApiServer.cs +++ b/CommunityMapLocation/Api/ApiServer.cs @@ -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() diff --git a/CommunityMapLocation/Config/Settings.cs b/CommunityMapLocation/Config/Settings.cs index 76560f9..48ea3c6 100644 --- a/CommunityMapLocation/Config/Settings.cs +++ b/CommunityMapLocation/Config/Settings.cs @@ -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(ref T field, T value, [CallerMemberName] string propertyName = null) + { + if (!EqualityComparer.Default.Equals(field, value)) + { + field = value; + PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); + } + } } } diff --git a/CommunityMapLocation/Main.cs b/CommunityMapLocation/Main.cs index 464a030..4f29ee3 100644 --- a/CommunityMapLocation/Main.cs +++ b/CommunityMapLocation/Main.cs @@ -47,7 +47,7 @@ namespace CommunityMapLocation { Settings.Save(modEntry); - apiServer.Reload(); + apiServer.UpdateSettings(); } } }