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

View File

@@ -923,7 +923,7 @@ namespace Barotrauma
SoundManager?.Update();
GameMain.LuaCs.Update();
GameMain.LuaCs.hook.Call("think");
GameMain.LuaCs.HookBase.Call("think");
Timing.Accumulator -= Timing.Step;

View File

@@ -2830,7 +2830,7 @@ namespace Barotrauma.Networking
public override void AddChatMessage(ChatMessage message)
{
var should = new LuaResult(GameMain.LuaCs.hook.Call("chatMessage", message.Text, message.SenderClient, message.Type, message));
var should = new LuaResult(GameMain.LuaCs.HookBase.Call("chatMessage", message.Text, message.SenderClient, message.Type, message));
if (should.Bool()) return;

View File

@@ -562,7 +562,7 @@ namespace Barotrauma
allowInput = true;
}
GameMain.LuaCs.hook.Call("keyUpdate", deltaTime);
GameMain.LuaCs.HookBase.Call("keyUpdate", deltaTime);
oldMouseState = mouseState;
mouseState = latestMouseState;

View File

@@ -25,7 +25,7 @@ namespace Barotrauma
GameServer.Log(GameServer.CharacterLogName(this) + " has died (Cause of death: " + causeOfDeath + ")", ServerLog.MessageType.Attack);
}
}
GameMain.LuaCs.hook.Call("characterDeath", this,causeOfDeathAffliction);
GameMain.LuaCs.HookBase.Call("characterDeath", this,causeOfDeathAffliction);
if (HasAbilityFlag(AbilityFlags.RetainExperienceForNewCharacter))
{

View File

@@ -1293,7 +1293,7 @@ namespace Barotrauma
}
catch (Exception e)
{
GameMain.LuaCs.HandleLuaException(e);
GameMain.LuaCs.HandleException(e);
return;
}

View File

@@ -348,7 +348,7 @@ namespace Barotrauma
CoroutineManager.Update((float)Timing.Step, (float)Timing.Step);
GameMain.LuaCs.Update();
GameMain.LuaCs.hook.Call("think", new object[] { });
GameMain.LuaCs.HookBase.Call("think", new object[] { });
performanceCounterTimer.Stop();
LuaTimer.LastUpdateTime = performanceCounterTimer.ElapsedMilliseconds;
performanceCounterTimer.Reset();

View File

