From 0366f602974f878f5e25a3f427badafae2477593 Mon Sep 17 00:00:00 2001 From: Xilophor Date: Thu, 25 Jan 2024 14:35:19 -0500 Subject: [PATCH] Minor Refactoring & Reorganization --- LobbyCompatibility/Features/HarmonyHelper.cs | 30 +++++++++++++++++++ .../LobbyCompatibilityPlugin.cs | 19 ------------ .../Patches/ChallengeSaveFilePatch.cs | 16 ---------- .../Patches/ES3SettingsConstructorPrefix.cs | 21 +++++++++++++ .../Patches/FindOrCreateLeaderboardPrefix.cs | 21 +++++++++++++ .../Patches/LeaderboardNamePatch.cs | 17 ----------- 6 files changed, 72 insertions(+), 52 deletions(-) create mode 100644 LobbyCompatibility/Features/HarmonyHelper.cs delete mode 100644 LobbyCompatibility/Patches/ChallengeSaveFilePatch.cs create mode 100644 LobbyCompatibility/Patches/ES3SettingsConstructorPrefix.cs create mode 100644 LobbyCompatibility/Patches/FindOrCreateLeaderboardPrefix.cs delete mode 100644 LobbyCompatibility/Patches/LeaderboardNamePatch.cs diff --git a/LobbyCompatibility/Features/HarmonyHelper.cs b/LobbyCompatibility/Features/HarmonyHelper.cs new file mode 100644 index 0000000..d0b9f47 --- /dev/null +++ b/LobbyCompatibility/Features/HarmonyHelper.cs @@ -0,0 +1,30 @@ +using System; +using System.Reflection; +using System.Runtime.CompilerServices; +using HarmonyLib; + +namespace LobbyCompatibility.Features; + +/// +/// Helper class related to Harmony Patching. +/// +internal class HarmonyHelper +{ + /// + /// Retrieves the MethodInfo of the compiler-generated async method. + /// Async method content is not actually in the async Method(), but instead is in a separate struct under the method "MoveNext"; + /// this function retrieves that method info. + /// + /// () The type of the class housing the method. + /// () The name of the method being patched. + /// () The info of the async "MoveNext" method. + public static MethodInfo? GetAsyncInfo(Type type, string method) + { + // Get the Method Info of the target Async Method + return AccessTools.Method(type, method) + // Find the AsyncStateMachine class from target method + .GetCustomAttribute() + // Get the struct type (random compiler junk) + .StateMachineType.GetMethod("MoveNext", BindingFlags.Instance | BindingFlags.NonPublic); + } +} \ No newline at end of file diff --git a/LobbyCompatibility/LobbyCompatibilityPlugin.cs b/LobbyCompatibility/LobbyCompatibilityPlugin.cs index 8e186b8..c0afcae 100644 --- a/LobbyCompatibility/LobbyCompatibilityPlugin.cs +++ b/LobbyCompatibility/LobbyCompatibilityPlugin.cs @@ -30,25 +30,6 @@ private void Awake() // Plugin startup logic Logger.LogInfo($"Plugin {PluginInfo.PLUGIN_GUID} is loaded!"); } - - - /// - /// Retrieves the MethodInfo of the compiler-generated async method. - /// Async method content is not actually in the async Method(), but instead is in a separate struct under the method "MoveNext"; - /// this function retrieves that method info. - /// - /// () The type of the class housing the method. - /// () The name of the method being patched. - /// () The info of the async "MoveNext" method. - public static MethodInfo? GetAsyncInfo(Type type, string method) - { - // Get the Method Info of the target Async Method - return AccessTools.Method(type, method) - // Find the AsyncStateMachine class from target method - .GetCustomAttribute() - // Get the struct type (random compiler junk) - .StateMachineType.GetMethod("MoveNext", BindingFlags.Instance | BindingFlags.NonPublic); - } public void PatchAll() { diff --git a/LobbyCompatibility/Patches/ChallengeSaveFilePatch.cs b/LobbyCompatibility/Patches/ChallengeSaveFilePatch.cs deleted file mode 100644 index a77cfb6..0000000 --- a/LobbyCompatibility/Patches/ChallengeSaveFilePatch.cs +++ /dev/null @@ -1,16 +0,0 @@ -using HarmonyLib; - -namespace LobbyCompatibility.Patches; - -[HarmonyPatch] -[HarmonyPriority(Priority.Last)] -[HarmonyWrapSafe] -internal class ChallengeSaveFilePatch -{ - [HarmonyPrefix] - [HarmonyPatch(typeof(ES3Settings), MethodType.Constructor, new Type[] { typeof(string), typeof(ES3Settings) })] - private static void FileSettings(ref string path, ES3Settings settings) - { - path = (path == "LCChallengeFile") ? "LCModdedChallengeFile" : path; - } -} \ No newline at end of file diff --git a/LobbyCompatibility/Patches/ES3SettingsConstructorPrefix.cs b/LobbyCompatibility/Patches/ES3SettingsConstructorPrefix.cs new file mode 100644 index 0000000..d6b35c4 --- /dev/null +++ b/LobbyCompatibility/Patches/ES3SettingsConstructorPrefix.cs @@ -0,0 +1,21 @@ +using System; +using HarmonyLib; + +namespace LobbyCompatibility.Patches; + +/// +/// Patches . +/// Creates a separate modded challenge save file. +/// +/// +[HarmonyPatch(typeof(ES3Settings), MethodType.Constructor, new Type[] { typeof(string), typeof(ES3Settings) })] +[HarmonyPriority(Priority.Last)] +[HarmonyWrapSafe] +internal class ES3SettingsConstructorPrefix +{ + [HarmonyPrefix] + private static void FileSettings(ref string path) + { + path = path == "LCChallengeFile" ? "LCModdedChallengeFile" : path; + } +} \ No newline at end of file diff --git a/LobbyCompatibility/Patches/FindOrCreateLeaderboardPrefix.cs b/LobbyCompatibility/Patches/FindOrCreateLeaderboardPrefix.cs new file mode 100644 index 0000000..d4df41d --- /dev/null +++ b/LobbyCompatibility/Patches/FindOrCreateLeaderboardPrefix.cs @@ -0,0 +1,21 @@ +using HarmonyLib; +using Steamworks; + +namespace LobbyCompatibility.Patches; + +/// +/// Patches . +/// Changes the target leaderboard to a modded one. +/// +/// +[HarmonyPatch(typeof(ISteamUserStats), nameof(ISteamUserStats.FindOrCreateLeaderboard))] +[HarmonyPriority(Priority.Last)] +[HarmonyWrapSafe] +internal class FindOrCreateLeaderboardPrefix +{ + [HarmonyPrefix] + private static void Prefix(ref string pchLeaderboardName) + { + pchLeaderboardName = (pchLeaderboardName.StartsWith("challenge")) ? $"modded_{pchLeaderboardName}" : pchLeaderboardName; + } +} \ No newline at end of file diff --git a/LobbyCompatibility/Patches/LeaderboardNamePatch.cs b/LobbyCompatibility/Patches/LeaderboardNamePatch.cs deleted file mode 100644 index 662802c..0000000 --- a/LobbyCompatibility/Patches/LeaderboardNamePatch.cs +++ /dev/null @@ -1,17 +0,0 @@ -using HarmonyLib; -using Steamworks; - -namespace LobbyCompatibility.Patches; - -[HarmonyPatch] -[HarmonyPriority(Priority.Last)] -[HarmonyWrapSafe] -internal class LeaderboardNamePatch -{ - [HarmonyPatch(typeof(ISteamUserStats), nameof(ISteamUserStats.FindOrCreateLeaderboard))] - [HarmonyPrefix] - private static void SavePrefix(ref string pchLeaderboardName) - { - pchLeaderboardName = (pchLeaderboardName.StartsWith("challenge")) ? $"modded_{pchLeaderboardName}" : pchLeaderboardName; - } -} \ No newline at end of file