diff --git a/Source/WulaFallenEmpire/Harmony/MapLifecycle_Patches.cs b/Source/WulaFallenEmpire/Harmony/MapLifecycle_Patches.cs new file mode 100644 index 00000000..07999a78 --- /dev/null +++ b/Source/WulaFallenEmpire/Harmony/MapLifecycle_Patches.cs @@ -0,0 +1,48 @@ +using System; +using HarmonyLib; +using RimWorld.Planet; +using Verse; + +namespace WulaFallenEmpire +{ + [HarmonyPatch(typeof(MapParent), "ShouldRemoveMapNow")] + [HarmonyPriority(600)] + public static class MapParent_ShouldRemoveMapNow_Patch + { + public static void Postfix(ref bool __result, MapParent __instance) + { + if (!__result) + { + return; + } + try + { + if (__instance.HasMap && WulaMapProtectionHelper.ShouldProtectMap(__instance.Map)) + { + __result = false; + } + } + catch (Exception arg) + { + Log.Error($"[WULA] Error in MapParent_ShouldRemoveMapNow_Patch: {arg}"); + } + } + } + + [HarmonyPatch(typeof(Game), "DeinitAndRemoveMap")] + [HarmonyPatch(new Type[] { typeof(Map), typeof(bool) })] + [HarmonyPriority(600)] + public static class Game_DeinitAndRemoveMap_Patch + { + [HarmonyPrefix] + private static bool PreventMapRemoval(Map map) + { + if (WulaMapProtectionHelper.ShouldProtectMap(map)) + { + Log.Message("[WULA] Map destruction prevented by WulaMapProtectionHelper at Game.DeinitAndRemoveMap level."); + return false; // 返回 false 来阻止原始方法的执行 + } + return true; // 返回 true 来继续执行原始方法 + } + } +} diff --git a/Source/WulaFallenEmpire/Harmony/WulaMapProtectionHelper.cs b/Source/WulaFallenEmpire/Harmony/WulaMapProtectionHelper.cs new file mode 100644 index 00000000..276ddfd2 --- /dev/null +++ b/Source/WulaFallenEmpire/Harmony/WulaMapProtectionHelper.cs @@ -0,0 +1,28 @@ +using System; +using System.Linq; +using Verse; + +namespace WulaFallenEmpire +{ + public static class WulaMapProtectionHelper + { + public static bool ShouldProtectMap(Map map) + { + if (map == null) + { + return false; + } + try + { + // 检查地图上是否存在一个口袋空间已经初始化的武装穿梭机 + return map.listerThings.AllThings.OfType() + .Any(shuttle => shuttle != null && shuttle.Spawned && shuttle.PocketMapGenerated); + } + catch (Exception arg) + { + Log.Error($"[WULA] Error in WulaMapProtectionHelper.ShouldProtectMap: {arg}"); + return false; + } + } + } +} \ No newline at end of file