From 5747d896ebc5b0fb451ffd4239b64109ff97cf5b Mon Sep 17 00:00:00 2001 From: MapleWheels Date: Thu, 12 Feb 2026 14:53:33 -0500 Subject: [PATCH] - Removed ImpromptuInterfaces --- Barotrauma/BarotraumaShared/Luatrauma.props | 1 - .../SharedSource/LuaCs/IEvents.cs | 210 +++++++++++++----- .../LuaCs/_Services/EventService.cs | 22 +- .../_Services/HarmonyEventPatchesService.cs | 3 + .../_Services/PluginManagementService.cs | 1 - .../_Services/_Interfaces/IEventService.cs | 4 +- .../LuaCs/_Services/_Lua/ILuaEventService.cs | 6 +- 7 files changed, 165 insertions(+), 82 deletions(-) diff --git a/Barotrauma/BarotraumaShared/Luatrauma.props b/Barotrauma/BarotraumaShared/Luatrauma.props index a93e33094..cbcdb4bcc 100644 --- a/Barotrauma/BarotraumaShared/Luatrauma.props +++ b/Barotrauma/BarotraumaShared/Luatrauma.props @@ -8,7 +8,6 @@ - diff --git a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/IEvents.cs b/Barotrauma/BarotraumaShared/SharedSource/LuaCs/IEvents.cs index 85d670054..afffcaa7b 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/IEvents.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/LuaCs/IEvents.cs @@ -1,12 +1,8 @@ -using System.Runtime.CompilerServices; -using System; +using System; using System.Collections.Generic; using System.Reflection; using Barotrauma.LuaCs.Data; -using Barotrauma.LuaCs; using Barotrauma.Networking; -using Dynamitey; -using ImpromptuInterface; namespace Barotrauma.LuaCs.Events; @@ -19,9 +15,16 @@ namespace Barotrauma.LuaCs.Events; public interface IEvent { bool IsLuaRunner() => false; + + public abstract class LuaWrapperBase : IEvent + { + protected readonly IDictionary LuaFuncs; + protected LuaWrapperBase(IDictionary luaFuncs) => LuaFuncs = luaFuncs; + public bool IsLuaRunner() => true; + } } -public interface IEvent : IEvent where T : IEvent +public interface IEvent : IEvent where T : class, IEvent { static virtual T GetLuaRunner(IDictionary luaFunc) { @@ -73,33 +76,64 @@ internal interface IEventSettingInstanceLifetime : IEvent { void OnAfflictionUpdate(Affliction affliction, CharacterHealth characterHealth, Limb targetLimb, float deltaTime); - static IEventAfflictionUpdate IEvent.GetLuaRunner(IDictionary luaFunc) => new + + static IEventAfflictionUpdate IEvent.GetLuaRunner(IDictionary luaFunc) => + new LuaWrapper(luaFunc); + + public sealed class LuaWrapper : LuaWrapperBase, IEventAfflictionUpdate { - IsLuaRunner = Return.Arguments(() => true), - OnAfflictionUpdate = ReturnVoid.Arguments((Affliction affliction, CharacterHealth characterHealth, Limb targetLimb, float deltaTime) => luaFunc[nameof(OnAfflictionUpdate)](affliction, characterHealth, targetLimb, deltaTime)) - }.ActLike(); + public LuaWrapper(IDictionary luaFuncs) : base(luaFuncs) + { + } + + public void OnAfflictionUpdate(Affliction affliction, CharacterHealth characterHealth, Limb targetLimb, float deltaTime) + { + LuaFuncs[nameof(OnAfflictionUpdate)](affliction, characterHealth, targetLimb, deltaTime); + } + } } internal interface IEventGiveCharacterJobItems : IEvent { void OnGiveCharacterJobItems(Character character, WayPoint spawnPoint, bool isPvPMode); - static IEventGiveCharacterJobItems IEvent.GetLuaRunner(IDictionary luaFunc) => new + + static IEventGiveCharacterJobItems IEvent.GetLuaRunner( + IDictionary luaFunc) => new LuaWrapper(luaFunc); + + public sealed class LuaWrapper : LuaWrapperBase, IEventGiveCharacterJobItems { - IsLuaRunner = Return.Arguments(() => true), - OnGiveCharacterJobItems = ReturnVoid.Arguments((Character character, WayPoint spawnPoint, bool isPvPMode) => luaFunc[nameof(OnGiveCharacterJobItems)](character, spawnPoint, isPvPMode)) - }.ActLike(); + public LuaWrapper(IDictionary luaFuncs) : base(luaFuncs) + { + } + + public void OnGiveCharacterJobItems(Character character, WayPoint spawnPoint, bool isPvPMode) + { + LuaFuncs[nameof(OnGiveCharacterJobItems)](character, spawnPoint, isPvPMode); + } + } } internal interface IEventCharacterCreated : IEvent { void OnCharacterCreated(Character character); - static IEventCharacterCreated IEvent.GetLuaRunner(IDictionary luaFunc) => new + + static IEventCharacterCreated IEvent.GetLuaRunner(IDictionary luaFunc) + => new LuaWrapper(luaFunc); + + public sealed class LuaWrapper : LuaWrapperBase, IEventCharacterCreated { - IsLuaRunner = Return.Arguments(() => true), - OnCharacterCreated = ReturnVoid.Arguments((Character character) => luaFunc[nameof(OnCharacterCreated)](character)) - }.ActLike(); + public LuaWrapper(IDictionary luaFuncs) : base(luaFuncs) + { + } + + public void OnCharacterCreated(Character character) + { + LuaFuncs[nameof(OnCharacterCreated)](character); + } + } } + /* internal interface IEventHumanCPRFailed : IEvent { @@ -126,11 +160,21 @@ internal interface IEventHumanCPRSuccess : IEvent public interface IEventKeyUpdate : IEvent { void OnKeyUpdate(double deltaTime); - static IEventKeyUpdate IEvent.GetLuaRunner(IDictionary luaFunc) => new + + static IEventKeyUpdate IEvent.GetLuaRunner(IDictionary luaFunc) + => new LuaWrapper(luaFunc); + + public sealed class LuaWrapper : LuaWrapperBase, IEventKeyUpdate { - IsLuaRunner = Return.Arguments(() => true), - OnKeyUpdate = ReturnVoid.Arguments((double deltaTime) => luaFunc[nameof(OnKeyUpdate)](deltaTime)) - }.ActLike(); + public LuaWrapper(IDictionary luaFuncs) : base(luaFuncs) + { + } + + public void OnKeyUpdate(double deltaTime) + { + LuaFuncs[nameof(OnKeyUpdate)](deltaTime); + } + } } /// @@ -139,11 +183,21 @@ public interface IEventKeyUpdate : IEvent public interface IEventRoundStarting : IEvent { void OnRoundStarting(); - static IEventRoundStarting IEvent.GetLuaRunner(IDictionary luaFunc) => new + + static IEventRoundStarting IEvent.GetLuaRunner(IDictionary luaFunc) + => new LuaWrapper(luaFunc); + + public sealed class LuaWrapper : LuaWrapperBase, IEventRoundStarting { - IsLuaRunner = Return.Arguments(() => true), - OnRoundStarting = ReturnVoid.Arguments(() => luaFunc[nameof(OnRoundStarting)]()) - }.ActLike(); + public LuaWrapper(IDictionary luaFuncs) : base(luaFuncs) + { + } + + public void OnRoundStarting() + { + LuaFuncs[nameof(OnRoundStarting)](); + } + } } /// @@ -152,11 +206,21 @@ public interface IEventRoundStarting : IEvent public interface IEventRoundStarted : IEvent { void OnRoundStart(); - static IEventRoundStarted IEvent.GetLuaRunner(IDictionary luaFunc) => new + + static IEventRoundStarted IEvent.GetLuaRunner(IDictionary luaFunc) + => new LuaWrapper(luaFunc); + + public sealed class LuaWrapper : LuaWrapperBase, IEventRoundStarted { - IsLuaRunner = Return.Arguments(() => true), - OnRoundStart = ReturnVoid.Arguments(() => luaFunc[nameof(OnRoundStart)]()) - }.ActLike(); + public LuaWrapper(IDictionary luaFuncs) : base(luaFuncs) + { + } + + public void OnRoundStart() + { + LuaFuncs[nameof(OnRoundStart)](); + } + } } /// @@ -165,11 +229,20 @@ public interface IEventRoundStarted : IEvent public interface IEventUpdate : IEvent { void OnUpdate(double fixedDeltaTime); - static IEventUpdate IEvent.GetLuaRunner(IDictionary luaFunc) => new + static IEventUpdate IEvent.GetLuaRunner(IDictionary luaFunc) + => new LuaWrapper(luaFunc); + + public sealed class LuaWrapper : LuaWrapperBase, IEventUpdate { - IsLuaRunner = Return.Arguments(() => true), - OnUpdate = ReturnVoid.Arguments((fixedDeltaTime) => luaFunc[nameof(OnUpdate)](fixedDeltaTime)) - }.ActLike(); + public LuaWrapper(IDictionary luaFuncs) : base(luaFuncs) + { + } + + public void OnUpdate(double deltaTime) + { + LuaFuncs[nameof(OnUpdate)](deltaTime); + } + } } /// @@ -178,11 +251,21 @@ public interface IEventUpdate : IEvent public interface IEventDrawUpdate : IEvent { void OnDrawUpdate(double deltaTime); - static IEventDrawUpdate IEvent.GetLuaRunner(IDictionary luaFunc) => new + + static IEventDrawUpdate IEvent.GetLuaRunner(IDictionary luaFunc) + => new LuaWrapper(luaFunc); + + public sealed class LuaWrapper : LuaWrapperBase, IEventDrawUpdate { - IsLuaRunner = Return.Arguments(() => true), - OnDrawUpdate = ReturnVoid.Arguments((deltaTime) => luaFunc[nameof(OnDrawUpdate)](deltaTime)) - }.ActLike(); + public LuaWrapper(IDictionary luaFuncs) : base(luaFuncs) + { + } + + public void OnDrawUpdate(double deltaTime) + { + LuaFuncs[nameof(OnDrawUpdate)](deltaTime); + } + } } #endregion @@ -206,11 +289,21 @@ interface IEventClientConnected : IEvent /// /// The connecting client. void OnClientConnected(Client client); - static IEventClientConnected IEvent.GetLuaRunner(IDictionary luaFunc) => new + + static IEventClientConnected IEvent.GetLuaRunner(IDictionary luaFunc) + => new LuaWrapper(luaFunc); + + public sealed class LuaWrapper : LuaWrapperBase, IEventClientConnected { - IsLuaRunner = Return.Arguments(() => true), - OnClientConnected = ReturnVoid.Arguments((client) => luaFunc[nameof(OnClientConnected)](client)) - }.ActLike(); + public LuaWrapper(IDictionary luaFuncs) : base(luaFuncs) + { + } + + public void OnClientConnected(Client client) + { + LuaFuncs[nameof(OnClientConnected)](client); + } + } } #endif @@ -230,11 +323,21 @@ public interface IEventServerRawNetMessageReceived : IEvent { void OnServerConnected(); - static IEventServerConnected IEvent.GetLuaRunner(IDictionary luaFunc) => new + + static IEventServerConnected IEvent.GetLuaRunner(IDictionary luaFunc) + => new LuaWrapper(luaFunc); + + public sealed class LuaWrapper : LuaWrapperBase, IEventServerConnected { - IsLuaRunner = Return.Arguments(() => true), - OnServerConnected = ReturnVoid.Arguments(() => luaFunc[nameof(OnServerConnected)]()) - }.ActLike(); + public LuaWrapper(IDictionary luaFuncs) : base(luaFuncs) + { + } + + public void OnServerConnected() + { + LuaFuncs[nameof(OnServerConnected)](); + } + } } #endif #endregion @@ -249,11 +352,6 @@ public interface IEventServerConnected : IEvent public interface IEventPluginInitialize : IEvent { void Initialize(); - static IEventPluginInitialize IEvent.GetLuaRunner(IDictionary luaFunc) => new - { - IsLuaRunner = Return.Arguments(() => true), - Initialize = ReturnVoid.Arguments(() => luaFunc[nameof(Initialize)]()) - }.ActLike(); } /// @@ -262,11 +360,6 @@ public interface IEventPluginInitialize : IEvent public interface IEventPluginLoadCompleted : IEvent { void OnLoadCompleted(); - static IEventPluginLoadCompleted IEvent.GetLuaRunner(IDictionary luaFunc) => new - { - IsLuaRunner = Return.Arguments(() => true), - OnLoadCompleted = ReturnVoid.Arguments(() => luaFunc[nameof(OnLoadCompleted)]()) - }.ActLike(); } /// @@ -276,11 +369,6 @@ public interface IEventPluginLoadCompleted : IEvent public interface IEventPluginPreInitialize : IEvent { void PreInitPatching(); - static IEventPluginPreInitialize IEvent.GetLuaRunner(IDictionary luaFunc) => new - { - IsLuaRunner = Return.Arguments(() => true), - OnPreInitialize = ReturnVoid.Arguments(() => luaFunc[nameof(PreInitPatching)]()) - }.ActLike(); } /// diff --git a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/_Services/EventService.cs b/Barotrauma/BarotraumaShared/SharedSource/LuaCs/_Services/EventService.cs index 25716cf3d..224cc8128 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/_Services/EventService.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/LuaCs/_Services/EventService.cs @@ -1,19 +1,13 @@ -using Barotrauma.Extensions; -using Barotrauma.LuaCs.Events; -using Barotrauma.LuaCs.Compatibility; +using Barotrauma.LuaCs.Events; using FluentResults; -using FluentResults.LuaCs; -using HarmonyLib; using Microsoft.Toolkit.Diagnostics; using MoonSharp.Interpreter; using OneOf; -using RestSharp; using System; using System.Collections.Concurrent; using System.Collections.Generic; -using System.Collections.Immutable; -using System.Linq; using System.Reflection; +using System.Runtime.CompilerServices; namespace Barotrauma.LuaCs; @@ -189,7 +183,7 @@ public partial class EventService : IEventService return returnValue; } - public void Subscribe(string identifier, IDictionary callbacks) where T : IEvent + public void Subscribe(string identifier, IDictionary callbacks) where T : class, IEvent { Guard.IsNotNullOrWhiteSpace(identifier, nameof(identifier)); Guard.IsNotNull(callbacks, nameof(callbacks)); @@ -215,12 +209,12 @@ public partial class EventService : IEventService evtSubscribers.TryRemove(identifier, out _); } - public void PublishLuaEvent(LuaCsFunc subscriberRunner) where T : IEvent + public void PublishLuaEvent(LuaCsFunc subscriberRunner) where T : class, IEvent { this.PublishEvent(sub => subscriberRunner(sub)); } - public FluentResults.Result RegisterLuaEventAlias(string luaEventName, string targetMethod) where T : IEvent + public FluentResults.Result RegisterLuaEventAlias(string luaEventName, string targetMethod) where T : class, IEvent { Guard.IsNotNullOrWhiteSpace(luaEventName, nameof(luaEventName)); Guard.IsNotNullOrWhiteSpace(targetMethod, nameof(targetMethod)); @@ -281,7 +275,7 @@ public partial class EventService : IEventService evtSubscribers.TryRemove(subscriber, out _); } - public void ClearAllEventSubscribers() where T : IEvent + public void ClearAllEventSubscribers() where T : class, IEvent { using var lck = _operationsLock.AcquireWriterLock().ConfigureAwait(false).GetAwaiter().GetResult(); IService.CheckDisposed(this); @@ -295,7 +289,7 @@ public partial class EventService : IEventService _subscribers.Clear(); } - public FluentResults.Result PublishEvent(Action action) where T : IEvent + public FluentResults.Result PublishEvent(Action action) where T : class, IEvent { Guard.IsNotNull(action, nameof(action)); using var lck = _operationsLock.AcquireReaderLock().ConfigureAwait(false).GetAwaiter().GetResult(); @@ -312,7 +306,7 @@ public partial class EventService : IEventService { try { - action.Invoke((T)sub.Value); + action.Invoke(Unsafe.As(sub.Value)); } catch (Exception e) { diff --git a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/_Services/HarmonyEventPatchesService.cs b/Barotrauma/BarotraumaShared/SharedSource/LuaCs/_Services/HarmonyEventPatchesService.cs index 916858880..e3715af09 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/_Services/HarmonyEventPatchesService.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/LuaCs/_Services/HarmonyEventPatchesService.cs @@ -25,7 +25,10 @@ internal class HarmonyEventPatchesService : IService Harmony.PatchAll(typeof(HarmonyEventPatchesService)); } + // TODO: This causes like hell in Debug. +#if !DEBUG [HarmonyPatch(typeof(CoroutineManager), nameof(CoroutineManager.Update)), HarmonyPostfix] +#endif public static void CoroutineManager_Update_Post() { _eventService.PublishEvent(x => x.OnUpdate(Timing.TotalTime)); diff --git a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/_Services/PluginManagementService.cs b/Barotrauma/BarotraumaShared/SharedSource/LuaCs/_Services/PluginManagementService.cs index ecac7daaf..ad32a8d6f 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/_Services/PluginManagementService.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/LuaCs/_Services/PluginManagementService.cs @@ -16,7 +16,6 @@ using Barotrauma.LuaCs.Data; using Barotrauma.LuaCs.Events; using FluentResults; using FluentResults.LuaCs; -using ImpromptuInterface.Build; using LightInject; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp; diff --git a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/_Services/_Interfaces/IEventService.cs b/Barotrauma/BarotraumaShared/SharedSource/LuaCs/_Services/_Interfaces/IEventService.cs index eca7648ae..39c7ad347 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/_Services/_Interfaces/IEventService.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/LuaCs/_Services/_Interfaces/IEventService.cs @@ -25,7 +25,7 @@ public interface IEventService : IReusableService, ILuaEventService /// Clears all subscribers for a given event type and removes any registration to the type. /// /// The event type. - void ClearAllEventSubscribers() where T : IEvent; + void ClearAllEventSubscribers() where T : class, IEvent; /// /// Clears all subscribers lists. /// @@ -36,5 +36,5 @@ public interface IEventService : IReusableService, ILuaEventService /// /// /// - FluentResults.Result PublishEvent(Action action) where T : IEvent; + FluentResults.Result PublishEvent(Action action) where T : class, IEvent; } diff --git a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/_Services/_Lua/ILuaEventService.cs b/Barotrauma/BarotraumaShared/SharedSource/LuaCs/_Services/_Lua/ILuaEventService.cs index f075a5d78..ae789afe5 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/_Services/_Lua/ILuaEventService.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/LuaCs/_Services/_Lua/ILuaEventService.cs @@ -13,7 +13,7 @@ public interface ILuaSafeEventService : ILuaService, ILuaCsHook /// /// /// A 'method name'=='signature action' dictionary matching the interface method list. - void Subscribe(string identifier, IDictionary callbacks) where T : IEvent; + void Subscribe(string identifier, IDictionary callbacks) where T : class, IEvent; /// /// Removes a subscriber from an event that subscribed under the given identifier. /// @@ -26,7 +26,7 @@ public interface ILuaSafeEventService : ILuaService, ILuaCsHook /// Interface type. /// Execution runner, the subscriber is provided as the first argument in the lua runner. /// - void PublishLuaEvent(LuaCsFunc subscriberRunner) where T : IEvent; + void PublishLuaEvent(LuaCsFunc subscriberRunner) where T : class, IEvent; /// /// Defines the target method name for legacy to target on new @@ -37,7 +37,7 @@ public interface ILuaSafeEventService : ILuaService, ILuaCsHook /// The event interface type. /// Operation success. /// The is null or empty. - public FluentResults.Result RegisterLuaEventAlias(string luaEventName, string targetMethod) where T : IEvent; + public FluentResults.Result RegisterLuaEventAlias(string luaEventName, string targetMethod) where T : class, IEvent; } public interface ILuaEventService : ILuaSafeEventService