Files
LuaCsForBarotraumaEP/Barotrauma/BarotraumaShared/SharedSource/LuaCs/Services/NetworkingService.cs
MapleWheels 6880e5e9ee [Milestone] AssemblyLoader completed.
Details:
- Assembly Mgmt Service for loading now a separate interface, not intended for normal use.
- Assembly Loader work; implemented custom dictionary key and table.
- Assembly loading work.
- EventService completed.
- Moved assembly extensions to ModUtils.cs
- Work to event service.
NetworkService work
- Added ImpromptuInterfaces package.
- Networking Service work to support NetVars
- Event Service
- Added assemblies references package for script compilation. Updated Roslyn version for compatibility.
- Package Loading work.
Swap Harmony to HarmonyX
- More refactor conversion to FluentResults.
- Updated StylesService to return Results.
- Refactor of PackageService partially complete.
- Made IService.Reset() required to return a Result.
- Moved plugin/assembly related code to their own folder (same namespace).
- Updated interfaces to reflect the use of Result<T>.
- Partial refactor, incomplete.
- Added 'FluentResults' so we can stop using cursed Exception-based flow control in loading code.
- Added 'OneOf' nuget package: https://github.com/mcintyre321/OneOf
for the implementation of the Optional<T> pattern and complex discrete return types instead of cursed enums (see current AssemblyManager.cs).
- Reapplied old branch changes.
2026-02-07 20:10:26 -05:00

129 lines
3.4 KiB
C#

using Barotrauma.LuaCs.Services;
using Barotrauma.Networking;
using System;
using System.Collections.Generic;
namespace Barotrauma.LuaCs.Networking;
internal partial class NetworkingService : INetworkingService
{
private enum LuaCsClientToServer
{
NetMessageId,
NetMessageString,
RequestSingleId,
RequestAllIds,
}
private enum LuaCsServerToClient
{
NetMessageId,
NetMessageString,
ReceiveIds
}
private Dictionary<Guid, INetVar> netVars = new Dictionary<Guid, INetVar>();
private Dictionary<Guid, NetMessageReceived> netReceives = new Dictionary<Guid, NetMessageReceived>();
private Dictionary<ushort, Guid> packetToId = new Dictionary<ushort, Guid>();
private Dictionary<Guid, ushort> idToPacket = new Dictionary<Guid, ushort>();
public bool IsActive
{
get
{
return GameMain.NetworkMember != null; // ehh?
}
}
public bool IsSynchronized { get; private set; }
public bool IsDisposed { get; private set; }
public void Initialize()
{
#if SERVER
IsSynchronized = true;
#elif CLIENT
SendSyncMessage();
#endif
}
public void RegisterNetVar(INetVar netVar)
{
netVars[netVar.InstanceId] = netVar;
netReceives[netVar.InstanceId] = (IReadMessage netMessage) =>
{
INetReadMessage internalMind = new NetReadMessage();
internalMind.SetMessage(netMessage);
netVar.ReadNetMessage(internalMind);
};
}
public void SendNetVar(INetVar netVar)
{
if (netVars.ContainsKey(netVar.InstanceId))
{
INetWriteMessage message = Start(netVar.InstanceId);
netVar.WriteNetMessage(message);
Send(message.Message);
}
}
public void Receive(Guid netId, NetMessageReceived callback)
{
#if SERVER
RegisterId(netId);
#elif CLIENT
RequestId(netId);
#endif
netReceives[netId] = callback;
}
private void HandleNetMessage(IReadMessage netMessage, Guid netId, Client client = null)
{
if (netReceives.ContainsKey(netId))
{
try
{
netReceives[netId](netMessage);
}
catch (Exception e)
{
LuaCsLogger.LogError($"Exception thrown inside NetMessageReceive({netId})", LuaCsMessageOrigin.CSharpMod);
LuaCsLogger.HandleException(e, LuaCsMessageOrigin.CSharpMod);
}
}
else
{
if (GameSettings.CurrentConfig.VerboseLogging)
{
#if SERVER
LuaCsLogger.LogError($"Received NetMessage for unknown netid {netId} from {GameServer.ClientLogName(client)}.");
#else
LuaCsLogger.LogError($"Received NetMessage for unknown netid {netId} from server.");
#endif
}
}
}
private void HandleNetMessageString(IReadMessage netMessage, Client client = null)
{
Guid guid = new Guid(netMessage.ReadBytes(16));
HandleNetMessage(netMessage, guid, client);
}
public FluentResults.Result Reset()
{
IsSynchronized = false;
netReceives = new Dictionary<Guid, NetMessageReceived>();
packetToId = new Dictionary<ushort, Guid>();
idToPacket = new Dictionary<Guid, ushort>();
return FluentResults.Result.Ok();
}
public void Dispose()
{
IsDisposed = true;
}
}