make things less shit

This commit is contained in:
Evil Factory
2021-02-28 16:27:08 -03:00
parent ec74cf912b
commit 2ddffa3308
3 changed files with 399 additions and 358 deletions

View File

@@ -0,0 +1,308 @@
using System;
using System.Collections.Generic;
using System.Text;
using MoonSharp.Interpreter;
using Microsoft.Xna.Framework;
using Barotrauma.Networking;
using System.Threading.Tasks;
namespace Barotrauma
{
partial class LuaSetup
{
private class LuaPlayer
{
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 SetCharacterTeam(Character character, int team)
{
character.TeamID = (CharacterTeamType)team;
}
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();
}
}
public class LuaGame
{
LuaSetup env;
public bool overrideTraitors = false;
public bool overrideRespawnSub = false;
public LuaGame(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)
{
overrideTraitors = o;
}
public void OverrideRespawnSub(bool o)
{
overrideRespawnSub = 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 Character Spawn(string name, Vector2 worldPos)
{
Character spawnedCharacter = null;
Vector2 spawnPosition = worldPos;
string characterLowerCase = name.ToLowerInvariant();
JobPrefab job = null;
if (!JobPrefab.Prefabs.ContainsKey(characterLowerCase))
{
job = JobPrefab.Prefabs.Find(jp => jp.Name != null && jp.Name.Equals(characterLowerCase, StringComparison.OrdinalIgnoreCase));
}
else
{
job = JobPrefab.Prefabs[characterLowerCase];
}
bool human = job != null || characterLowerCase == CharacterPrefab.HumanSpeciesName;
if (string.IsNullOrWhiteSpace(name)) { return null; }
if (human)
{
var variant = job != null ? Rand.Range(0, job.Variants, Rand.RandSync.Server) : 0;
CharacterInfo characterInfo = new CharacterInfo(CharacterPrefab.HumanSpeciesName, jobPrefab: job, variant: variant);
spawnedCharacter = Character.Create(characterInfo, spawnPosition, ToolBox.RandomSeed(8));
if (GameMain.GameSession != null)
{
//TODO: a way to select which team to spawn to?
spawnedCharacter.TeamID = Character.Controlled != null ? Character.Controlled.TeamID : CharacterTeamType.Team1;
#if CLIENT
GameMain.GameSession.CrewManager.AddCharacter(spawnedCharacter);
#endif
}
spawnedCharacter.GiveJobItems(null);
spawnedCharacter.Info.StartItemsGiven = true;
}
else
{
if (CharacterPrefab.FindBySpeciesName(name) != null)
{
Character.Create(name, spawnPosition, ToolBox.RandomSeed(8));
}
}
return spawnedCharacter;
}
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;
}
public static Submarine GetRespawnSub()
{
return GameMain.Server.RespawnManager.RespawnShuttle;
}
public static void DispatchRespawnSub()
{
GameMain.Server.RespawnManager.DispatchShuttle();
}
}
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 LuaHook
{
public Script env;
public LuaHook(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;
}
}
}
}

View File

@@ -7,79 +7,90 @@ using MoonSharp.Interpreter.Loaders;
namespace Barotrauma
{
public class LuaScriptLoader : ScriptLoaderBase
{
public override object LoadFile(string file, Table globalContext)
{
return File.ReadAllText(file);
}
partial class LuaSetup {
public override bool ScriptFileExists(string file)
public class LuaScriptLoader : ScriptLoaderBase
{
return File.Exists(file);
}
public LuaSetup lua;
public void RunFolder(string folder, Script script)
{
foreach(var str in DirSearch(folder))
public LuaScriptLoader(LuaSetup l)
{
var s = str.Replace("\\", "/");
if (s.EndsWith(".lua"))
{
Console.WriteLine(s);
try
{
script.DoFile(s); // i hate windows
}
catch (Exception e)
{
if (e is InterpreterException)
{
Console.WriteLine(((InterpreterException)e).DecoratedMessage);
}
else
{
Console.WriteLine(e.ToString());
}
}
}
lua = l;
}
}
static string[] DirSearch(string sDir)
{
List<string> files = new List<string>();
try
public override object LoadFile(string file, Table globalContext)
{
foreach (string f in Directory.GetFiles(sDir))
{
files.Add(f);
}
return File.ReadAllText(file);
}
foreach (string d in Directory.GetDirectories(sDir))
public override bool ScriptFileExists(string file)
{
return File.Exists(file);
}
public void RunFolder(string folder)
{
foreach (var str in DirSearch(folder))
{
foreach (string f in Directory.GetFiles(d))
var s = str.Replace("\\", "/");
if (s.EndsWith(".lua"))
{
Console.WriteLine(s);
try
{
lua.DoFile(s);
}
catch (Exception e)
{
if (e is InterpreterException)
{
Console.WriteLine(((InterpreterException)e).DecoratedMessage);
}
else
{
Console.WriteLine(e.ToString());
}
}
}
}
}
static string[] DirSearch(string sDir)
{
List<string> files = new List<string>();
try
{
foreach (string f in Directory.GetFiles(sDir))
{
files.Add(f);
}
DirSearch(d);
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);
catch (System.Exception excpt)
{
Console.WriteLine(excpt.Message);
}
return files.ToArray();
}
return files.ToArray();
}
}
}
}

