From 90d1df271bde438271c162ec722537a5e56be957 Mon Sep 17 00:00:00 2001 From: DeathWeasel1337 Date: Tue, 26 Feb 2019 18:01:36 -0600 Subject: [PATCH] Multiple BepisPlugins version compatibility --- KK_MiscFixes/KK_MiscFixes.cs | 57 ++++++++++++++++++++++++-------- KK_MiscFixes/KK_MiscFixes.csproj | 5 +++ 2 files changed, 49 insertions(+), 13 deletions(-) diff --git a/KK_MiscFixes/KK_MiscFixes.cs b/KK_MiscFixes/KK_MiscFixes.cs index bd58d991..3ade7146 100644 --- a/KK_MiscFixes/KK_MiscFixes.cs +++ b/KK_MiscFixes/KK_MiscFixes.cs @@ -1,13 +1,14 @@ -using BepInEx; +using ActionGame; +using BepInEx; using FreeH; using Harmony; +using Illusion.Game; using System; using UniRx; using UnityEngine.UI; -using static ExtensibleSaveFormat.ExtendedSave; -using ActionGame; -using Illusion.Game; - +/// +/// Miscellaneous fixes aimed at improving the performance of the game +/// namespace KK_MiscFixes { [BepInPlugin(GUID, PluginName, Version)] @@ -17,23 +18,54 @@ public class KK_MiscFixes : BaseUnityPlugin public const string PluginName = "Misc Fixes"; public const string Version = "1.0"; + private static object ExtendedSaveInstance; + private static Version LoadedVersionNumber; + void Main() { var harmony = HarmonyInstance.Create(GUID); harmony.PatchAll(typeof(KK_MiscFixes)); + + ExtendedSaveInstance = GetComponent(); + LoadedVersionNumber = MetadataHelper.GetMetadata(ExtendedSaveInstance).Version; } /// + /// Get or set the value that determines whether card load events fire. + /// It's different depending on the BepisPlugins version so we use reflection to get it depending on the version the user is running. + /// Version r8 and below only toggles on/off sideloader events, but that's enough for the purposes of this plugin. + /// Once r9 is officially released nuke this, set the value directly, and force people to update their plugins. + /// + private static bool LoadEvents + { + get + { + if (LoadedVersionNumber.Major <= 8 && LoadedVersionNumber.MajorRevision <= 0 && LoadedVersionNumber.Minor <= 0 && LoadedVersionNumber.MinorRevision <= 0) + return (bool)typeof(Sideloader.AutoResolver.Hooks).GetProperty("IsResolving").GetValue(null, null); + else + return (bool)Traverse.Create(ExtendedSaveInstance).Field("LoadEventsEnabled").GetValue(); + } + set + { + //Version 8.0 and below + if (LoadedVersionNumber.Major <= 8 && LoadedVersionNumber.MajorRevision <= 0 && LoadedVersionNumber.Minor <= 0 && LoadedVersionNumber.MinorRevision <= 0) + typeof(Sideloader.AutoResolver.Hooks).GetProperty("IsResolving").SetValue(null, value, null); + else//Version 8.0.0.1 and above + Traverse.Create(ExtendedSaveInstance).Field("LoadEventsEnabled").SetValue(value); + } + } + #region Free H List + /// /// Turn off ExtensibleSaveFormat events /// [HarmonyPrefix, HarmonyPatch(typeof(FreeHClassRoomCharaFile), "Start")] - public static void FreeHClassRoomCharaFileStartPrefix() => LoadEventsEnabled = false; + public static void FreeHClassRoomCharaFileStartPrefix() => LoadEvents = false; /// /// Turn back on ExtensibleSaveFormat events, load a copy of the character with extended data on this time, and use that instead. /// [HarmonyPostfix, HarmonyPatch(typeof(FreeHClassRoomCharaFile), "Start")] public static void FreeHClassRoomCharaFileStartPostfix(FreeHClassRoomCharaFile __instance) { - LoadEventsEnabled = true; + LoadEvents = true; ReactiveProperty info = Traverse.Create(__instance).Field("info").GetValue>(); Button enterButton = Traverse.Create(__instance).Field("enterButton").GetValue