Skip to content

Commit

Permalink
Fix multiple issues
Browse files Browse the repository at this point in the history
- Add backward compatibility in Savemanager
- Add sprayer and moniter connection syncing
  • Loading branch information
starfi5h committed Feb 10, 2022
1 parent d839dbf commit dec2eee
Show file tree
Hide file tree
Showing 21 changed files with 223 additions and 52 deletions.
19 changes: 19 additions & 0 deletions NebulaModel/DataStructures/PlayerData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,25 @@ public void Deserialize(INetDataReader reader)
}
}

// Old import method
public void DeserializeOld(INetDataReader reader, ushort revision)
{
Username = reader.GetString();
PlayerId = reader.GetUShort();
LocalPlanetId = reader.GetInt();
MechaColors = new Float4[reader.GetInt()];
for (int i = 0; i < MechaColors.Length; i++)
{
MechaColors[i] = reader.GetFloat4();
}
LocalPlanetPosition = reader.GetFloat3();
UPosition = reader.GetDouble3();
Rotation = reader.GetFloat3();
BodyRotation = reader.GetFloat3();
Mecha = new MechaData();
Mecha.Deserialize(reader);
}

public IPlayerData CreateCopyWithoutMechaData()
{
return new PlayerData(PlayerId, LocalPlanetId, MechaColors, Username, LocalPlanetPosition, UPosition, Rotation, BodyRotation);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using NebulaAPI;

namespace NebulaModel.Packets.Belt
namespace NebulaModel.Packets.Factory.Belt
{
public class BeltUpdatePickupItemsPacket
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace NebulaModel.Packets.Belt
namespace NebulaModel.Packets.Factory.Belt
{
public class BeltUpdatePutItemOnPacket
{
Expand Down
19 changes: 19 additions & 0 deletions NebulaModel/Packets/Factory/Belt/ConnectToMonitorPacket.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
namespace NebulaModel.Packets.Factory.Belt
{
public class ConnectToMonitorPacket
{
public int MonitorId { get; set; }
public int BeltId { get; set; }
public int Offset { get; set; }
public int PlanetId { get; set; }

public ConnectToMonitorPacket() { }
public ConnectToMonitorPacket(int monitorId, int beltId, int offset, int planetId)
{
MonitorId = monitorId;
BeltId = beltId;
Offset = offset;
PlanetId = planetId;
}
}
}
19 changes: 19 additions & 0 deletions NebulaModel/Packets/Factory/Belt/ConnectToSpraycoaterPacket.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
namespace NebulaModel.Packets.Factory.Belt
{
public class ConnectToSpraycoaterPacket
{
public int SpraycoaterId { get; set; }
public int CargoBeltId { get; set; }
public int IncBeltId { get; set; }
public int PlanetId { get; set; }

public ConnectToSpraycoaterPacket() { }
public ConnectToSpraycoaterPacket(int spraycoaterId, int cargoBeltId, int incBeltId, int planetId)
{
SpraycoaterId = spraycoaterId;
CargoBeltId = cargoBeltId;
IncBeltId = incBeltId;
PlanetId = planetId;
}
}
}
6 changes: 5 additions & 1 deletion NebulaModel/Packets/Session/SyncComplete.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ public class SyncComplete
public PlayerData[] AllPlayers { get; set; }
public byte[] ClientCert { get; set; }

public SyncComplete() { AllPlayers = new PlayerData[] { }; }
public SyncComplete()
{
AllPlayers = new PlayerData[] { };
ClientCert = new byte[] { };
}
public SyncComplete(IPlayerData[] otherPlayers)
{
AllPlayers = otherPlayers.Select(data => (PlayerData)data).ToArray();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using NebulaModel.Logger;
using NebulaModel.Networking;
using NebulaModel.Packets;
using NebulaModel.Packets.Belt;
using NebulaModel.Packets.Factory.Belt;

namespace NebulaNetwork.PacketProcessors.Factory.Belt
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using NebulaModel.Logger;
using NebulaModel.Networking;
using NebulaModel.Packets;
using NebulaModel.Packets.Belt;
using NebulaModel.Packets.Factory.Belt;
using NebulaWorld;

namespace NebulaNetwork.PacketProcessors.Factory.Belt
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using NebulaAPI;
using NebulaModel.Networking;
using NebulaModel.Packets;
using NebulaModel.Packets.Factory.Belt;
using NebulaWorld;

namespace NebulaNetwork.PacketProcessors.Factory.Belt
{
[RegisterPacketProcessor]
internal class ConnectToMonitorProcessor : PacketProcessor<ConnectToMonitorPacket>
{
public override void ProcessPacket(ConnectToMonitorPacket packet, NebulaConnection conn)
{
using (Multiplayer.Session.Factories.IsIncomingRequest.On())
{
CargoTraffic cargoTraffic = GameMain.galaxy.PlanetById(packet.PlanetId)?.factory?.cargoTraffic;
if (cargoTraffic == null)
{
return;
}
cargoTraffic.ConnectToMonitor(packet.MonitorId, packet.BeltId, packet.Offset);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using NebulaAPI;
using NebulaModel.Networking;
using NebulaModel.Packets;
using NebulaModel.Packets.Factory.Belt;
using NebulaWorld;

namespace NebulaNetwork.PacketProcessors.Factory.Belt
{
[RegisterPacketProcessor]
internal class ConnectToSpraycoaterProcessor : PacketProcessor<ConnectToSpraycoaterPacket>
{
public override void ProcessPacket(ConnectToSpraycoaterPacket packet, NebulaConnection conn)
{
using (Multiplayer.Session.Factories.IsIncomingRequest.On())
{
CargoTraffic cargoTraffic = GameMain.galaxy.PlanetById(packet.PlanetId)?.factory?.cargoTraffic;
if (cargoTraffic == null)
{
return;
}
cargoTraffic.ConnectToSpraycoater(packet.SpraycoaterId, packet.CargoBeltId, packet.IncBeltId);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using NebulaModel.Networking;
using NebulaModel.Packets;
using NebulaModel.Packets.Factory.Laboratory;
using NebulaWorld;

namespace NebulaNetwork.PacketProcessors.Factory.Labratory
{
Expand All @@ -14,34 +15,38 @@ public override void ProcessPacket(LaboratoryUpdateEventPacket packet, NebulaCon
LabComponent[] pool = factory?.factorySystem?.labPool;
if (pool != null && packet.LabIndex != -1 && packet.LabIndex < pool.Length && pool[packet.LabIndex].id != -1)
{
if (packet.ProductId == -4)
using (Multiplayer.Session.Factories.IsIncomingRequest.On())
{
pool[packet.LabIndex].forceAccMode = !pool[packet.LabIndex].forceAccMode;
Multiplayer.Session.Factories.PacketAuthor = NebulaModAPI.AUTHOR_NONE;
if (packet.ProductId == -4)
{
pool[packet.LabIndex].forceAccMode = !pool[packet.LabIndex].forceAccMode;
}
else if (packet.ProductId == -3)
{
//Widthdraw produced cubes
pool[packet.LabIndex].produced[0] = 0;
}
else if (packet.ProductId == -2)
{
//Research recipe reseted
pool[packet.LabIndex].SetFunction(false, 0, 0, factory.entitySignPool);
}
else if (packet.ProductId == -1)
{
//Center chenged to research-mode
pool[packet.LabIndex].SetFunction(true, 0, GameMain.data.history.currentTech, factory.entitySignPool);
}
else
{
//Cube Recipe changed
int[] matrixIds = LabComponent.matrixIds;
RecipeProto recipeProto = LDB.items.Select(matrixIds[packet.ProductId]).maincraft;
pool[packet.LabIndex].SetFunction(false, (recipeProto == null) ? 0 : recipeProto.ID, 0, factory.entitySignPool);
}
factory.factorySystem?.SyncLabFunctions(GameMain.mainPlayer, packet.LabIndex);
factory.factorySystem?.SyncLabForceAccMode(GameMain.mainPlayer, packet.LabIndex);
}
else if (packet.ProductId == -3)
{
//Widthdraw produced cubes
pool[packet.LabIndex].produced[0] = 0;
}
else if (packet.ProductId == -2)
{
//Research recipe reseted
pool[packet.LabIndex].SetFunction(false, 0, 0, factory.entitySignPool);
}
else if (packet.ProductId == -1)
{
//Center chenged to research-mode
pool[packet.LabIndex].SetFunction(true, 0, GameMain.data.history.currentTech, factory.entitySignPool);
}
else
{
//Cube Recipe changed
int[] matrixIds = LabComponent.matrixIds;
RecipeProto recipeProto = LDB.items.Select(matrixIds[packet.ProductId]).maincraft;
pool[packet.LabIndex].SetFunction(false, (recipeProto == null) ? 0 : recipeProto.ID, 0, factory.entitySignPool);
}
factory.factorySystem?.SyncLabFunctions(GameMain.mainPlayer, packet.LabIndex);
factory.factorySystem?.SyncLabForceAccMode(GameMain.mainPlayer, packet.LabIndex);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,16 @@ public override void ProcessPacket(ILSAddStationComponent packet, NebulaConnecti
using (Multiplayer.Session.Ships.PatchLockILS.On())
{
GalacticTransport galacticTransport = GameMain.data.galacticTransport;

if (packet.PlanetId == GameMain.localPlanet?.id)
StationComponent[] stationPool = GameMain.galaxy.PlanetById(packet.PlanetId).factory?.transport.stationPool;
if (stationPool != null)
{
// If we're on the same planet as the new station was created on, should be able to find
// it in our local PlanetTransport.stationPool
StationComponent stationComponent = GameMain.localPlanet.factory.transport.stationPool[packet.StationId];
galacticTransport.AddStationComponent(packet.PlanetId, stationComponent);
// If we have loaded the factory where the new station was created on, should be able to find
// it in our PlanetTransport.stationPool
galacticTransport.AddStationComponent(packet.PlanetId, stationPool[packet.StationId]);
}
else
{
// If we're not on the same planet as the new station was create on, we need to create a
// If we haven't loaded the factory the new station was create on, we need to create a
// "fake" station that we can put into the GalacticTransport.stationPool instead of a real on
Multiplayer.Session.Ships.CreateFakeStationComponent(packet.StationGId, packet.PlanetId, packet.MaxShipCount);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using NebulaModel.Networking;
using NebulaAPI;
using NebulaModel.Networking;
using NebulaModel.Packets;

namespace NebulaNetwork.PacketProcessors.Players
{
[RegisterPacketProcessor]
internal class RemoveDroneOrdersProcessor : PacketProcessor<RemoveDroneOrdersPacket>
{
public override void ProcessPacket(RemoveDroneOrdersPacket packet, NebulaConnection conn)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,12 @@ public override void ProcessPacket(LobbyRequest packet, NebulaConnection conn)
// if user is known and host is ingame dont put him into lobby but let him join the game
if (!isNewUser && Multiplayer.Session.IsGameLoaded)
{
// Remove the new player from pending list
using (playerManager.GetPendingPlayers(out Dictionary<INebulaConnection, INebulaPlayer> pendingPlayers))
{
pendingPlayers.Remove(conn);
}

// Add the new player to the list
using (playerManager.GetSyncingPlayers(out Dictionary<INebulaConnection, INebulaPlayer> syncingPlayers))
{
Expand Down
22 changes: 18 additions & 4 deletions NebulaNetwork/SaveManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ public static void LoadServerData()

byte[] source = File.ReadAllBytes(path);
NetDataReader netDataReader = new NetDataReader(source);
ushort revision = 0;
try
{
string revString = netDataReader.GetString();
Expand All @@ -100,10 +101,15 @@ public static void LoadServerData()
throw new System.Exception();
}

ushort revision = netDataReader.GetUShort();
revision = netDataReader.GetUShort();
NebulaModel.Logger.Log.Info($"Loading server data, revision {revision}");
if (revision != REVISION)
{
throw new System.Exception();
// Supported revision: 4~5
if (revision < 4 || revision > REVISION)
{
throw new System.Exception();
}
}
}
catch (System.Exception)
Expand All @@ -119,8 +125,16 @@ public static void LoadServerData()
for (int i = 0; i < playerNum; i++)
{
string hash = netDataReader.GetString();

PlayerData playerData = netDataReader.Get<PlayerData>();
PlayerData playerData;
if (revision == REVISION)
{
playerData = netDataReader.Get<PlayerData>();
}
else
{
playerData = new PlayerData();
playerData.DeserializeOld(netDataReader, revision);
}
if (!savedPlayerData.ContainsKey(hash))
{
savedPlayerData.Add(hash, playerData);
Expand Down
2 changes: 2 additions & 0 deletions NebulaPatcher/Patches/Dynamic/BuildTool_Common_Patch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ public static bool CreatePrebuilds_Prefix(BuildTool __instance)
}
if(__instance is BuildTool_PathAddon)
{
// traffic monitors & sprayers cannot be drag build atm, so its always only one.
previews = new List<BuildPreview>();
previews.Add(((BuildTool_PathAddon)__instance).handbp);
}

Expand Down
32 changes: 31 additions & 1 deletion NebulaPatcher/Patches/Dynamic/CargoTraffic_Patch.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using HarmonyLib;
using NebulaAPI;
using NebulaModel.Packets.Belt;
using NebulaModel.Packets.Factory.Belt;
using NebulaWorld;

namespace NebulaPatcher.Patches.Dynamic
Expand Down Expand Up @@ -78,5 +78,35 @@ public static bool RefreshPathUV_Prefix()
//Do not call renderer, if user is not on the planet as the request
return !Multiplayer.IsActive || Multiplayer.Session.Factories.TargetPlanet == NebulaModAPI.PLANET_NONE || GameMain.mainPlayer.planetId == Multiplayer.Session.Factories.TargetPlanet;
}

[HarmonyPostfix]
[HarmonyPatch(nameof(CargoTraffic.ConnectToMonitor))]
public static void ConnectToMonitor_Postfix(int monitorId, int targetBeltId, int offset)
{
if (!Multiplayer.IsActive)
{
return;
}
// If host build, or client receive his build request
if((Multiplayer.Session.LocalPlayer.IsHost && !Multiplayer.Session.Factories.IsIncomingRequest.Value)|| Multiplayer.Session.Factories.PacketAuthor == Multiplayer.Session.LocalPlayer.Id)
{
Multiplayer.Session.Network.SendPacketToLocalStar(new ConnectToMonitorPacket(monitorId, targetBeltId, offset, GameMain.data.localPlanet.id));
}
}

[HarmonyPostfix]
[HarmonyPatch(nameof(CargoTraffic.ConnectToSpraycoater))]
public static void ConnectToSpraycoater(int spraycoaterId, int cargoBeltId, int incBeltId)
{
if (!Multiplayer.IsActive)
{
return;
}
// If host build, or client receive his build request
if ((Multiplayer.Session.LocalPlayer.IsHost && !Multiplayer.Session.Factories.IsIncomingRequest.Value) || Multiplayer.Session.Factories.PacketAuthor == Multiplayer.Session.LocalPlayer.Id)
{
Multiplayer.Session.Network.SendPacketToLocalStar(new ConnectToSpraycoaterPacket(spraycoaterId, cargoBeltId, incBeltId, GameMain.data.localPlanet.id));
}
}
}
}
2 changes: 0 additions & 2 deletions NebulaPatcher/Patches/Dynamic/SpeakerComponent_Patch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ public static void SetPitch_Prefix(MonitorComponent __instance, int __0)
[HarmonyPatch(nameof(SpeakerComponent.SetLength))]
public static void SetLength_Prefix(MonitorComponent __instance, float __0)
{
Log.Info($"SetLength {__0} {Multiplayer.Session.Warning.IsIncomingMonitorPacket == true}");
if (Multiplayer.IsActive && !Multiplayer.Session.Warning.IsIncomingMonitorPacket)
{
int planetId = GameMain.data.localPlanet == null ? -1 : GameMain.data.localPlanet.id;
Expand All @@ -77,7 +76,6 @@ public static void SetLength_Prefix(MonitorComponent __instance, bool __0)
[HarmonyPatch(nameof(SpeakerComponent.SetFalloffRadius))]
public static void SetFalloffRadius_Prefix(MonitorComponent __instance, float __0, float __1)
{
Log.Info($"SetFalloffRadius {__0} {__1} {Multiplayer.Session.Warning.IsIncomingMonitorPacket == true}");
if (Multiplayer.IsActive && !Multiplayer.Session.Warning.IsIncomingMonitorPacket)
{
int planetId = GameMain.data.localPlanet == null ? -1 : GameMain.data.localPlanet.id;
Expand Down
Loading

0 comments on commit dec2eee

Please sign in to comment.