diff --git a/UXAssist/Patches/PersistPatch.cs b/UXAssist/Patches/PersistPatch.cs index e43ab41..53682ef 100644 --- a/UXAssist/Patches/PersistPatch.cs +++ b/UXAssist/Patches/PersistPatch.cs @@ -191,6 +191,78 @@ public class PersistPatch : PatchImpl return matcher.InstructionEnumeration(); } + // Disable rendering when hideAllUI0 is true + [HarmonyTranspiler] + [HarmonyPatch(typeof(GameLogic), nameof(GameLogic.LateUpdate))] + [HarmonyPatch(typeof(GameLogic), nameof(GameLogic.Draw))] + [HarmonyPatch(typeof(GameLogic), nameof(GameLogic.DrawPost))] + private static IEnumerable GameLogic_LateUpdate_Transpiler(IEnumerable instructions, ILGenerator generator) + { + var matcher = new CodeMatcher(instructions, generator); + matcher.Start(); + matcher.CreateLabel(out var label); + matcher.InsertAndAdvance( + new CodeInstruction(OpCodes.Call, AccessTools.PropertyGetter(typeof(UIRoot), nameof(UIRoot.instance))), + new CodeInstruction(OpCodes.Ldfld, AccessTools.Field(typeof(UIRoot), nameof(UIRoot.uiGame))), + new CodeInstruction(OpCodes.Ldfld, AccessTools.Field(typeof(UIGame), nameof(UIGame.hideAllUI0))), + new CodeInstruction(OpCodes.Brfalse, label), + new CodeInstruction(OpCodes.Ret) + ); + + return matcher.InstructionEnumeration(); + } + + static bool _preHideAllUI0 = false; + static bool _localPlanetChanged = false; + [HarmonyPrefix] + [HarmonyPatch(typeof(UIGame), nameof(UIGame._OnLateUpdate))] + private static void UIGame__OnLateUpdate_Prefix(UIGame __instance) + { + _preHideAllUI0 = __instance.hideAllUI0; + } + + [HarmonyPostfix] + [HarmonyPatch(typeof(UIGame), nameof(UIGame._OnLateUpdate))] + private static void UIGame__OnLateUpdate_Postfix(UIGame __instance) + { + var hideAllUI0 = __instance.hideAllUI0; + if (_localPlanetChanged) + { + _localPlanetChanged = false; + } + else + { + if (_preHideAllUI0 == hideAllUI0) return; + } + var show = GameMain.localPlanet != null || !hideAllUI0; + GameMain.data?.mainPlayer?.effect.sailEffect.gameObject.SetActive(!hideAllUI0); + var universeSimulator = GameMain.universeSimulator; + if (universeSimulator == null) return; + universeSimulator.gameObject.SetActive(show); + var planetSimulators = universeSimulator.planetSimulators; + if (planetSimulators == null) return; + foreach (var planet in planetSimulators) + { + if (planet == null) continue; + planet.gameObject.SetActive(show); + } + } + + [HarmonyPostfix] + [HarmonyPatch(typeof(UniverseSimulator), nameof(UniverseSimulator.SetPlanetSimulator))] + private static void UniverseSimulator_SetPlanetSimulator_Postfix(UniverseSimulator __instance, PlanetSimulator sim) + { + if (GameMain.localPlanet == null && UIRoot.instance.uiGame.hideAllUI0) sim.gameObject.SetActive(false); + } + + [HarmonyPrefix] + [HarmonyPatch(typeof(GameData), nameof(GameData.localPlanet), MethodType.Setter)] + private static void GameData_localPlanet_Setter_Prefix(GameData __instance, PlanetData value) + { + var oldPlanet = __instance.localPlanet; + if (oldPlanet == null && value != null || oldPlanet != null && value == null) _localPlanetChanged = true; + } + #region Cluster Upload Result [HarmonyPostfix]