@@ -133,7 +133,7 @@ namespace Barotrauma.Networking
return;
}
var should = new LuaResult(GameMain.LuaCs.hook.Call("chatMessage", txt, c, type));
var should = new LuaResult(GameMain.LuaCs.HookBase.Call("chatMessage", txt, c, type));
if (should.Bool())
{

View File

@@ -290,7 +290,7 @@ namespace Barotrauma.Networking
SendConsoleMessage("Granted all permissions to " + newClient.Name + ".", newClient);
}
GameMain.LuaCs.hook.Call("clientConnected", newClient);
GameMain.LuaCs.HookBase.Call("clientConnected", newClient);
SendChatMessage($"ServerMessage.JoinedServer~[client]={clName}", ChatMessageType.Server, null, changeType: PlayerConnectionChangeType.Joined);
@@ -1804,7 +1804,7 @@ namespace Barotrauma.Networking
outmsg.Write((byte)ServerNetObject.CLIENT_LIST);
outmsg.Write(LastClientListUpdateID);
GameMain.LuaCs.hook.Call("writeClientList", c, outmsg);
GameMain.LuaCs.HookBase.Call("writeClientList", c, outmsg);
outmsg.Write((byte)connectedClients.Count);
foreach (Client client in connectedClients)
@@ -2251,7 +2251,7 @@ namespace Barotrauma.Networking
AssignJobs(teamClients);
var result = new LuaResult(GameMain.LuaCs.hook.Call("jobsAssigned"));
var result = new LuaResult(GameMain.LuaCs.HookBase.Call("jobsAssigned"));
List<CharacterInfo> characterInfos = new List<CharacterInfo>();
foreach (Client client in teamClients)
@@ -2455,7 +2455,7 @@ namespace Barotrauma.Networking
roundStartTime = DateTime.Now;
GameMain.LuaCs.hook.Call("roundStart");
GameMain.LuaCs.HookBase.Call("roundStart");
startGameCoroutine = null;
yield return CoroutineStatus.Success;
@@ -2586,7 +2586,7 @@ namespace Barotrauma.Networking
Log("Ending the round...", ServerLog.MessageType.ServerMessage);
}
GameMain.LuaCs.hook.Call("roundEnd");
GameMain.LuaCs.HookBase.Call("roundEnd");
string endMessage = TextManager.FormatServerMessage("RoundSummaryRoundHasEnded");
@@ -2697,7 +2697,7 @@ namespace Barotrauma.Networking
newName = Client.SanitizeName(newName);
if (newName == c.Name && newJob == c.PreferredJob && newTeam == c.PreferredTeam) { return false; }
var result = new LuaResult(GameMain.LuaCs.hook.Call("tryChangeClientName", c, newName, newJob, newTeam));
var result = new LuaResult(GameMain.LuaCs.HookBase.Call("tryChangeClientName", c, newName, newJob, newTeam));
if (!result.IsNull())
{
@@ -2892,7 +2892,7 @@ namespace Barotrauma.Networking
{
if (client == null) return;
GameMain.LuaCs.hook.Call("clientDisconnected", client);
GameMain.LuaCs.HookBase.Call("clientDisconnected", client);
if (gameStarted && client.Character != null)
{
@@ -3142,7 +3142,7 @@ namespace Barotrauma.Networking
var hookChatMsg = ChatMessage.Create(senderName, message, (ChatMessageType)type, senderCharacter, senderClient, changeType);
var should = new LuaResult(GameMain.LuaCs.hook.Call("modifyChatMessage", hookChatMsg, senderRadio));
var should = new LuaResult(GameMain.LuaCs.HookBase.Call("modifyChatMessage", hookChatMsg, senderRadio));
if (should.Bool())
return;
@@ -3874,7 +3874,7 @@ namespace Barotrauma.Networking
{
if (GameMain.Server == null || !GameMain.Server.ServerSettings.SaveServerLogs) { return; }
GameMain.LuaCs?.hook?.Call("serverLog", line, messageType);
GameMain.LuaCs?.HookBase?.Call("serverLog", line, messageType);
GameMain.Server.ServerSettings.ServerLog.WriteLine(line, messageType);

View File

@@ -184,7 +184,7 @@ namespace Barotrauma.Networking
var skipDeny = false;
{
var result = new LuaResult(GameMain.LuaCs.hook.Call("lidgren.handleConnection", inc));
var result = new LuaResult(GameMain.LuaCs.HookBase.Call("lidgren.handleConnection", inc));
if (!result.IsNull()) {
if (result.Bool()) skipDeny = true;
else return;

View File

@@ -206,7 +206,7 @@ namespace Barotrauma.Networking
protected void UpdatePendingClient(PendingClient pendingClient)
{
var result = new LuaResult(GameMain.LuaCs.hook.Call("handlePendingClient", pendingClient));
var result = new LuaResult(GameMain.LuaCs.HookBase.Call("handlePendingClient", pendingClient));
if (result.Bool())
goto ignore;

View File

@@ -94,7 +94,7 @@ namespace Barotrauma.Networking
ChatMessage.CanUseRadio(sender.Character, out WifiComponent senderRadio) &&
ChatMessage.CanUseRadio(recipient.Character, out WifiComponent recipientRadio))
{
var should = new LuaResult(GameMain.LuaCs.hook.Call("canUseVoiceRadio", new object[] { sender, recipient }));
var should = new LuaResult(GameMain.LuaCs.HookBase.Call("canUseVoiceRadio", new object[] { sender, recipient }));
if (!should.IsNull())
return should.Bool();
@@ -102,7 +102,7 @@ namespace Barotrauma.Networking
if (recipientRadio.CanReceive(senderRadio)) { return true; }
}
var should2 = new LuaResult(GameMain.LuaCs.hook.Call("changeLocalVoiceRange", new object[] { sender, recipient }));
var should2 = new LuaResult(GameMain.LuaCs.HookBase.Call("changeLocalVoiceRange", new object[] { sender, recipient }));
float range = 1.0f;
if (!should2.IsNull())

View File

@@ -92,7 +92,7 @@ namespace Barotrauma
var traitorCandidates = new List<Tuple<Client, Character>>();
foreach (Client c in server.ConnectedClients)
{
var result = new LuaResult(GameMain.LuaCs.hook.Call("traitor.findTraitorCandidate", new object[] { c, team }));
var result = new LuaResult(GameMain.LuaCs.HookBase.Call("traitor.findTraitorCandidate", new object[] { c, team }));
if (result.Bool())
{
traitorCandidates.Add(Tuple.Create(c, c.Character));
@@ -234,7 +234,7 @@ namespace Barotrauma
foreach (var traitor in Traitors.Values)
{
traitor.Greet(server, CodeWords, CodeResponse, message => pendingMessages[traitor].Add(message));
GameMain.LuaCs.hook.Call("traitor.traitorAssigned", new object[] { traitor });
GameMain.LuaCs.HookBase.Call("traitor.traitorAssigned", new object[] { traitor });
}
pendingMessages.ForEach(traitor => traitor.Value.ForEach(message => traitor.Key.SendChatMessage(message, Identifier)));
pendingMessages.ForEach(traitor => traitor.Value.ForEach(message => traitor.Key.SendChatMessageBox(message, Identifier)));

View File

@@ -80,7 +80,7 @@ namespace Barotrauma
public void AddObjective<T>(T objective) where T : AIObjective
{
var result = new LuaResult(GameMain.LuaCs.hook.Call("AI.AddObjective", this, objective));
var result = new LuaResult(GameMain.LuaCs.HookBase.Call("AI.AddObjective", this, objective));
if (result.Bool()) return;

View File

@@ -1440,11 +1440,11 @@ namespace Barotrauma
// -> the character should be revived if there are no major afflictions in addition to lack of oxygen
target.Oxygen = Math.Max(target.Oxygen + 10.0f, 10.0f);
GameMain.LuaCs.hook.Call("human.CPRSuccess", this);
GameMain.LuaCs.HookBase.Call("human.CPRSuccess", this);
}
else
{
GameMain.LuaCs.hook.Call("human.CPRFailed", this);
GameMain.LuaCs.HookBase.Call("human.CPRFailed", this);
}
}
}

View File

@@ -745,7 +745,7 @@ namespace Barotrauma
float impactDamage = Math.Min((impact - ImpactTolerance) * ImpactDamageMultiplayer, character.MaxVitality * MaxImpactDamage);
var should = new LuaResult(GameMain.LuaCs.hook.Call("changeFallDamage", new object[] { impactDamage, character, impactPos, velocity }));
var should = new LuaResult(GameMain.LuaCs.HookBase.Call("changeFallDamage", new object[] { impactDamage, character, impactPos, velocity }));
if (!should.IsNull())
{

View File

@@ -1043,7 +1043,7 @@ namespace Barotrauma
}
#endif
GameMain.LuaCs.hook.Call("characterCreated", new object[] { newCharacter });
GameMain.LuaCs.HookBase.Call("characterCreated", new object[] { newCharacter });
return newCharacter;
}

View File

@@ -388,7 +388,7 @@ namespace Barotrauma
AdditionStrength -= amount;
}
#if SERVER
GameMain.LuaCs.hook.Call("afflictionUpdate", new object[] { this, characterHealth, targetLimb, deltaTime });
GameMain.LuaCs.HookBase.Call("afflictionUpdate", new object[] { this, characterHealth, targetLimb, deltaTime });
#endif
}

View File

@@ -308,7 +308,7 @@ namespace Barotrauma
if (client != null)
{
GameMain.Server.SetClientCharacter(client, husk);
GameMain.LuaCs.hook.Call("husk.clientControlHusk", new object[] { client, husk });
GameMain.LuaCs.HookBase.Call("husk.clientControlHusk", new object[] { client, husk });
}
#else
if (!character.IsRemotelyControlled && character == Character.Controlled)

View File

@@ -541,7 +541,7 @@ namespace Barotrauma
return;
}
var should = new LuaResult(GameMain.LuaCs.hook.Call("character.applyDamage", new object[] { this, attackResult, hitLimb, allowStacking }));
var should = new LuaResult(GameMain.LuaCs.HookBase.Call("character.applyDamage", new object[] { this, attackResult, hitLimb, allowStacking }));
if (should.Bool())
return;
@@ -674,7 +674,7 @@ namespace Barotrauma
}
}
var should = new LuaResult(GameMain.LuaCs.hook.Call("character.applyAffliction", new object[] { this, limbHealth, newAffliction, allowStacking }));
var should = new LuaResult(GameMain.LuaCs.HookBase.Call("character.applyAffliction", new object[] { this, limbHealth, newAffliction, allowStacking }));
if (should.Bool())
return;

View File

@@ -0,0 +1,41 @@
using System;
using System.Collections.Generic;
using System.Reflection;
namespace Barotrauma
{
public abstract class ACsMod : IDisposable
{
private static List<ACsMod> mods = new List<ACsMod>();
public static List<ACsMod> LoadedMods { get => mods; }
//public static ACsMod CreateInstance(Type type)
//{
// if (!type.IsSubclassOf(typeof(ACsMod))) throw new Exception("Type argument is not the subclass of ACsMod.");
// return type.GetConstructor(new Type[] { }).Invoke(new object[] { }) as ACsMod;
//}
public bool IsDisposed { get; private set; }
public ACsMod()
{
IsDisposed = false;
LoadedMods.Add(this);
Start();
}
public void Dispose() {
Stop();
LoadedMods.Remove(this);
IsDisposed = true;
}
// TODO: some hooks
/// Mod initialization
public abstract void Start();
/// Error or client exit
public abstract void Stop();
public virtual void Update() { }
}
}

