diff --git a/StarMap.Core/ModRepository/ModLoader.cs b/StarMap.Core/ModRepository/ModLoader.cs index caa743c..5b25c60 100644 --- a/StarMap.Core/ModRepository/ModLoader.cs +++ b/StarMap.Core/ModRepository/ModLoader.cs @@ -1,6 +1,7 @@ using KSA; using StarMap.API; using StarMap.Core.Config; +using StarMap.Core.Patches; using System.Reflection; using System.Runtime.Loader; using Tomlet; @@ -44,6 +45,7 @@ public ModLoader(AssemblyLoadContext coreAssemblyLoadContext) public void Init() { + DocumentsPathPatches.Apply(); PrepareMods(); } @@ -60,7 +62,7 @@ private void PrepareMods() { if (!mod.Enabled) { - Console.WriteLine($"StarMap - Not loading mod: {mod.Id} because it is disable in manifest"); + Console.WriteLine($"StarMap - Not loading mod: {mod.Id} because it is disabled in manifest"); continue; } diff --git a/StarMap.Core/Patches/DocumentsPathPatches.cs b/StarMap.Core/Patches/DocumentsPathPatches.cs new file mode 100644 index 0000000..9f59af3 --- /dev/null +++ b/StarMap.Core/Patches/DocumentsPathPatches.cs @@ -0,0 +1,47 @@ +using HarmonyLib; +using KSA; + +namespace StarMap.Core.Patches +{ + internal static class DocumentsPathPatches + { + private const string CliFlag = "-InstancePath"; + private const string EnvVarName = "STARMAP_INSTANCE_PATH"; + private const string HarmonyId = "com.starmap.core.documentspathoverride"; + + public static void Apply() + { + // Check if an override path is provided via command-line argument or environment variable + var overridePath = TryGetOverride(); + if (string.IsNullOrEmpty(overridePath)) + return; + + // Apply the Harmony patch to override the DocumentsFolderPath property + var harmony = new Harmony(HarmonyId); + var original = AccessTools.PropertyGetter(typeof(Constants), nameof(Constants.DocumentsFolderPath)); + var prefix = new HarmonyMethod(typeof(DocumentsPathPatches), nameof(Prefix)); + harmony.Patch(original, prefix: prefix); + Console.WriteLine($"StarMap - Using Instance Path: {overridePath}"); + } + + private static bool Prefix(ref string __result) + { + __result = TryGetOverride()!; + return false; + } + + private static string? TryGetOverride() + { + var args = Environment.GetCommandLineArgs(); + for (int i = 0; i < args.Length - 1; i++) + { + if (string.Equals(args[i], CliFlag, StringComparison.OrdinalIgnoreCase)) + { + return args[i + 1]; + } + } + + return Environment.GetEnvironmentVariable(EnvVarName); + } + } +} \ No newline at end of file diff --git a/StarMap.Core/StarMapCore.cs b/StarMap.Core/StarMapCore.cs index 8cc1411..fe660fc 100644 --- a/StarMap.Core/StarMapCore.cs +++ b/StarMap.Core/StarMapCore.cs @@ -9,7 +9,7 @@ internal class StarMapCore : IStarMapCore { public static StarMapCore? Instance; - private readonly Harmony _harmony = new("StarMap.Core"); + private readonly Harmony _harmony = new("com.starmap.core"); private readonly AssemblyLoadContext _coreAssemblyLoadContext; private readonly ModLoader _loader;