From 1ad01bb9c555f0634e64537d11ec7a99f4001a93 Mon Sep 17 00:00:00 2001 From: edbmods Date: Sat, 25 Sep 2021 22:12:55 -0700 Subject: [PATCH] More changes for scenario parts to make sure don't modify the originally selected scenario --- Source/Controller.cs | 36 +++++++++++++++++++++++++-------- Source/HarmonyPatches.cs | 10 +++++---- Source/Page_PrepareCarefully.cs | 2 +- Source/PrepareCarefully.cs | 8 ++++---- 4 files changed, 39 insertions(+), 17 deletions(-) diff --git a/Source/Controller.cs b/Source/Controller.cs index 9c1f6bc..09eda35 100644 --- a/Source/Controller.cs +++ b/Source/Controller.cs @@ -107,16 +107,36 @@ public void PrepareGame() { PrepareRelatedPawns(); PrepareColonists(); PrepareWorldPawns(); + PrepareScenario(); + } - // When replacing the scenario parts, we'll get a list of parts that will be added to the scenario to spawn the new game. - // We also get back a set of vanilla-friendly parts that match the actual parts used. Once we've finishing spawning the - // scenario parts into the map, we'll replace the actual parts with the vanilla-friendly ones instead. We do this because - // the actual parts may have Prepare Carefully-specific scene parts. We want to make sure that the scenario that's saved with - // the game does not have anything specific to Prepare Carefully so that saves are not dependent on the mod. + // Replace the originally selected scenario with one that reflects the equipment and pawns chosen in Prepare Carefully. + protected void PrepareScenario() { + // We're going to create two copies of the original scenario. The first one is the one that we'll actually use to spawn the new game. + // This one is potentially going to include custom scenario parts that are specific to Prepare Carefully. Once we've spawned the game, + // we don't want to leave that scenario associated with the save, because we don't want the save to be dependent on Prepare Carefully. + // So we have a second copy of the scenario. This one uses vanilla-friendly alternatives to the Prepare Carefully-specific scenario parts. + // After we spawn the game, we'll swap in this vanilla-friendly version of the scenario. + var actualScenario = CopyScenarioWithoutParts(Find.Scenario); + var vanillaFriendlyScenario = CopyScenarioWithoutParts(Find.Scenario); (var actualParts, var vanillaFriendlyParts) = ReplaceScenarioParts(Find.Scenario); - Scenario scenario = Current.Game.Scenario; - scenario.SetPrivateField("parts", actualParts); - PrepareCarefully.OriginalScenarioParts = vanillaFriendlyParts; + + actualScenario.SetPrivateField("parts", actualParts); + Current.Game.Scenario = actualScenario; + + vanillaFriendlyScenario.SetPrivateField("parts", vanillaFriendlyParts); + PrepareCarefully.VanillaFriendlyScenario = vanillaFriendlyScenario; + } + + protected Scenario CopyScenarioWithoutParts(Scenario source) { + Scenario result = new Scenario() { + name = source.name, + summary = source.summary, + description = source.description, + }; + ScenPart_PlayerFaction faction = source.GetPrivateField("playerFaction"); + result.SetPrivateField("playerFaction", faction); + return result; } protected void PrepareColonists() { diff --git a/Source/HarmonyPatches.cs b/Source/HarmonyPatches.cs index 15d0f92..1076258 100644 --- a/Source/HarmonyPatches.cs +++ b/Source/HarmonyPatches.cs @@ -40,7 +40,7 @@ static Main() { class ClearOriginalScenarioPatch { [HarmonyPostfix] static void Postfix() { - PrepareCarefully.ClearOriginalScenario(); + PrepareCarefully.ClearVanillaFriendlyScenario(); } } @@ -50,9 +50,11 @@ static void Postfix() { class ReplaceScenarioPatch { [HarmonyPostfix] static void Postfix() { - if (PrepareCarefully.OriginalScenarioParts != null) { - Current.Game.Scenario.SetPrivateField("parts", PrepareCarefully.OriginalScenarioParts); - PrepareCarefully.ClearOriginalScenario(); + // After we've initialized the new game, swap in the vanilla-friendly version of the scenario so that the game save + // doesn't include any Prepare Carefully-specific scene parts. + if (PrepareCarefully.VanillaFriendlyScenario != null) { + Current.Game.Scenario = PrepareCarefully.VanillaFriendlyScenario; + PrepareCarefully.ClearVanillaFriendlyScenario(); } } } diff --git a/Source/Page_PrepareCarefully.cs b/Source/Page_PrepareCarefully.cs index 49b72c8..079ee1b 100644 --- a/Source/Page_PrepareCarefully.cs +++ b/Source/Page_PrepareCarefully.cs @@ -186,7 +186,7 @@ public override void DoWindowContents(Rect inRect) { protected void ConfirmExit() { Find.WindowStack.Add(new Dialog_Confirm("EdB.PC.Page.ConfirmExit".Translate(), delegate { PrepareCarefully.Instance.Clear(); - PrepareCarefully.ClearOriginalScenario(); + PrepareCarefully.ClearVanillaFriendlyScenario(); this.Close(true); }, true, null, true)); } diff --git a/Source/PrepareCarefully.cs b/Source/PrepareCarefully.cs index 56448cd..c218615 100644 --- a/Source/PrepareCarefully.cs +++ b/Source/PrepareCarefully.cs @@ -67,12 +67,12 @@ public State State { } } - public static List OriginalScenarioParts { + public static Scenario VanillaFriendlyScenario { get; set; } - public static void ClearOriginalScenario() { - OriginalScenarioParts = null; + public static void ClearVanillaFriendlyScenario() { + VanillaFriendlyScenario = null; } public Providers Providers { @@ -123,7 +123,7 @@ public void DoNextInBasePage() { } public void Clear() { - ClearOriginalScenario(); + ClearVanillaFriendlyScenario(); this.Active = false; this.Providers = new Providers(); this.equipmentDatabase = new EquipmentDatabase();