View File

@@ -0,0 +1,45 @@
using System;
using System.Reflection;
namespace Barotrauma
{
partial class LuaCsSetup
{
// CSharp wrapper for LuaCsHook
public class CsHook : LuaCsHookWrapper
{
public CsHook(LuaCsHook hook) : base(hook) { }
//public enum class HookMethodTypeProxy
//{
// Before = Barotrauma.HookMethodType.Before;
// After = Barotrauma.HookMethodType.After;
// public Barotrauma.HookMethodType type;
// public HookMethodTypeProxy(int i) => type = (Barotrauma.HookMethodType)i;
// public HookMethodTypeProxy(Barotrauma.HookMethodType t) => type = t;
// public static implicit operator Barotrauma.HookMethodType(HookMethodTypeProxy t) => t.type;
// public static implicit operator int(HookMethodTypeProxy t) => (int)t.type;
// public static implicit operator HookMethodTypeProxy(Barotrauma.HookMethodType t) => new HookMethodTypeProxy(t);
// public static implicit operator HookMethodTypeProxy(int i) => new HookMethodTypeProxy(i);
//}
//public readonly HookMethodTypeProxy HookMethodType = new HookMethodTypeProxy(Barotrauma.HookMethodType.Before);
public void HookMethod(string identifier, MethodInfo method, CsPatchDelegate hook, HookMethodType hookType = HookMethodType.Before, ACsMod owner = null) =>
_hook.HookCsMethod(identifier, method, hook, hookType, owner);
public void UnhookMethod(string identifier, MethodInfo method, HookMethodType hookType = HookMethodType.Before) =>
_hook.RemovePatch(identifier, method, hookType);
public void Add(string name, string hookName, CsHookDelegate hook, ACsMod owner = null) =>
_hook.AddCsHook(name, hookName, hook, owner);
}
}
}

View File

@@ -8,9 +8,8 @@ using System.IO;
using System.Linq;
using System.Reflection.Metadata;
partial class NetScript
{
public class NetScriptFilter
namespace Barotrauma {
class CsScriptFilter
{
private const bool useWhitelist = false;

View File

@@ -9,19 +9,17 @@ using Microsoft.CodeAnalysis;
using System.Runtime.Loader;
using System.Reflection.PortableExecutable;
using System.Reflection.Metadata;
using static NetScript;
namespace Barotrauma
{
class NetScriptLoader : AssemblyLoadContext
class CsScriptLoader : AssemblyLoadContext
{
public LuaCsSetup setup;
private List<MetadataReference> defaultReferences;
private List<SyntaxTree> syntaxTrees;
public Assembly Assembly { get; private set; }
public NetScriptLoader(LuaCsSetup setup)
public CsScriptLoader(LuaCsSetup setup)
{
this.setup = setup;
@@ -70,7 +68,7 @@ namespace Barotrauma
foreach (var file in scriptFiles)
{
var tree = SyntaxFactory.ParseSyntaxTree(File.ReadAllText(file), CSharpParseOptions.Default, file);
var error = NetScriptFilter.FilterSyntaxTree(tree as CSharpSyntaxTree);
var error = CsScriptFilter.FilterSyntaxTree(tree as CSharpSyntaxTree);
if (error != null) throw new Exception(error);
syntaxTrees.Add(tree);
@@ -114,7 +112,7 @@ namespace Barotrauma
else
{
mem.Seek(0, SeekOrigin.Begin);
var errStr = NetScriptFilter.FilterMetadata(new PEReader(mem).GetMetadataReader());
var errStr = CsScriptFilter.FilterMetadata(new PEReader(mem).GetMetadataReader());
if (errStr == null)
{
mem.Seek(0, SeekOrigin.Begin);
@@ -126,7 +124,7 @@ namespace Barotrauma
syntaxTrees.Clear();
if (Assembly != null)
return Assembly.GetTypes().Where(t => t.IsSubclassOf(typeof(ANetMod))).ToList();
return Assembly.GetTypes().Where(t => t.IsSubclassOf(typeof(ACsMod))).ToList();
else
throw new Exception("Unable to create net mods assembly.");
}

View File

@@ -526,7 +526,7 @@ namespace Barotrauma
HintManager.OnRoundStarted();
GameMain.LuaCs.hook.Call("roundStart");
GameMain.LuaCs.HookBase.Call("roundStart");
#endif
}
@@ -781,7 +781,7 @@ namespace Barotrauma
RoundEnding = true;
#if CLIENT
GameMain.LuaCs.hook.Call("roundEnd");
GameMain.LuaCs.HookBase.Call("roundEnd");
#endif
//Clear the grids to allow for garbage collection
Powered.Grids.Clear();

View File

@@ -387,7 +387,7 @@ namespace Barotrauma.Items.Components
Limb targetLimb = target.UserData as Limb;
Character targetCharacter = targetLimb?.character ?? target.UserData as Character;
GameMain.LuaCs.hook.Call("meleeWeapon.handleImpact", this, target);
GameMain.LuaCs.HookBase.Call("meleeWeapon.handleImpact", this, target);
if (Attack != null)
{
Attack.SetUser(User);

View File

@@ -325,8 +325,8 @@ namespace Barotrauma.Items.Components
Connection connection = recipient;
object[] obj = new object[] { signal, connection };
GameMain.LuaCs.hook.Call("signalReceived", obj);
GameMain.LuaCs.hook.Call("signalReceived." + recipient.item.Prefab.Identifier, obj);
GameMain.LuaCs.HookBase.Call("signalReceived", obj);
GameMain.LuaCs.HookBase.Call("signalReceived." + recipient.item.Prefab.Identifier, obj);
foreach (ItemComponent ic in recipient.item.Components)
{

View File

@@ -205,7 +205,7 @@ namespace Barotrauma.Items.Components
public void TransmitSignal(Signal signal, bool sentFromChat)
{
var should = new LuaResult(GameMain.LuaCs.hook.Call("wifiSignalTransmitted", new object[] { this, signal, sentFromChat }));
var should = new LuaResult(GameMain.LuaCs.HookBase.Call("wifiSignalTransmitted", new object[] { this, signal, sentFromChat }));
if (should.Bool())
return;

View File

@@ -540,7 +540,7 @@ namespace Barotrauma
return;
}
var should = new LuaResult(GameMain.LuaCs.hook.Call("inventoryPutItem", new object[] { this, item, user, i, removeItem }));
var should = new LuaResult(GameMain.LuaCs.HookBase.Call("inventoryPutItem", new object[] { this, item, user, i, removeItem }));
if (should.Bool())
return;
@@ -648,7 +648,7 @@ namespace Barotrauma
if (slots[index].Items.Any(it => !it.IsInteractable(user))) { return false; }
if (!AllowSwappingContainedItems) { return false; }
var should = new LuaResult(GameMain.LuaCs.hook.Call("inventoryItemSwap", new object[] { this, item, user, index, swapWholeStack }));
var should = new LuaResult(GameMain.LuaCs.HookBase.Call("inventoryItemSwap", new object[] { this, item, user, index, swapWholeStack }));
if (!should.IsNull())
return should.Bool();

View File

@@ -999,7 +999,7 @@ namespace Barotrauma
if (Components.Any(ic => ic is Wire) && Components.All(ic => ic is Wire || ic is Holdable)) { isWire = true; }
if (HasTag("logic")) { isLogic = true; }
GameMain.LuaCs.hook.Call("item.created", this);
GameMain.LuaCs.HookBase.Call("item.created", this);
ApplyStatusEffects(ActionType.OnSpawn, 1.0f);
Components.ForEach(c => c.ApplyStatusEffects(ActionType.OnSpawn, 1.0f));
@@ -2469,7 +2469,7 @@ namespace Barotrauma
if (condition == 0.0f) { return; }
var should = new LuaResult(GameMain.LuaCs.hook.Call("item.use", new object[] { this, character, targetLimb }));
var should = new LuaResult(GameMain.LuaCs.HookBase.Call("item.use", new object[] { this, character, targetLimb }));
if (should.Bool())
return;
@@ -2507,7 +2507,7 @@ namespace Barotrauma
{
if (condition == 0.0f) { return; }
var should = new LuaResult(GameMain.LuaCs.hook.Call("item.secondaryUse", new object[] { this, character}));
var should = new LuaResult(GameMain.LuaCs.HookBase.Call("item.secondaryUse", new object[] { this, character}));
if (should.Bool())
return;
@@ -2851,7 +2851,7 @@ namespace Barotrauma
}
}
var result = new LuaResult(GameMain.LuaCs.hook.Call("item.readPropertyChange", this, property, parentObject, allowEditing));
var result = new LuaResult(GameMain.LuaCs.HookBase.Call("item.readPropertyChange", this, property, parentObject, allowEditing));
if (result.Bool())
return;
@@ -3343,7 +3343,7 @@ namespace Barotrauma
body = null;
}
GameMain.LuaCs.hook.Call("item.removed", this);
GameMain.LuaCs.HookBase.Call("item.removed", this);
}
public override void Remove()
@@ -3427,7 +3427,7 @@ namespace Barotrauma
RemoveProjSpecific();
GameMain.LuaCs.hook.Call("item.removed", this);
GameMain.LuaCs.HookBase.Call("item.removed", this);
}
partial void RemoveProjSpecific();

View File

@@ -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());
}
}

View File

@@ -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

View File

@@ -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);
}
}
}

