From 1a85fb6a96118479711cae5f73ae9fe39b9331c3 Mon Sep 17 00:00:00 2001 From: starfish <50672801+starfi5h@users.noreply.github.com> Date: Tue, 2 Jul 2024 16:01:58 +0800 Subject: [PATCH] Add extra check in enemies --- .../GroundEnemy/DFGKillEnemyProcessor.cs | 2 +- .../EnemyDFHiveSystem_Transpiler.cs | 37 +++++++++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/NebulaNetwork/PacketProcessors/Combat/GroundEnemy/DFGKillEnemyProcessor.cs b/NebulaNetwork/PacketProcessors/Combat/GroundEnemy/DFGKillEnemyProcessor.cs index f98d09696..16832acd6 100644 --- a/NebulaNetwork/PacketProcessors/Combat/GroundEnemy/DFGKillEnemyProcessor.cs +++ b/NebulaNetwork/PacketProcessors/Combat/GroundEnemy/DFGKillEnemyProcessor.cs @@ -16,7 +16,7 @@ public class DFGKillEnemyProcessor : PacketProcessor protected override void ProcessPacket(DFGKillEnemyPacket packet, NebulaConnection conn) { var factory = GameMain.galaxy.PlanetById(packet.PlanetId)?.factory; - if (factory == null) return; + if (factory == null || packet.EnemyId >= factory.enemyPool.Length) return; if (IsHost) { diff --git a/NebulaPatcher/Patches/Transpilers/EnemyDFHiveSystem_Transpiler.cs b/NebulaPatcher/Patches/Transpilers/EnemyDFHiveSystem_Transpiler.cs index 47a8289fa..d130655b0 100644 --- a/NebulaPatcher/Patches/Transpilers/EnemyDFHiveSystem_Transpiler.cs +++ b/NebulaPatcher/Patches/Transpilers/EnemyDFHiveSystem_Transpiler.cs @@ -37,6 +37,43 @@ public static IEnumerable GameTickLogic_Transpiler(IEnumerable< .MatchForward(true, new CodeMatch(OpCodes.Callvirt, AccessTools.Method(typeof(DFRelayComponent), nameof(DFRelayComponent.RealizePlanetBase)))) .Set(OpCodes.Call, AccessTools.Method(typeof(EnemyDFHiveSystem_Transpiler), nameof(RealizePlanetBase))); + + // Insert null guard in EnemyUnitComponent loop + // if (ptr10.id == k) + // { + // ref EnemyData ptr11 = ref enemyPool[ptr10.enemyId]; + // PrefabDesc prefabDesc = SpaceSector.PrefabDescByModelIndex[(int)ptr11.modelIndex]; + // if (prefabDesc == null) continue; // null guard + + codeMatcher.MatchForward(true, + new CodeMatch(OpCodes.Ldloc_S), + new CodeMatch(OpCodes.Ldfld, AccessTools.Field(typeof(EnemyUnitComponent), nameof(EnemyUnitComponent.id))), + new CodeMatch(OpCodes.Ldloc_S), + new CodeMatch(OpCodes.Bne_Un)); + if (codeMatcher.IsInvalid) + { + Log.Warn("EnemyDFHiveSystem.GameTickLogic: Can't find operand_continue"); + return codeMatcher.InstructionEnumeration(); + } + var operand_continue = codeMatcher.Operand; + + codeMatcher.MatchForward(true, + new CodeMatch(OpCodes.Ldsfld, AccessTools.Field(typeof(SpaceSector), nameof(SpaceSector.PrefabDescByModelIndex))), + new CodeMatch(OpCodes.Ldloc_S), + new CodeMatch(OpCodes.Ldfld), + new CodeMatch(OpCodes.Ldelem_Ref), + new CodeMatch(OpCodes.Stloc_S)); + if (codeMatcher.IsInvalid) + { + Log.Warn("EnemyDFHiveSystem.GameTickLogic: Can't find operand_prefabDesc"); + return codeMatcher.InstructionEnumeration(); + } + var operand_prefabDesc = codeMatcher.Operand; + + codeMatcher.Advance(1).Insert( + new CodeInstruction(OpCodes.Ldloc_S, operand_prefabDesc), + new CodeInstruction(OpCodes.Brfalse_S, operand_continue)); + return codeMatcher.InstructionEnumeration(); } catch (System.Exception e)