Skip to content

Commit

Permalink
Multiple BepisPlugins version compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
DeathWeasel1337 committed Feb 27, 2019
1 parent 6d899c3 commit 90d1df2
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 13 deletions.
57 changes: 44 additions & 13 deletions KK_MiscFixes/KK_MiscFixes.cs
Original file line number Diff line number Diff line change
@@ -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;

/// <summary>
/// Miscellaneous fixes aimed at improving the performance of the game
/// </summary>
namespace KK_MiscFixes
{
[BepInPlugin(GUID, PluginName, Version)]
Expand All @@ -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<ExtensibleSaveFormat.ExtendedSave>();
LoadedVersionNumber = MetadataHelper.GetMetadata(ExtendedSaveInstance).Version;
}
/// <summary>
/// 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.
/// </summary>
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
/// <summary>
/// Turn off ExtensibleSaveFormat events
/// </summary>
[HarmonyPrefix, HarmonyPatch(typeof(FreeHClassRoomCharaFile), "Start")]
public static void FreeHClassRoomCharaFileStartPrefix() => LoadEventsEnabled = false;
public static void FreeHClassRoomCharaFileStartPrefix() => LoadEvents = false;
/// <summary>
/// Turn back on ExtensibleSaveFormat events, load a copy of the character with extended data on this time, and use that instead.
/// </summary>
[HarmonyPostfix, HarmonyPatch(typeof(FreeHClassRoomCharaFile), "Start")]
public static void FreeHClassRoomCharaFileStartPostfix(FreeHClassRoomCharaFile __instance)
{
LoadEventsEnabled = true;
LoadEvents = true;

ReactiveProperty<ChaFileControl> info = Traverse.Create(__instance).Field("info").GetValue<ReactiveProperty<ChaFileControl>>();
Button enterButton = Traverse.Create(__instance).Field("enterButton").GetValue<Button>();
Expand All @@ -48,23 +80,21 @@ public static void FreeHClassRoomCharaFileStartPostfix(FreeHClassRoomCharaFile _
onEnter(chaFileControl);
});
}
#endregion

#region Classroom list
/// <summary>
/// Turn off ExtensibleSaveFormat events
/// </summary>
[HarmonyPrefix, HarmonyPatch(typeof(ClassRoomCharaFile), "Start")]
public static void ClassRoomCharaFileStartPrefix()
{
Logger.Log(BepInEx.Logging.LogLevel.Info, "ClassRoomCharaFileStartPrefix");
LoadEventsEnabled = false;
}
public static void ClassRoomCharaFileStartPrefix() => LoadEvents = false;
/// <summary>
/// Turn back on ExtensibleSaveFormat events, load a copy of the character with extended data on this time, and use that instead.
/// </summary>
[HarmonyPostfix, HarmonyPatch(typeof(ClassRoomCharaFile), "Start")]
public static void ClassRoomCharaFileStartPostfix(ClassRoomCharaFile __instance)
{
LoadEventsEnabled = true;
LoadEvents = true;

ReactiveProperty<ChaFileControl> info = Traverse.Create(__instance).Field("info").GetValue<ReactiveProperty<ChaFileControl>>();
Button enterButton = Traverse.Create(__instance).Field("enterButton").GetValue<Button>();
Expand All @@ -80,5 +110,6 @@ public static void ClassRoomCharaFileStartPostfix(ClassRoomCharaFile __instance)
Utils.Sound.Play(SystemSE.sel);
});
}
#endregion
}
}
5 changes: 5 additions & 0 deletions KK_MiscFixes/KK_MiscFixes.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@
<HintPath>..\lib\ExtensibleSaveFormat.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="Sideloader, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\lib\Sideloader.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
Expand Down

0 comments on commit 90d1df2

Please sign in to comment.