royitaqi / HollowKnight.GodhomeWinLossTracker

A mod for the game Hollow Knight. Goal is to improve boss fight training experience in Godhome by tracking per-boss win/loss counts for Hall of Gods and pantheons.
3 stars 1 forks source link

Simplify save/load slot decision logic (and support multi-save mods) #46

Closed royitaqi closed 2 years ago

royitaqi commented 2 years ago

See D:\GitHubRepositories\HollowKnight.QoL\QoL\Modules\UnencryptedSaves.cs

ModHooks.SavegameLoadHook += OnSaveLoad;
ModHooks.SavegameSaveHook += ModHooks_SavegameSaveHook;

private void OnSaveLoad(int saveSlot)
{
            // Q: WHY DO WE NEED THIS?
            // A: There are mods which can change save file name.
            saveSlot = GetRealID(saveSlot);
            ...
}

        private void ModHooks_SavegameSaveHook(int obj)
        {
            throw new NotImplementedException();
        }

        private static int GetRealID(int id)
        {
            string? s = (string?) GetSaveFileName(null, id);

            return s == null
                ? id
                : int.Parse(new string(s.SkipWhile(c => !char.IsDigit(c)).TakeWhile(char.IsDigit).ToArray()));
        }

        //
        // Summary:
        //     Overrides the filename to load for a given slot. Return null to use vanilla names.
        internal static string GetSaveFileName(int saveSlot)
        {
            Logger.APILogger.LogFine("GetSaveFileName Invoked");
            if (ModHooks.GetSaveFileNameHook == null)
            {
                return null;
            }

            string result = null;
            Delegate[] invocationList = ModHooks.GetSaveFileNameHook.GetInvocationList();
            for (int i = 0; i < invocationList.Length; i++)
            {
                Func<int, string> func = (Func<int, string>)invocationList[i];
                try
                {
                    result = func(saveSlot);
                }
                catch (Exception message)
                {
                    Logger.APILogger.LogError(message);
                }
            }

            return result;
        }
royitaqi commented 2 years ago

NOTE: Not supporting multi-save mods.

If needed in the future, we can open a new issue.