diff --git a/Barotrauma/BarotraumaClient/ClientSource/GameMain.cs b/Barotrauma/BarotraumaClient/ClientSource/GameMain.cs index 89fa222fc..005ae4e48 100644 --- a/Barotrauma/BarotraumaClient/ClientSource/GameMain.cs +++ b/Barotrauma/BarotraumaClient/ClientSource/GameMain.cs @@ -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; diff --git a/Barotrauma/BarotraumaClient/ClientSource/Networking/GameClient.cs b/Barotrauma/BarotraumaClient/ClientSource/Networking/GameClient.cs index faa5195b5..059f2dafe 100644 --- a/Barotrauma/BarotraumaClient/ClientSource/Networking/GameClient.cs +++ b/Barotrauma/BarotraumaClient/ClientSource/Networking/GameClient.cs @@ -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; diff --git a/Barotrauma/BarotraumaClient/ClientSource/PlayerInput.cs b/Barotrauma/BarotraumaClient/ClientSource/PlayerInput.cs index b406f1514..ec3d4b665 100644 --- a/Barotrauma/BarotraumaClient/ClientSource/PlayerInput.cs +++ b/Barotrauma/BarotraumaClient/ClientSource/PlayerInput.cs @@ -562,7 +562,7 @@ namespace Barotrauma allowInput = true; } - GameMain.LuaCs.hook.Call("keyUpdate", deltaTime); + GameMain.LuaCs.HookBase.Call("keyUpdate", deltaTime); oldMouseState = mouseState; mouseState = latestMouseState; diff --git a/Barotrauma/BarotraumaServer/ServerSource/Characters/Character.cs b/Barotrauma/BarotraumaServer/ServerSource/Characters/Character.cs index 44a605fe8..efd219c94 100644 --- a/Barotrauma/BarotraumaServer/ServerSource/Characters/Character.cs +++ b/Barotrauma/BarotraumaServer/ServerSource/Characters/Character.cs @@ -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)) { diff --git a/Barotrauma/BarotraumaServer/ServerSource/DebugConsole.cs b/Barotrauma/BarotraumaServer/ServerSource/DebugConsole.cs index 112a503d2..e9717c40e 100644 --- a/Barotrauma/BarotraumaServer/ServerSource/DebugConsole.cs +++ b/Barotrauma/BarotraumaServer/ServerSource/DebugConsole.cs @@ -1293,7 +1293,7 @@ namespace Barotrauma } catch (Exception e) { - GameMain.LuaCs.HandleLuaException(e); + GameMain.LuaCs.HandleException(e); return; } diff --git a/Barotrauma/BarotraumaServer/ServerSource/GameMain.cs b/Barotrauma/BarotraumaServer/ServerSource/GameMain.cs index fca11ec4f..fe8576eff 100644 --- a/Barotrauma/BarotraumaServer/ServerSource/GameMain.cs +++ b/Barotrauma/BarotraumaServer/ServerSource/GameMain.cs @@ -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(); diff --git a/Barotrauma/BarotraumaServer/ServerSource/Networking/ChatMessage.cs b/Barotrauma/BarotraumaServer/ServerSource/Networking/ChatMessage.cs index 715457b15..ee6ce4b71 100644 --- a/Barotrauma/BarotraumaServer/ServerSource/Networking/ChatMessage.cs +++ b/Barotrauma/BarotraumaServer/ServerSource/Networking/ChatMessage.cs @@ -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()) { diff --git a/Barotrauma/BarotraumaServer/ServerSource/Networking/GameServer.cs b/Barotrauma/BarotraumaServer/ServerSource/Networking/GameServer.cs index 265edaa96..f1f4d19a7 100644 --- a/Barotrauma/BarotraumaServer/ServerSource/Networking/GameServer.cs +++ b/Barotrauma/BarotraumaServer/ServerSource/Networking/GameServer.cs @@ -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 characterInfos = new List(); 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); diff --git a/Barotrauma/BarotraumaServer/ServerSource/Networking/Primitives/Peers/Server/LidgrenServerPeer.cs b/Barotrauma/BarotraumaServer/ServerSource/Networking/Primitives/Peers/Server/LidgrenServerPeer.cs index 75a9ac7d6..bded9b025 100644 --- a/Barotrauma/BarotraumaServer/ServerSource/Networking/Primitives/Peers/Server/LidgrenServerPeer.cs +++ b/Barotrauma/BarotraumaServer/ServerSource/Networking/Primitives/Peers/Server/LidgrenServerPeer.cs @@ -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; diff --git a/Barotrauma/BarotraumaServer/ServerSource/Networking/Primitives/Peers/Server/ServerPeer.cs b/Barotrauma/BarotraumaServer/ServerSource/Networking/Primitives/Peers/Server/ServerPeer.cs index 9b1f66b55..4bd6632c7 100644 --- a/Barotrauma/BarotraumaServer/ServerSource/Networking/Primitives/Peers/Server/ServerPeer.cs +++ b/Barotrauma/BarotraumaServer/ServerSource/Networking/Primitives/Peers/Server/ServerPeer.cs @@ -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; diff --git a/Barotrauma/BarotraumaServer/ServerSource/Networking/Voip/VoipServer.cs b/Barotrauma/BarotraumaServer/ServerSource/Networking/Voip/VoipServer.cs index e6def7658..a8a16e6be 100644 --- a/Barotrauma/BarotraumaServer/ServerSource/Networking/Voip/VoipServer.cs +++ b/Barotrauma/BarotraumaServer/ServerSource/Networking/Voip/VoipServer.cs @@ -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()) diff --git a/Barotrauma/BarotraumaServer/ServerSource/Traitors/TraitorMission.cs b/Barotrauma/BarotraumaServer/ServerSource/Traitors/TraitorMission.cs index ffb5a2aac..e5119eb24 100644 --- a/Barotrauma/BarotraumaServer/ServerSource/Traitors/TraitorMission.cs +++ b/Barotrauma/BarotraumaServer/ServerSource/Traitors/TraitorMission.cs @@ -92,7 +92,7 @@ namespace Barotrauma var traitorCandidates = new List>(); 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))); diff --git a/Barotrauma/BarotraumaShared/SharedSource/Characters/AI/Objectives/AIObjectiveManager.cs b/Barotrauma/BarotraumaShared/SharedSource/Characters/AI/Objectives/AIObjectiveManager.cs index 067371328..9ce521ab5 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/Characters/AI/Objectives/AIObjectiveManager.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/Characters/AI/Objectives/AIObjectiveManager.cs @@ -80,7 +80,7 @@ namespace Barotrauma public void AddObjective(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; diff --git a/Barotrauma/BarotraumaShared/SharedSource/Characters/Animation/HumanoidAnimController.cs b/Barotrauma/BarotraumaShared/SharedSource/Characters/Animation/HumanoidAnimController.cs index d1312c280..fb8857c70 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/Characters/Animation/HumanoidAnimController.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/Characters/Animation/HumanoidAnimController.cs @@ -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); } } } diff --git a/Barotrauma/BarotraumaShared/SharedSource/Characters/Animation/Ragdoll.cs b/Barotrauma/BarotraumaShared/SharedSource/Characters/Animation/Ragdoll.cs index 56eed8f70..5a6b9560c 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/Characters/Animation/Ragdoll.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/Characters/Animation/Ragdoll.cs @@ -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()) { diff --git a/Barotrauma/BarotraumaShared/SharedSource/Characters/Character.cs b/Barotrauma/BarotraumaShared/SharedSource/Characters/Character.cs index cc4f9979c..5c39e15da 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/Characters/Character.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/Characters/Character.cs @@ -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; } diff --git a/Barotrauma/BarotraumaShared/SharedSource/Characters/Health/Afflictions/Affliction.cs b/Barotrauma/BarotraumaShared/SharedSource/Characters/Health/Afflictions/Affliction.cs index ac261a2ec..7f7af6897 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/Characters/Health/Afflictions/Affliction.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/Characters/Health/Afflictions/Affliction.cs @@ -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 } diff --git a/Barotrauma/BarotraumaShared/SharedSource/Characters/Health/Afflictions/AfflictionHusk.cs b/Barotrauma/BarotraumaShared/SharedSource/Characters/Health/Afflictions/AfflictionHusk.cs index 8faa09785..e5a1e7eeb 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/Characters/Health/Afflictions/AfflictionHusk.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/Characters/Health/Afflictions/AfflictionHusk.cs @@ -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) diff --git a/Barotrauma/BarotraumaShared/SharedSource/Characters/Health/CharacterHealth.cs b/Barotrauma/BarotraumaShared/SharedSource/Characters/Health/CharacterHealth.cs index 70542fb17..039be9c21 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/Characters/Health/CharacterHealth.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/Characters/Health/CharacterHealth.cs @@ -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; diff --git a/Barotrauma/BarotraumaShared/SharedSource/Cs/ACsMod.cs b/Barotrauma/BarotraumaShared/SharedSource/Cs/ACsMod.cs new file mode 100644 index 000000000..4a3598f55 --- /dev/null +++ b/Barotrauma/BarotraumaShared/SharedSource/Cs/ACsMod.cs @@ -0,0 +1,41 @@ +using System; +using System.Collections.Generic; +using System.Reflection; + +namespace Barotrauma +{ + public abstract class ACsMod : IDisposable + { + private static List mods = new List(); + public static List 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() { } + } +} \ No newline at end of file diff --git a/Barotrauma/BarotraumaShared/SharedSource/Cs/CsHook.cs b/Barotrauma/BarotraumaShared/SharedSource/Cs/CsHook.cs new file mode 100644 index 000000000..077e2a1f0 --- /dev/null +++ b/Barotrauma/BarotraumaShared/SharedSource/Cs/CsHook.cs @@ -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); + + } + } + +} \ No newline at end of file diff --git a/Barotrauma/BarotraumaShared/SharedSource/Net/NetScriptFilter.cs b/Barotrauma/BarotraumaShared/SharedSource/Cs/CsScriptFilter.cs similarity index 97% rename from Barotrauma/BarotraumaShared/SharedSource/Net/NetScriptFilter.cs rename to Barotrauma/BarotraumaShared/SharedSource/Cs/CsScriptFilter.cs index 4d2b46b29..00aa08d9d 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/Net/NetScriptFilter.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/Cs/CsScriptFilter.cs @@ -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; diff --git a/Barotrauma/BarotraumaShared/SharedSource/Net/NetScriptLoader.cs b/Barotrauma/BarotraumaShared/SharedSource/Cs/CsScriptLoader.cs similarity index 93% rename from Barotrauma/BarotraumaShared/SharedSource/Net/NetScriptLoader.cs rename to Barotrauma/BarotraumaShared/SharedSource/Cs/CsScriptLoader.cs index c4cc9c872..69fc30410 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/Net/NetScriptLoader.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/Cs/CsScriptLoader.cs @@ -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 defaultReferences; private List 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."); } diff --git a/Barotrauma/BarotraumaShared/SharedSource/GameSession/GameSession.cs b/Barotrauma/BarotraumaShared/SharedSource/GameSession/GameSession.cs index ef998a8f7..984f28a04 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/GameSession/GameSession.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/GameSession/GameSession.cs @@ -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(); diff --git a/Barotrauma/BarotraumaShared/SharedSource/Items/Components/Holdable/MeleeWeapon.cs b/Barotrauma/BarotraumaShared/SharedSource/Items/Components/Holdable/MeleeWeapon.cs index 95f76db14..ec176e893 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/Items/Components/Holdable/MeleeWeapon.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/Items/Components/Holdable/MeleeWeapon.cs @@ -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); diff --git a/Barotrauma/BarotraumaShared/SharedSource/Items/Components/Signal/Connection.cs b/Barotrauma/BarotraumaShared/SharedSource/Items/Components/Signal/Connection.cs index cc899a070..ad2fb88ab 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/Items/Components/Signal/Connection.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/Items/Components/Signal/Connection.cs @@ -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) { diff --git a/Barotrauma/BarotraumaShared/SharedSource/Items/Components/Signal/WifiComponent.cs b/Barotrauma/BarotraumaShared/SharedSource/Items/Components/Signal/WifiComponent.cs index d2beb3877..4abeebc88 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/Items/Components/Signal/WifiComponent.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/Items/Components/Signal/WifiComponent.cs @@ -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; diff --git a/Barotrauma/BarotraumaShared/SharedSource/Items/Inventory.cs b/Barotrauma/BarotraumaShared/SharedSource/Items/Inventory.cs index a44bc7ed0..ed2a7a75f 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/Items/Inventory.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/Items/Inventory.cs @@ -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(); diff --git a/Barotrauma/BarotraumaShared/SharedSource/Items/Item.cs b/Barotrauma/BarotraumaShared/SharedSource/Items/Item.cs index 3fa54b557..bda8f340a 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/Items/Item.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/Items/Item.cs @@ -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(); diff --git a/Barotrauma/BarotraumaShared/SharedSource/Lua/LuaClasses/LuaClasses.cs b/Barotrauma/BarotraumaShared/SharedSource/Lua/LuaClasses/LuaClasses.cs index 5c345de02..f68699b0a 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/Lua/LuaClasses/LuaClasses.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/Lua/LuaClasses/LuaClasses.cs @@ -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()); } } diff --git a/Barotrauma/BarotraumaShared/SharedSource/Lua/LuaClasses/LuaGame.cs b/Barotrauma/BarotraumaShared/SharedSource/Lua/LuaClasses/LuaGame.cs index 576523ac0..c13a332ed 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/Lua/LuaClasses/LuaGame.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/Lua/LuaClasses/LuaGame.cs @@ -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 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 diff --git a/Barotrauma/BarotraumaShared/SharedSource/Lua/LuaClasses/LuaHook.cs b/Barotrauma/BarotraumaShared/SharedSource/Lua/LuaClasses/LuaHook.cs new file mode 100644 index 000000000..118f0718e --- /dev/null +++ b/Barotrauma/BarotraumaShared/SharedSource/Lua/LuaClasses/LuaHook.cs @@ -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); + } + } + +} \ No newline at end of file diff --git a/Barotrauma/BarotraumaShared/SharedSource/Lua/LuaClasses/LuaUserData.cs b/Barotrauma/BarotraumaShared/SharedSource/Lua/LuaClasses/LuaUserData.cs index 7449f5736..48393ab6b 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/Lua/LuaClasses/LuaUserData.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/Lua/LuaClasses/LuaUserData.cs @@ -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; } diff --git a/Barotrauma/BarotraumaShared/SharedSource/Lua/LuaCustomConverters.cs b/Barotrauma/BarotraumaShared/SharedSource/Lua/LuaCustomConverters.cs index 6ccd076d3..adddb77db 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/Lua/LuaCustomConverters.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/Lua/LuaCustomConverters.cs @@ -29,7 +29,7 @@ namespace Barotrauma RegisterAction(); { - 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(Func 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), v => { var function = v.Function; - return (Action)(p => GameMain.LuaCs.CallFunction(function, p)); + return (Action)(p => GameMain.LuaCs.CallLuaFunction(function, p)); }); } @@ -131,7 +131,7 @@ namespace Barotrauma Script.GlobalOptions.CustomConverters.SetScriptToClrCustomConversion(DataType.Function, typeof(Action), v => { var function = v.Function; - return (Action)((a1, a2) => GameMain.LuaCs.CallFunction(function, a1, a2)); + return (Action)((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)); }); } diff --git a/Barotrauma/BarotraumaShared/SharedSource/Lua/LuaScriptLoader.cs b/Barotrauma/BarotraumaShared/SharedSource/Lua/LuaScriptLoader.cs index b75657617..cff04ecdb 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/Lua/LuaScriptLoader.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/Lua/LuaScriptLoader.cs @@ -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); + } + } } \ No newline at end of file diff --git a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/LuaCsHook.cs b/Barotrauma/BarotraumaShared/SharedSource/LuaCs/LuaCsHook.cs index 5cdc4a28e..68f324723 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/LuaCsHook.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/LuaCs/LuaCsHook.cs @@ -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>(); - luaHookPostfixMethods = new Dictionary>(); - csHookPrefixMethods = new Dictionary>(); - csHookPostfixMethods = new Dictionary>(); + _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> luaHookFunctions = new Dictionary>(); - private Dictionary> csHookFunctions = new Dictionary>(); + private Dictionary> luaHookFunctions; + private Dictionary> csHookFunctions; - private static Dictionary> luaHookPrefixMethods; - private static Dictionary> luaHookPostfixMethods; - private static Dictionary> csHookPrefixMethods; - private static Dictionary> csHookPostfixMethods; + private Dictionary> luaHookPrefixMethods; + private Dictionary> luaHookPostfixMethods; + private Dictionary> csHookPrefixMethods; + private Dictionary> csHookPostfixMethods; - private Queue> queuedFunctionCalls = new Queue>(); + private Queue> queuedFunctionCalls; - public enum HookMethodType - { - Before, After + private LuaCsHook() { + luaHookFunctions = new Dictionary>(); + csHookFunctions = new Dictionary>(); + + luaHookPrefixMethods = new Dictionary>(); + luaHookPostfixMethods = new Dictionary>(); + csHookPrefixMethods = new Dictionary>(); + csHookPostfixMethods = new Dictionary>(); + + queuedFunctionCalls = new Queue>(); } + 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(); + 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> luaMethods; + Dictionary> 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(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(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()); + + 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()); + 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(); + 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}'"); + } } } diff --git a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/LuaCsSetup.cs b/Barotrauma/BarotraumaShared/SharedSource/LuaCs/LuaCsSetup.cs index be286db38..9b76d80b7 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/LuaCsSetup.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/LuaCs/LuaCsSetup.cs @@ -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(); + //UserData.RegisterType(); + UserData.RegisterType(); UserData.RegisterType(); UserData.RegisterType(); UserData.RegisterType(); @@ -324,7 +347,8 @@ namespace Barotrauma lua.Globals["LuaUserData"] = UserData.CreateStatic(); 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(); 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(); - } - } diff --git a/Barotrauma/BarotraumaShared/SharedSource/Map/Gap.cs b/Barotrauma/BarotraumaShared/SharedSource/Map/Gap.cs index 4c2b6a060..447d75b29 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/Map/Gap.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/Map/Gap.cs @@ -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; diff --git a/Barotrauma/BarotraumaShared/SharedSource/Net/ANetMod.cs b/Barotrauma/BarotraumaShared/SharedSource/Net/ANetMod.cs deleted file mode 100644 index 3074206ca..000000000 --- a/Barotrauma/BarotraumaShared/SharedSource/Net/ANetMod.cs +++ /dev/null @@ -1,26 +0,0 @@ -using System; -using System.Collections.Generic; - -namespace Barotrauma -{ - public abstract class ANetMod : IDisposable - { - private static List mods = new List(); - public static List 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() { } - } -} \ No newline at end of file diff --git a/Barotrauma/BarotraumaShared/SharedSource/StatusEffects/StatusEffect.cs b/Barotrauma/BarotraumaShared/SharedSource/StatusEffects/StatusEffect.cs index f95c79ef3..47f0da02c 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/StatusEffects/StatusEffect.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/StatusEffects/StatusEffect.cs @@ -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;