Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions Source/HotSwap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using System.Runtime.CompilerServices;
using RimWorld;
using Verse;
using UnityEngine;

namespace HotSwap
{
Expand All @@ -18,11 +19,23 @@ class HotSwapMain : Mod
public static Dictionary<Assembly, FileInfo> 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<HotSwapSettings>();
}

public override string SettingsCategory()
{
return Content.Name;
}

public override void DoSettingsWindowContents(Rect inRect)
{
settings.DoSettingsWindowContents(inRect);
}

static Dictionary<Assembly, FileInfo> MapModAssemblies()
Expand Down
47 changes: 47 additions & 0 deletions Source/HotSwapGameComponent.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
}
39 changes: 39 additions & 0 deletions Source/HotSwapSettings.cs
Original file line number Diff line number Diff line change
@@ -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();
}
}

}