Skip to content

Commit

Permalink
[KKS] Fix potentially losing changes on character controllers by call…
Browse files Browse the repository at this point in the history
…ing save/reload when heroines are reloaded
  • Loading branch information
ManlyMarco committed Nov 10, 2022
1 parent dcb18bf commit 2e3c883
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
16 changes: 14 additions & 2 deletions src/Shared.Core/Chara/CharacterApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -271,8 +271,6 @@ private static void CreateOrAddBehaviours(ChaControl target)

private static void OnCardBeingSaved(ChaFile chaFile)
{
KoikatuAPI.Logger.LogDebug("Character save: " + GetLogName(chaFile));

var gamemode = KoikatuAPI.GetCurrentGameMode();

#if HS2
Expand All @@ -287,9 +285,23 @@ private static void OnCardBeingSaved(ChaFile chaFile)
var chaControl = gamemode == GameMode.Maker ? MakerAPI.GetCharacterControl() : chaFile.GetChaControl();
if (chaControl == null)
{
#if KKS // todo probably should handle this in other games as well
if (MainGame.GameAPI.GameBeingSaved)
{
KoikatuAPI.Logger.LogWarning("Could not find chaControl responsible for this chaFile! The save event will NOT be triggered for " + GetLogName(chaFile));
return;
}
#endif
KoikatuAPI.Logger.LogWarning("Could not find chaControl responsible for this chaFile! Triggering the save event on all chaControls!");
}

OnCardBeingSaved(chaControl, gamemode);
}

private static void OnCardBeingSaved(ChaControl chaControl, GameMode gamemode)
{
KoikatuAPI.Logger.LogDebug("Character save: " + GetLogName(chaControl?.chaFile));

foreach (var behaviour in GetBehaviours(chaControl))
behaviour.OnCardBeingSavedInternal(gamemode);
}
Expand Down
19 changes: 16 additions & 3 deletions src/Shared.KKalike/Chara/CharacterApi.Hooks.KK.cs
Original file line number Diff line number Diff line change
Expand Up @@ -174,19 +174,32 @@ public static void LiveCharaSelectSprite_StartPostHook(LiveCharaSelectSprite __i
/// Force reload when going to next day in school
/// It's needed because after 1st day since loading the characters are reset but not reloaded, and can cause issues
/// </summary>
[HarmonyPrefix]
#if KKS
[HarmonyPrefix]
// For some reason both of these methods are copies of each other and are called from the same place (one or the other)
// todo this needs a check when full KKS game comes out
[HarmonyPatch(typeof(ActionScene), nameof(ActionScene.NPCLoadAll), new Type[0])]
[HarmonyPatch(typeof(ActionScene), nameof(ActionScene.NPCLoadAll), typeof(bool))]
public static void ActionScene_NPCLoadAllPreHook()
{
Manager.Game.HeroineList.Where(x => x?.chaCtrl != null).Do(x => OnCardBeingSaved(x.chaCtrl, GameMode.MainGame));
}
[HarmonyPostfix]
[HarmonyPatch(typeof(ActionScene), nameof(ActionScene.NPCLoadAll), new Type[0])]
[HarmonyPatch(typeof(ActionScene), nameof(ActionScene.NPCLoadAll), typeof(bool))]
public static void ActionScene_NPCLoadAllPostHook(ActionScene __instance, Cysharp.Threading.Tasks.UniTask __result)
{
//todo this might only be necessary on the bool overload because only this one replaces the chaFile
__instance.StartCoroutine(__result.WaitForFinishCo().AppendCo(() => Manager.Game.HeroineList.Where(x => x?.chaCtrl).Do(x => ReloadChara(x.chaCtrl))));
}
#else
[HarmonyPrefix]
[HarmonyPatch(typeof(ActionScene), nameof(ActionScene.NPCLoadAll))]
#endif
public static void ActionScene_NPCLoadAllPreHook(ActionScene __instance)
{
__instance.StartCoroutine(DelayedReloadChara(null));
}
#endif

#endif

/// <summary>
Expand Down

0 comments on commit 2e3c883

Please sign in to comment.