using System; using System.Collections.Generic; using System.Text; using System.IO; using Barotrauma.Networking; using MoonSharp.Interpreter; using Microsoft.Xna.Framework; namespace Barotrauma { class LuaSetup { public Script lua; public Hook hook; public void DoString(string code) { try { lua.DoString(code); } catch (Exception e) { Console.WriteLine(e.ToString()); } } private class Player { public static List GetAllCharacters() { List values = new List(); foreach (Character ch in Character.CharacterList) { values.Add(UserData.Create(ch)); } return values; } public static List GetAllClients() { List values = new List(); foreach (Client ch in GameMain.Server.ConnectedClients) { values.Add(UserData.Create(ch)); } return values; } public static void SetClientCharacter(Client client, Character character) { GameMain.Server.SetClientCharacter(client, character); } public static void Kick(Client client, string reason="") { GameMain.Server.KickClient(client.Connection, reason); } public static void Ban(Client client, string reason = "", bool range = false, float seconds=-1) { if(seconds == -1) { GameMain.Server.BanClient(client, reason, range, null); } else { GameMain.Server.BanClient(client, reason, range, TimeSpan.FromSeconds(seconds)); } } public static void StartGame() { GameMain.Server.StartGame(); } } private class Game { public static void SendMessage(string msg, int messageType = 0, Client sender = null, Character character = null) { GameMain.Server.SendChatMessage(msg, (ChatMessageType)messageType, sender, character); } public static void Explode(Vector2 pos, float range=100, float force=30, float damage=30, float structureDamage=30, float itemDamage=30, float empStrength=0, float ballastFloraStrength=0) { new Explosion(range, force, damage, structureDamage, itemDamage, empStrength, ballastFloraStrength).Explode(pos, null); } public static string Spawn(string name, Vector2 pos) { string error; DebugConsole.SpawnCharacter(new string[] {name, "cursor"}, pos, out error); return error; } public static string SpawnItem(string name, Vector2 pos, bool inventory = false, Character character=null) { string error; DebugConsole.SpawnItem(new string[] { name, inventory ? "inventory" : "cursor" }, pos, character, out error); return error; } } // hooks: // chatMessage // think // update // clientConnected // clientDisconnected // roundStart // roundEnd public class Hook { public Script env; public Hook(Script e) { env = e; } public class HookFunction { public string name; public string hookName; public DynValue function; public HookFunction(string n, string hn, DynValue func) { name = n; hookName = hn; function = func; } } public List hookFunctions = new List(); public void Add(string name, string hookName, DynValue function) { foreach (HookFunction hf in hookFunctions) { if(hf.hookName == hookName && hf.name == name) { hf.function = function; return; } } hookFunctions.Add(new HookFunction(name, hookName, function)); } public void Call(string name, DynValue[] args) { foreach(HookFunction hf in hookFunctions) { if (hf.name == name) { try { env.Call(hf.function, args); }catch(Exception e) { Console.WriteLine(e.ToString()); } } } } } public LuaSetup() { Console.WriteLine("Lua!"); LuaCustomConverters.RegisterAll(); LuaScriptLoader luaScriptLoader = new LuaScriptLoader(); //UserData.RegisterAssembly(); UserData.RegisterType(); UserData.RegisterType(); UserData.RegisterType(); UserData.RegisterType(); UserData.RegisterType(); UserData.RegisterType(); UserData.RegisterType(); UserData.RegisterType(); lua = new Script(CoreModules.Preset_SoftSandbox); lua.Options.ScriptLoader = luaScriptLoader; hook = new Hook(lua); lua.Globals["Player"] = new Player(); lua.Globals["Game"] = new Game(); lua.Globals["Hook"] = hook; luaScriptLoader.RunFolder("Lua/autorun", lua); } } }