diff --git a/NebulaModel/Networking/NebulaConnection.cs b/NebulaModel/Networking/NebulaConnection.cs index ccad95b7f..5d0306fbb 100644 --- a/NebulaModel/Networking/NebulaConnection.cs +++ b/NebulaModel/Networking/NebulaConnection.cs @@ -55,8 +55,11 @@ public NebulaConnection(WebSocket peerSocket, EndPoint peerEndpoint, NebulaNetPa { lock (pendingPackets) { - var rawData = packetProcessor.Write(packet); - pendingPackets.Enqueue(rawData); + lock (packetProcessor) + { + var rawData = packetProcessor.Write(packet); //not-threadsafe + pendingPackets.Enqueue(rawData); + } ProcessPacketQueue(); } } diff --git a/NebulaModel/Packets/Logistics/ILSAddStationComponent.cs b/NebulaModel/Packets/Logistics/ILSAddStationComponent.cs index 3e35fb258..0b70aee24 100644 --- a/NebulaModel/Packets/Logistics/ILSAddStationComponent.cs +++ b/NebulaModel/Packets/Logistics/ILSAddStationComponent.cs @@ -4,17 +4,18 @@ public class ILSAddStationComponent { public ILSAddStationComponent() { } - public ILSAddStationComponent(int planetId, int stationId, int stationGId, int maxShipCount) + public ILSAddStationComponent(int planetId, int stationId, int stationGId, int entityId, int maxShipCount) { StationGId = stationGId; PlanetId = planetId; StationId = stationId; + EntityId = entityId; MaxShipCount = maxShipCount; } public int PlanetId { get; set; } public int StationId { get; set; } public int StationGId { get; set; } - + public int EntityId { get; set; } public int MaxShipCount { get; set; } } diff --git a/NebulaNetwork/PacketProcessors/Logistics/ILSAddStationComponentProcessor.cs b/NebulaNetwork/PacketProcessors/Logistics/ILSAddStationComponentProcessor.cs index 1509fed41..835aab785 100644 --- a/NebulaNetwork/PacketProcessors/Logistics/ILSAddStationComponentProcessor.cs +++ b/NebulaNetwork/PacketProcessors/Logistics/ILSAddStationComponentProcessor.cs @@ -1,5 +1,6 @@ #region +using System; using NebulaAPI.Packets; using NebulaModel.Logger; using NebulaModel.Networking; @@ -23,14 +24,24 @@ protected override void ProcessPacket(ILSAddStationComponent packet, NebulaConne using (Multiplayer.Session.Ships.PatchLockILS.On()) { var galacticTransport = GameMain.data.galacticTransport; - var stationPool = GameMain.galaxy.PlanetById(packet.PlanetId).factory?.transport.stationPool; + var stationPool = GameMain.galaxy.PlanetById(packet.PlanetId)?.factory?.transport.stationPool; if (stationPool != null) { // If we have loaded the factory where the new station was created on, should be able to find // it in our PlanetTransport.stationPool // Assgin gid here so this station will go to galacticTransport.stationPool[gid] stationPool[packet.StationId].gid = packet.StationGId; - galacticTransport.AddStationComponent(packet.PlanetId, stationPool[packet.StationId]); + if (galacticTransport.AddStationComponent(packet.PlanetId, stationPool[packet.StationId]) != packet.StationGId) + { + Log.WarnInform($"AddStationComponent gid mismatch: {stationPool[packet.StationId].gid} => packet.StationGId"); + galacticTransport.stationPool[packet.StationGId] = stationPool[packet.StationId]; + } + galacticTransport.stationCursor = Math.Max(galacticTransport.stationCursor, packet.StationGId + 1); + + if (stationPool[packet.StationId].entityId != packet.EntityId) + { + Log.WarnInform($"Station gid {packet.StationGId} entityId mismatch: {stationPool[packet.StationId].entityId} => {packet.EntityId}"); + } } else { diff --git a/NebulaNetwork/PacketProcessors/Logistics/ILSIdleShipBackToWorkProcessor.cs b/NebulaNetwork/PacketProcessors/Logistics/ILSIdleShipBackToWorkProcessor.cs index 1ccdd993e..187862c75 100644 --- a/NebulaNetwork/PacketProcessors/Logistics/ILSIdleShipBackToWorkProcessor.cs +++ b/NebulaNetwork/PacketProcessors/Logistics/ILSIdleShipBackToWorkProcessor.cs @@ -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 @@ -16,18 +17,18 @@ public class ILSIdleShipBackToWorkProcessor : PacketProcessor= stationComponent.workShipDatas.Length) + { + return; // Ship count is outside the range + } stationComponent.workShipDatas[stationComponent.workShipCount].stage = -2; stationComponent.workShipDatas[stationComponent.workShipCount].planetA = packet.PlanetA; @@ -75,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; @@ -113,20 +110,21 @@ public static void WorkShipBackToIdle(ILSWorkShipBackToIdle packet) { CreateFakeStationComponent(packet.GId, packet.PlanetA, packet.StationMaxShipCount); } - else if (stationPool[packet.GId].shipDockPos == Vector3.zero) - { - RequestStationDockPos(packet.GId); - } var stationComponent = stationPool[packet.GId]; + if (stationComponent == null) + { + 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); @@ -156,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 diff --git a/NebulaWorld/Logistics/StationUIManager.cs b/NebulaWorld/Logistics/StationUIManager.cs index 183ec7b59..a775e2f52 100644 --- a/NebulaWorld/Logistics/StationUIManager.cs +++ b/NebulaWorld/Logistics/StationUIManager.cs @@ -260,11 +260,11 @@ private static StationComponent GetStation(int planetId, int stationId, int stat return null; } - var gStationPool = GameMain.data.galacticTransport.stationPool; - var stationPool = planet?.factory?.transport?.stationPool; + // Get the station from stationId on the planet + var stationPool = planet.factory.transport.stationPool; + var stationComponent = stationPool[stationId]; + var _ = stationGid; // Should ILS be dealt with differently? - // Figure out if we're dealing with a PLS or a ILS station - var stationComponent = stationGid > 0 ? gStationPool[stationGid] : stationPool?[stationId]; return stationComponent; } diff --git a/NebulaWorld/SimulatedWorld.cs b/NebulaWorld/SimulatedWorld.cs index 94da7062b..e65ddfc89 100644 --- a/NebulaWorld/SimulatedWorld.cs +++ b/NebulaWorld/SimulatedWorld.cs @@ -110,21 +110,7 @@ public void SetupInitialPlayerState() GameMain.mainPlayer.mecha.groundCombatModule.AfterImport(GameMain.data); // do we need to do something about the spaceSector? GameMain.mainPlayer.mecha.spaceCombatModule.AfterImport(GameMain.data); // do we need to do something about the spaceSector? - // Recycle all fleets - var module = GameMain.mainPlayer.mecha.groundCombatModule; - for (var fleetIndex = 0; fleetIndex < module.fleetCount; fleetIndex++) - { - ref var ptr = ref module.moduleFleets[fleetIndex]; - if (ptr.fleetId == 0) continue; - ptr.OnFleetComponentRemoved(); - } - module = GameMain.mainPlayer.mecha.spaceCombatModule; - for (var fleetIndex = 0; fleetIndex < module.fleetCount; fleetIndex++) - { - ref var ptr = ref module.moduleFleets[fleetIndex]; - if (ptr.fleetId == 0) continue; - ptr.OnFleetComponentRemoved(); - } + FixPlayerAfterImport(); } // Initialization on the host side after game is loaded @@ -206,6 +192,30 @@ public void SetupInitialPlayerState() GameMain.mainPlayer.gameObject.AddComponentIfMissing(); } + public static void FixPlayerAfterImport() + { + var player = GameMain.mainPlayer; + + // Inventory Capacity level 7 will increase package columncount from 10 -> 12 + var packageRowCount = (player.package.size - 1) / player.GetPackageColumnCount() + 1; + // Make sure all slots are available on UI + player.package.SetSize(player.packageColCount * packageRowCount); + player.deliveryPackage.rowCount = packageRowCount; + player.deliveryPackage.NotifySizeChange(); + + // Set fleetId = 0, fleetAstroId = 0 and fighter.craftId = 0 + var moduleFleets = player.mecha.groundCombatModule.moduleFleets; + for (var index = 0; index < moduleFleets.Length; index++) + { + moduleFleets[index].ClearFleetForeignKey(); + } + moduleFleets = player.mecha.spaceCombatModule.moduleFleets; + for (var index = 0; index < moduleFleets.Length; index++) + { + moduleFleets[index].ClearFleetForeignKey(); + } + } + public void OnPlayerJoining(string username) { if (IsPlayerJoining)