Files
Project-M/Assets/_Project/Scripts/Server/Connection/ServerConnectionControlSystem.cs
T
kronic f31ffe910b Frontend menu + settings + saves foundation
Netcode frontend pattern: UITK main menu / pause / settings (MenuUi + controllers), on-demand world lifecycle (WorldLauncher/SessionRunner), GameBootstrap menu branch; Graphics/Audio settings (SettingsService/GameVolume); single-slot save foundation (SaveData/SaveService, born-correct load at director spawn, autosave on Siege->Calm + quit); RuntimePanelSettings + theme; BuildTool menu; 10 EditMode tests.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-06 15:05:36 -07:00

44 lines
1.8 KiB
C#

using ProjectM.Simulation;
using Unity.Collections;
using Unity.Entities;
using Unity.NetCode;
using Unity.Networking.Transport;
namespace ProjectM.Server
{
/// <summary>
/// Server-world half of the M4 LAN host flow. When the per-world <see cref="ConnectionConfig"/>
/// requests <see cref="ConnectionMode.Host"/>, creates a <see cref="NetworkStreamRequestListen"/>
/// entity (netcode binds the server driver and starts listening) and clears the request. Replaces the
/// old <c>AutoConnectPort</c> auto-listen. Not Burst-compiled: a cold path that fires only on an
/// explicit host request.
/// </summary>
[WorldSystemFilter(WorldSystemFilterFlags.ServerSimulation)]
public partial struct ServerConnectionControlSystem : ISystem
{
public void OnCreate(ref SystemState state)
{
state.RequireForUpdate<ConnectionConfig>();
}
public void OnUpdate(ref SystemState state)
{
var cfgRef = SystemAPI.GetSingletonRW<ConnectionConfig>();
if (!cfgRef.ValueRO.Requested || cfgRef.ValueRO.Mode != ConnectionMode.Host)
return;
cfgRef.ValueRW.Requested = false;
var endpoint = cfgRef.ValueRO.Address.ToString() == "127.0.0.1"
? NetworkEndpoint.LoopbackIpv4.WithPort(cfgRef.ValueRO.Port) // single-player: bind loopback only (no firewall prompt)
: NetworkEndpoint.AnyIpv4.WithPort(cfgRef.ValueRO.Port); // host: accept LAN peers
var ecb = new EntityCommandBuffer(Allocator.Temp);
var req = ecb.CreateEntity();
ecb.AddComponent(req, new NetworkStreamRequestListen { Endpoint = endpoint });
ecb.Playback(state.EntityManager);
ecb.Dispose();
}
}
}