refactoring multiplayer

This commit is contained in:
2025-07-21 19:58:07 +02:00
parent a0a0f6303d
commit ea99249fe2
6 changed files with 238 additions and 114 deletions

View File

@@ -23,62 +23,72 @@ namespace RebootKit.Engine.Simulation {
}
}
public struct ActorCommand : INetworkSerializable {
public struct ActorCommand : ISerializableEntity {
public ulong ActorID;
public ulong ClientID;
public ushort CommandID;
public NativeArray<byte> Data;
public void NetworkSerialize<T>(BufferSerializer<T> serializer) where T : IReaderWriter {
serializer.SerializeValue(ref ActorID);
serializer.SerializeValue(ref ClientID);
serializer.SerializeValue(ref CommandID);
if (serializer.IsWriter) {
bool hasData = Data.IsCreated;
serializer.SerializeValue(ref hasData);
if (hasData) {
serializer.SerializeValue(ref Data, Allocator.Temp);
}
} else if (serializer.IsReader) {
bool hasData = false;
serializer.SerializeValue(ref hasData);
if (hasData) {
serializer.SerializeValue(ref Data, Allocator.Temp);
}
public int GetMaxBytes() {
return sizeof(ulong) + // ActorID
sizeof(ulong) + // ClientID
sizeof(ushort) + // CommandID
sizeof(ushort) + // Data length
sizeof(byte) * Data.Length; // Data
}
public void Serialize(NetworkBufferWriter writer) {
writer.Write(ActorID);
writer.Write(ClientID);
writer.Write(CommandID);
writer.Write((ushort) Data.Length);
if (Data.IsCreated) {
writer.Write(Data);
}
}
public void Deserialize(NetworkBufferReader reader) {
reader.Read(out ActorID);
reader.Read(out ClientID);
reader.Read(out CommandID);
reader.Read(out ushort dataLength);
if (dataLength > 0) {
reader.Read(out Data, dataLength, Allocator.Temp);
}
}
}
// @NOTE: ActorEvent is used to send events from the server to clients and only clients.
// Server should not receive ActorEvents.
public struct ActorEvent : INetworkSerializable {
public struct ActorEvent : ISerializableEntity {
public ulong ActorID;
public ulong ClientID;
public ushort EventID;
public NativeArray<byte> Data;
public void NetworkSerialize<T>(BufferSerializer<T> serializer) where T : IReaderWriter {
serializer.SerializeValue(ref ActorID);
serializer.SerializeValue(ref ClientID);
serializer.SerializeValue(ref EventID);
public int GetMaxBytes() {
return sizeof(ulong) + // ActorID
sizeof(ushort) + // EventID
sizeof(ushort) + // Data length
sizeof(byte) * Data.Length; // Data
}
if (serializer.IsWriter) {
bool hasData = Data.IsCreated;
serializer.SerializeValue(ref hasData);
public void Serialize(NetworkBufferWriter writer) {
writer.Write(ActorID);
writer.Write(EventID);
if (hasData) {
serializer.SerializeValue(ref Data, Allocator.Temp);
}
} else if (serializer.IsReader) {
bool hasData = false;
serializer.SerializeValue(ref hasData);
writer.Write((ushort) Data.Length);
if (Data.IsCreated) {
writer.Write(Data);
}
}
if (hasData) {
serializer.SerializeValue(ref Data, Allocator.Temp);
}
public void Deserialize(NetworkBufferReader reader) {
reader.Read(out ActorID);
reader.Read(out EventID);
reader.Read(out ushort dataLength);
if (dataLength > 0) {
reader.Read(out Data, dataLength, Allocator.Temp);
}
}
}
@@ -342,7 +352,6 @@ namespace RebootKit.Engine.Simulation {
// Override this method to implement client-side logic
public virtual void OnClientTick(float deltaTime) { }
protected virtual void OnActorEventClient(ActorEvent actorEvent) { }
protected virtual void OnClientFinishedInitialSync() { }
//
// @MARK: Server API
@@ -479,7 +488,7 @@ namespace RebootKit.Engine.Simulation {
Data = data
};
Manager.SendActorCommandToServerRpc(command);
Manager.SendActorCommand(command);
}
protected void SendActorEvent<TEventData>(ushort eventID, ref TEventData eventData)
@@ -501,7 +510,6 @@ namespace RebootKit.Engine.Simulation {
ActorEvent actorEvent = new ActorEvent {
ActorID = ActorID,
ClientID = NetworkManager.Singleton.LocalClientId,
EventID = eventID,
Data = data
};
@@ -712,10 +720,6 @@ namespace RebootKit.Engine.Simulation {
return CreateActorData();
}
internal void InitialSyncFinished() {
OnClientFinishedInitialSync();
}
internal void HandleActorCommand(ActorCommand actorCommand) {
if (!RR.IsServer()) {
s_ActorLogger.Error($"Only the server can handle actor commands. Actor: {name} (ID: {ActorID})");

View File

@@ -295,16 +295,29 @@ namespace RebootKit.Engine.Simulation {
s_Logger.Error($"Failed to find actor with ID {header.ActorID} for event handling.");
return;
}
using NetworkBufferReader reader = new NetworkBufferReader(data);
ActorEvent actorEvent = new ActorEvent();
actorEvent.Deserialize(reader);
throw new NotImplementedException();
actor.HandleActorEvent(actorEvent);
} else if (header.Type == NetworkDataType.ActorCommand) {
if (!RR.IsServer()) {
s_Logger.Error($"Received ActorCommand on client, but this should only be handled on the server.");
return;
}
Actor actor = FindActorByID(header.ActorID);
if (actor == null) {
s_Logger.Error($"Failed to find actor with ID {header.ActorID} for command handling.");
return;
}
using NetworkBufferReader reader = new NetworkBufferReader(data);
ActorCommand actorCommand = new ActorCommand();
actorCommand.Deserialize(reader);
throw new NotImplementedException();
actor.HandleActorCommand(actorCommand);
} else if (header.Type == NetworkDataType.SynchronizeActor) {
Actor actor = FindActorByID(header.ActorID);
if (actor == null) {
@@ -381,48 +394,17 @@ namespace RebootKit.Engine.Simulation {
//
// @MARK: Actor Commands and Events
//
[Rpc(SendTo.Server, Delivery = RpcDelivery.Reliable)]
internal void SendActorCommandToServerRpc(ActorCommand cmd) {
if (!IsServer) {
s_Logger.Error("Only the server can handle actor events.");
return;
}
Actor actor = FindActorByID(cmd.ActorID);
if (actor is null) {
s_Logger.Error($"Actor with ID {cmd.ActorID} not found for command {cmd.CommandID}");
return;
}
actor.HandleActorCommand(cmd);
}
internal void SendActorEvent(ActorEvent actorEvent) {
if (!RR.IsServer()) {
s_Logger.Error("Only the server can send actor events.");
return;
}
foreach ((ulong clientID, NetworkClientState state) in RR.NetworkSystemInstance.Clients) {
if (NetworkManager.Singleton.LocalClientId == clientID) {
continue;
}
if (state.IsReady) {
SendActorEventRpc(actorEvent, RpcTarget.Single(clientID, RpcTargetUse.Temp));
}
}
RR.NetworkSystemInstance.WriteActorEvent(NetworkPacketTarget.AllClients(), actorEvent);
}
[Rpc(SendTo.SpecifiedInParams)]
void SendActorEventRpc(ActorEvent actorEvent, RpcParams rpcParams) {
Actor actor = FindActorByID(actorEvent.ActorID);
if (actor is null) {
s_Logger.Error($"Actor with ID {actorEvent.ActorID} not found for event {actorEvent.EventID}");
return;
}
actor.HandleActorEvent(actorEvent);
internal void SendActorCommand(ActorCommand actorCommand) {
RR.NetworkSystemInstance.WriteActorCommand(actorCommand);
}
}
}