This commit is contained in:
Evil Factory
2021-04-14 01:00:56 -03:00
parent d557b4491d
commit bb0ea74961
8 changed files with 851 additions and 58 deletions
@@ -37,6 +37,11 @@ namespace Barotrauma
return values;
}
public static CharacterInfo CreateCharacterInfo(string speciesName, string name = "", JobPrefab jobPrefab = null, string ragdollFileName = null, int variant = 0, Rand.RandSync randSync = Rand.RandSync.Unsynced)
{
return new CharacterInfo(speciesName, name, jobPrefab, ragdollFileName, variant, randSync);
}
public static void SetClientCharacter(Client client, Character character)
{
GameMain.Server.SetClientCharacter(client, character);
@@ -65,10 +70,11 @@ namespace Barotrauma
}
public static void StartGame()
public static void SetSpectatorPos(Client client, Vector2 pos)
{
GameMain.Server.StartGame();
client.SpectatePos = pos;
}
}
public class LuaGame
@@ -89,9 +95,9 @@ namespace Barotrauma
GameMain.Server.SendChatMessage(msg, messageType, sender, character);
}
public static void SendTraitorMessage(Client client, string msg, TraitorMessageType type)
public static void SendTraitorMessage(Client client, string msg, string missionid, TraitorMessageType type)
{
GameMain.Server.SendTraitorMessage(client, msg, "", type);
GameMain.Server.SendTraitorMessage(client, msg, missionid, type);
}
public static void SendDirectChatMessage(string sendername, string text, Character sender, ChatMessageType messageType = ChatMessageType.Private, Client client = null)
@@ -163,7 +169,7 @@ namespace Barotrauma
{
if (CharacterPrefab.FindBySpeciesName(name) != null)
{
Character.Create(name, spawnPosition, ToolBox.RandomSeed(8));
spawnedCharacter = Character.Create(name, spawnPosition, ToolBox.RandomSeed(8));
}
}
@@ -212,6 +218,12 @@ namespace Barotrauma
{
DebugConsole.ExecuteCommand(command);
}
public static void StartGame()
{
GameMain.Server.StartGame();
}
}
@@ -251,6 +263,17 @@ namespace Barotrauma
{
return random.Next(min, max);
}
public float RangeFloat(float min, float max)
{
double range = (double)max - (double)min;
double sample = random.NextDouble();
double scaled = (sample * range) + min;
float f = (float)scaled;
return f;
}
}
// hooks:
@@ -264,9 +287,9 @@ namespace Barotrauma
public class LuaHook
{
public Script env;
public LuaSetup env;
public LuaHook(Script e)
public LuaHook(LuaSetup e)
{
env = e;
}
@@ -310,7 +333,7 @@ namespace Barotrauma
{
try
{
var result = env.Call(hf.function, args);
var result = env.lua.Call(hf.function, args);
if (result.IsNil() == false)
{
return result;
@@ -318,15 +341,7 @@ namespace Barotrauma
}
catch (Exception e)
{
if (e is InterpreterException)
{
Console.WriteLine(((InterpreterException)e).DecoratedMessage);
}
else
{
Console.WriteLine(e.ToString());
}
env.HandleLuaException(e);
}
}
}
@@ -37,7 +37,7 @@ namespace Barotrauma
if (s.EndsWith(".lua"))
{
Console.WriteLine(s);
lua.PrintMessage(s);
try
{
@@ -45,15 +45,7 @@ namespace Barotrauma
}
catch (Exception e)
{
if (e is InterpreterException)
{
Console.WriteLine(((InterpreterException)e).DecoratedMessage);
}
else
{
Console.WriteLine(e.ToString());
}
lua.HandleLuaException(e);
}
}
@@ -17,6 +17,31 @@ namespace Barotrauma
public LuaHook hook;
public LuaGame game;
public void HandleLuaException(Exception ex)
{
if(ex is InterpreterException)
{
PrintMessage(((InterpreterException)ex).DecoratedMessage);
}
else
{
PrintMessage(ex.ToString());
}
}
public void PrintMessage(object message)
{
Console.WriteLine(message.ToString());
if (GameMain.Server != null)
{
foreach (var c in GameMain.Server.ConnectedClients)
{
GameMain.Server.SendDirectChatMessage(message.ToString(), c, ChatMessageType.Console);
GameServer.Log("[LUA] " + message.ToString(), ServerLog.MessageType.ServerMessage);
}
}
}
public void DoString(string code)
{
try
@@ -25,15 +50,7 @@ namespace Barotrauma
}
catch (Exception e)
{
if (e is InterpreterException)
{
Console.WriteLine(((InterpreterException)e).DecoratedMessage);
}
else
{
Console.WriteLine(e.ToString());
}
HandleLuaException(e);
}
}
@@ -46,15 +63,7 @@ namespace Barotrauma
}
catch (Exception e)
{
if (e is InterpreterException)
{
Console.WriteLine(((InterpreterException)e).DecoratedMessage);
}
else
{
Console.WriteLine(e.ToString());
}
HandleLuaException(e);
}
}
@@ -66,27 +75,24 @@ namespace Barotrauma
}
catch (Exception e)
{
if (e is InterpreterException)
{
Console.WriteLine(((InterpreterException)e).DecoratedMessage);
}
else
{
Console.WriteLine(e.ToString());
}
HandleLuaException(e);
}
}
public LuaSetup()
{
Console.WriteLine("Lua!");
PrintMessage("Lua!");
LuaScriptLoader luaScriptLoader = new LuaScriptLoader(this);
LuaCustomConverters.RegisterAll();
UserData.RegisterType<TraitorMessageType>();
UserData.RegisterType<JobPrefab>();
UserData.RegisterType<CharacterInfo>();
UserData.RegisterType<Rectangle>();
UserData.RegisterType<Point>();
UserData.RegisterType<Level.InterestingPosition>();
UserData.RegisterType<Level.PositionType>();
UserData.RegisterType<Level>();
@@ -107,12 +113,15 @@ namespace Barotrauma
UserData.RegisterType<Vector2>();
UserData.RegisterType<Vector3>();
UserData.RegisterType<Vector4>();
lua = new Script(CoreModules.Preset_SoftSandbox | CoreModules.LoadMethods);
lua.Options.DebugPrint = PrintMessage;
lua.Options.ScriptLoader = luaScriptLoader;
hook = new LuaHook(lua);
hook = new LuaHook(this);
game = new LuaGame(this);
lua.Globals["Player"] = new LuaPlayer();
@@ -132,6 +141,8 @@ namespace Barotrauma
lua.Globals["Vector2"] = UserData.CreateStatic<Vector2>();
lua.Globals["Vector3"] = UserData.CreateStatic<Vector3>();
lua.Globals["PositionType"] = UserData.CreateStatic<Level.PositionType>();
lua.Globals["JobPrefab"] = UserData.CreateStatic<JobPrefab>();
lua.Globals["TraitorMessageType"] = UserData.CreateStatic<TraitorMessageType>();
foreach (string d in Directory.GetDirectories("Lua"))
{
@@ -2363,8 +2363,6 @@ namespace Barotrauma.Networking
Log("Round started.", ServerLog.MessageType.ServerMessage);
GameMain.Lua.hook.Call("roundStart", new MoonSharp.Interpreter.DynValue[] { });
gameStarted = true;
initiatedStartGame = false;
@@ -2374,6 +2372,9 @@ namespace Barotrauma.Networking
roundStartTime = DateTime.Now;
GameMain.Lua.hook.Call("roundStart", new MoonSharp.Interpreter.DynValue[] { });
yield return CoroutineStatus.Success;
}