worldbox-observer-mod/IdleSpectator/HappinessGameApi.cs

73 lines
1.8 KiB
C#

using System;
using System.Reflection;
using HarmonyLib;
namespace IdleSpectator;
/// <summary>Reflection helpers for happiness library / changeHappiness harness paths.</summary>
internal static class HappinessGameApi
{
private static MethodInfo _changeHappiness;
public static bool TryChangeHappiness(Actor actor, string effectId, int bonus = 0)
{
if (actor == null || string.IsNullOrEmpty(effectId))
{
return false;
}
try
{
if (_changeHappiness == null)
{
_changeHappiness = AccessTools.Method(
typeof(Actor),
"changeHappiness",
new[] { typeof(string), typeof(int) });
}
if (_changeHappiness == null)
{
// Public method should be callable directly.
return actor.changeHappiness(effectId.Trim(), bonus);
}
object result = _changeHappiness.Invoke(actor, new object[] { effectId.Trim(), bonus });
return result is bool ok && ok;
}
catch
{
try
{
return actor.changeHappiness(effectId.Trim(), bonus);
}
catch
{
return false;
}
}
}
public static HappinessAsset TryGetAsset(string effectId)
{
return ActivityAssetCatalog.TryGetHappinessAsset(effectId);
}
public static int ResolveAmount(string effectId, int bonus = 0)
{
HappinessAsset asset = TryGetAsset(effectId);
if (asset == null)
{
return bonus;
}
try
{
return bonus + asset.value;
}
catch
{
return bonus;
}
}
}