Fix EventService.Call not implemented correctly
This commit is contained in:
committed by
Maplewheels
parent
4cf4b1604b
commit
cf251451ed
@@ -5,6 +5,7 @@ using FluentResults;
|
|||||||
using FluentResults.LuaCs;
|
using FluentResults.LuaCs;
|
||||||
using HarmonyLib;
|
using HarmonyLib;
|
||||||
using Microsoft.Toolkit.Diagnostics;
|
using Microsoft.Toolkit.Diagnostics;
|
||||||
|
using MoonSharp.Interpreter;
|
||||||
using OneOf;
|
using OneOf;
|
||||||
using RestSharp;
|
using RestSharp;
|
||||||
using System;
|
using System;
|
||||||
@@ -132,6 +133,11 @@ public partial class EventService : IEventService
|
|||||||
}
|
}
|
||||||
|
|
||||||
public object Call(string eventName, params object[] args)
|
public object Call(string eventName, params object[] args)
|
||||||
|
{
|
||||||
|
return Call<object>(eventName, args);
|
||||||
|
}
|
||||||
|
|
||||||
|
public T Call<T>(string eventName, params object[] args)
|
||||||
{
|
{
|
||||||
Guard.IsNotNullOrWhiteSpace(eventName, nameof(eventName));
|
Guard.IsNotNullOrWhiteSpace(eventName, nameof(eventName));
|
||||||
using var lck = _operationsLock.AcquireReaderLock().ConfigureAwait(false).GetAwaiter().GetResult();
|
using var lck = _operationsLock.AcquireReaderLock().ConfigureAwait(false).GetAwaiter().GetResult();
|
||||||
@@ -140,16 +146,36 @@ public partial class EventService : IEventService
|
|||||||
if (!_luaLegacyEventsSubscribers.TryGetValue(eventName, out var eventSubscribers)
|
if (!_luaLegacyEventsSubscribers.TryGetValue(eventName, out var eventSubscribers)
|
||||||
|| eventSubscribers.IsEmpty)
|
|| eventSubscribers.IsEmpty)
|
||||||
{
|
{
|
||||||
return null;
|
return default;
|
||||||
}
|
}
|
||||||
|
|
||||||
object returnValue = null;
|
T returnValue = default;
|
||||||
|
|
||||||
foreach (var subscriber in eventSubscribers)
|
foreach (var subscriber in eventSubscribers)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
returnValue = subscriber.Value.Invoke(args);
|
object result = subscriber.Value.Invoke(args);
|
||||||
|
if (result is DynValue luaResult)
|
||||||
|
{
|
||||||
|
if (luaResult.Type == DataType.Tuple)
|
||||||
|
{
|
||||||
|
bool replaceNil = luaResult.Tuple.Length > 1 && luaResult.Tuple[1].CastToBool();
|
||||||
|
|
||||||
|
if (!luaResult.Tuple[0].IsNil() || replaceNil)
|
||||||
|
{
|
||||||
|
returnValue = luaResult.ToObject<T>();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (!luaResult.IsNil())
|
||||||
|
{
|
||||||
|
returnValue = luaResult.ToObject<T>();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
returnValue = (T)result;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
@@ -163,11 +189,6 @@ public partial class EventService : IEventService
|
|||||||
return returnValue;
|
return returnValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
public T Call<T>(string eventName, params object[] args)
|
|
||||||
{
|
|
||||||
return (T)Call(eventName, args);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Subscribe<T>(string identifier, IDictionary<string, LuaCsFunc> callbacks) where T : IEvent<T>
|
public void Subscribe<T>(string identifier, IDictionary<string, LuaCsFunc> callbacks) where T : IEvent<T>
|
||||||
{
|
{
|
||||||
Guard.IsNotNullOrWhiteSpace(identifier, nameof(identifier));
|
Guard.IsNotNullOrWhiteSpace(identifier, nameof(identifier));
|
||||||
|
|||||||
Reference in New Issue
Block a user