Merge branch 'heads/upstream' into OBT/1.2.0(SpringUpdate)
This commit is contained in:
@@ -1,107 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace Barotrauma.Networking
|
||||
{
|
||||
partial class Client
|
||||
{
|
||||
public void SetClientCharacter(Character character)
|
||||
{
|
||||
GameMain.Server.SetClientCharacter(this, character);
|
||||
}
|
||||
|
||||
public void Kick(string reason = "")
|
||||
{
|
||||
GameMain.Server.KickClient(this.Connection, reason);
|
||||
}
|
||||
|
||||
public void Ban(string reason = "", float seconds = -1)
|
||||
{
|
||||
if (seconds == -1)
|
||||
{
|
||||
GameMain.Server.BanClient(this, reason, null);
|
||||
}
|
||||
else
|
||||
{
|
||||
GameMain.Server.BanClient(this, reason, TimeSpan.FromSeconds(seconds));
|
||||
}
|
||||
}
|
||||
|
||||
public static void UnbanPlayer(string playerName)
|
||||
{
|
||||
GameMain.Server.UnbanPlayer(playerName);
|
||||
}
|
||||
|
||||
public static void BanPlayer(string player, string reason, bool range = false, float seconds = -1)
|
||||
{
|
||||
if (seconds == -1)
|
||||
{
|
||||
GameMain.Server.BanPlayer(player, reason, null);
|
||||
}
|
||||
else
|
||||
{
|
||||
GameMain.Server.BanPlayer(player, reason, TimeSpan.FromSeconds(seconds));
|
||||
}
|
||||
}
|
||||
|
||||
public bool CheckPermission(ClientPermissions permissions)
|
||||
{
|
||||
return this.Permissions.HasFlag(permissions);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
namespace Barotrauma
|
||||
{
|
||||
using Microsoft.Xna.Framework;
|
||||
using System.Reflection;
|
||||
|
||||
partial class Item
|
||||
{
|
||||
public object CreateServerEventString(string component)
|
||||
{
|
||||
var comp = GetComponentString(component);
|
||||
|
||||
if (comp == null)
|
||||
return null;
|
||||
|
||||
MethodInfo method = typeof(Item).GetMethod(nameof(Item.CreateServerEvent), new Type[]{ Type.MakeGenericMethodParameter(0) });
|
||||
MethodInfo generic = method.MakeGenericMethod(comp.GetType());
|
||||
return generic.Invoke(this, new object[]{ comp });
|
||||
}
|
||||
|
||||
public object CreateServerEventString(string component, object[] extraData)
|
||||
{
|
||||
var comp = GetComponentString(component);
|
||||
|
||||
if (comp == null)
|
||||
return null;
|
||||
|
||||
MethodInfo method = typeof(Item).GetMethod(nameof(Item.CreateServerEvent), new Type[]{ Type.MakeGenericMethodParameter(0), typeof(object[]) });
|
||||
MethodInfo generic = method.MakeGenericMethod(comp.GetType());
|
||||
return generic.Invoke(this, new object[]{comp, extraData });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
namespace Barotrauma.Items.Components
|
||||
{
|
||||
using Barotrauma.Networking;
|
||||
|
||||
partial struct Signal
|
||||
{
|
||||
public static Signal Create(string value, int stepsTaken = 0, Character sender = null, Item source = null, float power = 0.0f, float strength = 1.0f)
|
||||
{
|
||||
return new Signal(value, stepsTaken, sender, source, power, strength);
|
||||
}
|
||||
}
|
||||
|
||||
partial class Quality
|
||||
{
|
||||
public void SetValue(StatType statType, float value)
|
||||
{
|
||||
statValues[statType] = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,7 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using Barotrauma.LuaCs;
|
||||
|
||||
namespace Barotrauma
|
||||
{
|
||||
@@ -9,7 +10,7 @@ namespace Barotrauma
|
||||
{
|
||||
public static void Install()
|
||||
{
|
||||
ContentPackage luaPackage = LuaCsSetup.GetPackage(LuaCsSetup.LuaForBarotraumaId);
|
||||
ContentPackage luaPackage = LuaCsSetup.GetLuaCsPackage();
|
||||
|
||||
if (luaPackage == null)
|
||||
{
|
||||
@@ -45,8 +46,9 @@ namespace Barotrauma
|
||||
File.Copy(Path.Combine(path, "Binary", file), file, true);
|
||||
}
|
||||
|
||||
File.WriteAllText(LuaCsSetup.VersionFile, luaPackage.ModVersion);
|
||||
|
||||
#if WINDOWS
|
||||
File.WriteAllText("LuaCsDedicatedServer.bat", "\"%LocalAppData%/Daedalic Entertainment GmbH/Barotrauma/WorkshopMods/Installed/2559634234/Binary/DedicatedServer.exe\"");
|
||||
#endif
|
||||
}
|
||||
catch (UnauthorizedAccessException e)
|
||||
{
|
||||
|
||||
@@ -1,194 +0,0 @@
|
||||
using Barotrauma.Networking;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace Barotrauma
|
||||
{
|
||||
partial class LuaCsNetworking
|
||||
{
|
||||
private const int MaxRegisterPerClient = 1000;
|
||||
|
||||
private Dictionary<string, int> clientRegisterCount = new Dictionary<string, int>();
|
||||
|
||||
private ushort currentId = 0;
|
||||
|
||||
public void NetMessageReceived(IReadMessage netMessage, ClientPacketHeader header, Client client = null)
|
||||
{
|
||||
if (header != ClientPacketHeader.LUA_NET_MESSAGE)
|
||||
{
|
||||
GameMain.LuaCs.Hook.Call("netMessageReceived", netMessage, header, client);
|
||||
return;
|
||||
}
|
||||
|
||||
LuaCsClientToServer luaCsHeader = (LuaCsClientToServer)netMessage.ReadByte();
|
||||
|
||||
switch (luaCsHeader)
|
||||
{
|
||||
case LuaCsClientToServer.NetMessageString:
|
||||
HandleNetMessageString(netMessage, client);
|
||||
break;
|
||||
|
||||
case LuaCsClientToServer.NetMessageId:
|
||||
HandleNetMessageId(netMessage, client);
|
||||
break;
|
||||
|
||||
case LuaCsClientToServer.RequestAllIds:
|
||||
WriteAllIds(client);
|
||||
break;
|
||||
|
||||
case LuaCsClientToServer.RequestSingleId:
|
||||
RequestIdSingle(netMessage, client);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private void HandleNetMessageId(IReadMessage netMessage, Client client = null)
|
||||
{
|
||||
ushort id = netMessage.ReadUInt16();
|
||||
|
||||
if (idToString.ContainsKey(id))
|
||||
{
|
||||
string name = idToString[id];
|
||||
|
||||
HandleNetMessage(netMessage, name, client);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (GameSettings.CurrentConfig.VerboseLogging)
|
||||
{
|
||||
LuaCsLogger.LogError($"Received NetMessage for unknown id {id} from {GameServer.ClientLogName(client)}.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public IWriteMessage Start(string netMessageName)
|
||||
{
|
||||
var message = new WriteOnlyMessage();
|
||||
|
||||
message.WriteByte((byte)ServerPacketHeader.LUA_NET_MESSAGE);
|
||||
|
||||
if (stringToId.ContainsKey(netMessageName))
|
||||
{
|
||||
message.WriteByte((byte)LuaCsServerToClient.NetMessageId);
|
||||
message.WriteUInt16(stringToId[netMessageName]);
|
||||
}
|
||||
else
|
||||
{
|
||||
message.WriteByte((byte)LuaCsServerToClient.NetMessageString);
|
||||
message.WriteString(netMessageName);
|
||||
}
|
||||
|
||||
return message;
|
||||
}
|
||||
|
||||
public void Receive(string netMessageName, LuaCsAction callback)
|
||||
{
|
||||
RegisterId(netMessageName);
|
||||
|
||||
netReceives[netMessageName] = callback;
|
||||
}
|
||||
|
||||
public ushort RegisterId(string name)
|
||||
{
|
||||
if (stringToId.ContainsKey(name))
|
||||
{
|
||||
return stringToId[name];
|
||||
}
|
||||
|
||||
if (currentId >= ushort.MaxValue)
|
||||
{
|
||||
LuaCsLogger.LogError($"Tried to register more than {ushort.MaxValue} network ids!");
|
||||
return 0;
|
||||
}
|
||||
|
||||
currentId++;
|
||||
|
||||
idToString[currentId] = name;
|
||||
stringToId[name] = currentId;
|
||||
|
||||
WriteIdToAll(currentId, name);
|
||||
|
||||
return currentId;
|
||||
}
|
||||
|
||||
private void RequestIdSingle(IReadMessage netMessage, Client client)
|
||||
{
|
||||
string name = netMessage.ReadString();
|
||||
|
||||
if (!stringToId.ContainsKey(name) && client.AccountId.TryUnwrap(out AccountId id))
|
||||
{
|
||||
if (!clientRegisterCount.ContainsKey(id.StringRepresentation))
|
||||
{
|
||||
clientRegisterCount[id.StringRepresentation] = 0;
|
||||
}
|
||||
|
||||
clientRegisterCount[id.StringRepresentation]++;
|
||||
|
||||
if (clientRegisterCount[id.StringRepresentation] > MaxRegisterPerClient)
|
||||
{
|
||||
LuaCsLogger.Log($"{GameServer.ClientLogName(client)} Tried to register more than {MaxRegisterPerClient} Ids!");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
RegisterId(name);
|
||||
}
|
||||
|
||||
private void WriteIdToAll(ushort id, string name)
|
||||
{
|
||||
WriteOnlyMessage message = new WriteOnlyMessage();
|
||||
message.WriteByte((byte)ServerPacketHeader.LUA_NET_MESSAGE);
|
||||
message.WriteByte((byte)LuaCsServerToClient.ReceiveIds);
|
||||
|
||||
message.WriteUInt16(1);
|
||||
message.WriteUInt16(id);
|
||||
message.WriteString(name);
|
||||
|
||||
Send(message, null, DeliveryMethod.Reliable);
|
||||
}
|
||||
|
||||
private void WriteAllIds(Client client)
|
||||
{
|
||||
WriteOnlyMessage message = new WriteOnlyMessage();
|
||||
message.WriteByte((byte)ServerPacketHeader.LUA_NET_MESSAGE);
|
||||
message.WriteByte((byte)LuaCsServerToClient.ReceiveIds);
|
||||
|
||||
message.WriteUInt16((ushort)idToString.Count());
|
||||
foreach ((ushort id, string name) in idToString)
|
||||
{
|
||||
message.WriteUInt16(id);
|
||||
message.WriteString(name);
|
||||
}
|
||||
|
||||
Send(message, client.Connection, DeliveryMethod.Reliable);
|
||||
}
|
||||
|
||||
public void ClientWriteLobby(Client client) => GameMain.Server.ClientWriteLobby(client);
|
||||
|
||||
public void Send(IWriteMessage netMessage, NetworkConnection connection = null, DeliveryMethod deliveryMethod = DeliveryMethod.Reliable)
|
||||
{
|
||||
if (connection == null)
|
||||
{
|
||||
foreach (NetworkConnection conn in Client.ClientList.Select(c => c.Connection))
|
||||
{
|
||||
GameMain.Server.ServerPeer.Send(netMessage, conn, deliveryMethod);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
GameMain.Server.ServerPeer.Send(netMessage, connection, deliveryMethod);
|
||||
}
|
||||
}
|
||||
|
||||
public void UpdateClientPermissions(Client client)
|
||||
{
|
||||
GameMain.Server.UpdateClientPermissions(client);
|
||||
}
|
||||
|
||||
public int FileSenderMaxPacketsPerUpdate
|
||||
{
|
||||
get { return FileSender.FileTransferOut.MaxPacketsPerUpdate; }
|
||||
set { FileSender.FileTransferOut.MaxPacketsPerUpdate = value; }
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using Barotrauma.Networking;
|
||||
|
||||
namespace Barotrauma;
|
||||
|
||||
partial class LuaCsSetup
|
||||
{
|
||||
partial void CheckReadyToRun(Action onReadyToRun)
|
||||
{
|
||||
onReadyToRun?.Invoke();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Handles changes in game states tracked by screen changes.
|
||||
/// </summary>
|
||||
/// <param name="screen">The new game screen.</param>
|
||||
public partial void OnScreenSelected(Screen screen)
|
||||
{
|
||||
// the server is always in the running state unless explicitly stopped.
|
||||
if (screen == UnimplementedScreen.Instance)
|
||||
SetRunState(RunState.Unloaded);
|
||||
SetRunState(RunState.Running);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,188 @@
|
||||
using Barotrauma.LuaCs.Events;
|
||||
using Barotrauma.Networking;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
// ReSharper disable once CheckNamespace
|
||||
namespace Barotrauma.LuaCs;
|
||||
|
||||
partial class NetworkingService : INetworkingService, IEventClientRawNetMessageReceived
|
||||
{
|
||||
private const int MaxRegisterPerClient = 1000;
|
||||
|
||||
private Dictionary<string, int> clientRegisterCount = new Dictionary<string, int>();
|
||||
|
||||
private ushort currentId = 0;
|
||||
|
||||
public IWriteMessage Start(NetId netId)
|
||||
{
|
||||
var message = new WriteOnlyMessage();
|
||||
|
||||
message.WriteByte((byte)ServerHeader);
|
||||
|
||||
if (idToPacket.ContainsKey(netId))
|
||||
{
|
||||
message.WriteByte((byte)ServerToClient.NetMessageInternalId);
|
||||
message.WriteUInt16(idToPacket[netId]);
|
||||
}
|
||||
else
|
||||
{
|
||||
message.WriteByte((byte)ServerToClient.NetMessageNetId);
|
||||
NetId.Write(message, netId);
|
||||
}
|
||||
|
||||
return message;
|
||||
}
|
||||
|
||||
public bool? OnReceivedClientNetMessage(IReadMessage netMessage, ClientPacketHeader clientPacketHeader, NetworkConnection sender)
|
||||
{
|
||||
if (clientPacketHeader != ClientHeader)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
Client client = GameMain.Server.ConnectedClients.First(c => c.Connection == sender);
|
||||
|
||||
ClientToServer luaCsHeader = (ClientToServer)netMessage.ReadByte();
|
||||
|
||||
switch (luaCsHeader)
|
||||
{
|
||||
case ClientToServer.NetMessageNetId:
|
||||
HandleNetMessageString(netMessage, client);
|
||||
break;
|
||||
|
||||
case ClientToServer.NetMessageInternalId:
|
||||
HandleNetMessageId(netMessage, client);
|
||||
break;
|
||||
|
||||
case ClientToServer.RequestSync:
|
||||
WriteSync(client);
|
||||
break;
|
||||
|
||||
case ClientToServer.RequestSingleNetId:
|
||||
RequestIdSingle(netMessage, client);
|
||||
break;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private void HandleNetMessageId(IReadMessage netMessage, Client client = null)
|
||||
{
|
||||
ushort id = netMessage.ReadUInt16();
|
||||
|
||||
if (packetToId.ContainsKey(id))
|
||||
{
|
||||
NetId netId = packetToId[id];
|
||||
|
||||
HandleNetMessage(netMessage, netId, client);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (GameSettings.CurrentConfig.VerboseLogging)
|
||||
{
|
||||
_loggerService.LogError($"Received NetMessage for unknown id {id} from {GameServer.ClientLogName(client)}.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private ushort RegisterId(NetId netId)
|
||||
{
|
||||
if (idToPacket.ContainsKey(netId))
|
||||
{
|
||||
return idToPacket[netId];
|
||||
}
|
||||
|
||||
if (currentId >= ushort.MaxValue)
|
||||
{
|
||||
_loggerService.LogError($"Tried to register more than {ushort.MaxValue} network ids!");
|
||||
return 0;
|
||||
}
|
||||
|
||||
currentId++;
|
||||
|
||||
packetToId[currentId] = netId;
|
||||
idToPacket[netId] = currentId;
|
||||
|
||||
WriteIdToAll(currentId, netId);
|
||||
|
||||
return currentId;
|
||||
}
|
||||
|
||||
private void RequestIdSingle(IReadMessage netMessage, Client client)
|
||||
{
|
||||
NetId netId = NetId.Read(netMessage);
|
||||
|
||||
if (!idToPacket.ContainsKey(netId) && client.AccountId.TryUnwrap(out AccountId id))
|
||||
{
|
||||
if (!clientRegisterCount.ContainsKey(id.StringRepresentation))
|
||||
{
|
||||
clientRegisterCount[id.StringRepresentation] = 0;
|
||||
}
|
||||
|
||||
clientRegisterCount[id.StringRepresentation]++;
|
||||
|
||||
if (clientRegisterCount[id.StringRepresentation] > MaxRegisterPerClient)
|
||||
{
|
||||
_loggerService.Log($"{GameServer.ClientLogName(client)} Tried to register more than {MaxRegisterPerClient} Ids!");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
RegisterId(netId);
|
||||
}
|
||||
|
||||
private void WriteIdToAll(ushort packet, NetId netId)
|
||||
{
|
||||
WriteOnlyMessage message = new WriteOnlyMessage();
|
||||
message.WriteByte((byte)ServerHeader);
|
||||
message.WriteByte((byte)ServerToClient.ReceiveNetIds);
|
||||
|
||||
message.WriteUInt16(1);
|
||||
message.WriteUInt16(packet);
|
||||
NetId.Write(message, netId);
|
||||
|
||||
SendToClient(message, null, DeliveryMethod.Reliable);
|
||||
}
|
||||
|
||||
private void WriteSync(Client client)
|
||||
{
|
||||
WriteOnlyMessage message = new WriteOnlyMessage();
|
||||
message.WriteByte((byte)ServerHeader);
|
||||
message.WriteByte((byte)ServerToClient.ReceiveNetIds);
|
||||
|
||||
message.WriteUInt16((ushort)packetToId.Count());
|
||||
foreach ((ushort packet, NetId netId) in packetToId)
|
||||
{
|
||||
message.WriteUInt16(packet);
|
||||
NetId.Write(message, netId);
|
||||
}
|
||||
|
||||
SendToClient(message, client.Connection, DeliveryMethod.Reliable);
|
||||
|
||||
// TODO: when we move to using GUIDs for everything, this should combined into a single message
|
||||
foreach (INetworkSyncVar netVar in netVars.Keys)
|
||||
{
|
||||
SendNetVar(netVar, client.Connection);
|
||||
}
|
||||
}
|
||||
|
||||
public void SendToClient(IWriteMessage netMessage, NetworkConnection connection = null, DeliveryMethod deliveryMethod = DeliveryMethod.Reliable)
|
||||
{
|
||||
if (connection == null)
|
||||
{
|
||||
foreach (NetworkConnection conn in ModUtils.Client.ClientList.Select(c => c.Connection))
|
||||
{
|
||||
GameMain.Server.ServerPeer.Send(netMessage, conn, deliveryMethod);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
GameMain.Server.ServerPeer.Send(netMessage, connection, deliveryMethod);
|
||||
}
|
||||
}
|
||||
|
||||
public void Send(IWriteMessage netMessage, NetworkConnection connection = null, DeliveryMethod deliveryMethod = DeliveryMethod.Reliable)
|
||||
=> SendToClient(netMessage, connection, deliveryMethod);
|
||||
}
|
||||
Reference in New Issue
Block a user