Skip to content

Commit

Permalink
Merge pull request #658 from starfi5h/pr-fix
Browse files Browse the repository at this point in the history
Fixed various errors
  • Loading branch information
starfi5h authored Mar 9, 2024
2 parents 94cb2bc + 4faccce commit 506b96f
Show file tree
Hide file tree
Showing 26 changed files with 196 additions and 56 deletions.
1 change: 0 additions & 1 deletion NebulaModel/DataStructures/MechaData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using NebulaAPI.DataStructures;
using NebulaAPI.Interfaces;
using NebulaAPI.Packets;
using NebulaModel.Packets.Players;
using static NebulaModel.Networking.BinaryUtils;

#endregion
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@

using NebulaAPI.DataStructures;
using NebulaAPI.Interfaces;
using static NebulaModel.Networking.BinaryUtils;

#endregion

namespace NebulaModel.Packets.Players;
namespace NebulaModel.DataStructures;

public class PlayerTechBonuses : IPlayerTechBonuses
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,18 @@ public class GameHistoryResearchUpdatePacket
{
public GameHistoryResearchUpdatePacket() { }

public GameHistoryResearchUpdatePacket(int techId, long hashUploaded, long hashNeeded, int techHashedFor10Frames)
public GameHistoryResearchUpdatePacket(int techId, long hashUploaded, long hashNeeded, int techHashedFor10Frames, int techQueueLength)
{
TechId = techId;
HashUploaded = hashUploaded;
HashNeeded = hashNeeded;
TechHashedFor10Frames = techHashedFor10Frames;
TechQueueLength = (ushort)techQueueLength;
}

public int TechId { get; set; }
public long HashUploaded { get; set; }
public long HashNeeded { get; set; }
public int TechHashedFor10Frames { get; set; }
public ushort TechQueueLength { get; set; }
}
13 changes: 13 additions & 0 deletions NebulaModel/Packets/GameHistory/GameHistoryTechQueueSyncRequest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace NebulaModel.Packets.GameHistory;

