Skip to content

Commit

Permalink
[WIP] Fixed KCM crashing and not loading in
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesVeug committed Jan 7, 2023
1 parent 2885b88 commit cc19704
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 88 deletions.
4 changes: 4 additions & 0 deletions Core/Models/GraveControllerExt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@ private void Start()

public static void SetupNewController(GravestoneCardAnimationController graveController)
{
Log.LogInfo($"[GraveControllerExt] SetupNewController [{graveController}]");
Log.LogInfo($"[GraveControllerExt] SetupNewController [{graveController.gameObject}]");
GraveControllerExt ext = graveController.gameObject.AddComponent<GraveControllerExt>();
Log.LogInfo($"[GraveControllerExt] ext [{ext}]");
ext.cardRenderer = graveController.cardRenderer;
ext.intendedRendererYPos = ext.cardRenderer.transform.localPosition.y;
ext.armAnim = graveController.armAnim;
Expand All @@ -50,6 +53,7 @@ public static void SetupNewController(GravestoneCardAnimationController graveCon
ext.statsLater = graveController.statsLater;
ext._graveAnim = graveController.Anim;

Log.LogInfo($"[GraveControllerExt] graveController.PlayableCard [{graveController.PlayableCard}]");
if (graveController.PlayableCard)
{
ext._playableCard = graveController.PlayableCard;
Expand Down
4 changes: 2 additions & 2 deletions Core/Utils/GrimoraSaveUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ public static void ClearDeck()
DeckInfo.cardIds.Clear();
}

public static bool IsGrimora => SaveManager.SaveFile.IsGrimora || SaveDataRelatedPatches.IsGrimoraRun;
public static bool IsGrimora => SaveDataRelatedPatches.IsGrimoraRun;

public static bool IsNotGrimora => !SaveManager.SaveFile.IsGrimora || SaveDataRelatedPatches.IsNotGrimoraRun;
public static bool IsNotGrimora => SaveDataRelatedPatches.IsNotGrimoraRun;

public static DeckInfo DeckInfo => GrimoraSaveData.Data.deck;

Expand Down
51 changes: 13 additions & 38 deletions Patches/AscensionRelatedPatches.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using DiskCardGame;
using GBC;
using GrimoraMod.Saving;
using HarmonyLib;
using InscryptionAPI.Ascension;
using Sirenix.Utilities;
Expand Down Expand Up @@ -133,12 +134,13 @@ public class AscensionRelatedPatches
[HarmonyBefore(ConfigHelper.PackManagerGuid, ConfigHelper.P03ModGuid)]
public static bool InitializeGrimoraSaveData(ref AscensionMenuScreens __instance, bool newRun = true)
{
GrimoraAscensionSaveData ascensionSaveData = (GrimoraAscensionSaveData)AscensionSaveData.Data;
Log.LogInfo($"[AscensionMenuScreens.TransitionToGame] " +
$"IsGrimoraRun [{SaveDataRelatedPatches.IsGrimoraRun}] " +
$"newRun [{newRun}] " +
$"screen state [{ScreenManagement.ScreenState}] " +
$"currentStarterDeck [{AscensionSaveData.Data.currentStarterDeck}]" +
$"currentRun [{AscensionSaveData.Data.currentRun}]"
$"IsGrimoraRun [{SaveDataRelatedPatches.IsGrimoraRun}] " +
$"newRun [{newRun}] " +
$"screen state [{ScreenManagement.ScreenState}] " +
$"currentStarterDeck [{ascensionSaveData.currentStarterDeck}]" +
$"currentRun [{ascensionSaveData.currentRun}]"
);

if (newRun)
Expand All @@ -159,18 +161,18 @@ public static bool InitializeGrimoraSaveData(ref AscensionMenuScreens __instance
{
Log.LogInfo($"[AscensionMenuScreens.TransitionToGame] Setting ScreenState to Undead");
ScreenManagement.ScreenState = CardTemple.Undead;
if (AscensionSaveData.Data.currentStarterDeck.Equals("Vanilla", StringComparison.InvariantCultureIgnoreCase))
if (ascensionSaveData.currentStarterDeck.Equals("Vanilla", StringComparison.InvariantCultureIgnoreCase))
{
Log.LogInfo($"[AscensionMenuScreens.TransitionToGame] --> Changing current starter deck to default");
AscensionSaveData.Data.currentStarterDeck = StarterDecks.DefaultStarterDeck;
ascensionSaveData.currentStarterDeck = StarterDecks.DefaultStarterDeck;
}

Log.LogInfo($"[AscensionMenuScreens.TransitionToGame] Creating new ascension run from starter deck [{StarterDecks.DefaultStarterDeck}]");
StarterDeckInfo deckInfo = StarterDecksUtil.GetInfo(StarterDecks.DefaultStarterDeck);
Log.LogInfo($"[AscensionMenuScreens.TransitionToGame] Creating new ascension run from starter deck [{ascensionSaveData.currentStarterDeck}]");
StarterDeckInfo deckInfo = StarterDecksUtil.GetInfo(ascensionSaveData.currentStarterDeck);
if (deckInfo)
{
Log.LogInfo($"[AscensionMenuScreens.TransitionToGame] --> DeckInfo not null [{deckInfo.cards.Join(c => c.name)}]");
AscensionSaveData.Data.NewRun(deckInfo.cards);
ascensionSaveData.NewRun(deckInfo.cards);
SaveManager.SaveToFile(false);
}
else
Expand All @@ -191,34 +193,6 @@ public static bool InitializeGrimoraSaveData(ref AscensionMenuScreens __instance
return true;
}

private static void ClearGrimoraData()
{
if (!AscensionMenuScreens.ReturningFromFailedRun && !AscensionMenuScreens.ReturningFromSuccessfulRun)
{
ScreenManagement.ScreenState = CardTemple.Undead;
}
}

[HarmonyPrefix, HarmonyPatch(nameof(AscensionMenuScreens.SwitchToScreen))]
public static void ClearGrimoraSaveOnNewRun(AscensionMenuScreens __instance, AscensionMenuScreens.Screen screen)
{
if (screen == AscensionMenuScreens.Screen.Start) // At the main screen, you can't be in any style of run. Not yet.
{
ClearGrimoraData();
}
}

[HarmonyPrefix, HarmonyPatch(nameof(AscensionMenuScreens.Start))]
public static void ClearScreenStatePrefix(AscensionMenuScreens __instance)
{
ClearGrimoraData();
}






internal static AscensionMenuInteractable CreateAscensionButton(AscensionMenuInteractable newRunButton)
{
Log.LogDebug($"[AscensionMenuScreens.Start] Creating new Grimora ascension run button");
Expand All @@ -229,6 +203,7 @@ internal static AscensionMenuInteractable CreateAscensionButton(AscensionMenuInt
SaveDataRelatedPatches.IsGrimoraRun = true;
ScreenManagement.ScreenState = CardTemple.Undead;
ChallengeManager.SyncChallengeList();
GrimoraSaveManager.CurrentSaveFile.NewAscensionRun();
Log.LogDebug($"[AscensionMenuScreens.Start] Set screen state to undead, invoking CursorSelectStart");
newRunButton.CursorSelectStart();
};
Expand Down
6 changes: 5 additions & 1 deletion Patches/Cards/BaseCardPatches.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,11 @@ public static void AddNewController(Card __instance, CardInfo info)
if (__instance.GetComponentInParent<SelectableCard>().SafeIsUnityNull() && __instance.GetComponent<GraveControllerExt>().SafeIsUnityNull())
{
var oldController = __instance.GetComponent<GravestoneCardAnimationController>();
GraveControllerExt.SetupNewController(oldController);
if (oldController != null)
{
GrimoraPlugin.Log.LogInfo($"[AddNewController] oldController [{oldController}][{info.name}]");
GraveControllerExt.SetupNewController(oldController);
}
}
}
}
10 changes: 1 addition & 9 deletions Patches/SaveDataRelatedPatches.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,7 @@ public static string SaveKey

public static bool IsGrimoraRun
{
get
{
string activeSceneName = SceneLoader.ActiveSceneName.ToLowerInvariant();
// Log.LogDebug($"[IsGrimoraRun] Active scene name is [{activeSceneName}] Screen State [{ScreenManagement.ScreenState}]");
if (activeSceneName.Contains("grimora") || ScreenManagement.ScreenState == CardTemple.Undead)
return true;

return AscensionSaveData.Data is { currentRun.playerLives: > 0 } && ModdedSaveManager.SaveData.GetValueAsBoolean(GUID, IsGrimoraRunKey);
}
get => ModdedSaveManager.SaveData.GetValueAsBoolean(GUID, IsGrimoraRunKey);
set => ModdedSaveManager.SaveData.SetValue(GUID, IsGrimoraRunKey, value);
}
}
41 changes: 8 additions & 33 deletions Saving/GrimoraAscensionSaveData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,51 +26,26 @@ public class GrimoraAscensionSaveData : AscensionSaveData

public new void NewRun(List<CardInfo> starterDeck)
{
GrimoraPlugin.Log.LogInfo("[GrimoraAscensionSaveData] NewRun");
this.currentRunSeed = Environment.TickCount;
this.currentOuroborosDeaths = 0;

GrimoraRunState grimoraRunState = new GrimoraRunState();
this.currentRun = grimoraRunState;
grimoraRunState.Initialize();

this.RollCurrentRunRegionOrder();
this.oilPaintingState.TryAdvanceRewardIndex();
this.oilPaintingState.puzzleSolution = OilPaintingPuzzle.GenerateSolution(true);
this.currentRun.playerDeck = new DeckInfo();

this.currentRun.consumables.Clear();
this.currentRun.consumables.Add("Pliers");

foreach (CardInfo cardInfo in starterDeck)
{
this.currentRun.playerDeck.AddCard(CardLoader.GetCardByName(cardInfo.name));
GrimoraPlugin.Log.LogInfo("[GrimoraAscensionSaveData] card " + cardInfo.name);
}
if (this.numRunsSinceReachedFirstBoss == 0)
{
this.currentRun.playerDeck.AddCard(CardLoader.GetCardByName("PeltHare"));
this.currentRun.playerDeck.AddCard(CardLoader.GetCardByName("PeltHare"));
}
else if (this.numRunsSinceReachedFirstBoss == 1)
{
this.currentRun.playerDeck.AddCard(CardLoader.GetCardByName("Opossum"));
this.currentRun.playerDeck.AddCard(CardLoader.GetCardByName("PeltHare"));
}
else if (this.numRunsSinceReachedFirstBoss > 1)
{
this.currentRun.playerDeck.AddCard(CardLoader.GetCardByName("Opossum"));
this.currentRun.playerDeck.AddCard(CardLoader.GetCardByName("RingWorm"));
}
this.currentRun.consumables.Add("SquirrelBottle");
if (this.GetNumChallengesOfTypeActive(AscensionChallenge.LessConsumables) < 2)
{
this.currentRun.consumables.Add("Pliers");
}
if (!this.ChallengeIsActive(AscensionChallenge.NoHook))
{
if (this.currentRun.consumables.Count == this.currentRun.MaxConsumables)
{
this.currentRun.consumables.RemoveAt(this.currentRun.consumables.Count - 1);
}
this.currentRun.consumables.Add("FishHook");
}
if (this.ChallengeIsActive(AscensionChallenge.LessLives))
{
this.currentRun.maxPlayerLives = (this.currentRun.playerLives = 1);
}
GrimoraPlugin.Log.LogInfo("[GrimoraAscensionSaveData] " + this.currentRun.playerDeck.Cards.Count);
}
}
5 changes: 2 additions & 3 deletions Saving/GrimoraSaveFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@ public void Initialize()

public void NewAscensionRun()
{
GrimoraRunState currentRun = (GrimoraRunState)AscensionSaveData.currentRun;
currentRun.Initialize();
currentRun.NewStandardGame();
AscensionSaveData.Initialize();
AscensionSaveData.NewRun(new List<CardInfo>());
}

public void NewStandardRun()
Expand Down
19 changes: 17 additions & 2 deletions Saving/GrimoraSaveManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ public static void ResetStandardRun()
CurrentSaveFile.NewStandardRun();
}

public static void NewAscensionRun()
{
CurrentSaveFile.NewAscensionRun();
}

#region Patches
public static string SaveFilePath => SaveManager.SaveFolderPath + "GrimoraSaveFile.gwsave";

Expand All @@ -41,6 +46,7 @@ public static bool SaveManager_LoadFromFile()
{
if (File.Exists(SaveFilePath))
{
GrimoraPlugin.Log.LogInfo("Loaded from file");
string json = File.ReadAllText(SaveFilePath);
CurrentSaveFile = SaveManager.FromJSON<GrimoraSaveFile>(json);
}
Expand Down Expand Up @@ -80,7 +86,14 @@ public static bool SaveFile_CurrentDeck(ref DeckInfo __result)
{
if (GrimoraSaveUtil.IsGrimora)
{
__result = CurrentSaveFile.CurrentRun.playerDeck;
if (SaveFile.IsAscension)
{
__result = CurrentSaveFile.AscensionSaveData.currentRun.playerDeck;
}
else
{
__result = CurrentSaveFile.CurrentRun.playerDeck;
}
return false;
}

Expand All @@ -91,10 +104,12 @@ public static bool SaveFile_CurrentDeck(ref DeckInfo __result)
[HarmonyPrefix]
public static bool AscensionSaveData_Data(ref AscensionSaveData __result)
{
//GrimoraPlugin.Log.LogInfo(Environment.StackTrace);
//GrimoraPlugin.Log.LogInfo("Done");
if (GrimoraSaveUtil.IsGrimora)
{
__result = CurrentSaveFile.AscensionSaveData;
return false;
return false; // *****************************
}

return true;
Expand Down

0 comments on commit cc19704

Please sign in to comment.