View File

@@ -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;
}

View File

@@ -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));
});
}

View File

@@ -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);
}
}
}

View File

@@ -8,20 +8,37 @@ using System.Text;
namespace Barotrauma
{
delegate object CsHookDelegate(params object[] args);
delegate object CsPatchDelegate(object self, params object[] args);
partial class LuaCsHook
public enum HookMethodType
{
public LuaCsHook()
Before, After
}
public delegate object CsHookDelegate(params object[] args);
public delegate object CsPatchDelegate(object self, params object[] args);
public abstract class LuaCsHookWrapper {
protected LuaCsHook _hook;
public LuaCsHookWrapper(LuaCsHook hook)
{
luaHookPrefixMethods = new Dictionary<long, HashSet<(string, object)>>();
luaHookPostfixMethods = new Dictionary<long, HashSet<(string, object)>>();
csHookPrefixMethods = new Dictionary<long, HashSet<(IDisposable, CsPatchDelegate)>>();
csHookPostfixMethods = new Dictionary<long, HashSet<(IDisposable, CsPatchDelegate)>>();
_hook = hook;
}
public class LuaHookFunction
public void Remove(string name, string hookName) =>
_hook.RemoveHook(name, hookName);
public void Update() =>
_hook.Update();
public object Call(string name, params object[] args) =>
_hook.Call(name, args);
}
public class LuaCsHook
{
private class LuaHookFunction
{
public string name;
public string hookName;
@@ -35,28 +52,40 @@ namespace Barotrauma
}
}
private Dictionary<string, Dictionary<string, LuaHookFunction>> luaHookFunctions = new Dictionary<string, Dictionary<string, LuaHookFunction>>();
private Dictionary<string, Dictionary<CsHookDelegate, IDisposable>> csHookFunctions = new Dictionary<string, Dictionary<CsHookDelegate, IDisposable>>();
private Dictionary<string, Dictionary<string, LuaHookFunction>> luaHookFunctions;
private Dictionary<string, Dictionary<string, (CsHookDelegate, ACsMod)>> csHookFunctions;
private static Dictionary<long, HashSet<(string, object)>> luaHookPrefixMethods;
private static Dictionary<long, HashSet<(string, object)>> luaHookPostfixMethods;
private static Dictionary<long, HashSet<(IDisposable, CsPatchDelegate)>> csHookPrefixMethods;
private static Dictionary<long, HashSet<(IDisposable, CsPatchDelegate)>> csHookPostfixMethods;
private Dictionary<long, HashSet<(string, object)>> luaHookPrefixMethods;
private Dictionary<long, HashSet<(string, object)>> luaHookPostfixMethods;
private Dictionary<long, HashSet<(string, CsPatchDelegate, ACsMod)>> csHookPrefixMethods;
private Dictionary<long, HashSet<(string, CsPatchDelegate, ACsMod)>> csHookPostfixMethods;
private Queue<Tuple<float, object, object[]>> queuedFunctionCalls = new Queue<Tuple<float, object, object[]>>();
private Queue<Tuple<float, object, object[]>> queuedFunctionCalls;
public enum HookMethodType
{
Before, After
private LuaCsHook() {
luaHookFunctions = new Dictionary<string, Dictionary<string, LuaHookFunction>>();
csHookFunctions = new Dictionary<string, Dictionary<string, (CsHookDelegate, ACsMod)>>();
luaHookPrefixMethods = new Dictionary<long, HashSet<(string, object)>>();
luaHookPostfixMethods = new Dictionary<long, HashSet<(string, object)>>();
csHookPrefixMethods = new Dictionary<long, HashSet<(string, CsPatchDelegate, ACsMod)>>();
csHookPostfixMethods = new Dictionary<long, HashSet<(string, CsPatchDelegate, ACsMod)>>();
queuedFunctionCalls = new Queue<Tuple<float, object, object[]>>();
}
private static LuaCsHook _inst;
static LuaCsHook() => _inst = new LuaCsHook();
public static LuaCsHook Instance { get => _inst; }
static void _hookLuaPatch(MethodBase __originalMethod, object[] __args, object __instance, out LuaResult result, HookMethodType hookMethodType)
{
result = new LuaResult(null);
#if CLIENT
if (GameMain.GameSession?.IsRunning == false && GameMain.IsSingleplayer)
return;
if (GameMain.GameSession?.IsRunning == false && GameMain.IsSingleplayer)
return;
#endif
try
@@ -66,10 +95,10 @@ namespace Barotrauma
switch (hookMethodType)
{
case HookMethodType.Before:
luaHookPrefixMethods.TryGetValue(funcAddr, out methodSet);
_inst.luaHookPrefixMethods.TryGetValue(funcAddr, out methodSet);
break;
case HookMethodType.After:
luaHookPostfixMethods.TryGetValue(funcAddr, out methodSet);
_inst.luaHookPostfixMethods.TryGetValue(funcAddr, out methodSet);
break;
default:
break;
@@ -86,16 +115,66 @@ namespace Barotrauma
foreach (var tuple in methodSet)
{
result = new LuaResult(GameMain.LuaCs.lua.Call(tuple.Item2, __instance, ptable));
var luaResult = new LuaResult(GameMain.LuaCs.lua.Call(tuple.Item2, __instance, ptable));
if (!luaResult.IsNull()) result = luaResult;
}
}
}
catch (Exception ex)
{
GameMain.LuaCs.HandleLuaException(ex);
GameMain.LuaCs.HandleException(ex);
}
}
static void _hookCsPatch(MethodBase __originalMethod, object[] __args, object __instance, ref object result, HookMethodType hookMethodType)
{
#if CLIENT
if (GameMain.GameSession?.IsRunning == false && GameMain.IsSingleplayer)
return;
#endif
try
{
var funcAddr = ((long)__originalMethod.MethodHandle.GetFunctionPointer());
HashSet<(string, CsPatchDelegate, ACsMod)> methodSet = null;
switch (hookMethodType)
{
case HookMethodType.Before:
_inst.csHookPrefixMethods.TryGetValue(funcAddr, out methodSet);
break;
case HookMethodType.After:
_inst.csHookPostfixMethods.TryGetValue(funcAddr, out methodSet);
break;
default:
break;
}
if (methodSet != null)
{
var @params = __originalMethod.GetParameters();
var args = new Dictionary<string, object>();
for (int i = 0; i < @params.Length; i++)
{
args.Add(@params[i].Name, __args[i]);
}
var outOfSocpe = new HashSet<(string, CsPatchDelegate, ACsMod)>();
foreach (var tuple in methodSet)
{
if (tuple.Item3 != null && tuple.Item3.IsDisposed)
outOfSocpe.Add(tuple);
else
result = tuple.Item2(__instance, args) ?? result;
}
foreach (var tuple in outOfSocpe) methodSet.Remove(tuple);
}
}
catch (Exception ex)
{
GameMain.LuaCs.HandleException(ex, exceptionType: LuaCsSetup.ExceptionType.CSharp);
}
}
private static bool HookLuaPatchPrefix(MethodBase __originalMethod, object[] __args, object __instance)
{
_hookLuaPatch(__originalMethod, __args, __instance, out LuaResult result, HookMethodType.Before);
@@ -129,7 +208,6 @@ namespace Barotrauma
{
_hookLuaPatch(__originalMethod, __args, __instance, out LuaResult result, HookMethodType.After);
}
private static void HookLuaPatchRetPostfix(MethodBase __originalMethod, object[] __args, ref object __result, object __instance)
{
_hookLuaPatch(__originalMethod, __args, __instance, out LuaResult result, HookMethodType.After);
@@ -147,19 +225,35 @@ namespace Barotrauma
}
}
private static bool HookCsPatchPrefix(MethodBase __originalMethod, object[] __args, ref object __result, object __instance)
{
_hookCsPatch(__originalMethod, __args, __instance, ref __result, HookMethodType.Before);
if (__result != null) return false;
else return true;
}
private static void HookCsPatchPostfix(MethodBase __originalMethod, object[] __args, ref object __result, object __instance) =>
_hookCsPatch(__originalMethod, __args, __instance, ref __result, HookMethodType.After);
private const BindingFlags DefaultBindingFlags = BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;
private static MethodInfo _miHookLuaPatchPrefix = typeof(LuaCsHook).GetMethod("HookLuaPatchPrefix", BindingFlags.NonPublic | BindingFlags.Static);
private static MethodInfo _miHookLuaPatchRetPrefix = typeof(LuaCsHook).GetMethod("HookLuaPatchRetPrefix", BindingFlags.NonPublic | BindingFlags.Static);
private static MethodInfo _miHookLuaPatchPostfix = typeof(LuaCsHook).GetMethod("HookLuaPatchPostfix", BindingFlags.NonPublic | BindingFlags.Static);
private static MethodInfo _miHookLuaPatchRetPostfix = typeof(LuaCsHook).GetMethod("HookLuaPatchRetPostfix", BindingFlags.NonPublic | BindingFlags.Static);
private void HookMethod(string identifier, string className, string methodName, string[] parameterNames, object hookMethod, HookMethodType hookMethodType = HookMethodType.Before)
{
private static MethodInfo _miHookCsPatchRetPrefix = typeof(LuaCsHook).GetMethod("HookCsPatchPrefix", BindingFlags.NonPublic | BindingFlags.Static);
private static MethodInfo _miHookCsPatchRetPostfix = typeof(LuaCsHook).GetMethod("HookCsPatchPostfix", BindingFlags.NonPublic | BindingFlags.Static);
private static MethodInfo ResolveMethod(string where, string className, string methodName, string[] parameterNames)
{
var classType = LuaUserData.GetType(className);
if (classType == null)
{
GameMain.LuaCs.HandleLuaException(new Exception($"Tried to use HookMethod with an invalid class name '{className}'."));
return;
GameMain.LuaCs.HandleException(new Exception($"Tried to use {where} with an invalid class name '{className}'."));
return null;
}
MethodInfo methodInfo = null;
@@ -177,10 +271,18 @@ namespace Barotrauma
if (methodInfo == null)
{
string parameterNamesStr = parameterNames == null ? "" : string.Join(", ", parameterNames == null);
GameMain.LuaCs.HandleLuaException(new Exception($"Method '{methodName}' with parameters '{parameterNamesStr}' not found in class '{className}'"));
return;
GameMain.LuaCs.HandleException(new Exception($"Method '{methodName}' with parameters '{parameterNamesStr}' not found in class '{className}'"));
}
return methodInfo;
}
public void HookLuaMethod(string identifier, string className, string methodName, string[] parameterNames, object hookMethod, HookMethodType hookMethodType = HookMethodType.Before)
{
MethodInfo methodInfo = ResolveMethod("HookMethod", className, methodName, parameterNames);
if (methodInfo == null) return;
identifier = identifier.ToLower();
var funcAddr = ((long)methodInfo.MethodHandle.GetFunctionPointer());
var patches = Harmony.GetPatchInfo(methodInfo);
@@ -256,86 +358,93 @@ namespace Barotrauma
}
private void HookMethod(string className, string methodName, object hookMethod, HookMethodType hookMethodType = HookMethodType.Before)
public void HookCsMethod(string identifier, MethodInfo method, CsPatchDelegate hook, HookMethodType hookType = HookMethodType.Before, ACsMod owner = null)
{
HookMethod("", className, methodName, null, hookMethod, hookMethodType);
}
if (identifier == null || method == null || hook == null) throw new ArgumentNullException("Identifier, Method and Hook arguments must not be null.");
private void HookMethod(string className, string methodName, string[] parameterNames, object hookMethod, HookMethodType hookMethodType = HookMethodType.Before)
{
HookMethod("", className, methodName, parameterNames, hookMethod, hookMethodType);
}
var funcAddr = ((long)method.MethodHandle.GetFunctionPointer());
var patches = Harmony.GetPatchInfo(method);
private void HookMethod(string identifier, string className, string methodName, object hookMethod, HookMethodType hookMethodType = HookMethodType.Before)
{
HookMethod(identifier, className, methodName, null, hookMethod, hookMethodType);
}
public void HookMethod(IDisposable owner, MethodInfo methodInfo, CsPatchDelegate hookMethod, HookMethodType hookMethodType = HookMethodType.Before)
{
if (owner == null || methodInfo == null || hookMethod == null) throw new ArgumentNullException("All 'HookMethod' arguments must not be null.");
var funcAddr = ((long)methodInfo.MethodHandle.GetFunctionPointer());
var patches = Harmony.GetPatchInfo(methodInfo);
if (hookMethodType == HookMethodType.Before)
if (hookType == HookMethodType.Before)
{
if (patches == null || patches.Prefixes == null || patches.Prefixes.Find(patch => patch.PatchMethod == hookMethod.Method) == null)
if (patches == null || patches.Prefixes == null || patches.Prefixes.Find(patch => patch.PatchMethod == _miHookCsPatchRetPrefix) == null)
{
GameMain.LuaCs.harmony.Patch(methodInfo, prefix: new HarmonyMethod(hookMethod.Method));
GameMain.LuaCs.harmony.Patch(method, prefix: new HarmonyMethod(_miHookCsPatchRetPrefix));
}
if (csHookPrefixMethods.TryGetValue(funcAddr, out HashSet<(IDisposable, CsPatchDelegate)> methodSet))
if (csHookPrefixMethods.TryGetValue(funcAddr, out HashSet<(string, CsPatchDelegate, ACsMod)> methodSet))
{
methodSet.RemoveWhere(tuple => tuple.Item1 == owner);
if (hookMethod != null)
{
methodSet.Add((owner, hookMethod));
}
methodSet.RemoveWhere(tuple => tuple.Item1 == identifier);
methodSet.Add((identifier, hook, owner));
}
else if (hookMethod != null)
else if (hook != null)
{
csHookPrefixMethods.Add(funcAddr, new HashSet<(IDisposable, CsPatchDelegate)>() { (owner, hookMethod) });
csHookPrefixMethods.Add(funcAddr, new HashSet<(string, CsPatchDelegate, ACsMod)>() { (identifier, hook, owner) });
}
}
else if (hookMethodType == HookMethodType.After)
else if (hookType == HookMethodType.After)
{
if (patches == null || patches.Postfixes == null || patches.Postfixes.Find(patch => patch.PatchMethod == hookMethod.Method) == null)
if (patches == null || patches.Postfixes == null || patches.Postfixes.Find(patch => patch.PatchMethod == _miHookCsPatchRetPrefix) == null)
{
GameMain.LuaCs.harmony.Patch(methodInfo, postfix: new HarmonyMethod(hookMethod.Method));
GameMain.LuaCs.harmony.Patch(method, postfix: new HarmonyMethod(_miHookCsPatchRetPostfix));
}
if (csHookPostfixMethods.TryGetValue(funcAddr, out HashSet<(IDisposable, CsPatchDelegate)> methodSet))
if (csHookPostfixMethods.TryGetValue(funcAddr, out HashSet<(string, CsPatchDelegate, ACsMod)> methodSet))
{
methodSet.RemoveWhere(tuple => tuple.Item1 == owner);
if (hookMethod != null)
{
methodSet.Add((owner, hookMethod));
}
methodSet.RemoveWhere(tuple => tuple.Item1 == identifier);
methodSet.Add((identifier, hook, owner));
}
else if (hookMethod != null)
else if (hook != null)
{
csHookPostfixMethods.Add(funcAddr, new HashSet<(IDisposable, CsPatchDelegate)>() { (owner, hookMethod) });
csHookPostfixMethods.Add(funcAddr, new HashSet<(string, CsPatchDelegate, ACsMod)>() { (identifier, hook, owner) });
}
}
}
public void RemoveLuaPatch(string identifier, string className, string methodName, string[] parameterNames, HookMethodType hookType = HookMethodType.Before)
{
MethodInfo methodInfo = ResolveMethod("UnhookMathod", className, methodName, parameterNames);
if (methodInfo == null) return;
RemovePatch(identifier, methodInfo, hookType);
}
public void RemovePatch(string identifier, MethodInfo method, HookMethodType hookType = HookMethodType.Before)
{
var funcAddr = ((long)method.MethodHandle.GetFunctionPointer());
public void EnqueueFunction(object function, params object[] args)
Dictionary<long, HashSet<(string, object)>> luaMethods;
Dictionary<long, HashSet<(string, CsPatchDelegate, ACsMod)>> csMethods;
if (hookType == HookMethodType.Before)
{
luaMethods = luaHookPrefixMethods;
csMethods = csHookPrefixMethods;
}
else if (hookType == HookMethodType.After)
{
luaMethods = luaHookPostfixMethods;
csMethods = csHookPostfixMethods;
}
else throw null;
if (luaMethods.ContainsKey(funcAddr)) luaMethods[funcAddr]?.RemoveWhere(t => t.Item1 == identifier);
if (csMethods.ContainsKey(funcAddr)) csMethods[funcAddr]?.RemoveWhere(t => t.Item1 == identifier);
}
public void EnqueueLuaFunction(object function, params object[] args)
{
queuedFunctionCalls.Enqueue(new Tuple<float, object, object[]>(0, function, args));
}
public void EnqueueTimedFunction(float time, object function, params object[] args)
public void EnqueueTimedLuaFunction(float time, object function, params object[] args)
{
queuedFunctionCalls.Enqueue(new Tuple<float, object, object[]>(time, function, args));
}
public void Add(string name, string hookName, object function)
public void AddLuaHook(string name, string hookName, object function)
{
if (name == null || hookName == null || function == null) return;
@@ -347,38 +456,39 @@ namespace Barotrauma
luaHookFunctions[name][hookName] = new LuaHookFunction(name, hookName, function);
}
public void Remove(string name, string hookName)
public void AddCsHook(string name, string hookName, CsHookDelegate hook, ACsMod owner = null)
{
if (name == null || hookName == null || hook == null) throw new ArgumentNullException("Names and Hook must not be null");
if (!csHookFunctions.ContainsKey(name))
csHookFunctions.Add(name, new Dictionary<string, (CsHookDelegate, ACsMod)>());
csHookFunctions[name][hookName] = (hook, owner);
}
public void RemoveHook(string name, string hookName)
{
if (name == null || hookName == null) return;
name = name.ToLower();
if (!luaHookFunctions.ContainsKey(name))
return;
if (luaHookFunctions[name].ContainsKey(hookName))
if (luaHookFunctions.ContainsKey(name) && luaHookFunctions[name].ContainsKey(hookName))
luaHookFunctions[name].Remove(hookName);
if (csHookFunctions.ContainsKey(name) && csHookFunctions[name].ContainsKey(hookName))
csHookFunctions[name].Remove(hookName);
}
public void Add(string name, CsHookDelegate hook, IDisposable owner = null)
{
if (name == null || hook == null) throw new ArgumentNullException("Name and Hook must not be null");
public void Clear()
{
luaHookFunctions.Clear();
csHookFunctions.Clear();
if (!csHookFunctions.ContainsKey(name))
csHookFunctions.Add(name, new Dictionary<CsHookDelegate, IDisposable>());
luaHookPrefixMethods.Clear();
luaHookPostfixMethods.Clear();
csHookPrefixMethods.Clear();
csHookPostfixMethods.Clear();
csHookFunctions[name][hook] = owner;
}
public void Remove(string name, CsHookDelegate hook)
{
if (name == null || hook == null) throw new ArgumentNullException("All arguments must not be null");
if (!csHookFunctions.ContainsKey(name))
return;
if (csHookFunctions[name].ContainsKey(hook))
csHookFunctions[name].Remove(hook);
queuedFunctionCalls.Clear();
}
@@ -390,7 +500,7 @@ namespace Barotrauma
{
if (Timing.TotalTime >= result.Item1)
{
GameMain.LuaCs.CallFunction(result.Item2, result.Item3);
GameMain.LuaCs.CallLuaFunction(result.Item2, result.Item3);
queuedFunctionCalls.Dequeue();
}
@@ -398,15 +508,15 @@ namespace Barotrauma
}
catch (Exception ex)
{
GameMain.LuaCs.HandleLuaException(ex, $"queuedFunctionCalls was {queuedFunctionCalls}");
GameMain.LuaCs.HandleException(ex, $"queuedFunctionCalls was {queuedFunctionCalls}");
}
}
public object Call(string name, params object[] args)
{
#if CLIENT
if (GameMain.GameSession?.IsRunning == false && GameMain.IsSingleplayer)
return null;
if (GameMain.GameSession?.IsRunning == false && GameMain.IsSingleplayer)
return null;
#endif
if (GameMain.LuaCs == null) return null;
if (name == null) return null;
@@ -419,26 +529,56 @@ namespace Barotrauma
object lastResult = null;
foreach (LuaHookFunction hf in luaHookFunctions[name].Values)
if (csHookFunctions.ContainsKey(name))
{
try
var outOfScope = new List<string>();
foreach ((var key, var tuple) in csHookFunctions[name])
{
if (hf.function is Closure)
if (tuple.Item2 != null && tuple.Item2.IsDisposed)
outOfScope.Add(key);
else
{
var result = GameMain.LuaCs.lua.Call(hf.function, args);
if (!result.IsNil())
lastResult = result;
try
{
var result = tuple.Item1(args);
if (result != null) lastResult = result;
}
catch (Exception e)
{
StringBuilder argsSb = new StringBuilder();
foreach (var arg in args) argsSb.Append(arg + " ");
GameMain.LuaCs.HandleException(
e, $"Error in Hook '{name}'->'{key}', with args '{argsSb}':\n{e}",
LuaCsSetup.ExceptionType.CSharp);
}
}
}
catch (Exception e)
{
StringBuilder argsSb = new StringBuilder();
foreach (var arg in args)
{
argsSb.Append(arg + " ");
}
foreach (var key in outOfScope) csHookFunctions[name].Remove(key);
}
GameMain.LuaCs.HandleLuaException(e, $"Error in Hook '{name}'->'{hf.hookName}', with args '{argsSb}'");
if (luaHookFunctions.ContainsKey(name))
{
foreach (LuaHookFunction hf in luaHookFunctions[name].Values)
{
try
{
if (hf.function is Closure)
{
var result = GameMain.LuaCs.lua.Call(hf.function, args);
if (!result.IsNil())
lastResult = result;
}
}
catch (Exception e)
{
StringBuilder argsSb = new StringBuilder();
foreach (var arg in args)
{
argsSb.Append(arg + " ");
}
GameMain.LuaCs.HandleException(e, $"Error in Hook '{name}'->'{hf.hookName}', with args '{argsSb}'");
}
}
}

View File

@@ -8,7 +8,6 @@ using Microsoft.Xna.Framework;
using MoonSharp.Interpreter.Interop;
using System.IO.Compression;
using HarmonyLib;
using static Barotrauma.LuaCsSetup;
using System.Runtime.CompilerServices;
[assembly: InternalsVisibleTo("NetScriptAssembly", AllInternalsVisible = true)]
@@ -21,13 +20,27 @@ namespace Barotrauma
public Script lua;
public LuaCsHook hook;
private LuaHook luaHook;
public CsHook Hook { get; private set; }
internal LuaCsHook HookBase { get; private set; }
public LuaGame game;
public LuaNetworking networking;
public Harmony harmony;
public LuaScriptLoader luaScriptLoader;
public NetScriptLoader netScriptLoader;
public CsScriptLoader netScriptLoader;
public LuaCsSetup()
{
HookBase = LuaCsHook.Instance;
Hook = new CsHook(HookBase);
luaHook = new LuaHook(HookBase);
game = new LuaGame();
networking = new LuaNetworking();
}
public static ContentPackage GetPackage()
{
@@ -50,10 +63,16 @@ namespace Barotrauma
return null;
}
public void HandleLuaException(Exception ex, string extra = "")
public enum ExceptionType
{
Lua,
CSharp
}
public void HandleException(Exception ex, string extra = "", ExceptionType exceptionType = ExceptionType.Lua)
{
if (!string.IsNullOrWhiteSpace(extra))
PrintError(extra);
if (exceptionType == ExceptionType.Lua) PrintError(extra);
else PrintCsError(extra);
if (ex is InterpreterException)
{
@@ -64,7 +83,8 @@ namespace Barotrauma
}
else
{
PrintError(ex.ToString());
if (exceptionType == ExceptionType.Lua) PrintError(ex);
else PrintCsError(ex);
}
}
@@ -144,7 +164,7 @@ namespace Barotrauma
}
catch (Exception e)
{
HandleLuaException(e);
HandleException(e);
}
return null;
@@ -155,7 +175,7 @@ namespace Barotrauma
if (!LuaFile.IsPathAllowedLuaException(file, false)) return null;
if (!LuaFile.Exists(file))
{
HandleLuaException(new Exception($"dofile: File {file} not found."));
HandleException(new Exception($"dofile: File {file} not found."));
return null;
}
@@ -166,7 +186,7 @@ namespace Barotrauma
}
catch (Exception e)
{
HandleLuaException(e);
HandleException(e);
}
return null;
@@ -182,7 +202,7 @@ namespace Barotrauma
}
catch (Exception e)
{
HandleLuaException(e);
HandleException(e);
}
return null;
@@ -193,7 +213,7 @@ namespace Barotrauma
if (!LuaFile.IsPathAllowedLuaException(file, false)) return null;
if (!LuaFile.Exists(file))
{
HandleLuaException(new Exception($"loadfile: File {file} not found."));
HandleException(new Exception($"loadfile: File {file} not found."));
return null;
}
@@ -204,7 +224,7 @@ namespace Barotrauma
}
catch (Exception e)
{
HandleLuaException(e);
HandleException(e);
}
return null;
@@ -219,13 +239,13 @@ namespace Barotrauma
}
catch (Exception e)
{
HandleLuaException(e);
HandleException(e);
}
return null;
}
public object CallFunction(object function, params object[] arguments)
public object CallLuaFunction(object function, params object[] arguments)
{
try
{
@@ -233,7 +253,7 @@ namespace Barotrauma
}
catch (Exception e)
{
HandleLuaException(e);
HandleException(e);
}
return null;
@@ -246,19 +266,20 @@ namespace Barotrauma
public void Update()
{
hook?.Update();
HookBase?.Update();
}
public void Stop()
{
ANetMod.LoadedMods.ForEach(m => m.Dispose());
ANetMod.LoadedMods.Clear();
hook?.Call("stop");
ACsMod.LoadedMods.ForEach(m => m.Dispose());
ACsMod.LoadedMods.Clear();
HookBase?.Call("stop");
game?.Stop();
harmony?.UnpatchAll();
hook = new LuaCsHook();
//HookBase = new LuaCsHook();
HookBase.Clear();
game = new LuaGame();
networking = new LuaNetworking();
luaScriptLoader = null;
@@ -266,13 +287,14 @@ namespace Barotrauma
private void InitCs()
{
netScriptLoader = new NetScriptLoader(this);
netScriptLoader = new CsScriptLoader(this);
netScriptLoader.SearchFolders();
if (netScriptLoader == null) throw new Exception("LuaCsSetup was not properly initialized.");
try
{
var modTypes = netScriptLoader.Compile();
modTypes.ForEach(t => t.GetConstructor(new Type[] { }).Invoke(null));
//modTypes.ForEach(t => ACsMod.CreateInstance(t));
modTypes.ForEach(t => t.GetConstructor(new Type[] { })?.Invoke(null));
}
catch (Exception ex)
{
@@ -299,11 +321,12 @@ namespace Barotrauma
harmony = new Harmony("com.LuaForBarotrauma");
harmony.UnpatchAll();
hook = new LuaCsHook();
//HookBase = new LuaCsHook();
game = new LuaGame();
networking = new LuaNetworking();
UserData.RegisterType<LuaCsHook>();
//UserData.RegisterType<LuaCsHook>();
UserData.RegisterType<LuaHook>();
UserData.RegisterType<LuaGame>();
UserData.RegisterType<LuaTimer>();
UserData.RegisterType<LuaFile>();
@@ -324,7 +347,8 @@ namespace Barotrauma
lua.Globals["LuaUserData"] = UserData.CreateStatic<LuaUserData>();
lua.Globals["Game"] = game;
lua.Globals["Hook"] = hook;
//lua.Globals["Hook"] = HookBase;
lua.Globals["Hook"] = luaHook;
lua.Globals["Timer"] = new LuaTimer();
lua.Globals["File"] = UserData.CreateStatic<LuaFile>();
lua.Globals["Networking"] = networking;
@@ -352,7 +376,7 @@ namespace Barotrauma
}
catch (Exception e)
{
HandleLuaException(e);
HandleException(e);
}
}
else if (luaPackage != null)
@@ -361,27 +385,20 @@ namespace Barotrauma
try
{
string luaPath = Path.Combine(path, "Binary/LuaCs/LuaCsSetup.lua");
string luaPath = Path.Combine(path, "Binary/Lua/LuaSetup.lua");
lua.Call(lua.LoadFile(luaPath), Path.GetDirectoryName(luaPath));
}
catch (Exception e)
{
HandleLuaException(e);
HandleException(e);
}
}
else
{
PrintError("LuaCs loader not found! LuaCs/LuaCsSetup.lua, no LuaCs scripts will be executed or work.");
PrintError("LuaCs loader not found! Lua/LuaSetup.lua, no Lua scripts will be executed or work.");
}
}
public LuaCsSetup()
{
hook = new LuaCsHook();
game = new LuaGame();
networking = new LuaNetworking();
}
}