public class GameHistoryTechQueueSyncRequest
{
public GameHistoryTechQueueSyncRequest() { }

public GameHistoryTechQueueSyncRequest(int[] techQueue)
{
TechQueue = techQueue;
}

public int[] TechQueue { get; set; }
}
27 changes: 14 additions & 13 deletions NebulaModel/Packets/Session/GlobalGameDataResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,21 @@ public class GlobalGameDataResponse
{
public GlobalGameDataResponse() { }

public GlobalGameDataResponse(bool sandboxToolsEnabled,
byte[] historyBinaryData, byte[] spaceSectorBinaryData,
byte[] milestoneSystemBinaryData, byte[] trashSystemBinaryData)
public GlobalGameDataResponse(EDataType dataType, byte[] binaryData)
{
SandboxToolsEnabled = sandboxToolsEnabled;
HistoryBinaryData = historyBinaryData;
SpaceSectorBinaryData = spaceSectorBinaryData;
MilestoneSystemBinaryData = milestoneSystemBinaryData;
TrashSystemBinaryData = trashSystemBinaryData;
DataType = dataType;
BinaryData = binaryData;
}

public bool SandboxToolsEnabled { get; set; }
public byte[] HistoryBinaryData { get; set; }
public byte[] SpaceSectorBinaryData { get; set; }
public byte[] MilestoneSystemBinaryData { get; set; }
public byte[] TrashSystemBinaryData { get; set; }
public enum EDataType : byte
{
History = 1,
SpaceSector,
MilestoneSystem,
TrashSystem,
Ready
}

public EDataType DataType { get; set; }
public byte[] BinaryData { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

#endregion

namespace NebulaNetwork.PacketProcessors.Factory.Entity;
namespace NebulaNetwork.PacketProcessors.Factory;

[RegisterPacketProcessor]
public class BuildEntityRequestProcessor : PacketProcessor<BuildEntityRequest>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

#endregion

namespace NebulaNetwork.PacketProcessors.Factory.Entity;
namespace NebulaNetwork.PacketProcessors.Factory;

[RegisterPacketProcessor]
internal class CreatePrebuildsRequestProcessor : PacketProcessor<CreatePrebuildsRequest>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

#endregion

namespace NebulaNetwork.PacketProcessors.Factory.Entity;
namespace NebulaNetwork.PacketProcessors.Factory;

[RegisterPacketProcessor]
public class DestructEntityRequestProcessor : PacketProcessor<DestructEntityRequest>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

#endregion

namespace NebulaNetwork.PacketProcessors.Factory.Entity;
namespace NebulaNetwork.PacketProcessors.Factory;

[RegisterPacketProcessor]
internal class EntityBoostSwitchProcessor : PacketProcessor<EntityBoostSwitchPacket>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

#endregion

namespace NebulaNetwork.PacketProcessors.Factory.Entity;
namespace NebulaNetwork.PacketProcessors.Factory;

[RegisterPacketProcessor]
internal class EntityWarningSwitchProcessor : PacketProcessor<EntityWarningSwitchPacket>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

#endregion

namespace NebulaNetwork.PacketProcessors.Factory.Entity;
namespace NebulaNetwork.PacketProcessors.Factory;

[RegisterPacketProcessor]
internal class ExtraInfoUpdateProcessor : PacketProcessor<ExtraInfoUpdatePacket>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

#endregion

namespace NebulaNetwork.PacketProcessors.Factory.Entity;
namespace NebulaNetwork.PacketProcessors.Factory;

[RegisterPacketProcessor]
public class KillEntityRequestProcessor : PacketProcessor<KillEntityRequest>
Expand All @@ -23,6 +23,7 @@ protected override void ProcessPacket(KillEntityRequest packet, NebulaConnection
using (Multiplayer.Session.Factories.IsIncomingRequest.On())
{
Multiplayer.Session.Factories.TargetPlanet = packet.PlanetId;
Multiplayer.Session.Factories.EventFactory = factory;

if (!factory.planet.factoryLoaded)
{
Expand All @@ -41,6 +42,7 @@ protected override void ProcessPacket(KillEntityRequest packet, NebulaConnection
}

Multiplayer.Session.Factories.TargetPlanet = NebulaModAPI.PLANET_NONE;
Multiplayer.Session.Factories.EventFactory = null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

#endregion

namespace NebulaNetwork.PacketProcessors.Factory.Entity;
namespace NebulaNetwork.PacketProcessors.Factory;

// Processes pasting settings (e.g. item to make in an assembler) onto buildings events
[RegisterPacketProcessor]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

#endregion

namespace NebulaNetwork.PacketProcessors.Factory.Entity;
namespace NebulaNetwork.PacketProcessors.Factory;

[RegisterPacketProcessor]
public class PrebuildItemRequiredUpdateProcessor : PacketProcessor<PrebuildItemRequiredUpdate>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

#endregion

namespace NebulaNetwork.PacketProcessors.Factory.Entity;
namespace NebulaNetwork.PacketProcessors.Factory;

[RegisterPacketProcessor]
public class PrebuildReconstructProcessor : PacketProcessor<PrebuildReconstructPacket>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

#endregion

namespace NebulaNetwork.PacketProcessors.Factory.Entity;
namespace NebulaNetwork.PacketProcessors.Factory;

[RegisterPacketProcessor]
public class UpgradeEntityRequestProcessor : PacketProcessor<UpgradeEntityRequest>
Expand Down
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 Down Expand Up @@ -29,5 +30,11 @@ protected override void ProcessPacket(GameHistoryResearchUpdatePacket packet, Ne
state.hashNeeded = packet.HashNeeded;
data.techStates[data.currentTech] = state;
Multiplayer.Session.Statistics.TechHashedFor10Frames = packet.TechHashedFor10Frames;

if (packet.TechQueueLength != GameMain.history.techQueueLength)
{
// TechQueue length mismatch. Ask from server to get a full queue to stay in sync
conn.SendPacket(new GameHistoryTechQueueSyncRequest([]));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#region

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

#endregion

namespace NebulaNetwork.PacketProcessors.GameHistory;

[RegisterPacketProcessor]
internal class GameHistoryTechQueueSyncProcessor : PacketProcessor<GameHistoryTechQueueSyncRequest>
{
protected override void ProcessPacket(GameHistoryTechQueueSyncRequest packet, NebulaConnection conn)
{
if (IsHost)
{
packet.TechQueue = GameMain.history.techQueue;
conn.SendPacket(packet);
}
else
{
using (Multiplayer.Session.History.IsIncomingRequest.On())
{
var length = GameMain.history.techQueue.Length;
for (var i = 0; i < length; i++)
{
// Clear the original queue by dequeue for compatibility
GameMain.history.DequeueTech();
}
for (var i = 0; i < packet.TechQueue.Length; i++)
{
if (packet.TechQueue[i] == 0) return;
GameMain.history.EnqueueTech(packet.TechQueue[i]);
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

using NebulaAPI.Packets;
using NebulaModel;
using NebulaModel.Logger;
using NebulaModel.Networking;
using NebulaModel.Packets;
using NebulaModel.Packets.Players;
Expand All @@ -26,6 +27,11 @@ protected override void ProcessPacket(PlayerMechaData packet, NebulaConnection c
}

var player = Multiplayer.Session.Server.Players.Get(conn);
if (player == null)
{
Log.Warn("Can't find the connected player for PlayerMechaData!");
return;
}

//Find correct player for data to update, preserve sand count if syncing is enabled
var sandCount = player.Data.Mecha.SandCount;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using NebulaAPI.Packets;
using NebulaModel.Networking;
using NebulaModel.Packets;
using NebulaModel.Packets.GameStates;
using NebulaModel.Packets.Session;

#endregion
Expand All @@ -21,46 +20,51 @@ protected override void ProcessPacket(GlobalGameDataRequest packet, NebulaConnec
}

//Export GameHistoryData, SpaceSector, TrashSystem, MilestoneSystem
//PlanetFactory, Dysonsphere, GalacticTransport will be handle else where
var responsePacket = new GlobalGameDataResponse();
var totalSize = 0;
//PlanetFactory, Dysonsphere, GalacticTransport will be handle else where

using (var writer = new BinaryUtils.Writer())
{
GameMain.history.Export(writer.BinaryWriter);
responsePacket.SandboxToolsEnabled = GameMain.sandboxToolsEnabled;
responsePacket.HistoryBinaryData = writer.CloseAndGetBytes();
totalSize += responsePacket.HistoryBinaryData.Length;

conn.SendPacket(new GlobalGameDataResponse(
GlobalGameDataResponse.EDataType.History, writer.CloseAndGetBytes()));
}

using (var writer = new BinaryUtils.Writer())
{
// Initial syncing from vanilla, to be refined later in future.
// Note: Initial syncing from vanilla. May be refined later in future
NebulaWorld.Combat.CombatManager.SerializeOverwrite = true;
GameMain.data.spaceSector.BeginSave();
GameMain.data.spaceSector.Export(writer.BinaryWriter);
GameMain.data.spaceSector.EndSave();
NebulaWorld.Combat.CombatManager.SerializeOverwrite = false;

responsePacket.SpaceSectorBinaryData = writer.CloseAndGetBytes();
totalSize += responsePacket.SpaceSectorBinaryData.Length;
conn.SendPacket(new GlobalGameDataResponse(
GlobalGameDataResponse.EDataType.SpaceSector, writer.CloseAndGetBytes()));
}

using (var writer = new BinaryUtils.Writer())
{
GameMain.data.milestoneSystem.Export(writer.BinaryWriter);
responsePacket.MilestoneSystemBinaryData = writer.CloseAndGetBytes();
totalSize += responsePacket.MilestoneSystemBinaryData.Length;

conn.SendPacket(new GlobalGameDataResponse(
GlobalGameDataResponse.EDataType.MilestoneSystem, writer.CloseAndGetBytes()));
}

using (var writer = new BinaryUtils.Writer())
{
GameMain.data.trashSystem.Export(writer.BinaryWriter);
responsePacket.TrashSystemBinaryData = writer.CloseAndGetBytes();
totalSize += responsePacket.TrashSystemBinaryData.Length;

conn.SendPacket(new GlobalGameDataResponse(
GlobalGameDataResponse.EDataType.TrashSystem, writer.CloseAndGetBytes()));
}

conn.SendPacket(new FragmentInfo(totalSize));
conn.SendPacket(responsePacket);
using (var writer = new BinaryUtils.Writer())
{
writer.BinaryWriter.Write(GameMain.sandboxToolsEnabled);

conn.SendPacket(new GlobalGameDataResponse(
GlobalGameDataResponse.EDataType.Ready, writer.CloseAndGetBytes()));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,7 @@ protected override void ProcessPacket(GlobalGameDataResponse packet, NebulaConne
{
if (IsHost) return;

// The whole fragment is received
GameStatesManager.FragmentSize = 0;

// Store the binary data in GameStatesManager then later overwrite those system in GameData.NewGame
GameStatesManager.ImportGlobalGameData(packet);

// We are ready to start the game now
DSPGame.StartGameSkipPrologue(DSPGame.GameDesc);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ protected override void ProcessPacket(HandshakeResponse packet, NebulaConnection
Multiplayer.Session.Network.SendPacket(new GlobalGameDataRequest());
if (DSPGame.Game != null)
{
// Client: Close planetDetail before requesting to prevent error
UIRoot.instance.uiGame.SetPlanetDetail(null);
DSPGame.EndGame();
}
// Prepare gameDesc to later start in GlobalGameDataResponseProcessor
Expand Down
2 changes: 1 addition & 1 deletion NebulaNetwork/Server.cs
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ public void Update()
{
var state = GameMain.data.history.techStates[GameMain.data.history.currentTech];
SendPacket(new GameHistoryResearchUpdatePacket(GameMain.data.history.currentTech, state.hashUploaded,
state.hashNeeded, GameMain.statistics.techHashedFor10Frames));
state.hashNeeded, GameMain.statistics.techHashedFor10Frames, GameMain.data.history.techQueueLength));
}
}

Expand Down
Loading

0 comments on commit 506b96f

Please sign in to comment.