155 lines
3.3 KiB
C#
155 lines
3.3 KiB
C#
using System;
|
|
using System.Reflection;
|
|
using HarmonyLib;
|
|
|
|
namespace IdleSpectator;
|
|
|
|
/// <summary>Reflection helpers for internal WorldBox status APIs used by the harness.</summary>
|
|
internal static class StatusGameApi
|
|
{
|
|
private static MethodInfo _addStatusString;
|
|
private static MethodInfo _finishAll;
|
|
|
|
public static bool TryAddStatus(Actor actor, string statusId, float overrideTimer = 0f, bool colorEffect = true)
|
|
{
|
|
if (actor == null || string.IsNullOrEmpty(statusId))
|
|
{
|
|
return false;
|
|
}
|
|
|
|
try
|
|
{
|
|
if (_addStatusString == null)
|
|
{
|
|
_addStatusString = AccessTools.Method(
|
|
typeof(BaseSimObject),
|
|
"addStatusEffect",
|
|
new[] { typeof(string), typeof(float), typeof(bool) });
|
|
}
|
|
|
|
if (_addStatusString == null)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
object result = _addStatusString.Invoke(
|
|
actor,
|
|
new object[] { statusId.Trim(), overrideTimer, colorEffect });
|
|
return result is bool ok && ok;
|
|
}
|
|
catch
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public static bool TryFinishStatus(Actor actor, string statusId)
|
|
{
|
|
if (actor == null || string.IsNullOrEmpty(statusId))
|
|
{
|
|
return false;
|
|
}
|
|
|
|
try
|
|
{
|
|
actor.finishStatusEffect(statusId.Trim());
|
|
return true;
|
|
}
|
|
catch
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public static bool TryFinishAll(Actor actor)
|
|
{
|
|
if (actor == null)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
try
|
|
{
|
|
if (_finishAll == null)
|
|
{
|
|
_finishAll = AccessTools.Method(typeof(BaseSimObject), "finishAllStatusEffects");
|
|
}
|
|
|
|
if (_finishAll == null)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
_finishAll.Invoke(actor, null);
|
|
return true;
|
|
}
|
|
catch
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public static bool HasStatus(Actor actor, string statusId)
|
|
{
|
|
if (actor == null || string.IsNullOrEmpty(statusId))
|
|
{
|
|
return false;
|
|
}
|
|
|
|
try
|
|
{
|
|
var ids = actor.getStatusesIds();
|
|
if (ids == null)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
foreach (string id in ids)
|
|
{
|
|
if (!string.IsNullOrEmpty(id)
|
|
&& id.Equals(statusId, StringComparison.OrdinalIgnoreCase))
|
|
{
|
|
return true;
|
|
}
|
|
}
|
|
}
|
|
catch
|
|
{
|
|
// ignore
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
public static int CountActiveStatuses(Actor actor)
|
|
{
|
|
if (actor == null)
|
|
{
|
|
return 0;
|
|
}
|
|
|
|
try
|
|
{
|
|
int n = 0;
|
|
var ids = actor.getStatusesIds();
|
|
if (ids == null)
|
|
{
|
|
return 0;
|
|
}
|
|
|
|
foreach (string id in ids)
|
|
{
|
|
if (!string.IsNullOrEmpty(id))
|
|
{
|
|
n++;
|
|
}
|
|
}
|
|
|
|
return n;
|
|
}
|
|
catch
|
|
{
|
|
return 0;
|
|
}
|
|
}
|
|
}
|