336 lines
6.6 KiB
C#
336 lines
6.6 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Text;
|
|
using System.IO;
|
|
using Barotrauma.Networking;
|
|
using MoonSharp.Interpreter;
|
|
using Microsoft.Xna.Framework;
|
|
using System.Threading.Tasks;
|
|
using MoonSharp.VsCodeDebugger;
|
|
|
|
namespace Barotrauma
|
|
{
|
|
class LuaSetup
|
|
{
|
|
|
|
public Script lua;
|
|
public Hook hook;
|
|
|
|
public bool overrideTraitors = false;
|
|
|
|
public void DoString(string code)
|
|
{
|
|
try
|
|
{
|
|
lua.DoString(code);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
if (e is InterpreterException)
|
|
{
|
|
|
|
Console.WriteLine(((InterpreterException)e).DecoratedMessage);
|
|
}
|
|
else
|
|
{
|
|
Console.WriteLine(e.ToString());
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
public void RunFunction(DynValue func)
|
|
{
|
|
try
|
|
{
|
|
lua.Call(func);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
if (e is InterpreterException)
|
|
{
|
|
|
|
Console.WriteLine(((InterpreterException)e).DecoratedMessage);
|
|
}
|
|
else
|
|
{
|
|
Console.WriteLine(e.ToString());
|
|
}
|
|
}
|
|
}
|
|
|
|
private class Player
|
|
{
|
|
|
|
public static List<DynValue> GetAllCharacters()
|
|
{
|
|
List<DynValue> values = new List<DynValue>();
|
|
|
|
foreach (Character ch in Character.CharacterList)
|
|
{
|
|
values.Add(UserData.Create(ch));
|
|
}
|
|
|
|
return values;
|
|
}
|
|
|
|
public static List<DynValue> GetAllClients()
|
|
{
|
|
List<DynValue> values = new List<DynValue>();
|
|
|
|
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
|
|
{
|
|
LuaSetup env;
|
|
|
|
public Game(LuaSetup e)
|
|
{
|
|
env = e;
|
|
}
|
|
|
|
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 SendTraitorMessage(Client client, string msg, int type)
|
|
{
|
|
GameMain.Server.SendTraitorMessage(client, msg, "", (TraitorMessageType)type);
|
|
}
|
|
|
|
public static void SendDirectChatMessage(string sendername, string text, Character sender, int messageType = 0, Client client = null)
|
|
{
|
|
|
|
ChatMessage cm = ChatMessage.Create(sendername, text, (ChatMessageType)messageType, sender, client);
|
|
|
|
GameMain.Server.SendDirectChatMessage(cm, client);
|
|
|
|
}
|
|
|
|
public void OverrideTraitors(bool o)
|
|
{
|
|
env.overrideTraitors = o;
|
|
}
|
|
|
|
public static void Log(string message, int type)
|
|
{
|
|
GameServer.Log(message, (ServerLog.MessageType)type);
|
|
}
|
|
|
|
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;
|
|
}
|
|
}
|
|
|
|
|
|
private class LuaTimer
|
|
{
|
|
public LuaSetup env;
|
|
|
|
public LuaTimer(LuaSetup e)
|
|
{
|
|
env = e;
|
|
}
|
|
|
|
public void Simple(int time, DynValue function)
|
|
{
|
|
|
|
Task.Delay(time).ContinueWith(o => { env.RunFunction(function); });
|
|
}
|
|
|
|
public static double GetTime()
|
|
{
|
|
return Timing.TotalTime;
|
|
}
|
|
|
|
|
|
}
|
|
|
|
private class LuaRandom
|
|
{
|
|
Random random;
|
|
|
|
public LuaRandom()
|
|
{
|
|
random = new Random();
|
|
}
|
|
|
|
public int Range(int min, int max)
|
|
{
|
|
return random.Next(min, max);
|
|
}
|
|
}
|
|
|
|
// 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<HookFunction> hookFunctions = new List<HookFunction>();
|
|
|
|
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 DynValue Call(string name, DynValue[] args)
|
|
{
|
|
foreach(HookFunction hf in hookFunctions)
|
|
{
|
|
if (hf.name == name)
|
|
{
|
|
try
|
|
{
|
|
return env.Call(hf.function, args);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
if (e is InterpreterException)
|
|
{
|
|
|
|
Console.WriteLine(((InterpreterException)e).DecoratedMessage);
|
|
}
|
|
else
|
|
{
|
|
Console.WriteLine(e.ToString());
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return null;
|
|
}
|
|
}
|
|
|
|
public LuaSetup()
|
|
{
|
|
Console.WriteLine("Lua!");
|
|
|
|
|
|
LuaScriptLoader luaScriptLoader = new LuaScriptLoader();
|
|
|
|
//UserData.RegisterAssembly();
|
|
UserData.RegisterType<Character>();
|
|
UserData.RegisterType<Client>();
|
|
UserData.RegisterType<Player>();
|
|
UserData.RegisterType<Hook>();
|
|
UserData.RegisterType<Game>();
|
|
UserData.RegisterType<LuaRandom>();
|
|
UserData.RegisterType<LuaTimer>();
|
|
UserData.RegisterType<Vector2>();
|
|
UserData.RegisterType<Vector3>();
|
|
UserData.RegisterType<Vector4>();
|
|
|
|
lua = new Script(CoreModules.Preset_SoftSandbox);
|
|
|
|
lua.Options.ScriptLoader = luaScriptLoader;
|
|
|
|
hook = new Hook(lua);
|
|
|
|
lua.Globals["Player"] = new Player();
|
|
lua.Globals["Game"] = new Game(this);
|
|
lua.Globals["Hook"] = hook;
|
|
lua.Globals["Random"] = new LuaRandom();
|
|
lua.Globals["Timer"] = new LuaTimer(this);
|
|
|
|
luaScriptLoader.RunFolder("Lua/autorun", lua);
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|