Skip to content

Commit

Permalink
BetterScoop updated for Update 3
Browse files Browse the repository at this point in the history
- Also update README with the new flattened structure
  • Loading branch information
Maverik committed Feb 26, 2024
1 parent 9a291d1 commit 1fdb76a
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 9 deletions.
73 changes: 67 additions & 6 deletions BetterScoop/BetterScoop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@
sealed class BetterScoop : MavsBepInExPlugin<BetterScoop, PluginConfiguration>
{
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");
Expand All @@ -25,6 +22,38 @@ sealed class BetterScoop : MavsBepInExPlugin<BetterScoop, PluginConfiguration>
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;
Expand All @@ -40,12 +69,10 @@ static void CarryableAttractorAwakePrefix(CarryableAttractor __instance)
if (PhotonNetwork.IsMasterClient)
LoggedExceptions(() =>
{
scoop.Set<CarryableAttractor, float>(_itemSearchInterval, _ => Configuration.ItemSearchInterval, Logger, nameof(Configuration.ItemSearchInterval));
scoop.Set<CarryableAttractor, float>(_itemSearchInitialDelay, _ => Configuration.ItemSearchInitialDelay, Logger, nameof(Configuration.ItemSearchInitialDelay));
scoop.Set(x => x.MaxRange, _ => Configuration.MaxRange, Logger, nameof(Configuration.MaxRange));
scoop.Set<CarryableAttractor, float>(_coneAngle, _ => Configuration.ConeAngle, Logger, nameof(Configuration.ConeAngle));
scoop.Set<CarryableAttractor, float>(_catchRadius, _ => Configuration.CatchRadius, Logger, nameof(Configuration.CatchRadius));
scoop.Set<CarryableAttractor, float>(_inheritVelocityValue, _ => Configuration.InheritVelocityValue, Logger, nameof(Configuration.InheritVelocityValue));
scoop.Set<CarryableAttractor, ModifiableFloat>(_maxRange, _ => Configuration.MaxRange, Logger, nameof(Configuration.MaxRange));
scoop.Set<CarryableAttractor, ModifiableFloat>(_pullVelocity, _ => Configuration.PullVelocity, Logger, nameof(Configuration.PullVelocity));
Logger.LogMessage("Scoop successfully supercharged!");
Expand Down Expand Up @@ -77,4 +104,38 @@ static void CarryableAttractorAwakePrefix(CarryableAttractor __instance)
#endif
return __result;
}))!;

[HarmonyTranspiler]
[HarmonyPatch(typeof(CarryableAttractor), "SubscribeAsMaster")]
static IEnumerable<CodeInstruction> CarryableAttractorSubscribeAsMasterTranspiler(IEnumerable<CodeInstruction> 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;
})!;
}
2 changes: 1 addition & 1 deletion BetterScoop/BetterScoop.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
<PropertyGroup>
<Title>Better Scoop</Title>
<Description>Make the gravity scoop, space worthy.</Description>
<Version>0.0.4</Version>
<Version>0.0.5</Version>
</PropertyGroup>
</Project>
4 changes: 4 additions & 0 deletions BetterScoop/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
4 changes: 2 additions & 2 deletions Solution Items/README.targets
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<ReaderMePluginContent Condition="'$(ReaderMePluginContent)' == '' and Exists('$(ProjectDir)_README.md')">$([System.IO.File]::ReadAllText('$(ProjectDir)_README.md'))</ReaderMePluginContent>
<ReadmeHeaderContent><![CDATA[
[![License: CC BY-NC-SA 4.0](https://img.shields.io/badge/License-CC%2520BY--NC--SA%25204.0-orange.svg?style=for-the-badge&logo=creativecommons&link=https%253A%252F%252Fcreativecommons.org%252Flicenses%252Fby-nc-sa%252F4.0%252F)](https://creativecommons.org/licenses/by-nc-sa/4.0/) &nbsp%3B ![Discord - Maverik](https://img.shields.io/badge/-Maverik-5865F2?style=for-the-badge&logo=discord&labelColor=black) &nbsp%3B [![Github - Maverik](https://img.shields.io/badge/-Maverik-111111?style=for-the-badge&logo=github&labelColor=24292f&link=https%3A%2F%2Fgithub.com%2FMaverik%2F)](https://github.com/Maverik)
[![License: CC BY-NC-SA 4.0](https://img.shields.io/badge/License-CC%2520BY--NC--SA%25204.0-orange.svg?style=for-the-badge&logo=creativecommons&link=https%253A%252F%252Fcreativecommons.org%252Flicenses%252Fby-nc-sa%252F4.0%252F)](https://creativecommons.org/licenses/by-nc-sa/4.0/) &nbsp%3B [![Github - Maverik](https://img.shields.io/badge/-Maverik-111111?style=for-the-badge&logo=github&labelColor=24292f&link=https%3A%2F%2Fgithub.com%2FMaverik%2F)](https://github.com/Maverik) &nbsp%3B ![Discord - Maverik](https://img.shields.io/badge/-Maverik-5865F2?style=for-the-badge&logo=discord&labelColor=black) &nbsp%3B&nbsp%3B [![Ko-Fi](https://img.shields.io/badge/Ko--fi-F16061?style=for-the-badge&logo=ko-fi&logoColor=white&labelColor=333&link=https%253A%252F%252Fko-fi.com%25%2FZ8Z6T7OWX)](https://ko-fi.com/Z8Z6T7OWX)
# $(Product)
Expand Down Expand Up @@ -37,7 +37,7 @@ My mods support configuration through [BepInEx Configuration Manager](https://gi
So you've got a working BepInEx 5 Mono installation running successfully. Now you can download this mods zip file. Unzip the contents into your game directory where you have your `Void Crew.exe`.
You should find the mod installed under `Bepinex\plugins\MavsPlugins\$(MSBuildProjectName)`.
You should find the mod installed under `Bepinex\plugins` as `$(TargetFileName)`.
### 👋 Shoutout
Expand Down

0 comments on commit 1fdb76a

Please sign in to comment.