- Refactored the EventService interfaces and event system, incomplete.

This commit is contained in:
MapleWheels
2026-02-01 18:48:40 -05:00
committed by Maplewheels
parent cb171d350d
commit bb8869268e
4 changed files with 31 additions and 20 deletions

View File

@@ -24,9 +24,7 @@ public interface IEvent<out T> : IEvent where T : IEvent<T>
{
static virtual T GetLuaRunner(IDictionary<string, LuaCsFunc> luaFunc)
{
// throw error if not overriden since we don't have 'static abstract'.
// Implementers must provide the runner.
throw new NotImplementedException();
throw new InvalidOperationException($"Lua runners forbidden for {typeof(T).Name}");
}
}
@@ -179,7 +177,7 @@ public interface IEventPluginInitialize : IEvent<IEventPluginInitialize>
static IEventPluginInitialize IEvent<IEventPluginInitialize>.GetLuaRunner(IDictionary<string, LuaCsFunc> luaFunc) => new
{
IsLuaRunner = Return<bool>.Arguments(() => true),
OnInitialize = ReturnVoid.Arguments(() => luaFunc[nameof(Initialize)]())
Initialize = ReturnVoid.Arguments(() => luaFunc[nameof(Initialize)]())
}.ActLike<IEventPluginInitialize>();
}

View File

@@ -4,14 +4,16 @@ namespace Barotrauma.LuaCs.Services.Compatibility;
public interface ILuaCsHook : ILuaCsShim
{
// Event Services
[Obsolete("ACsMod is deprecated. Use ILuaEventService.Add() instead.")]
void Add(string eventName, string identifier, LuaCsFunc callback, ACsMod mod = null);
void Add(string eventName, string identifier, LuaCsFunc callback);
[Obsolete("ACsMod is deprecated. Use ILuaEventService.Add() instead.")]
void Add(string eventName, LuaCsFunc callback, ACsMod mod = null);
bool Exists(string eventName, string identifier);
[Obsolete("Only Lua subscribers will receive events from call. Use ILuaEventService.Add() instead.")]
T Call<T>(string eventName, params object[] args);
void Add(string eventName, LuaCsFunc callback);
// Does anyone use this? TODO: Analyze old Lua mods for usage scenarios.
//bool Exists(string eventName, string identifier);
object Call(string eventName, params object[] args);
// Hook/Method Patching
string Patch(string identifier, string className, string methodName, string[] parameterTypes, LuaCsPatchFunc patch, EventService.HookMethodType hookType = EventService.HookMethodType.Before);
string Patch(string identifier, string className, string methodName, LuaCsPatchFunc patch, EventService.HookMethodType hookType = EventService.HookMethodType.Before);
string Patch(string className, string methodName, string[] parameterTypes, LuaCsPatchFunc patch, EventService.HookMethodType hookType = EventService.HookMethodType.Before);

View File

@@ -7,7 +7,13 @@ namespace Barotrauma.LuaCs.Services.Safe;
public interface ILuaSafeEventService : ILuaService, ILuaCsHook
{
void Subscribe(string interfaceName, string identifier, IDictionary<string, LuaCsFunc> callbacks);
/// <summary>
/// Subscribes lua scripts via <see cref="ImpromptuInterface"/> for the given <see cref="IEvent{T}"/> interface.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="identifier"></param>
/// <param name="callbacks">A 'method name'=='signature action' dictionary matching the interface method list.</param>
void Subscribe<T>(string identifier, IDictionary<string, LuaCsFunc> callbacks);
/// <summary>
/// Removes a subscriber from an event that subscribed under the given identifier.
/// </summary>
@@ -17,14 +23,24 @@ public interface ILuaSafeEventService : ILuaService, ILuaCsHook
/// <summary>
/// Send an event to all subscribers to an interface.
/// </summary>
/// <param name="interfaceName">Name of the interface (must be registered with Lua).</param>
/// <param name="runner">Execution runner, the subscriber is provided as the first argument in the lua runner.</param>
/// <typeparam name="T">Interface type.</typeparam>
/// <param name="subscriberRunner">Execution runner, the subscriber is provided as the first argument in the lua runner.</param>
/// <returns></returns>
void PublishLuaEvent(string interfaceName, LuaCsFunc runner);
void PublishLuaEvent<T>(LuaCsFunc subscriberRunner);
/// <summary>
/// Defines the target method name for legacy <see cref="ILuaCsHook.Add(string, LuaCsFunc)"/> to target on new <see cref="IEvent{T}"/>
/// interfaces.
/// </summary>
/// <param name="luaEventName">The <see cref="ILuaCsHook.Add(string, LuaCsFunc)"/> legacy event name.</param>
/// <param name="targetMethod">.</param>
/// <typeparam name="T">The event interface type.</typeparam>
/// <returns>Operation success.</returns>
/// <exception cref="ArgumentNullException">The <see cref="luaEventName"/> is <b>null or empty.</b></exception>
public FluentResults.Result RegisterLuaEventAlias<T>(string luaEventName, string targetMethod) where T : IEvent<T>;
}
public interface ILuaEventService : ILuaSafeEventService
{
public FluentResults.Result RegisterSafeEvent<T>() where T : IEvent<T>;
public FluentResults.Result UnregisterSafeEvent<T>() where T : IEvent<T>;
}

View File

@@ -8,11 +8,6 @@ namespace Barotrauma.LuaCs.Services;
public interface IEventService : IReusableService, ILuaEventService
{
FluentResults.Result SetLegacyLuaRunnerFactory<T>(Func<LuaCsFunc, T> runnerFactory) where T : IEvent<T>;
void RemoveLegacyLuaRunnerFactory<T>() where T : IEvent<T>;
void SetAliasToEvent<T>(string alias) where T : IEvent<T>;
void RemoveEventAlias(string alias);
void RemoveAllEventAliases<T>() where T : IEvent<T>;
/// <summary>
///
/// </summary>