#if UNITY_EDITOR
using ProjectM.Simulation;
using Unity.Collections;
using Unity.Entities;
using Unity.NetCode;
using UnityEngine;
namespace ProjectM.Client
{
///
/// MC-0 — EDITOR-ONLY client owner of the local singleton (the value the PREDICTED
/// DashSystem reads on this client). Each tick it (1) drains any authoritative
/// into the static, then (2) pushes into the
/// client TuningConfig singleton. The overlay mutates the readout OPTIMISTICALLY via
/// (so the tuner's own dash uses a nudged value instantly — instant, then
/// EVENTUALLY CONSISTENT with the server within ~1 RTT once the SetTuning RPC lands and the next report
/// reconciles). Runs EVERY tick (no RequireForUpdate) so an overlay-only change with no report still reaches the
/// singleton. Plain client (mutated at most once/tick → the predicted
/// re-sim reads a per-tick-constant value); non-Burst (touches a managed static). This DELIBERATELY extends the
/// DevTelemetry pattern (which writes only a static) with a per-world singleton; the release path has no such
/// system, so DashSystem's TryGetSingleton ? : Defaults() fallback is the permanent release behaviour.
///
[WorldSystemFilter(WorldSystemFilterFlags.ClientSimulation)]
[UpdateInGroup(typeof(SimulationSystemGroup))]
public partial struct DevTuningReceiveSystem : ISystem
{
public void OnCreate(ref SystemState state)
{
if (state.GetEntityQuery(ComponentType.ReadWrite()).IsEmpty)
{
var e = state.EntityManager.CreateEntity(typeof(TuningConfig));
state.EntityManager.SetComponentData(e, TuningConfig.Defaults());
}
}
public void OnUpdate(ref SystemState state)
{
// (1) Authoritative server snapshot wins — overwrite the readout (FULL state).
var ecb = new EntityCommandBuffer(Allocator.Temp);
foreach (var (report, reqEntity) in
SystemAPI.Query>()
.WithAll().WithEntityAccess())
{
TuningReadout.Current = TuningConfig.FromReport(report.ValueRO);
TuningReadout.Initialized = true;
ecb.DestroyEntity(reqEntity);
}
ecb.Playback(state.EntityManager);
ecb.Dispose();
// (2) Push the readout (authoritative, or optimistic SetLocal, or Defaults()) into the client singleton.
if (SystemAPI.TryGetSingletonEntity(out var cfgEntity))
state.EntityManager.SetComponentData(cfgEntity, TuningReadout.Current);
}
}
///
/// MC-0 — static bridge from the ECS tuning receiver to the IMGUI DebugOverlay (so the overlay reads/sets
/// a plain struct, never ECS state). seeds to on
/// play-enter (so it matches the server's seeded Defaults before the first report). is the
/// overlay's OPTIMISTIC apply — it runs the same clamp so an overlay nudge can
/// never feed a 0/negative into the predicted DashSystem.
///
public static class TuningReadout
{
public static TuningConfig Current;
/// True once an authoritative server report has been received (else is Defaults()).
public static bool Initialized;
/// Optimistic local apply (overlay button) — clamped via .
public static void SetLocal(byte knob, float value) => TuningConfig.Apply(ref Current, knob, value);
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)]
static void Reset()
{
Current = TuningConfig.Defaults();
Initialized = false;
}
}
}
#endif