-
-
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.
Merge pull request #693 from AlienXAXS/relay_state_sync
Enemy Relay Direction Sync
- Loading branch information
Showing
3 changed files
with
150 additions
and
0 deletions.
There are no files selected for viewing
26 changes: 26 additions & 0 deletions
26
NebulaModel/Packets/Combat/DFRelay/DFRelayDirectionStateChangePacket.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,26 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace NebulaModel.Packets.Combat.DFRelay | ||
{ | ||
public class DFRelayDirectionStateChangePacket | ||
{ | ||
public DFRelayDirectionStateChangePacket() { } | ||
|
||
public DFRelayDirectionStateChangePacket(in int relayId, in int hiveAstroId, in int stage, in int newDirection) | ||
{ | ||
HiveAstroId = hiveAstroId; | ||
RelayId = relayId; | ||
Stage = stage; | ||
NewDirection = newDirection; | ||
} | ||
|
||
public int HiveAstroId { get; set; } | ||
public int RelayId { get; set; } | ||
public int Stage { get; set; } | ||
public int NewDirection { get; set; } | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
NebulaNetwork/PacketProcessors/Combat/DFRelay/DFRelayDirectionStateChangeProcessor.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,42 @@ | ||
using NebulaAPI.Packets; | ||
using NebulaModel.Logger; | ||
using NebulaModel.Networking; | ||
using NebulaModel.Packets; | ||
using NebulaModel.Packets.Combat.DFRelay; | ||
using UnityEngine; | ||
|
||
namespace NebulaNetwork.PacketProcessors.Combat.DFRelay | ||
{ | ||
[RegisterPacketProcessor] | ||
public class DFRelayDirectionStateChangeProcessor : PacketProcessor<DFRelayDirectionStateChangePacket> | ||
{ | ||
protected override void ProcessPacket(DFRelayDirectionStateChangePacket packet, NebulaConnection conn) | ||
{ | ||
var hiveSystem = GameMain.spaceSector.GetHiveByAstroId(packet.HiveAstroId); | ||
if (hiveSystem == null) return; | ||
|
||
Check warning on line 17 in NebulaNetwork/PacketProcessors/Combat/DFRelay/DFRelayDirectionStateChangeProcessor.cs GitHub Actions / build (Debug)
Check warning on line 17 in NebulaNetwork/PacketProcessors/Combat/DFRelay/DFRelayDirectionStateChangeProcessor.cs GitHub Actions / build (Debug)
Check warning on line 17 in NebulaNetwork/PacketProcessors/Combat/DFRelay/DFRelayDirectionStateChangeProcessor.cs GitHub Actions / build (Release)
|
||
var dfRelayComponent = hiveSystem.relays.buffer[packet.RelayId]; | ||
if (dfRelayComponent?.id != packet.RelayId) return; | ||
|
||
switch (packet.NewDirection) | ||
{ | ||
case -1: //Relay is being sent home | ||
dfRelayComponent.targetAstroId = 0; | ||
dfRelayComponent.targetLPos = Vector3.zero; | ||
dfRelayComponent.targetYaw = 0f; | ||
dfRelayComponent.baseState = 0; | ||
dfRelayComponent.baseId = 0; | ||
dfRelayComponent.baseTicks = 0; | ||
dfRelayComponent.baseEvolve = default(EvolveData); | ||
dfRelayComponent.baseRespawnCD = 0; | ||
dfRelayComponent.direction = -1; | ||
dfRelayComponent.param0 = 0f; | ||
|
||
dfRelayComponent.stage = packet.Stage; | ||
|
||
Log.Debug($"Relay {dfRelayComponent.id} returning home"); | ||
break; | ||
} | ||
} | ||
} | ||
} |
82 changes: 82 additions & 0 deletions
82
NebulaPatcher/Patches/Transpilers/EnemyDFRelayComponent_Transplier.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,82 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Reflection; | ||
using HarmonyLib; | ||
using System.Reflection.Emit; | ||
using NebulaModel.Logger; | ||
using NebulaModel.Packets.Combat.DFRelay; | ||
using NebulaWorld; | ||
|
||
namespace NebulaPatcher.Patches.Transpilers | ||
{ | ||
[HarmonyPatch(typeof(DFRelayComponent))] | ||
internal class EnemyDFRelayComponent_Transplier | ||
{ | ||
[HarmonyTranspiler] | ||
[HarmonyPatch(nameof(DFRelayComponent.RelaySailLogic))] | ||
public static IEnumerable<CodeInstruction> RelaySailLogic_Transpiler(IEnumerable<CodeInstruction> instructions) | ||
{ | ||
try | ||
{ | ||
// Attempt to match anywhere where `direction = -1` is set. | ||
// Change to: | ||
// direction = -1 | ||
// call UpdateRelayDirectionState(relayId, hive) | ||
|
||
var codeMatcher = new CodeMatcher(instructions); | ||
codeMatcher.MatchForward(true, | ||
new CodeMatch(OpCodes.Ldarg_0), | ||
new CodeMatch(OpCodes.Ldc_I4_M1), | ||
new CodeMatch(i => i.opcode == OpCodes.Stfld && ((FieldInfo)i.operand).Name == "direction")); | ||
|
||
if (codeMatcher.IsInvalid) | ||
{ | ||
Log.Error("Transpiler DFRelayComponent.RelaySailLogic matcher is not valid. Mod version not compatible with game version."); | ||
return instructions; | ||
} | ||
|
||
codeMatcher.Repeat(matcher => | ||
{ | ||
matcher.InsertAndAdvance( | ||
// Relay ID argument | ||
new CodeInstruction(OpCodes.Ldarg_0), | ||
new CodeInstruction(OpCodes.Ldfld, | ||
AccessTools.Field(typeof(DFRelayComponent), nameof(DFRelayComponent.id))), | ||
|
||
// Relay sail stage argument | ||
new CodeInstruction(OpCodes.Ldarg_0), | ||
new CodeInstruction(OpCodes.Ldfld, | ||
AccessTools.Field(typeof(DFRelayComponent), nameof(DFRelayComponent.stage))), | ||
|
||
// Hive Astro ID argument | ||
new CodeInstruction(OpCodes.Ldarg_0), | ||
new CodeInstruction(OpCodes.Ldfld, | ||
AccessTools.Field(typeof(DFRelayComponent), nameof(DFRelayComponent.hiveAstroId))), | ||
|
||
//Call our method | ||
new CodeInstruction(OpCodes.Call, | ||
AccessTools.Method(typeof(EnemyDFRelayComponent_Transplier), | ||
nameof(ReplicateRelayDirectionChange)))); | ||
}); | ||
|
||
return codeMatcher.InstructionEnumeration(); | ||
} | ||
catch (Exception e) | ||
{ | ||
Log.Error("Transpiler DFRelayComponent.RelaySailLogic failed. Mod version not compatible with game version."); | ||
Log.Error(e); | ||
return instructions; | ||
} | ||
} | ||
|
||
static void ReplicateRelayDirectionChange(int relayId, int stage, int hiveAstroId) | ||
{ | ||
if (!Multiplayer.IsActive) return; | ||
if (!Multiplayer.Session.IsClient) | ||
{ | ||
Multiplayer.Session.Network.SendPacket(new DFRelayDirectionStateChangePacket(relayId, hiveAstroId, stage, -1)); | ||
} | ||
} | ||
} | ||
} |