my attempt
This commit is contained in:
@@ -0,0 +1,59 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using MoonSharp.Interpreter;
|
||||
using Microsoft.Xna.Framework;
|
||||
|
||||
namespace Barotrauma
|
||||
{
|
||||
|
||||
public static class LuaCustomConverters
|
||||
{
|
||||
|
||||
public static void RegisterAll()
|
||||
{
|
||||
|
||||
// Vector 2
|
||||
|
||||
Script.GlobalOptions.CustomConverters.SetScriptToClrCustomConversion(DataType.Table, typeof(Vector2),
|
||||
dynVal => {
|
||||
Table table = dynVal.Table;
|
||||
float x = (float)((Double)table[1]);
|
||||
float y = (float)((Double)table[2]);
|
||||
return new Vector2(x, y);
|
||||
}
|
||||
);
|
||||
Script.GlobalOptions.CustomConverters.SetClrToScriptCustomConversion<Vector2>(
|
||||
(script, vector) => {
|
||||
DynValue x = DynValue.NewNumber((double)vector.X);
|
||||
DynValue y = DynValue.NewNumber((double)vector.Y);
|
||||
DynValue dynVal = DynValue.NewTable(script, new DynValue[] { x, y });
|
||||
return dynVal;
|
||||
}
|
||||
);
|
||||
|
||||
// Vector3
|
||||
|
||||
Script.GlobalOptions.CustomConverters.SetScriptToClrCustomConversion(DataType.Table, typeof(Vector3),
|
||||
dynVal => {
|
||||
Table table = dynVal.Table;
|
||||
float x = (float)((Double)table[1]);
|
||||
float y = (float)((Double)table[2]);
|
||||
float z = (float)((Double)table[3]);
|
||||
return new Vector3(x, y, z);
|
||||
}
|
||||
);
|
||||
Script.GlobalOptions.CustomConverters.SetClrToScriptCustomConversion<Vector3>(
|
||||
(script, vector) => {
|
||||
DynValue x = DynValue.NewNumber((double)vector.X);
|
||||
DynValue y = DynValue.NewNumber((double)vector.Y);
|
||||
DynValue z = DynValue.NewNumber((double)vector.Z);
|
||||
DynValue dynVal = DynValue.NewTable(script, new DynValue[] { x, y, z });
|
||||
return dynVal;
|
||||
}
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.IO;
|
||||
using MoonSharp.Interpreter;
|
||||
using MoonSharp.Interpreter.Loaders;
|
||||
|
||||
namespace Barotrauma
|
||||
{
|
||||
public class LuaScriptLoader : ScriptLoaderBase
|
||||
{
|
||||
public override object LoadFile(string file, Table globalContext)
|
||||
{
|
||||
return File.ReadAllText(file);
|
||||
}
|
||||
|
||||
public override bool ScriptFileExists(string file)
|
||||
{
|
||||
return File.Exists(file);
|
||||
}
|
||||
|
||||
public void RunFolder(string folder, Script script)
|
||||
{
|
||||
foreach(var str in DirSearch(folder))
|
||||
{
|
||||
script.DoFile(str.Replace("\\", "/")); // i hate windows
|
||||
}
|
||||
}
|
||||
|
||||
static string[] DirSearch(string sDir)
|
||||
{
|
||||
List<string> files = new List<string>();
|
||||
|
||||
try
|
||||
{
|
||||
foreach (string f in Directory.GetFiles(sDir))
|
||||
{
|
||||
files.Add(f);
|
||||
}
|
||||
|
||||
foreach (string d in Directory.GetDirectories(sDir))
|
||||
{
|
||||
foreach (string f in Directory.GetFiles(d))
|
||||
{
|
||||
files.Add(f);
|
||||
}
|
||||
DirSearch(d);
|
||||
}
|
||||
}
|
||||
catch (System.Exception excpt)
|
||||
{
|
||||
Console.WriteLine(excpt.Message);
|
||||
}
|
||||
|
||||
return files.ToArray();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,222 @@
|
||||
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<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
|
||||
{
|
||||
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<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 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<Character>();
|
||||
UserData.RegisterType<Client>();
|
||||
UserData.RegisterType<Player>();
|
||||
UserData.RegisterType<Hook>();
|
||||
UserData.RegisterType<Game>();
|
||||
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();
|
||||
lua.Globals["Hook"] = hook;
|
||||
|
||||
|
||||
luaScriptLoader.RunFolder("Lua/autorun", lua);
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user