View File

@@ -10,12 +10,12 @@ using MoonSharp.VsCodeDebugger;
namespace Barotrauma
{
class LuaSetup
partial class LuaSetup
{
public Script lua;
public Hook hook;
public Game game;
public LuaHook hook;
public LuaGame game;
public void DoString(string code)
{
@@ -27,7 +27,7 @@ namespace Barotrauma
{
if (e is InterpreterException)
{
Console.WriteLine(((InterpreterException)e).DecoratedMessage);
}
else
@@ -58,319 +58,42 @@ namespace Barotrauma
}
}
private class Player
public void DoFile(string file)
{
public static List<DynValue> GetAllCharacters()
try
{
List<DynValue> values = new List<DynValue>();
foreach (Character ch in Character.CharacterList)
lua.DoFile(file);
}
catch (Exception e)
{
if (e is InterpreterException)
{
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 SetCharacterTeam(Character character, int team)
{
character.TeamID = (CharacterTeamType)team;
}
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);
Console.WriteLine(((InterpreterException)e).DecoratedMessage);
}
else
{
GameMain.Server.BanClient(client, reason, range, TimeSpan.FromSeconds(seconds));
Console.WriteLine(e.ToString());
}
}
public static void StartGame()
{
GameMain.Server.StartGame();
}
}
public class Game
{
LuaSetup env;
public bool overrideTraitors = false;
public bool overrideRespawnSub = false;
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)
{
overrideTraitors = o;
}
public void OverrideRespawnSub(bool o)
{
overrideRespawnSub = 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 Character Spawn(string name, Vector2 worldPos)
{
Character spawnedCharacter = null;
Vector2 spawnPosition = worldPos;
string characterLowerCase = name.ToLowerInvariant();
JobPrefab job = null;
if (!JobPrefab.Prefabs.ContainsKey(characterLowerCase))
{
job = JobPrefab.Prefabs.Find(jp => jp.Name != null && jp.Name.Equals(characterLowerCase, StringComparison.OrdinalIgnoreCase));
}
else
{
job = JobPrefab.Prefabs[characterLowerCase];
}
bool human = job != null || characterLowerCase == CharacterPrefab.HumanSpeciesName;
if (string.IsNullOrWhiteSpace(name)) { return null; }
if (human)
{
var variant = job != null ? Rand.Range(0, job.Variants, Rand.RandSync.Server) : 0;
CharacterInfo characterInfo = new CharacterInfo(CharacterPrefab.HumanSpeciesName, jobPrefab: job, variant: variant);
spawnedCharacter = Character.Create(characterInfo, spawnPosition, ToolBox.RandomSeed(8));
if (GameMain.GameSession != null)
{
//TODO: a way to select which team to spawn to?
spawnedCharacter.TeamID = Character.Controlled != null ? Character.Controlled.TeamID : CharacterTeamType.Team1;
#if CLIENT
GameMain.GameSession.CrewManager.AddCharacter(spawnedCharacter);
#endif
}
spawnedCharacter.GiveJobItems(null);
spawnedCharacter.Info.StartItemsGiven = true;
}
else
{
if (CharacterPrefab.FindBySpeciesName(name) != null)
{
Character.Create(name, spawnPosition, ToolBox.RandomSeed(8));
}
}
return spawnedCharacter;
}
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;
}
public static Submarine GetRespawnSub()
{
return GameMain.Server.RespawnManager.RespawnShuttle;
}
public static void DispatchRespawnSub()
{
GameMain.Server.RespawnManager.DispatchShuttle();
}
}
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();
LuaScriptLoader luaScriptLoader = new LuaScriptLoader(this);
LuaCustomConverters.RegisterAll();
// GameMain.Server.RespawnManager.RespawnShuttle.SetPosition();
//UserData.RegisterAssembly();
UserData.RegisterType<Character>();
UserData.RegisterType<Submarine>();
UserData.RegisterType<Client>();
UserData.RegisterType<Player>();
UserData.RegisterType<Hook>();
UserData.RegisterType<Game>();
UserData.RegisterType<LuaPlayer>();
UserData.RegisterType<LuaHook>();
UserData.RegisterType<LuaGame>();
UserData.RegisterType<LuaRandom>();
UserData.RegisterType<LuaTimer>();
UserData.RegisterType<Vector2>();
@@ -381,18 +104,18 @@ namespace Barotrauma
lua.Options.ScriptLoader = luaScriptLoader;
hook = new Hook(lua);
game = new Game(this);
hook = new LuaHook(lua);
game = new LuaGame(this);
lua.Globals["Player"] = new Player();
lua.Globals["Player"] = new LuaPlayer();
lua.Globals["Game"] = game;
lua.Globals["Hook"] = hook;
lua.Globals["Random"] = new LuaRandom();
lua.Globals["Timer"] = new LuaTimer(this);
luaScriptLoader.RunFolder("Lua/autorun", lua);
luaScriptLoader.RunFolder("Lua/autorun");
}
}
@@ -401,4 +124,3 @@ namespace Barotrauma
}