diff --git a/Source/HotSwap.cs b/Source/HotSwap.cs index 6dc02fc..f63091c 100644 --- a/Source/HotSwap.cs +++ b/Source/HotSwap.cs @@ -10,6 +10,7 @@ using System.Runtime.CompilerServices; using RimWorld; using Verse; +using UnityEngine; namespace HotSwap { @@ -18,11 +19,23 @@ class HotSwapMain : Mod public static Dictionary AssemblyFiles; static Harmony harmony = new("HotSwap"); static DateTime startTime = DateTime.Now; + internal static HotSwapSettings settings; public HotSwapMain(ModContentPack content) : base(content) { harmony.PatchAll(); AssemblyFiles = MapModAssemblies(); + settings = GetSettings(); + } + + public override string SettingsCategory() + { + return Content.Name; + } + + public override void DoSettingsWindowContents(Rect inRect) + { + settings.DoSettingsWindowContents(inRect); } static Dictionary MapModAssemblies() diff --git a/Source/HotSwapGameComponent.cs b/Source/HotSwapGameComponent.cs new file mode 100644 index 0000000..149ce17 --- /dev/null +++ b/Source/HotSwapGameComponent.cs @@ -0,0 +1,47 @@ + +using System; +using System.IO; +using Verse; + +namespace HotSwap +{ + public class HotSwapGameComponent : GameComponent + { + private static TimeSpan UpdateFrequency = TimeSpan.FromSeconds(5); + + private DateTime lastUpdate = DateTime.UtcNow; + + public HotSwapGameComponent(Game _) + { + } + + public override void GameComponentUpdate() + { + if (HotSwapMain.settings.EnableAutoHotSwap && (DateTime.UtcNow - lastUpdate) > UpdateFrequency) + { + bool triggerHotswap = false; + foreach (var assemblyFile in HotSwapMain.AssemblyFiles) + { + var hotSwapFile = assemblyFile.Value.FullPath + ".hotswap"; + if (File.Exists(hotSwapFile)) + { + try + { + File.Delete(hotSwapFile); + } + catch (Exception ex) + { + Log.Warning($"Couldn't delete {hotSwapFile}: {ex}"); + } + triggerHotswap = true; + } + } + if (triggerHotswap) + { + HotSwapMain.ScheduleHotSwap(); + } + lastUpdate = DateTime.UtcNow; + } + } + } +} diff --git a/Source/HotSwapSettings.cs b/Source/HotSwapSettings.cs new file mode 100644 index 0000000..43d50e2 --- /dev/null +++ b/Source/HotSwapSettings.cs @@ -0,0 +1,39 @@ +using UnityEngine; +using Verse; + + +namespace HotSwap +{ + public class HotSwapSettings : ModSettings + { + private bool _enableAutoHotSwap = false; + public bool EnableAutoHotSwap + { + get => _enableAutoHotSwap; + } + + public override void ExposeData() + { + base.ExposeData(); + + Scribe_Values.Look(ref _enableAutoHotSwap, "enableAutoHotSwap", false); + } + + internal void DoSettingsWindowContents(Rect inRect) + { + Listing_Standard listingStandard = new(); + listingStandard.Begin(inRect); + + listingStandard.CheckboxLabeled( + "Enable auto-hotswap", + ref _enableAutoHotSwap, + "If this setting is enabled, the mod will regularly look for a file named " + + "[AssemblyName].dll.hotswap next to any hot-swappable [AssemblyName].dll, " + + "and if it finds one, it will trigger a hot-swap, and then delete the " + + "[AssemblyName].dll.hotswap file."); + + listingStandard.End(); + } + } + +}