Skip to content
Open
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
52 changes: 52 additions & 0 deletions Patches/Features/LoadFoldersPatch.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using System.Text.Json;
using HarmonyLib;
using MegaCrit.Sts2.Core.Modding;
using MegaCrit.Sts2.Core.Platform;

namespace BaseLib.Patches.Features;

[HarmonyPatch(typeof(ModManager), "TryLoadMod")]
public static class LoadFoldersPatch
{
[HarmonyPrefix]
private static void Prefix(Mod mod)
{
var loadFoldersPath = Path.Combine(mod.path, "loadFolders");
if (!File.Exists(loadFoldersPath))
return;

try
{
var json = File.ReadAllText(loadFoldersPath);
var map = JsonSerializer.Deserialize<Dictionary<string, string>>(json);

if (map == null)
{
BaseLibMain.Logger.Warn($"[LoadFolders] {mod.manifest?.id}: loadFolders deserialized to null");
return;
}

var branch = PlatformUtil.GetPlatformBranch();
var branchName = branch.ToName();

if (!map.TryGetValue(branchName, out var subdir))
{
BaseLibMain.Logger
.Info($"[LoadFolders] {mod.manifest?.id}: no entry for branch '{branchName}', loading from root");
return;
}

subdir = subdir.TrimStart('/', '\\');
var resolvedPath = Path.Combine(mod.path, subdir);

BaseLibMain.Logger
.Info($"[LoadFolders] {mod.manifest?.id}: loading folder '{resolvedPath}' for branch '{branchName}'");

mod.path = resolvedPath;
}
catch (Exception e)
{
BaseLibMain.Logger.Warn($"[LoadFolders] {mod.manifest?.id}: failed to process loadFolders: {e}");
}
}
}