Move Lua compatibility files around and start adding them to LuaScriptManagementService
This commit is contained in:
committed by
Maplewheels
parent
fe982b15b0
commit
1dad2babb7
@@ -1,10 +1,12 @@
|
||||
using System;
|
||||
using Barotrauma.LuaCs.Services;
|
||||
using Barotrauma.LuaCs.Services.Compatibility;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
|
||||
namespace Barotrauma
|
||||
{
|
||||
public class LuaCsTimer
|
||||
public class LuaCsTimer : ILuaCsTimer
|
||||
{
|
||||
public static double Time => Timing.TotalTime;
|
||||
public static double GetTime() => Time;
|
||||
@@ -1,17 +1,26 @@
|
||||
/*
|
||||
using System;
|
||||
using System;
|
||||
using MoonSharp.Interpreter;
|
||||
using Microsoft.Xna.Framework;
|
||||
using FarseerPhysics.Dynamics;
|
||||
using LuaCsCompatPatchFunc = Barotrauma.LuaCsPatch;
|
||||
using Barotrauma.Networking;
|
||||
using System.Collections.Immutable;
|
||||
using Barotrauma.LuaCs.Services;
|
||||
|
||||
namespace Barotrauma
|
||||
{
|
||||
partial class LuaCsSetup
|
||||
public class LuaConverters
|
||||
{
|
||||
private void RegisterLuaConverters()
|
||||
private readonly Script _script;
|
||||
|
||||
public LuaConverters(Script script)
|
||||
{
|
||||
_script = script;
|
||||
}
|
||||
|
||||
private DynValue Call(object function, params object[] arguments) => _script.Call(function, arguments);
|
||||
|
||||
public void RegisterLuaConverters()
|
||||
{
|
||||
RegisterAction<Item>();
|
||||
RegisterAction<Character>();
|
||||
@@ -25,41 +34,40 @@ namespace Barotrauma
|
||||
|
||||
Script.GlobalOptions.CustomConverters.SetScriptToClrCustomConversion(DataType.Function, typeof(LuaCsAction), v => (LuaCsAction)(args =>
|
||||
{
|
||||
if (v.Function.OwnerScript == Lua)
|
||||
if (v.Function.OwnerScript == _script)
|
||||
{
|
||||
CallLuaFunction(v.Function, args);
|
||||
Call(v.Function, args);
|
||||
}
|
||||
}));
|
||||
|
||||
Script.GlobalOptions.CustomConverters.SetScriptToClrCustomConversion(DataType.Function, typeof(LuaCsFunc), v => (LuaCsFunc)(args =>
|
||||
{
|
||||
if (v.Function.OwnerScript == Lua)
|
||||
if (v.Function.OwnerScript == _script)
|
||||
{
|
||||
return CallLuaFunction(v.Function, args);
|
||||
return Call(v.Function, args);
|
||||
}
|
||||
return default;
|
||||
}));
|
||||
|
||||
Script.GlobalOptions.CustomConverters.SetScriptToClrCustomConversion(DataType.Function, typeof(LuaCsCompatPatchFunc), v => (LuaCsCompatPatchFunc)((self, args) =>
|
||||
{
|
||||
if (v.Function.OwnerScript == Lua)
|
||||
if (v.Function.OwnerScript == _script)
|
||||
{
|
||||
return CallLuaFunction(v.Function, self, args);
|
||||
return Call(v.Function, self, args);
|
||||
}
|
||||
return default;
|
||||
}));
|
||||
|
||||
Script.GlobalOptions.CustomConverters.SetScriptToClrCustomConversion(DataType.Function, typeof(LuaCsPatchFunc), v => (LuaCsPatchFunc)((self, args) =>
|
||||
{
|
||||
if (v.Function.OwnerScript == Lua)
|
||||
if (v.Function.OwnerScript == _script)
|
||||
{
|
||||
return CallLuaFunction(v.Function, self, args);
|
||||
return Call(v.Function, self, args);
|
||||
}
|
||||
return default;
|
||||
}));
|
||||
|
||||
|
||||
DynValue Call(object function, params object[] arguments) => CallLuaFunction(function, arguments);
|
||||
void RegisterHandler<T>(Func<Closure, T> converter) => Script.GlobalOptions.CustomConverters.SetScriptToClrCustomConversion(DataType.Function, typeof(T), v => converter(v.Function));
|
||||
|
||||
RegisterHandler(f => (Character.OnDeathHandler)((a1, a2) => Call(f, a1, a2)));
|
||||
@@ -229,7 +237,7 @@ namespace Barotrauma
|
||||
RegisterImmutableArray<FactionPrefab.HireableCharacter>();
|
||||
}
|
||||
|
||||
private void RegisterImmutableArray<T>()
|
||||
private static void RegisterImmutableArray<T>()
|
||||
{
|
||||
Script.GlobalOptions.CustomConverters.SetScriptToClrCustomConversion(DataType.Table, typeof(ImmutableArray<T>), v =>
|
||||
{
|
||||
@@ -237,7 +245,7 @@ namespace Barotrauma
|
||||
});
|
||||
}
|
||||
|
||||
private void RegisterEither<T1, T2>()
|
||||
private static void RegisterEither<T1, T2>()
|
||||
{
|
||||
DynValue convertEitherIntoDynValue(Either<T1, T2> either)
|
||||
{
|
||||
@@ -275,7 +283,7 @@ namespace Barotrauma
|
||||
});
|
||||
}
|
||||
|
||||
private void RegisterOption<T>(DataType dataType)
|
||||
private static void RegisterOption<T>(DataType dataType)
|
||||
{
|
||||
Script.GlobalOptions.CustomConverters.SetClrToScriptCustomConversion(typeof(Option<T>), (Script v, object obj) =>
|
||||
{
|
||||
@@ -306,13 +314,13 @@ namespace Barotrauma
|
||||
Script.GlobalOptions.CustomConverters.SetScriptToClrCustomConversion(DataType.Function, typeof(Action<T>), v =>
|
||||
{
|
||||
var function = v.Function;
|
||||
return (Action<T>)(p => CallLuaFunction(function, p));
|
||||
return (Action<T>)(p => Call(function, p));
|
||||
});
|
||||
|
||||
Script.GlobalOptions.CustomConverters.SetScriptToClrCustomConversion(DataType.ClrFunction, typeof(Action<T>), v =>
|
||||
{
|
||||
var function = v.Function;
|
||||
return (Action<T>)(p => CallLuaFunction(function, p));
|
||||
return (Action<T>)(p => Call(function, p));
|
||||
});
|
||||
}
|
||||
|
||||
@@ -321,13 +329,13 @@ namespace Barotrauma
|
||||
Script.GlobalOptions.CustomConverters.SetScriptToClrCustomConversion(DataType.Function, typeof(Action<T1, T2>), v =>
|
||||
{
|
||||
var function = v.Function;
|
||||
return (Action<T1, T2>)((a1, a2) => CallLuaFunction(function, a1, a2));
|
||||
return (Action<T1, T2>)((a1, a2) => Call(function, a1, a2));
|
||||
});
|
||||
|
||||
Script.GlobalOptions.CustomConverters.SetScriptToClrCustomConversion(DataType.ClrFunction, typeof(Action<T1, T2>), v =>
|
||||
{
|
||||
var function = v.Function;
|
||||
return (Action<T1, T2>)((a1, a2) => CallLuaFunction(function, a1, a2));
|
||||
return (Action<T1, T2>)((a1, a2) => Call(function, a1, a2));
|
||||
});
|
||||
}
|
||||
|
||||
@@ -336,13 +344,13 @@ namespace Barotrauma
|
||||
Script.GlobalOptions.CustomConverters.SetScriptToClrCustomConversion(DataType.Function, typeof(Action), v =>
|
||||
{
|
||||
var function = v.Function;
|
||||
return (Action)(() => CallLuaFunction(function));
|
||||
return (Action)(() => Call(function));
|
||||
});
|
||||
|
||||
Script.GlobalOptions.CustomConverters.SetScriptToClrCustomConversion(DataType.ClrFunction, typeof(Action), v =>
|
||||
{
|
||||
var function = v.Function;
|
||||
return (Action)(() => CallLuaFunction(function));
|
||||
return (Action)(() => Call(function));
|
||||
});
|
||||
}
|
||||
|
||||
@@ -392,4 +400,3 @@ namespace Barotrauma
|
||||
}
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Barotrauma.LuaCs.Services.Compatibility;
|
||||
|
||||
internal partial interface ILuaCsTimer : ILuaCsShim
|
||||
{
|
||||
public static double Time => Timing.TotalTime;
|
||||
public static double GetTime() => Time;
|
||||
public static double AccumulatorMax { get; set; }
|
||||
|
||||
public void Clear();
|
||||
public void Wait(LuaCsAction action, int millisecondDelay);
|
||||
public void NextFrame(LuaCsAction action);
|
||||
}
|
||||
@@ -1,10 +1,13 @@
|
||||
#nullable enable
|
||||
|
||||
using Barotrauma.LuaCs.Data;
|
||||
using Barotrauma.LuaCs.Services.Compatibility;
|
||||
using Barotrauma.LuaCs.Services.Safe;
|
||||
using Barotrauma.Networking;
|
||||
using FluentResults;
|
||||
using Microsoft.CodeAnalysis;
|
||||
using Microsoft.CodeAnalysis.CSharp.Syntax;
|
||||
using MonoMod.RuntimeDetour;
|
||||
using MoonSharp.Interpreter;
|
||||
using MoonSharp.Interpreter.Interop;
|
||||
using MoonSharp.Interpreter.Loaders;
|
||||
@@ -20,25 +23,46 @@ using System.Reflection;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using static Barotrauma.GameSettings;
|
||||
|
||||
namespace Barotrauma.LuaCs.Services;
|
||||
|
||||
public class LuaScriptManagementService : ILuaScriptManagementService, ILuaDataService
|
||||
class LuaScriptManagementService : ILuaScriptManagementService, ILuaDataService
|
||||
{
|
||||
private Script? _script;
|
||||
private bool _isRunning;
|
||||
[MemberNotNullWhen(true, nameof(_script))]
|
||||
public bool IsRunning => _isRunning;
|
||||
private List<ILuaScriptResourceInfo> _resourcesInfo = new List<ILuaScriptResourceInfo>();
|
||||
|
||||
private readonly ILuaScriptLoader _luaScriptLoader;
|
||||
private readonly ILuaScriptServicesConfig _luaScriptServicesConfig;
|
||||
private readonly ILoggerService _loggerService;
|
||||
private readonly LuaGame _luaGame;
|
||||
private readonly ILuaCsHook _luaCsHook;
|
||||
private readonly ILuaCsNetworking _luaCsNetworking;
|
||||
private readonly ILuaCsUtility _luaCsUtility;
|
||||
private readonly ILuaCsTimer _luaCsTimer;
|
||||
|
||||
public LuaScriptManagementService(ILoggerService loggerService, ILuaScriptLoader loader, ILuaScriptServicesConfig luaScriptServicesConfig)
|
||||
public LuaScriptManagementService(
|
||||
ILoggerService loggerService,
|
||||
ILuaScriptLoader loader,
|
||||
ILuaScriptServicesConfig luaScriptServicesConfig,
|
||||
LuaGame luaGame,
|
||||
ILuaCsHook luaCsHook,
|
||||
ILuaCsNetworking luaCsNetworking,
|
||||
ILuaCsUtility luaCsUtility,
|
||||
ILuaCsTimer luaCsTimer)
|
||||
{
|
||||
_luaScriptLoader = loader;
|
||||
_luaScriptServicesConfig = luaScriptServicesConfig;
|
||||
_loggerService = loggerService;
|
||||
|
||||
_luaGame = luaGame;
|
||||
_luaCsHook = luaCsHook;
|
||||
_luaCsNetworking = luaCsNetworking;
|
||||
_luaCsUtility = luaCsUtility;
|
||||
_luaCsTimer = luaCsTimer;
|
||||
}
|
||||
|
||||
public bool IsDisposed { get; private set; }
|
||||
@@ -63,6 +87,33 @@ public class LuaScriptManagementService : ILuaScriptManagementService, ILuaDataS
|
||||
_script.Options.CheckThreadAccess = false;
|
||||
|
||||
Script.GlobalOptions.ShouldPCallCatchException = (Exception ex) => { return true; };
|
||||
|
||||
RegisterType(typeof(LuaGame));
|
||||
RegisterType(typeof(ILuaCsHook));
|
||||
RegisterType(typeof(ILuaCsNetworking));
|
||||
RegisterType(typeof(ILuaCsUtility));
|
||||
RegisterType(typeof(ILuaCsTimer));
|
||||
RegisterType(typeof(LuaCsFile));
|
||||
|
||||
new LuaConverters(_script).RegisterLuaConverters();
|
||||
|
||||
_script.Globals["printerror"] = (DynValue o) => { LuaCsLogger.LogError(o.ToString()); };
|
||||
|
||||
_script.Globals["dostring"] = (Func<string, Table, string, DynValue>)_script.DoString;
|
||||
_script.Globals["load"] = (Func<string, Table, string, DynValue>)_script.LoadString;
|
||||
|
||||
_script.Globals["Game"] = _luaGame;
|
||||
_script.Globals["Hook"] = _luaCsHook;
|
||||
_script.Globals["Timer"] = _luaCsTimer;
|
||||
_script.Globals["File"] = UserData.CreateStatic<LuaCsFile>();
|
||||
_script.Globals["Networking"] = _luaCsNetworking;
|
||||
//_script.Globals["Steam"] = Steam;
|
||||
|
||||
_script.Globals["ExecutionNumber"] = 0;
|
||||
_script.Globals["CSActive"] = false;
|
||||
|
||||
_script.Globals["SERVER"] = LuaCsSetup.IsServer;
|
||||
_script.Globals["CLIENT"] = LuaCsSetup.IsClient;
|
||||
}
|
||||
|
||||
public FluentResults.Result ExecuteLoadedScripts()
|
||||
|
||||
Reference in New Issue
Block a user