Skip to content

Commit

Permalink
try-catch ILSIdleShipBackToWork and ILSWorkShipBackToIdle
Browse files Browse the repository at this point in the history
  • Loading branch information
starfi5h committed Apr 2, 2024
1 parent 78d2a24 commit 66cbb5f
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
#region

using System;
using NebulaAPI.Packets;
using NebulaModel.Logger;
using NebulaModel.Networking;
using NebulaModel.Packets;
using NebulaModel.Packets.Logistics;
using NebulaWorld;
using NebulaWorld.Logistics;

#endregion
Expand All @@ -16,18 +17,18 @@ public class ILSIdleShipBackToWorkProcessor : PacketProcessor<ILSIdleShipBackToW
{
protected override void ProcessPacket(ILSIdleShipBackToWork packet, NebulaConnection conn)
{
if (IsHost)
if (!IsClient)
{
return;
}

if (!IsClient)
try
{
return;
ILSShipManager.IdleShipGetToWork(packet);
}
using (Multiplayer.Session.Factories.IsIncomingRequest.On())
catch (Exception e)
{
ILSShipManager.IdleShipGetToWork(packet);
Log.Warn(e);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#region

using System;
using NebulaAPI.Packets;
using NebulaModel.Logger;
using NebulaModel.Networking;
using NebulaModel.Packets;
using NebulaModel.Packets.Logistics;
Expand All @@ -15,14 +17,18 @@ public class ILSWorkShipBackToIdleProcessor : PacketProcessor<ILSWorkShipBackToI
{
protected override void ProcessPacket(ILSWorkShipBackToIdle packet, NebulaConnection conn)
{
if (IsHost)
if (!IsClient)
{
return;
}

if (IsClient)
try
{
ILSShipManager.WorkShipBackToIdle(packet);
}
catch (Exception e)
{
Log.Warn(e);
}
}
}
24 changes: 13 additions & 11 deletions NebulaWorld/Logistics/ILSShipManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ public static void IdleShipGetToWork(ILSIdleShipBackToWork packet)
{
return; // This shouldn't happen, but guard just in case
}
if (stationComponent.idleShipCount <= 0 || stationComponent.workShipCount >= stationComponent.workShipDatas.Length)
{
return; // Ship count is outside the range
}

stationComponent.workShipDatas[stationComponent.workShipCount].stage = -2;
stationComponent.workShipDatas[stationComponent.workShipCount].planetA = packet.PlanetA;
Expand All @@ -71,11 +75,8 @@ public static void IdleShipGetToWork(ILSIdleShipBackToWork packet)
stationComponent.workShipDatas[stationComponent.workShipCount].warperCnt = packet.ShipWarperCount;
stationComponent.warperCount = packet.StationWarperCount;

if (stationComponent.idleShipCount > 0)
{
stationComponent.workShipCount++;
stationComponent.idleShipCount--;
}
stationComponent.workShipCount++;
stationComponent.idleShipCount--;
stationComponent.IdleShipGetToWork(packet.ShipIndex);

var shipSailSpeed = GameMain.history.logisticShipSailSpeedModified;
Expand Down Expand Up @@ -115,14 +116,15 @@ public static void WorkShipBackToIdle(ILSWorkShipBackToIdle packet)
{
return; // This shouldn't happen, but guard just in case
}
if (stationComponent.workShipCount <= 0 || stationComponent.workShipDatas.Length <= packet.WorkShipIndex)
{
return; // Ship count is outside the range
}

Array.Copy(stationComponent.workShipDatas, packet.WorkShipIndex + 1, stationComponent.workShipDatas,
packet.WorkShipIndex, stationComponent.workShipDatas.Length - packet.WorkShipIndex - 1);
if (stationComponent.workShipCount > 0)
{
stationComponent.workShipCount--;
stationComponent.idleShipCount++;
}
stationComponent.workShipCount--;
stationComponent.idleShipCount++;
stationComponent.WorkShipBackToIdle(packet.ShipIndex);
Array.Clear(stationComponent.workShipDatas, stationComponent.workShipCount,
stationComponent.workShipDatas.Length - stationComponent.workShipCount);
Expand Down Expand Up @@ -152,7 +154,7 @@ public static void CreateFakeStationComponent(int gId, int planetId, int maxShip
stationComponent.shipRenderers = new ShipRenderingData[maxShipCount];
stationComponent.shipUIRenderers = new ShipUIRenderingData[maxShipCount];
stationComponent.workShipCount = 0;
stationComponent.idleShipCount = 0;
stationComponent.idleShipCount = maxShipCount; // add dummy idle ship count to use in ILSShipManager
stationComponent.shipDockPos = Vector3.zero; //gets updated later by server packet
stationComponent.shipDockRot = Quaternion.identity; // gets updated later by server packet
stationComponent.storage = []; // zero-length array for mod compatibility
Expand Down

0 comments on commit 66cbb5f

Please sign in to comment.