-
-
Notifications
You must be signed in to change notification settings - Fork 124
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add IsClosestPlayer check before sending out mecha drones
- Loading branch information
Showing
3 changed files
with
103 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
NebulaPatcher/Patches/Transpilers/ConstructionSystem_Transpiler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
#region | ||
|
||
using System.Collections.Generic; | ||
using System.Reflection; | ||
using System.Reflection.Emit; | ||
using HarmonyLib; | ||
using NebulaModel.Logger; | ||
using NebulaModel.Packets.Factory; | ||
using NebulaWorld; | ||
using NebulaWorld.Player; | ||
using UnityEngine; | ||
|
||
#endregion | ||
|
||
namespace NebulaPatcher.Patches.Transpilers; | ||
|
||
[HarmonyPatch(typeof(ConstructionSystem))] | ||
internal class ConstructionSystem_Transpiler | ||
{ | ||
[HarmonyTranspiler] | ||
[HarmonyPatch(nameof(ConstructionSystem.AddBuildTargetToModules))] | ||
public static IEnumerable<CodeInstruction> AddBuildTargetToModules_Transpiler(IEnumerable<CodeInstruction> instructions) | ||
{ | ||
try | ||
{ | ||
/* Sync Prebuild.itemRequired changes by player, insert local method call after player.package.TakeTailItems | ||
Replace: if (num8 <= num) { this.player.mecha.constructionModule.InsertBuildTarget ... } | ||
With: if (num8 <= num && IsClosestPlayer(ref pos)) { this.player.mecha.constructionModule.InsertBuildTarget ... } | ||
*/ | ||
|
||
var codeMatcher = new CodeMatcher(instructions) | ||
.MatchForward(true, | ||
new CodeMatch(OpCodes.Ldloc_S), | ||
new CodeMatch(OpCodes.Ldloc_0), | ||
new CodeMatch(OpCodes.Bgt_Un) | ||
); | ||
Log.Info(codeMatcher.IsValid); | ||
var sqrDist = codeMatcher.InstructionAt(-2).operand; | ||
var skipLabel = codeMatcher.Operand; | ||
codeMatcher.Advance(1) | ||
.Insert( | ||
new CodeInstruction(OpCodes.Ldloc_S, sqrDist), | ||
new CodeInstruction(OpCodes.Ldarg_2), //ref Vector3 pos | ||
new CodeInstruction(OpCodes.Call, AccessTools.Method(typeof(ConstructionSystem_Transpiler), nameof(IsClosestPlayer))), | ||
new CodeInstruction(OpCodes.Brfalse_S, skipLabel) | ||
); | ||
|
||
return codeMatcher.InstructionEnumeration(); | ||
} | ||
catch (System.Exception e) | ||
{ | ||
Log.Error("Transpiler ConstructionSystem.AddBuildTargetToModules failed."); | ||
Log.Error(e); | ||
return instructions; | ||
} | ||
} | ||
|
||
static bool IsClosestPlayer(float sqrDist, ref Vector3 pos) | ||
{ | ||
if (!Multiplayer.IsActive || sqrDist < DroneManager.MinSqrDistance) return true; | ||
return sqrDist <= Multiplayer.Session.Drones.GetClosestRemotePlayerSqrDistance(pos); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters