Skip to content

Commit

Permalink
Add null check for DFGBaseComponent
Browse files Browse the repository at this point in the history
  • Loading branch information
starfi5h committed Apr 10, 2024
1 parent 3dc5fae commit f6d5304
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 3 deletions.
15 changes: 15 additions & 0 deletions NebulaModel/Packets/Combat/GroundEnemy/DFGRemoveBasePacket.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace NebulaModel.Packets.Combat.GroundEnemy;

public class DFGRemoveBasePacket
{
public DFGRemoveBasePacket() { }

public DFGRemoveBasePacket(int planetId, int baseId)
{
PlanetId = planetId;
BaseId = baseId;
}

public int PlanetId { get; set; }
public int BaseId { get; set; }
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#region

using NebulaAPI.DataStructures;
using NebulaAPI.Packets;
using NebulaModel.Networking;
using NebulaModel.Packets;
Expand All @@ -27,6 +26,7 @@ protected override void ProcessPacket(DFGActivateBasePacket packet, NebulaConnec
if (!packet.SetToSeekForm)
{
var dFBase = factory.enemySystem.bases.buffer[packet.BaseId];
if (dFBase == null) return;
dFBase.activeTick = 3;
using (Multiplayer.Session.Combat.IsIncomingRequest.On())
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ protected override void ProcessPacket(DFGActivateUnitPacket packet, NebulaConnec
{
EnemyManager.SetPlanetFactoryNextEnemyId(factory, packet.EnemyId);
var dfBase = factory.enemySystem.bases.buffer[packet.BaseId];
if (dfBase == null) return;
var gameTick = GameMain.gameTick;

// the value inside enemyFormation.units[portId] is not reliable, so just overwrite it
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ protected override void ProcessPacket(DFGDeferredCreateEnemyPacket packet, Nebul
using (Multiplayer.Session.Combat.IsIncomingRequest.On())
{
EnemyManager.SetPlanetFactoryNextEnemyId(factory, packet.EnemyId);
if (factory.enemySystem.bases.buffer[packet.BaseId] == null) return;
var enemyId = factory.CreateEnemyFinal(packet.BaseId, packet.BuilderIndex);

#if DEBUG
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using NebulaModel.Packets;
using NebulaModel.Packets.Combat.GroundEnemy;
using NebulaWorld;
using static UnityEngine.UI.CanvasScaler;

#endregion

Expand All @@ -20,6 +19,7 @@ protected override void ProcessPacket(DFGFormationAddUnitPacket packet, NebulaCo
if (factory == null) return;

var dFBase = factory.enemySystem.bases.buffer[packet.BaseId];
if (dFBase == null) return;
using (Multiplayer.Session.Combat.IsIncomingRequest.On())
{
// Set the next id in EnemyFormation
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#region

using System;
using NebulaAPI.DataStructures;
using NebulaAPI.Packets;
using NebulaModel.Networking;
Expand Down Expand Up @@ -32,6 +31,7 @@ protected override void ProcessPacket(DFGLaunchAssaultPacket packet, NebulaConne
EnemyManager.SetPlanetFactoryRecycle(factory, packet.EnemyCursor, packet.EnemyRecyle);

var dFBase = factory.enemySystem.bases.buffer[packet.BaseId];
if (dFBase == null) return;
dFBase.turboTicks = 60;
dFBase.turboRepress = 0;
dFBase.evolve.threat = packet.EvolveThreat;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#region

using NebulaAPI.Packets;
using NebulaModel.Networking;
using NebulaModel.Packets;
using NebulaModel.Packets.Combat.GroundEnemy;
using NebulaWorld;

#endregion

namespace NebulaNetwork.PacketProcessors.Combat.GroundEnemy;

[RegisterPacketProcessor]
public class DFGRemoveBaseProcessor : PacketProcessor<DFGRemoveBasePacket>
{
protected override void ProcessPacket(DFGRemoveBasePacket packet, NebulaConnection conn)
{
var factory = GameMain.galaxy.PlanetById(packet.PlanetId)?.factory;
if (IsHost || factory == null) return;

using (Multiplayer.Session.Combat.IsIncomingRequest.On())
{
// EnemyDFGroundSystem.bases.SetNull(id);
factory.enemySystem.RemoveDFGBaseComponent(packet.BaseId);
}
}
}
17 changes: 17 additions & 0 deletions NebulaPatcher/Patches/Dynamic/EnemyDFGroundSystem_Patch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,23 @@ public static bool RemoveBasePit_Prefix(EnemyDFGroundSystem __instance, int pitR
return false;
}

[HarmonyPrefix]
[HarmonyPatch(nameof(EnemyDFGroundSystem.RemoveDFGBaseComponent))]
public static bool RemoveDFGBaseComponent_Prefix(EnemyDFGroundSystem __instance, int id)
{
if (!Multiplayer.IsActive) return true;

if (Multiplayer.Session.IsServer)
{
var packet = new DFGRemoveBasePacket(__instance.factory.planetId, id);
Multiplayer.Session.Network.SendPacketToStar(packet, __instance.factory.planet.star.id);
return true;
}

// Client should wait for server to approve the removal of base from the base buffer
return Multiplayer.Session.Combat.IsIncomingRequest;
}

[HarmonyPrefix]
[HarmonyPatch(nameof(EnemyDFGroundSystem.GameTickLogic))]
public static void GameTickLogic_Prefix(EnemyDFGroundSystem __instance, long gameTick)
Expand Down

0 comments on commit f6d5304

Please sign in to comment.