making things more organized, turning Player deprecated, preparing for nlua and using partial classes for additions to another barotrauma classes

This commit is contained in:
Evil Factory
2021-09-10 18:32:19 -03:00
parent 176a24cb43
commit 813aaf29b8
8 changed files with 118 additions and 20 deletions
@@ -1250,7 +1250,7 @@ namespace Barotrauma
commands.Add(new Command("reloadlua", "reloads lua", (string[] args) => commands.Add(new Command("reloadlua", "reloads lua", (string[] args) =>
{ {
GameMain.Lua = new LuaSetup(); GameMain.Lua.Initialize();
})); }));
@@ -137,6 +137,8 @@ namespace Barotrauma
NetLobbyScreen = new NetLobbyScreen(); NetLobbyScreen = new NetLobbyScreen();
CheckContentPackage(); CheckContentPackage();
Lua = new LuaSetup();
} }
@@ -0,0 +1,62 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace Barotrauma.Networking
{
partial class Client
{
public static List<Client> ClientList
{
get
{
return GameMain.Server.ConnectedClients;
}
}
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 = "", bool range = false, float seconds = -1)
{
if (seconds == -1)
{
GameMain.Server.BanClient(this, reason, range, null);
}
else
{
GameMain.Server.BanClient(this, reason, range, TimeSpan.FromSeconds(seconds));
}
}
public static void Unban(string player, string endpoint)
{
GameMain.Server.UnbanPlayer(player, endpoint);
}
public bool CheckPermission(ClientPermissions permissions)
{
return this.Permissions.HasFlag(permissions);
}
}
}
namespace Barotrauma
{
partial class CharacterInfo
{
public static CharacterInfo Create(string speciesName, string name = "", JobPrefab jobPrefab = null, string ragdollFileName = null, int variant = 0, Rand.RandSync randSync = Rand.RandSync.Unsynced, string npcIdentifier = "")
{
return new CharacterInfo(speciesName, name, name, jobPrefab, ragdollFileName, variant, randSync, npcIdentifier);
}
}
}
@@ -13,6 +13,7 @@ using System.Xml.Linq;
namespace Barotrauma namespace Barotrauma
{ {
partial class LuaSetup partial class LuaSetup
{ {
private static Vector2 CreateVector2(float x, float y) private static Vector2 CreateVector2(float x, float y)
@@ -40,13 +41,12 @@ namespace Barotrauma
public static List<Client> GetAllClients() public static List<Client> GetAllClients()
{ {
return GameMain.Server.ConnectedClients; return GameMain.Server.ConnectedClients;
} }
public static CharacterInfo CreateCharacterInfo(string speciesName, string name = "", JobPrefab jobPrefab = null, string ragdollFileName = null, int variant = 0, Rand.RandSync randSync = Rand.RandSync.Unsynced) public static CharacterInfo CreateCharacterInfo(string speciesName, string name = "", JobPrefab jobPrefab = null, string ragdollFileName = null, int variant = 0, Rand.RandSync randSync = Rand.RandSync.Unsynced, string npcIdentifier = "")
{ {
return new CharacterInfo(speciesName, name, name, jobPrefab, ragdollFileName, variant, randSync); return new CharacterInfo(speciesName, name, name, jobPrefab, ragdollFileName, variant, randSync, npcIdentifier);
} }
public static void SetClientCharacter(Client client, Character character) public static void SetClientCharacter(Client client, Character character)
@@ -89,7 +89,7 @@ namespace Barotrauma
public static void SetSpectatorPos(Client client, Vector2 pos) public static void SetSpectatorPos(Client client, Vector2 pos)
{ {
client.SpectatePos = pos;
} }
public static void SetRadioRange(Character character, float range) public static void SetRadioRange(Character character, float range)
@@ -610,6 +610,8 @@ namespace Barotrauma
{ {
if (hf.function is Closure) if (hf.function is Closure)
lastResult = env.lua.Call(hf.function, args); lastResult = env.lua.Call(hf.function, args);
// else if (hf.function is NLua.LuaFunction luaFunction)
// lastResult = luaFunction.Call(args);
} }
catch (Exception e) catch (Exception e)
{ {
@@ -13,8 +13,10 @@ namespace Barotrauma
{ {
partial class LuaSetup partial class LuaSetup
{ {
public static LuaSetup luaSetup;
public Script lua; public Script lua;
public LuaHook hook; public LuaHook hook;
public LuaGame game; public LuaGame game;
@@ -22,9 +24,9 @@ namespace Barotrauma
public void HandleLuaException(Exception ex) public void HandleLuaException(Exception ex)
{ {
if(ex is InterpreterException) if (ex is InterpreterException)
{ {
if(((InterpreterException)ex).DecoratedMessage == null) if (((InterpreterException)ex).DecoratedMessage == null)
PrintMessage(((InterpreterException)ex).Message); PrintMessage(((InterpreterException)ex).Message);
else else
PrintMessage(((InterpreterException)ex).DecoratedMessage); PrintMessage(((InterpreterException)ex).DecoratedMessage);
@@ -70,7 +72,7 @@ namespace Barotrauma
return null; return null;
} }
public DynValue DoFile(string file, Table globalContext=null, string codeStringFriendly = null) public DynValue DoFile(string file, Table globalContext = null, string codeStringFriendly = null)
{ {
try try
{ {
@@ -133,7 +135,7 @@ namespace Barotrauma
public static DynValue CreateUserDataSafe(object o) public static DynValue CreateUserDataSafe(object o)
{ {
if(o == null) if (o == null)
return DynValue.Nil; return DynValue.Nil;
return UserData.Create(o); return UserData.Create(o);
@@ -146,8 +148,15 @@ namespace Barotrauma
luaScriptLoader.ModulePaths = str; luaScriptLoader.ModulePaths = str;
} }
public LuaSetup() public float TestFunction(float value)
{ {
return value * 2;
}
public void Initialize()
{
luaSetup = this;
PrintMessage("Lua!"); PrintMessage("Lua!");
luaScriptLoader = new LuaScriptLoader(this); luaScriptLoader = new LuaScriptLoader(this);
@@ -203,8 +212,8 @@ namespace Barotrauma
UserData.RegisterType<ItemComponent>(); UserData.RegisterType<ItemComponent>();
UserData.RegisterType<WifiComponent>(); UserData.RegisterType<WifiComponent>();
UserData.RegisterType<LightComponent>(); UserData.RegisterType<LightComponent>();
UserData.RegisterType<CustomInterface>();
UserData.RegisterType<Holdable>(); UserData.RegisterType<Holdable>();
UserData.RegisterType<CustomInterface>();
UserData.RegisterType<Inventory>(); UserData.RegisterType<Inventory>();
UserData.RegisterType<CharacterInventory>(); UserData.RegisterType<CharacterInventory>();
UserData.RegisterType<Hull>(); UserData.RegisterType<Hull>();
@@ -233,6 +242,8 @@ namespace Barotrauma
hook = new LuaHook(this); hook = new LuaHook(this);
game = new LuaGame(this); game = new LuaGame(this);
lua.Globals["TestFunction"] = (Func<float, float>)TestFunction;
lua.Globals["printNoLog"] = (Action<object>)PrintMessageNoLog; lua.Globals["printNoLog"] = (Action<object>)PrintMessageNoLog;
lua.Globals["dofile"] = (Func<string, Table, string, DynValue>)DoFile; lua.Globals["dofile"] = (Func<string, Table, string, DynValue>)DoFile;
@@ -258,6 +269,7 @@ namespace Barotrauma
lua.Globals["Submarine"] = UserData.CreateStatic<Submarine>(); lua.Globals["Submarine"] = UserData.CreateStatic<Submarine>();
lua.Globals["Client"] = UserData.CreateStatic<Client>(); lua.Globals["Client"] = UserData.CreateStatic<Client>();
lua.Globals["Character"] = UserData.CreateStatic<Character>(); lua.Globals["Character"] = UserData.CreateStatic<Character>();
lua.Globals["CharacterInfo"] = UserData.CreateStatic<CharacterInfo>();
lua.Globals["Item"] = UserData.CreateStatic<Item>(); lua.Globals["Item"] = UserData.CreateStatic<Item>();
lua.Globals["Level"] = UserData.CreateStatic<Level>(); lua.Globals["Level"] = UserData.CreateStatic<Level>();
lua.Globals["PositionType"] = UserData.CreateStatic<Level.PositionType>(); lua.Globals["PositionType"] = UserData.CreateStatic<Level.PositionType>();
@@ -279,9 +291,10 @@ namespace Barotrauma
lua.Globals["ContentPackage"] = UserData.CreateStatic<ContentPackage>(); lua.Globals["ContentPackage"] = UserData.CreateStatic<ContentPackage>();
lua.Globals["ClientPermissions"] = UserData.CreateStatic<ClientPermissions>(); lua.Globals["ClientPermissions"] = UserData.CreateStatic<ClientPermissions>();
if (File.Exists("Lua/MoonsharpSetup.lua")) // try the default loader if (File.Exists("Lua/MoonsharpSetup.lua")) // try the default loader
DoFile("Lua/MoonsharpSetup.lua"); DoFile("Lua/MoonsharpSetup.lua");
else if(File.Exists("Mods/LuaForBarotrauma/Lua/MoonsharpSetup.lua")) // in case its the workshop version else if (File.Exists("Mods/LuaForBarotrauma/Lua/MoonsharpSetup.lua")) // in case its the workshop version
DoFile("Mods/LuaForBarotrauma/Lua/MoonsharpSetup.lua"); DoFile("Mods/LuaForBarotrauma/Lua/MoonsharpSetup.lua");
else // fallback to c# script loading else // fallback to c# script loading
{ {
@@ -299,6 +312,11 @@ namespace Barotrauma
luaScriptLoader.ModulePaths = modulePaths.ToArray(); luaScriptLoader.ModulePaths = modulePaths.ToArray();
} }
}
public LuaSetup()
{
} }
@@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace Barotrauma
{
partial class LuaSetup
{
//private void NLua_HookException(object sender, NLua.Event.HookExceptionEventArgs e)
//{
// HandleLuaException(e.Exception);
//}
}
}
@@ -197,7 +197,7 @@ namespace Barotrauma.Networking
#endif #endif
} }
GameMain.Lua = new LuaSetup(); GameMain.Lua.Initialize();
TickRate = serverSettings.TickRate; TickRate = serverSettings.TickRate;
@@ -3844,7 +3844,7 @@ namespace Barotrauma.Networking
{ {
if (GameMain.Server == null || !GameMain.Server.ServerSettings.SaveServerLogs) { return; } if (GameMain.Server == null || !GameMain.Server.ServerSettings.SaveServerLogs) { return; }
if(GameMain.Lua != null) if(GameMain.Lua.hook != null)
GameMain.Lua.hook.Call("serverLog", new object[] { line, messageType }); GameMain.Lua.hook.Call("serverLog", new object[] { line, messageType });
GameMain.Server.ServerSettings.ServerLog.WriteLine(line, messageType); GameMain.Server.ServerSettings.ServerLog.WriteLine(line, messageType);