Skip to content
Merged
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
4 changes: 3 additions & 1 deletion StarMap.Core/ModRepository/ModLoader.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -44,6 +45,7 @@ public ModLoader(AssemblyLoadContext coreAssemblyLoadContext)

public void Init()
{
DocumentsPathPatches.Apply();
PrepareMods();
}

Expand All @@ -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;
}

Expand Down
47 changes: 47 additions & 0 deletions StarMap.Core/Patches/DocumentsPathPatches.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
2 changes: 1 addition & 1 deletion StarMap.Core/StarMapCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Loading