View File

@@ -660,7 +660,7 @@ namespace Barotrauma
if (Math.Max(hull1.WorldSurface + hull1.WaveY[hull1.WaveY.Length - 1], hull2.WorldSurface + hull2.WaveY[0]) > WorldRect.Y) { return; }
}
var should = new LuaResult(GameMain.LuaCs.hook.Call("gapOxygenUpdate", this, hull1, hull2));
var should = new LuaResult(GameMain.LuaCs.HookBase.Call("gapOxygenUpdate", this, hull1, hull2));
if (should.Bool())
return;

View File

@@ -1,26 +0,0 @@
using System;
using System.Collections.Generic;
namespace Barotrauma
{
public abstract class ANetMod : IDisposable
{
private static List<ANetMod> mods = new List<ANetMod>();
public static List<ANetMod> LoadedMods
{
get => mods;
}
public ANetMod()
{
LoadedMods.Add(this);
}
/// Error or client exit
public virtual void Dispose() {
LoadedMods.Remove(this);
}
// TODO: some hooks
public virtual void Update() { }
}
}

View File

@@ -1201,7 +1201,7 @@ namespace Barotrauma
{
if (entity is Item item)
{
var result = new LuaResult(GameMain.LuaCs.hook.Call("statusEffect.apply." + item.Prefab.Identifier, this, deltaTime, entity, targets, worldPosition));
var result = new LuaResult(GameMain.LuaCs.HookBase.Call("statusEffect.apply." + item.Prefab.Identifier, this, deltaTime, entity, targets, worldPosition));
if (result.Bool())
return;
@@ -1209,7 +1209,7 @@ namespace Barotrauma
if (entity is Character character)
{
var result = new LuaResult(GameMain.LuaCs.hook.Call("statusEffect.apply." + character.SpeciesName, this, deltaTime, entity, targets, worldPosition));
var result = new LuaResult(GameMain.LuaCs.HookBase.Call("statusEffect.apply." + character.SpeciesName, this, deltaTime, entity, targets, worldPosition));
if (result.Bool())
return;
@@ -1218,7 +1218,7 @@ namespace Barotrauma
foreach (string luaHooks in luaHook)
{
var result = new LuaResult(GameMain.LuaCs.hook.Call(luaHooks, this, deltaTime, entity, targets, worldPosition));
var result = new LuaResult(GameMain.LuaCs.HookBase.Call(luaHooks, this, deltaTime, entity, targets, worldPosition));
if (result.Bool())
return;