Files
Project-M/Assets/_Project/Scripts/Simulation/Connection/EditorAutoHostSystem.cs
T
Luis Gonzalez e851d5f8e9 Co-Op Layer
2026-06-01 10:48:18 -07:00

36 lines
1.6 KiB
C#

#if UNITY_EDITOR
using Unity.Entities;
using Unity.NetCode;
namespace ProjectM.Simulation
{
/// <summary>
/// EDITOR-ONLY dev convenience for the M4 LAN flow. With auto-connect disabled in <c>GameBootstrap</c>,
/// this seeds a <see cref="ConnectionConfig"/> in each world on its first update so entering Play "just
/// works" multi-client: the server world hosts on loopback and every client / thin-client world joins
/// it — reproducing the old single-key playflow but for N players. Runs once per world then disables
/// itself, and skips seeding if a <see cref="ConnectionConfig"/> already exists (e.g. set by
/// ConnectionUI). Does not exist in player builds.
/// </summary>
[WorldSystemFilter(WorldSystemFilterFlags.ServerSimulation | WorldSystemFilterFlags.ClientSimulation | WorldSystemFilterFlags.ThinClientSimulation)]
public partial struct EditorAutoHostSystem : ISystem
{
public void OnUpdate(ref SystemState state)
{
state.Enabled = false; // run exactly once per world
if (SystemAPI.HasSingleton<ConnectionConfig>())
return;
const ushort port = 7979;
var cfg = state.WorldUnmanaged.IsServer()
? new ConnectionConfig { Mode = ConnectionMode.Host, Address = "127.0.0.1", Port = port, Requested = true }
: new ConnectionConfig { Mode = ConnectionMode.Join, Address = "127.0.0.1", Port = port, Requested = true };
var e = state.EntityManager.CreateEntity();
state.EntityManager.AddComponentData(e, cfg);
}
}
}
#endif