From 6b1a683acfaaa8907b04ce4e70aa0019f1953882 Mon Sep 17 00:00:00 2001 From: LinnielDW Date: Sat, 4 Jul 2026 14:37:35 +0200 Subject: [PATCH] add LoadFolders patch to dynamically load mod directories based on platform branch --- Patches/Features/LoadFoldersPatch.cs | 52 ++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 Patches/Features/LoadFoldersPatch.cs diff --git a/Patches/Features/LoadFoldersPatch.cs b/Patches/Features/LoadFoldersPatch.cs new file mode 100644 index 00000000..f4625193 --- /dev/null +++ b/Patches/Features/LoadFoldersPatch.cs @@ -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>(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}"); + } + } +} \ No newline at end of file