-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
397 additions
and
116 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
using System; | ||
using Unity.Burst; | ||
using Unity.Collections; | ||
using Unity.NetCode; | ||
|
||
[BurstCompile] | ||
public struct SessionLoginRequestRpc : IRpcCommand | ||
{ | ||
public required FixedBytes16 Guid; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
using Unity.NetCode; | ||
|
||
public struct SessionRegisterRequestRpc : IRpcCommand | ||
{ | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
using Unity.Burst; | ||
using Unity.Collections; | ||
using Unity.NetCode; | ||
|
||
public enum SessionStatusCode : byte | ||
{ | ||
OK, | ||
AlreadyLoggedIn, | ||
InvalidGuid, | ||
} | ||
|
||
[BurstCompile] | ||
public struct SessionResponseRpc : IRpcCommand | ||
{ | ||
public required SessionStatusCode StatusCode; | ||
public required FixedBytes16 Guid; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
using System; | ||
using Unity.Burst; | ||
using Unity.Collections; | ||
using Unity.Entities; | ||
using Unity.NetCode; | ||
|
||
[BurstCompile] | ||
[WorldSystemFilter(WorldSystemFilterFlags.ClientSimulation | WorldSystemFilterFlags.ThinClientSimulation)] | ||
public partial struct PlayerSystemClient : ISystem | ||
{ | ||
bool _requestSent; | ||
SessionStatusCode _sessionStatus; | ||
Guid _guid; | ||
|
||
void ISystem.OnCreate(ref SystemState state) | ||
{ | ||
_requestSent = false; | ||
_guid = default; | ||
state.RequireForUpdate<NetworkStreamConnection>(); | ||
} | ||
|
||
void ISystem.OnUpdate(ref SystemState state) | ||
{ | ||
EntityCommandBuffer commandBuffer = SystemAPI.GetSingleton<EndSimulationEntityCommandBufferSystem.Singleton>().CreateCommandBuffer(state.WorldUnmanaged); | ||
|
||
NetworkStreamConnection connection = SystemAPI.GetSingleton<NetworkStreamConnection>(); | ||
|
||
foreach (var (request, command, entity) in | ||
SystemAPI.Query<RefRO<ReceiveRpcCommandRequest>, RefRO<SessionResponseRpc>>() | ||
.WithEntityAccess()) | ||
{ | ||
commandBuffer.DestroyEntity(entity); | ||
|
||
_sessionStatus = command.ValueRO.StatusCode; | ||
_guid = Marshal.As<FixedBytes16, Guid>(command.ValueRO.Guid); | ||
|
||
Debug.Log(string.Format("[Client] Session status: {0}\n guid: {1}", _sessionStatus, _guid)); | ||
} | ||
|
||
if (connection.CurrentState != ConnectionState.State.Connected) return; | ||
|
||
if (!TryGetLocalPlayer(ref state, out Player player)) | ||
{ | ||
if (!_requestSent) | ||
{ | ||
Entity response = commandBuffer.CreateEntity(); | ||
commandBuffer.AddComponent<SendRpcCommandRequest>(response); | ||
if (_guid == default) | ||
{ | ||
Debug.Log(string.Format("[Client] No player found, registering")); | ||
|
||
commandBuffer.AddComponent<SessionRegisterRequestRpc>(response, new() | ||
{ | ||
|
||
}); | ||
} | ||
else | ||
{ | ||
Debug.Log(string.Format("[Client] No player found, logging in with {0}", _guid)); | ||
|
||
commandBuffer.AddComponent<SessionLoginRequestRpc>(response, new() | ||
{ | ||
Guid = Marshal.As<Guid, FixedBytes16>(_guid) | ||
}); | ||
} | ||
|
||
_requestSent = true; | ||
} | ||
} | ||
else | ||
{ | ||
_requestSent = false; | ||
} | ||
} | ||
|
||
public bool TryGetLocalPlayer(ref SystemState state, out Player player) | ||
{ | ||
if (!SystemAPI.TryGetSingleton(out NetworkId networkId)) | ||
{ | ||
player = default; | ||
return false; | ||
} | ||
|
||
foreach (var _player in | ||
SystemAPI.Query<RefRO<Player>>()) | ||
{ | ||
if (_player.ValueRO.ConnectionId != networkId.Value) continue; | ||
player = _player.ValueRO; | ||
return true; | ||
} | ||
|
||
player = default; | ||
return false; | ||
} | ||
|
||
public static bool TryGetLocalPlayer(out Player player) | ||
{ | ||
using EntityQuery playersQ = ConnectionManager.ClientOrDefaultWorld.EntityManager.CreateEntityQuery(typeof(Player)); | ||
using EntityQuery connectionsQ = ConnectionManager.ClientOrDefaultWorld.EntityManager.CreateEntityQuery(typeof(NetworkId)); | ||
if (!connectionsQ.TryGetSingleton(out NetworkId networkId)) | ||
{ | ||
player = default; | ||
return false; | ||
} | ||
|
||
using NativeArray<Player> players = playersQ.ToComponentDataArray<Player>(Allocator.Temp); | ||
for (int i = 0; i < players.Length; i++) | ||
{ | ||
if (players[i].ConnectionId != networkId.Value) continue; | ||
player = players[i]; | ||
return true; | ||
} | ||
|
||
player = default; | ||
return false; | ||
} | ||
} |
Oops, something went wrong.