Files
Project-M/Assets/_Project/Scripts/Client/Connection/GoInGameClientSystem.cs
T
kronic 813c829420 Hygiene B4d: PlayerResolve dedup + explicit Temp-ECB Dispose tail
- New Server/PlayerResolve.TryResolve single-sources the RPC
  SourceConnection -> NetworkId -> conn->player map resolve (3 sites:
  ClassSelectReceive, PrepPurchase, DebugCommandReceive); EntityManager
  reads keep it source-gen-safe from Bursted receivers.
- ecb.Dispose() after Playback in 9 Temp-ECB systems (explicit-lifetime
  hygiene).
- The TuningConfig.GetOrDefault(ref state) variant of this tail was
  REVERTED: state.GetEntityQuery in OnUpdate trips the Entities
  "creates a query during OnUpdate" diagnostic per system per world
  (caught in Play smoke) - the SystemAPI.TryGetSingleton idiom is
  already source-gen-optimal, confirming the original B4 deferral.

Verified: 466/466 EditMode green on the final tree, console clean,
Play smoke 0 errors.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-09 12:39:55 -07:00

61 lines
2.9 KiB
C#

using ProjectM.Simulation;
using Unity.Burst;
using Unity.Collections;
using Unity.Entities;
using Unity.NetCode;
namespace ProjectM.Client
{
/// <summary>
/// Client-side connection handshake: for every connection that has been assigned a
/// <see cref="NetworkId"/> but is not yet <see cref="NetworkStreamInGame"/>, mark it in-game and
/// fire a <see cref="GoInGameRequest"/> RPC so the server spawns this client's player ghost.
/// Adding NetworkStreamInGame is what gates snapshot/command flow on. Mirrors the netcode
/// "networked-cube" go-in-game sample.
/// </summary>
[BurstCompile]
[WorldSystemFilter(WorldSystemFilterFlags.ClientSimulation | WorldSystemFilterFlags.ThinClientSimulation)]
public partial struct GoInGameClientSystem : ISystem
{
[BurstCompile]
public void OnCreate(ref SystemState state)
{
var builder = new EntityQueryBuilder(Allocator.Temp)
.WithAll<NetworkId>()
.WithNone<NetworkStreamInGame>();
state.RequireForUpdate(state.GetEntityQuery(builder));
}
[BurstCompile]
public void OnUpdate(ref SystemState state)
{
// A FULL client must not go in-game until the gameplay subscene's ghost prefabs have streamed in.
// Otherwise the server's first ghost snapshot arrives before the client can resolve those prefabs
// ("ghost ... ENTITY_NOT_FOUND" -> the server disconnects the connection -> "nothing loads"). On
// loopback / fast LAN the connect+go-in-game handshake easily beats the ~0.5s entity-subscene stream.
// PlayerSpawner is a subscene-baked singleton that co-loads with the ghost prefabs, so its presence
// is a sound "subscene ready" gate. Thin clients never instantiate ghosts (and don't stream the
// subscene), so they skip the gate and connect immediately.
bool isThinClient = (state.WorldUnmanaged.Flags & WorldFlags.GameThinClient) == WorldFlags.GameThinClient;
if (!isThinClient && !SystemAPI.HasSingleton<PlayerSpawner>())
return;
var ecb = new EntityCommandBuffer(Allocator.Temp);
foreach (var (_, connection) in
SystemAPI.Query<RefRO<NetworkId>>().WithNone<NetworkStreamInGame>().WithEntityAccess())
{
ecb.AddComponent<NetworkStreamInGame>(connection);
byte classId = SystemAPI.HasSingleton<ClassSelection>() ? SystemAPI.GetSingleton<ClassSelection>().ClassId : (byte)0;
var request = ecb.CreateEntity();
ecb.AddComponent(request, new GoInGameRequest { ClassId = classId }); // Slice 2: carry the chosen class
ecb.AddComponent(request, new SendRpcCommandRequest { TargetConnection = connection });
}
ecb.Playback(state.EntityManager);
ecb.Dispose();
}
}
}