Skip to content

Commit

Permalink
More changes for scenario parts to make sure don't modify the origina…
Browse files Browse the repository at this point in the history
…lly selected scenario
  • Loading branch information
edbmods committed Sep 26, 2021
1 parent 2b9869d commit 1ad01bb
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 17 deletions.
36 changes: 28 additions & 8 deletions Source/Controller.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ScenPart_PlayerFaction>("playerFaction");
result.SetPrivateField("playerFaction", faction);
return result;
}

protected void PrepareColonists() {
Expand Down
10 changes: 6 additions & 4 deletions Source/HarmonyPatches.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ static Main() {
class ClearOriginalScenarioPatch {
[HarmonyPostfix]
static void Postfix() {
PrepareCarefully.ClearOriginalScenario();
PrepareCarefully.ClearVanillaFriendlyScenario();
}
}

Expand All @@ -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();
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion Source/Page_PrepareCarefully.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand Down
8 changes: 4 additions & 4 deletions Source/PrepareCarefully.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,12 @@ public State State {
}
}

public static List<ScenPart> OriginalScenarioParts {
public static Scenario VanillaFriendlyScenario {
get; set;
}

public static void ClearOriginalScenario() {
OriginalScenarioParts = null;
public static void ClearVanillaFriendlyScenario() {
VanillaFriendlyScenario = null;
}

public Providers Providers {
Expand Down Expand Up @@ -123,7 +123,7 @@ public void DoNextInBasePage() {
}

public void Clear() {
ClearOriginalScenario();
ClearVanillaFriendlyScenario();
this.Active = false;
this.Providers = new Providers();
this.equipmentDatabase = new EquipmentDatabase();
Expand Down

0 comments on commit 1ad01bb

Please sign in to comment.