From 1fdb76a56a88d1a91fd5d47cd6745a00ff565b57 Mon Sep 17 00:00:00 2001 From: Maverik Date: Mon, 26 Feb 2024 20:08:43 +0000 Subject: [PATCH] BetterScoop updated for Update 3 - Also update README with the new flattened structure --- BetterScoop/BetterScoop.cs | 73 +++++++++++++++++++++++++++++++--- BetterScoop/BetterScoop.csproj | 2 +- BetterScoop/CHANGELOG.md | 4 ++ Solution Items/README.targets | 4 +- 4 files changed, 74 insertions(+), 9 deletions(-) diff --git a/BetterScoop/BetterScoop.cs b/BetterScoop/BetterScoop.cs index 0292e33..0d16134 100644 --- a/BetterScoop/BetterScoop.cs +++ b/BetterScoop/BetterScoop.cs @@ -5,10 +5,7 @@ sealed class BetterScoop : MavsBepInExPlugin { static readonly Type _carryableAttractorType = typeof(CarryableAttractor); - static readonly FieldInfo _itemSearchInterval = AccessTools.Field(_carryableAttractorType, "_itemSearchInterval"); - static readonly FieldInfo _itemSearchInitialDelay = AccessTools.Field(_carryableAttractorType, "_itemSearchInitialDelay"); static readonly FieldInfo _coneAngle = AccessTools.Field(_carryableAttractorType, "_coneAngle"); - static readonly FieldInfo _maxRange = AccessTools.Field(_carryableAttractorType, "_maxRange"); static readonly FieldInfo _pullVelocity = AccessTools.Field(_carryableAttractorType, "_pullVelocity"); static readonly FieldInfo _catchRadius = AccessTools.Field(_carryableAttractorType, "_catchRadius"); static readonly FieldInfo _inheritVelocityValue = AccessTools.Field(_carryableAttractorType, "_inheritVelocityValue"); @@ -25,6 +22,38 @@ sealed class BetterScoop : MavsBepInExPlugin new("ee69440bbce371e458daeba6eee12a49") ]; + static readonly MethodInfo _getGameObject = AccessTools.PropertyGetter(typeof(UnityEngine.Component), "gameObject") ?? throw new ArgumentNullException("get_gameObject", "gameObject property getter was not found"); + static readonly MethodInfo _updateIntervaled = AccessTools.Method(_carryableAttractorType, "UpdateIntervaled") ?? throw new ArgumentNullException("UpdateIntervaled", "UpdateIntervaled method was not found"); + + static readonly ConstructorInfo _actionContructor = AccessTools.Constructor(typeof(Action), [typeof(object), typeof(IntPtr)]); + + static readonly CodeMatch[] _itemSearchIntervalCodeMatches = + [ + //IL_0026: ldarg.0 + new(OpCodes.Ldarg_0), + + //IL_0027: ldc.r4 0.25 + new(OpCodes.Ldc_R4, 0.25f), + + //IL_002c: ldarg.0 + new(OpCodes.Ldarg_0), + + //IL_002d: call instance class [UnityEngine.CoreModule]UnityEngine.GameObject [UnityEngine.CoreModule]UnityEngine.Component::get_gameObject() + new(OpCodes.Call, _getGameObject), + + // IL_0032: ldarg.0 + new(OpCodes.Ldarg_0), + + //IL_0033: ldftn instance void CG.Ship.Modules.CarryableAttractor::UpdateIntervaled() + new(OpCodes.Ldftn, _updateIntervaled), + + //IL_0039: newobj instance void [mscorlib]System.Action::.ctor(object, native int) + new(OpCodes.Newobj, _actionContructor), + + //IL_003e: ldc.r4 0.5 + new(OpCodes.Ldc_R4, 0.5f) + ]; + public override string DisplayName => PluginInfo.Title; public override Version Version => PluginInfo.Version; @@ -40,12 +69,10 @@ static void CarryableAttractorAwakePrefix(CarryableAttractor __instance) if (PhotonNetwork.IsMasterClient) LoggedExceptions(() => { - scoop.Set(_itemSearchInterval, _ => Configuration.ItemSearchInterval, Logger, nameof(Configuration.ItemSearchInterval)); - scoop.Set(_itemSearchInitialDelay, _ => Configuration.ItemSearchInitialDelay, Logger, nameof(Configuration.ItemSearchInitialDelay)); + scoop.Set(x => x.MaxRange, _ => Configuration.MaxRange, Logger, nameof(Configuration.MaxRange)); scoop.Set(_coneAngle, _ => Configuration.ConeAngle, Logger, nameof(Configuration.ConeAngle)); scoop.Set(_catchRadius, _ => Configuration.CatchRadius, Logger, nameof(Configuration.CatchRadius)); scoop.Set(_inheritVelocityValue, _ => Configuration.InheritVelocityValue, Logger, nameof(Configuration.InheritVelocityValue)); - scoop.Set(_maxRange, _ => Configuration.MaxRange, Logger, nameof(Configuration.MaxRange)); scoop.Set(_pullVelocity, _ => Configuration.PullVelocity, Logger, nameof(Configuration.PullVelocity)); Logger.LogMessage("Scoop successfully supercharged!"); @@ -77,4 +104,38 @@ static void CarryableAttractorAwakePrefix(CarryableAttractor __instance) #endif return __result; }))!; + + [HarmonyTranspiler] + [HarmonyPatch(typeof(CarryableAttractor), "SubscribeAsMaster")] + static IEnumerable CarryableAttractorSubscribeAsMasterTranspiler(IEnumerable instructions) => + LoggedExceptions(() => + { + + var config = new PluginConfiguration(); + // ReSharper disable once Unity.IncorrectMonoBehaviourInstantiation + // Not meant to instantiated as behaviour but only to pull the config out of bepinex + config.LoadFrom(new BetterScoop().Config); + + var patchedInstructions = new CodeMatcher(instructions).MatchForward(false, _itemSearchIntervalCodeMatches) + .ThrowIfInvalid("Code sequence could not be found for CarryableAttractor.SubscribeAsMaster patching") + .Advance(1) + .SetOperandAndAdvance(config.ItemSearchInterval) //frequency + .Advance(5) + .SetOperandAndAdvance(config.ItemSearchInitialDelay) //delay + .InstructionEnumeration() +#if DEBUG + .Select(x => + { + Logger.LogDebug(x.ToString()); + + return x; + }) +#endif + ; + + Logger.LogInfo($"CarryableAttractor.ItemSearchInterval patched from 0.25f to {config.ItemSearchInterval}f"); + Logger.LogInfo($"CarryableAttractor.ItemSearchInitialDelay patched from 0.5f to {config.ItemSearchInitialDelay}f"); + + return patchedInstructions; + })!; } \ No newline at end of file diff --git a/BetterScoop/BetterScoop.csproj b/BetterScoop/BetterScoop.csproj index 926b7fe..04c26af 100644 --- a/BetterScoop/BetterScoop.csproj +++ b/BetterScoop/BetterScoop.csproj @@ -2,6 +2,6 @@ Better Scoop Make the gravity scoop, space worthy. - 0.0.4 + 0.0.5 \ No newline at end of file diff --git a/BetterScoop/CHANGELOG.md b/BetterScoop/CHANGELOG.md index 7acfc49..acc679b 100644 --- a/BetterScoop/CHANGELOG.md +++ b/BetterScoop/CHANGELOG.md @@ -1,5 +1,9 @@ # 🔖 CHANGELOG +## 0.0.5 +- Fix all the scoop mess again +- They've made the updated code mod unfriendly so expect this mod to break with any update that decides to touch the scoop again + ## 0.0.4 - Clamp the acceptable value for PullVelocity to MaxRange - Add second lure id in exclusion list for scoop diff --git a/Solution Items/README.targets b/Solution Items/README.targets index 9118228..a8941fc 100644 --- a/Solution Items/README.targets +++ b/Solution Items/README.targets @@ -3,7 +3,7 @@ $([System.IO.File]::ReadAllText('$(ProjectDir)_README.md'))