hook merge + hook wrappers

This commit is contained in:
Oiltanker
2022-04-13 01:34:38 +03:00
parent 1e6ac68e86
commit 5d06df437e
40 changed files with 539 additions and 284 deletions
@@ -31,7 +31,7 @@ namespace Barotrauma
public void Wait(object function, int millisecondDelay)
{
GameMain.LuaCs.hook.EnqueueTimedFunction((float)Timing.TotalTime + (millisecondDelay / 1000f), function);
GameMain.LuaCs.HookBase.EnqueueTimedLuaFunction((float)Timing.TotalTime + (millisecondDelay / 1000f), function);
}
public static double GetTime()
@@ -122,14 +122,14 @@ namespace Barotrauma
if (CanWriteToPath(path))
return true;
else
GameMain.LuaCs.HandleLuaException(new Exception("File access to \"" + path + "\" not allowed."));
GameMain.LuaCs.HandleException(new Exception("File access to \"" + path + "\" not allowed."));
}
else
{
if (CanReadFromPath(path))
return true;
else
GameMain.LuaCs.HandleLuaException(new Exception("File access to \"" + path + "\" not allowed."));
GameMain.LuaCs.HandleException(new Exception("File access to \"" + path + "\" not allowed."));
}
return false;
@@ -239,11 +239,11 @@ namespace Barotrauma
{
string netMessageName = netMessage.ReadString();
if (LuaNetReceives[netMessageName] is Closure)
GameMain.LuaCs.CallFunction(LuaNetReceives[netMessageName], new object[] { netMessage, client });
GameMain.LuaCs.CallLuaFunction(LuaNetReceives[netMessageName], new object[] { netMessage, client });
}
else
{
GameMain.LuaCs.hook.Call("netMessageReceived", netMessage, header, client);
GameMain.LuaCs.HookBase.Call("netMessageReceived", netMessage, header, client);
}
}
@@ -259,7 +259,7 @@ namespace Barotrauma
}
else
{
GameMain.LuaCs.hook.Call("netMessageReceived", netMessage, header, client);
GameMain.LuaCs.HookBase.Call("netMessageReceived", netMessage, header, client);
}
}
#endif
@@ -327,18 +327,18 @@ namespace Barotrauma
{
var httpResponse = httpWebRequest.EndGetResponse(result);
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
GameMain.LuaCs.hook.EnqueueFunction(callback, streamReader.ReadToEnd());
GameMain.LuaCs.HookBase.EnqueueLuaFunction(callback, streamReader.ReadToEnd());
}
catch (Exception e)
{
GameMain.LuaCs.hook.EnqueueFunction(callback, e.ToString());
GameMain.LuaCs.HookBase.EnqueueLuaFunction(callback, e.ToString());
}
}), null);
}
catch (Exception e)
{
GameMain.LuaCs.hook.EnqueueFunction(callback, e.ToString());
GameMain.LuaCs.HookBase.EnqueueLuaFunction(callback, e.ToString());
}
}
@@ -354,17 +354,17 @@ namespace Barotrauma
{
var httpResponse = httpWebRequest.EndGetResponse(result);
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
GameMain.LuaCs.hook.EnqueueFunction(callback, streamReader.ReadToEnd());
GameMain.LuaCs.HookBase.EnqueueLuaFunction(callback, streamReader.ReadToEnd());
}
catch (Exception e)
{
GameMain.LuaCs.hook.EnqueueFunction(callback, e.ToString());
GameMain.LuaCs.HookBase.EnqueueLuaFunction(callback, e.ToString());
}
}), null);
}
catch (Exception e)
{
GameMain.LuaCs.hook.EnqueueFunction(callback, e.ToString());
GameMain.LuaCs.HookBase.EnqueueLuaFunction(callback, e.ToString());
}
}
@@ -326,11 +326,11 @@ namespace Barotrauma
public void AddCommand(string name, string help, object onExecute, object getValidArgs = null, bool isCheat = false)
{
var cmd = new DebugConsole.Command(name, help, (string[] arg1) => { GameMain.LuaCs.CallFunction(onExecute, new object[] { arg1 }); },
var cmd = new DebugConsole.Command(name, help, (string[] arg1) => { GameMain.LuaCs.CallLuaFunction(onExecute, new object[] { arg1 }); },
() =>
{
if (getValidArgs == null) return null;
var result = new LuaResult(GameMain.LuaCs.CallFunction(getValidArgs, new object[] { }));
var result = new LuaResult(GameMain.LuaCs.CallLuaFunction(getValidArgs, new object[] { }));
var obj = result.Object();
if (obj is string[][]) return (string[][])obj;
return null;
@@ -357,7 +357,7 @@ namespace Barotrauma
public List<DebugConsole.Command> Commands => DebugConsole.Commands;
public void AssignOnExecute(string names, object onExecute) => DebugConsole.AssignOnExecute(names, (string[] a) => { GameMain.LuaCs.CallFunction(onExecute, new object[] { a }); });
public void AssignOnExecute(string names, object onExecute) => DebugConsole.AssignOnExecute(names, (string[] a) => { GameMain.LuaCs.CallLuaFunction(onExecute, new object[] { a }); });
#if SERVER
@@ -412,7 +412,7 @@ namespace Barotrauma
GameMain.Server.EndGame();
}
public void AssignOnClientRequestExecute(string names, object onExecute) => DebugConsole.AssignOnClientRequestExecute(names, (Client a, Vector2 b, string[] c) => { GameMain.LuaCs.CallFunction(onExecute, new object[] { a, b, c }); });
public void AssignOnClientRequestExecute(string names, object onExecute) => DebugConsole.AssignOnClientRequestExecute(names, (Client a, Vector2 b, string[] c) => { GameMain.LuaCs.CallLuaFunction(onExecute, new object[] { a, b, c }); });
#endif
@@ -0,0 +1,44 @@
using System;
namespace Barotrauma
{
partial class LuaCsSetup
{
// CSharp wrapper for LuaCsHook
public class LuaHook : LuaCsHookWrapper
{
public LuaHook(LuaCsHook hook) : base(hook) { }
public class HookMethodTypeProxy
{
public HookMethodTypeProxy() { }
public const HookMethodType Before = Barotrauma.HookMethodType.Before;
public const HookMethodType After = Barotrauma.HookMethodType.After;
}
public static readonly HookMethodTypeProxy HookMethodType = new HookMethodTypeProxy();
public void HookMethod(string identifier, string className, string methodName, string[] parameterNames, object hookMethod, HookMethodType hookMethodType = Barotrauma.HookMethodType.Before) =>
_hook.HookLuaMethod(identifier, className, methodName, parameterNames, hookMethod, hookMethodType);
public void HookMethod(string identifier, string className, string methodName, object hookMethod, HookMethodType hookMethodType = Barotrauma.HookMethodType.Before) =>
_hook.HookLuaMethod(identifier, className, methodName, null, hookMethod, hookMethodType);
public void HookMethod(string className, string methodName, object hookMethod, HookMethodType hookMethodType = Barotrauma.HookMethodType.Before) =>
_hook.HookLuaMethod("", className, methodName, null, hookMethod, hookMethodType);
public void HookMethod(string className, string methodName, string[] parameterNames, object hookMethod, HookMethodType hookMethodType = Barotrauma.HookMethodType.Before) =>
_hook.HookLuaMethod("", className, methodName, parameterNames, hookMethod, hookMethodType);
public void Add(string name, string hookName, object function) =>
_hook.AddLuaHook(name, hookName, function);
public void EnqueueFunction(object function, params object[] args) =>
_hook.EnqueueLuaFunction(function, args);
public void EnqueueTimedFunction(float time, object function, params object[] args) =>
_hook.EnqueueTimedLuaFunction(time, function, args);
}
}
}
@@ -28,7 +28,7 @@ namespace Barotrauma
if (type == null)
{
GameMain.LuaCs.HandleLuaException(new Exception($"Tried to register a type that doesn't exist: {typeName}."));
GameMain.LuaCs.HandleException(new Exception($"Tried to register a type that doesn't exist: {typeName}."));
return null;
}
@@ -41,7 +41,7 @@ namespace Barotrauma
if (type == null)
{
GameMain.LuaCs.HandleLuaException(new Exception($"Tried to unregister a type that doesn't exist: {typeName}."));
GameMain.LuaCs.HandleException(new Exception($"Tried to unregister a type that doesn't exist: {typeName}."));
return;
}
@@ -79,7 +79,7 @@ namespace Barotrauma
if (type == null)
{
GameMain.LuaCs.HandleLuaException(new Exception($"Tried to create a static userdata of a type that doesn't exist: {typeName}."));
GameMain.LuaCs.HandleException(new Exception($"Tried to create a static userdata of a type that doesn't exist: {typeName}."));
return null;
}
@@ -94,7 +94,7 @@ namespace Barotrauma
if (type == null)
{
GameMain.LuaCs.HandleLuaException(new Exception($"Tried to create an enum table with a type that doesn't exist:: {typeName}."));
GameMain.LuaCs.HandleException(new Exception($"Tried to create an enum table with a type that doesn't exist:: {typeName}."));
return null;
}
@@ -126,7 +126,7 @@ namespace Barotrauma
{
if (IUUD == null)
{
GameMain.LuaCs.HandleLuaException(new Exception($"Tried to use a UserDataDescriptor that is null to make {fieldName} accessible."));
GameMain.LuaCs.HandleException(new Exception($"Tried to use a UserDataDescriptor that is null to make {fieldName} accessible."));
return;
}
@@ -140,7 +140,7 @@ namespace Barotrauma
if (field == null)
{
GameMain.LuaCs.HandleLuaException(new Exception($"Tried to make field '{fieldName}' accessible, but the field doesn't exist."));
GameMain.LuaCs.HandleException(new Exception($"Tried to make field '{fieldName}' accessible, but the field doesn't exist."));
return;
}
@@ -164,7 +164,7 @@ namespace Barotrauma
{
if (IUUD == null)
{
GameMain.LuaCs.HandleLuaException(new Exception($"Tried to use a UserDataDescriptor that is null to make {methodName} accessible."));
GameMain.LuaCs.HandleException(new Exception($"Tried to use a UserDataDescriptor that is null to make {methodName} accessible."));
return;
}
@@ -178,7 +178,7 @@ namespace Barotrauma
if (method == null)
{
GameMain.LuaCs.HandleLuaException(new Exception($"Tried to make method '{methodName}' accessible, but the method doesn't exist."));
GameMain.LuaCs.HandleException(new Exception($"Tried to make method '{methodName}' accessible, but the method doesn't exist."));
return;
}
@@ -190,7 +190,7 @@ namespace Barotrauma
{
if (IUUD == null)
{
GameMain.LuaCs.HandleLuaException(new Exception($"Tried to use a UserDataDescriptor that is null to add method {methodName}."));
GameMain.LuaCs.HandleException(new Exception($"Tried to use a UserDataDescriptor that is null to add method {methodName}."));
return;
}
@@ -199,7 +199,7 @@ namespace Barotrauma
descriptor.AddMember(methodName, new ObjectCallbackMemberDescriptor(methodName, (object arg1, ScriptExecutionContext arg2, CallbackArguments arg3) =>
{
if (GameMain.LuaCs != null)
return GameMain.LuaCs.CallFunction(function, arg3.GetArray());
return GameMain.LuaCs.CallLuaFunction(function, arg3.GetArray());
return null;
}));
}
@@ -208,7 +208,7 @@ namespace Barotrauma
{
if (IUUD == null)
{
GameMain.LuaCs.HandleLuaException(new Exception($"Tried to use a UserDataDescriptor that is null to remove the member {memberName}."));
GameMain.LuaCs.HandleException(new Exception($"Tried to use a UserDataDescriptor that is null to remove the member {memberName}."));
return;
}
@@ -29,7 +29,7 @@ namespace Barotrauma
RegisterAction<Microsoft.Xna.Framework.Graphics.SpriteBatch, float>();
{
object Call(object function, params object[] arguments) => GameMain.LuaCs.CallFunction(function, arguments);
object Call(object function, params object[] arguments) => GameMain.LuaCs.CallLuaFunction(function, arguments);
void RegisterHandler<T>(Func<Closure, object> converter)
{
Script.GlobalOptions.CustomConverters.SetScriptToClrCustomConversion(DataType.Function, typeof(T), v => converter(v.Function));
@@ -122,7 +122,7 @@ namespace Barotrauma
Script.GlobalOptions.CustomConverters.SetScriptToClrCustomConversion(DataType.Function, typeof(Action<T>), v =>
{
var function = v.Function;
return (Action<T>)(p => GameMain.LuaCs.CallFunction(function, p));
return (Action<T>)(p => GameMain.LuaCs.CallLuaFunction(function, p));
});
}
@@ -131,7 +131,7 @@ namespace Barotrauma
Script.GlobalOptions.CustomConverters.SetScriptToClrCustomConversion(DataType.Function, typeof(Action<T1, T2>), v =>
{
var function = v.Function;
return (Action<T1, T2>)((a1, a2) => GameMain.LuaCs.CallFunction(function, a1, a2));
return (Action<T1, T2>)((a1, a2) => GameMain.LuaCs.CallLuaFunction(function, a1, a2));
});
}
@@ -140,7 +140,7 @@ namespace Barotrauma
Script.GlobalOptions.CustomConverters.SetScriptToClrCustomConversion(DataType.Function, typeof(Action), v =>
{
var function = v.Function;
return (Action)(() => GameMain.LuaCs.CallFunction(function));
return (Action)(() => GameMain.LuaCs.CallLuaFunction(function));
});
}
@@ -8,24 +8,21 @@ using System.Linq;
namespace Barotrauma
{
partial class LuaCsSetup {
class LuaScriptLoader : ScriptLoaderBase
{
public class LuaScriptLoader : ScriptLoaderBase
public override object LoadFile(string file, Table globalContext)
{
public override object LoadFile(string file, Table globalContext)
{
if (!LuaFile.IsPathAllowedLuaException(file, false)) return null;
if (!LuaFile.IsPathAllowedLuaException(file, false)) return null;
return File.ReadAllText(file);
}
return File.ReadAllText(file);
}
public override bool ScriptFileExists(string file)
{
if (!LuaFile.IsPathAllowedLuaException(file, false)) return false;
public override bool ScriptFileExists(string file)
{
if (!LuaFile.IsPathAllowedLuaException(file, false)) return false;
return File.Exists(file);
}
}
}
return File.Exists(file);
}
}
}