added client and server network communication

This commit is contained in:
Evil Factory
2021-09-20 16:15:29 -03:00
parent c5d365eaa7
commit 784baf550f
6 changed files with 79 additions and 11 deletions

View File

@@ -958,6 +958,9 @@ namespace Barotrauma.Networking
case ServerPacketHeader.EVENTACTION:
GameMain.GameSession?.EventManager.ClientRead(inc);
break;
case ServerPacketHeader.LUA_NET_MESSAGE:
GameMain.Lua.networking.NetMessageReceived(inc);
break;
}
}

View File

@@ -849,6 +849,9 @@ namespace Barotrauma.Networking
case ClientPacketHeader.ERROR:
HandleClientError(inc, connectedClient);
break;
case ClientPacketHeader.LUA_NET_MESSAGE:
GameMain.Lua.networking.NetMessageReceived(inc, connectedClient);
break;
}
}

View File

@@ -1,6 +1,6 @@
local runDisabledMods = false
if Game.IsDedicated then
if SERVER and Game.IsDedicated then
runDisabledMods = true
print("LUA LOADER: Dedicated server detected, loading mods regardless being disabled.")

View File

@@ -397,7 +397,7 @@ namespace Barotrauma
}
}
private partial class LuaNetworking
public partial class LuaNetworking
{
public LuaSetup env;
@@ -406,6 +406,61 @@ namespace Barotrauma
env = e;
}
public Dictionary<string, object> NetReceives = new Dictionary<string, object>();
[MoonSharpHidden]
public void NetMessageReceived(IReadMessage netMessage, Client client = null)
{
string netMessageName = netMessage.ReadString();
if (NetReceives[netMessageName] is Closure)
env.lua.Call(NetReceives[netMessageName], new object[] { netMessage, client });
}
public void Receive(string netMessageName, object callback)
{
NetReceives[netMessageName] = callback;
}
public IWriteMessage Start(string netMessageName)
{
var message = new WriteOnlyMessage();
#if SERVER
message.Write((byte)ServerPacketHeader.LUA_NET_MESSAGE);
#else
message.Write((byte)ClientPacketHeader.LUA_NET_MESSAGE);
#endif
message.Write(netMessageName);
return ((IWriteMessage)message);
}
public IWriteMessage Start()
{
return new WriteOnlyMessage();
}
#if SERVER
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);
}
}
#else
public void Send(IWriteMessage netMessage, DeliveryMethod deliveryMethod = DeliveryMethod.Reliable)
{
GameMain.Client.ClientPeer.Send(netMessage, deliveryMethod);
}
#endif
public string RequestPostHTTP(string url, string data, string contentType = "application/json")
{
try

View File

@@ -20,6 +20,7 @@ namespace Barotrauma
public LuaHook hook;
public LuaGame game;
public LuaNetworking networking;
public LuaScriptLoader luaScriptLoader;
@@ -281,6 +282,11 @@ namespace Barotrauma
UserData.RegisterType<InputType>();
UserData.RegisterType<Key>();
UserData.RegisterType<NetLobbyScreen>();
UserData.RegisterType<IWriteMessage>();
UserData.RegisterType<IReadMessage>();
UserData.RegisterType<ServerPacketHeader>();
UserData.RegisterType<ClientPacketHeader>();
UserData.RegisterType<DeliveryMethod>();
lua = new Script(CoreModules.Preset_SoftSandbox);
@@ -290,6 +296,7 @@ namespace Barotrauma
hook = new LuaHook(this);
game = new LuaGame(this);
networking = new LuaNetworking(this);
lua.Globals["TestFunction"] = (Func<float, float>)TestFunction;
@@ -310,7 +317,7 @@ namespace Barotrauma
lua.Globals["Random"] = new LuaRandom();
lua.Globals["Timer"] = new LuaTimer(this);
lua.Globals["File"] = UserData.CreateStatic<LuaFile>();
lua.Globals["Networking"] = new LuaNetworking(this);
lua.Globals["Networking"] = networking;
lua.Globals["WayPoint"] = UserData.CreateStatic<WayPoint>();
lua.Globals["SpawnType"] = UserData.CreateStatic<SpawnType>();
lua.Globals["ChatMessageType"] = UserData.CreateStatic<ChatMessageType>();
@@ -341,6 +348,9 @@ namespace Barotrauma
lua.Globals["ContentPackage"] = UserData.CreateStatic<ContentPackage>();
lua.Globals["ClientPermissions"] = UserData.CreateStatic<ClientPermissions>();
lua.Globals["Signal"] = UserData.CreateStatic<Signal>();
lua.Globals["DeliveryMethod"] = UserData.CreateStatic<DeliveryMethod>();
lua.Globals["ClientPacketHeader"] = UserData.CreateStatic<ClientPacketHeader>();
lua.Globals["ServerPacketHeader"] = UserData.CreateStatic<ServerPacketHeader>();
bool isServer = true;
@@ -352,11 +362,7 @@ namespace Barotrauma
lua.Globals["SERVER"] = isServer;
lua.Globals["CLIENT"] = !isServer;
#if CLIENT
return;
#endif
if (File.Exists("Lua/MoonsharpSetup.lua")) // try the default loader
DoFile("Lua/MoonsharpSetup.lua");
else if (File.Exists("Mods/LuaForBarotrauma/Lua/MoonsharpSetup.lua")) // in case its the workshop version

View File

@@ -31,8 +31,8 @@ namespace Barotrauma.Networking
ERROR, //tell the server that an error occurred
CREW,
READY_CHECK,
READY_TO_SPAWN
READY_TO_SPAWN,
LUA_NET_MESSAGE
}
enum ClientNetObject
{
@@ -80,7 +80,8 @@ namespace Barotrauma.Networking
MISSION,
EVENTACTION,
CREW, //anything related to managing bots in multiplayer
READY_CHECK //start, end and update a ready check
READY_CHECK, //start, end and update a ready check
LUA_NET_MESSAGE
}
enum ServerNetObject
{