Skip to content

Commit

Permalink
Merge pull request #670 from starfi5h/pr-bugfix
Browse files Browse the repository at this point in the history
Bugfixes (Game version 0.10.29.22015)
  • Loading branch information
starfi5h authored Apr 3, 2024
2 parents 0849f19 + 66cbb5f commit 80f98e6
Show file tree
Hide file tree
Showing 17 changed files with 199 additions and 87 deletions.
7 changes: 5 additions & 2 deletions NebulaModel/Networking/NebulaConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
Expand Down
5 changes: 3 additions & 2 deletions NebulaModel/Packets/Logistics/ILSAddStationComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#region

using System;
using NebulaAPI.Packets;
using NebulaModel.Logger;
using NebulaModel.Networking;
Expand All @@ -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
{
Expand Down
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
Expand Up @@ -2,13 +2,10 @@

using System.Collections.Generic;
using NebulaAPI.DataStructures;
using NebulaAPI.GameState;
using NebulaAPI.Networking;
using NebulaAPI.Packets;
using NebulaModel.Networking;
using NebulaModel.Packets;
using NebulaModel.Packets.Logistics;
using NebulaWorld;

#endregion

Expand All @@ -30,9 +27,8 @@ protected override void ProcessPacket(ILSRequestShipDock packet, NebulaConnectio
return;
}

var player = Players.Get(conn, EConnectionStatus.Connected) ?? Players.Get(conn, EConnectionStatus.Syncing);

if (player == null || GameMain.data.galacticTransport.stationCapacity <= packet.StationGId)
var stationPool = GameMain.data.galacticTransport.stationPool;
if (stationPool.Length <= packet.StationGId || stationPool[packet.StationGId] == null)
{
return;
}
Expand All @@ -47,12 +43,12 @@ protected override void ProcessPacket(ILSRequestShipDock packet, NebulaConnectio
// find ShipData that has otherGId set to packet.stationGId
for (var i = 0; i < GameMain.data.galacticTransport.stationCapacity; i++)
{
if (GameMain.data.galacticTransport.stationPool[i] == null)
if (stationPool[i] == null)
{
continue;
}

var shipData = GameMain.data.galacticTransport.stationPool[i].workShipDatas;
var shipData = stationPool[i].workShipDatas;

for (var j = 0; j < shipData.Length; j++)
{
Expand All @@ -73,7 +69,7 @@ protected override void ProcessPacket(ILSRequestShipDock packet, NebulaConnectio

// also add add ships of current station as they use the dock pos too in the pos calculation
// NOTE: we need to set this stations gid as otherStationGId so that the client accesses the array in the right way
var shipData2 = GameMain.data.galacticTransport.stationPool[packet.StationGId].workShipDatas;
var shipData2 = stationPool[packet.StationGId].workShipDatas;

for (var i = 0; i < shipData2.Length; i++)
{
Expand All @@ -86,14 +82,14 @@ protected override void ProcessPacket(ILSRequestShipDock packet, NebulaConnectio
}

var packet2 = new ILSShipDock(packet.StationGId,
GameMain.data.galacticTransport.stationPool[packet.StationGId].shipDockPos,
GameMain.data.galacticTransport.stationPool[packet.StationGId].shipDockRot,
shipOtherGId.ToArray(),
shipIndex.ToArray(),
shipPos.ToArray(),
shipRot.ToArray(),
shipPPosTemp.ToArray(),
shipPRotTemp.ToArray());
player.SendPacket(packet2);
stationPool[packet.StationGId].shipDockPos,
stationPool[packet.StationGId].shipDockRot,
[.. shipOtherGId],
[.. shipIndex],
[.. shipPos],
[.. shipRot],
[.. shipPPosTemp],
[.. shipPRotTemp]);
conn.SendPacket(packet2);
}
}
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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ protected override void ProcessPacket(StationUI packet, NebulaConnection conn)
StationUIManager.UpdateStation(ref packet);

// broadcast to other clients
var player = Players.Get(conn);
Server.SendPacketExclude(packet, conn);

// as we block the normal method for the client he must run it once he receives this packet.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#region

using NebulaAPI.GameState;
using NebulaAPI.Packets;
using NebulaModel.Networking;
using NebulaModel.Packets;
Expand All @@ -23,7 +22,6 @@ protected override void ProcessPacket(StorageUI packet, NebulaConnection conn)
Multiplayer.Session.StationsUI.UpdateStorage(packet);

// broadcast to other clients
var player = Players.Get(conn);
Server.SendPacketExclude(packet, conn);

// as we block some methods for the client he must run it once he receives this packet.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
#region

using NebulaAPI.GameState;
using NebulaAPI.Packets;
using NebulaModel.Networking;
using NebulaModel.Packets;
using NebulaModel.Packets.Players;
using NebulaWorld;

#endregion

Expand Down Expand Up @@ -69,10 +67,12 @@ protected override void ProcessPacket(PlayerMechaDIYArmor packet, NebulaConnecti
editor.mecha.diyAppearance = new MechaAppearance();
editor.mecha.diyAppearance.Init();
}
// Modify from UIMechaEditor.ApplyMechaAppearance
GameMain.mainPlayer.mecha.diyAppearance.CopyTo(editor.mecha.diyAppearance);
editor.mechaArmorModel.RefreshAllPartObjects();
editor.mechaArmorModel.RefreshAllBoneObjects();
editor.mecha.diyAppearance.NotifyAllEvents();
editor.CalcMechaProperty(); // set mecha.hp and mecha.energyConsumptionCoef
editor._left_content_height_max = 0f;
editor.SetLeftScrollTop();
editor.saveGroup._Open();
Expand Down
13 changes: 13 additions & 0 deletions NebulaPatcher/Patches/Dynamic/Dedicated_Server_Patch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public static void GameMainBegin_Postfix()
[HarmonyPatch(typeof(GameData), nameof(GameData.OnDraw))]
[HarmonyPatch(typeof(GameData), nameof(GameData.OnPostDraw))]
[HarmonyPatch(typeof(FactoryModel), nameof(FactoryModel.LateUpdate))]
[HarmonyPatch(typeof(SectorModel), nameof(SectorModel.LateUpdate))]
public static bool OnDraw_Prefix()
{
return false;
Expand Down Expand Up @@ -159,4 +160,16 @@ public static bool DoMeshGeneration_Prefix()
{
return false;
}

[HarmonyPostfix]
[HarmonyPatch(typeof(PlanetATField), nameof(PlanetATField.RecalculatePhysicsShape))]
public static void RecalculatePhysicsShape_Postfix(PlanetATField __instance)
{
// vanilla use GPU to calculate the shape of shield that is not fully covered the whole planet
// In this patch it will act as it has been fully covered to make the planet shield effective
if (!__instance.isEmpty)
{
__instance.isSpherical = true;
}
}
}
30 changes: 30 additions & 0 deletions NebulaPatcher/Patches/Dynamic/EnemyDFGroundSystem_Patch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

using System;
using HarmonyLib;
using NebulaModel.Logger;
using NebulaModel.Packets.Combat.GroundEnemy;
using NebulaWorld;

Expand Down Expand Up @@ -159,4 +160,33 @@ public static void GameTickLogic_Prefix(EnemyDFGroundSystem __instance, long gam
Multiplayer.Session.Enemies.BroadcastBaseStatusPackets(__instance, gameTick);
}
}

[HarmonyPrefix]
[HarmonyPatch(nameof(EnemyDFGroundSystem.KeyTickLogic))]
public static void KeyTickLogic_Prefix(EnemyDFGroundSystem __instance)
{
if (!Multiplayer.IsActive || Multiplayer.Session.IsServer) return;

// Fix NRE in EnemyDFGroundSystem.KeyTickLogic (System.Int64 time);(IL_0929)
var cursor = __instance.builders.cursor;
var buffer = __instance.builders.buffer;
var baseBuffer = __instance.bases.buffer;
var enemyPool = __instance.factory.enemyPool;
for (var builderId = 1; builderId < cursor; builderId++)
{
ref var builder = ref buffer[builderId];
if (builder.id == builderId)
{
if (baseBuffer[enemyPool[builder.enemyId].owner] == null)
{
var msg = $"Remove EnemyDFGroundSystem enemy[{builder.enemyId}]: owner = {enemyPool[builder.enemyId].owner}";
Log.WarnInform(msg);

__instance.factory.enemyPool[builder.enemyId].SetEmpty();
__instance.builders.Remove(builderId);
}
}
}
}

}
26 changes: 26 additions & 0 deletions NebulaPatcher/Patches/Dynamic/EnemyFormation_Patch.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#region

using HarmonyLib;

#endregion

namespace NebulaPatcher.Patches.Dynamic;

[HarmonyPatch(typeof(EnemyFormation))]
internal class EnemyFormation_Patch
{
[HarmonyPrefix]
[HarmonyPatch(typeof(EnemyFormation), nameof(EnemyFormation.RemoveUnit))]
public static bool RemoveUnit_Prefix(EnemyFormation __instance, int port)
{
if (__instance.units[port] != 0)
{
if (__instance.vacancyCursor < __instance.vacancies.Length) // guard
{
__instance.vacancies[__instance.vacancyCursor++] = port;
}
__instance.units[port] = 0;
}
return false;
}
}
13 changes: 5 additions & 8 deletions NebulaPatcher/Patches/Dynamic/PlanetTransport_Patch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ namespace NebulaPatcher.Patches.Dynamic;
[HarmonyPatch(typeof(PlanetTransport))]
internal class PlanetTransport_Patch
{
private static int RemovingStationGId;

[HarmonyPrefix]
[HarmonyPatch(nameof(PlanetTransport.SetStationStorage))]
public static bool SetStationStorage_Prefix(PlanetTransport __instance, int stationId, int storageIdx, int itemId,
Expand Down Expand Up @@ -62,7 +60,7 @@ public static void NewStationComponent_Postfix(PlanetTransport __instance, Stati
Log.Info(
$"Send AddStationComponen to all clients for planet {__result.planetId}, id {__result.id} with gId of {__result.gid}");
Multiplayer.Session.Network.SendPacket(new ILSAddStationComponent(__result.planetId, __result.id, __result.gid,
_desc.stationMaxShipCount));
__result.entityId, _desc.stationMaxShipCount));
}

[HarmonyPostfix]
Expand All @@ -89,10 +87,9 @@ public static void Import_Postfix(PlanetTransport __instance)
*/
[HarmonyPrefix]
[HarmonyPatch(nameof(PlanetTransport.RemoveStationComponent))]
public static bool RemoveStationComponent_Prefix(PlanetTransport __instance, int id)
public static bool RemoveStationComponent_Prefix(PlanetTransport __instance, int id, ref int __state)
{
RemovingStationGId =
__instance.stationPool[id].gid; // cache this as we need it in the postfix but its gone there already.
__state = __instance.stationPool[id].gid; // cache this as we need it in the postfix but its gone there already.
return !Multiplayer.IsActive || Multiplayer.Session.LocalPlayer.IsHost || Multiplayer.Session.Ships.PatchLockILS;
}

Expand All @@ -101,13 +98,13 @@ public static bool RemoveStationComponent_Prefix(PlanetTransport __instance, int
*/
[HarmonyPostfix]
[HarmonyPatch(nameof(PlanetTransport.RemoveStationComponent))]
public static void RemoveStationComponent_Postfix(PlanetTransport __instance, int id)
public static void RemoveStationComponent_Postfix(PlanetTransport __instance, int id, int __state)
{
if (!Multiplayer.IsActive || !Multiplayer.Session.LocalPlayer.IsHost)
{
return;
}
Multiplayer.Session.Network.SendPacket(new ILSRemoveStationComponent(id, __instance.planet.id, RemovingStationGId));
Multiplayer.Session.Network.SendPacket(new ILSRemoveStationComponent(id, __instance.planet.id, __state));
}

[HarmonyPrefix]
Expand Down
Loading

0 comments on commit 80f98e6

Please sign in to comment.