using System;
using System.Threading;
using System.Security.Cryptography;
using System.Collections.Concurrent;
using HarmonyLib;
using Sandbox.RenderDirect.ActorComponents;
namespace XFE.SeAgent.Plugin.Performance
{
// A stopped, unconstrained spinner has no speed update, target-angle event or
// rotation delta. The stock path still writes an identical transform and dirties
// the actor hierarchy/culling tree. Keep all moving and target-driven paths stock.
internal static class IdleRotation
{
private static readonly AccessTools.FieldRef<MySpinupAnimator, float> Increment =
AccessTools.FieldRefAccess<MySpinupAnimator, float>("m_speedIncrement");
private sealed class Counter { public long Value; }
[ThreadStatic] private static Counter threadCounter;
private static readonly ConcurrentBag<Counter> counters = new ConcurrentBag<Counter>();
// Avoid introducing a contended atomic write into the very parallel path
// being optimized. Only the owning thread writes each counter.
public static long Skipped
{
get { long sum = 0; foreach (var counter in counters) sum += Interlocked.Read(ref counter.Value); return sum; }
}
public static void ValidateEngine()
{
// Prefix reasoning depends on both original bodies. Reject unknown
// releases instead of silently assuming their side effects are unchanged.
Verify(typeof(MyRotationAnimator), "F1AF3896C695D1419DD97D5FE3E80CDDA174BF4C4C0D19A94A0619D90FD2C02B");
// 1.211.017 adds speed-reversal handling behind the unchanged nonzero
// increment guard. Stopped spinners still have no base-method work;
// the added helper changes the UpdateSpeed metadata token in this IL.
Verify(typeof(MySpinupAnimator), "61BD03E1130912A4B50CCE41A2F627A490E031258621F0AB79AEC601F84EE691",
"2698CEB5231E9D43BE8F772577415044CD36A80619A2EB517E2B029F4DC0394F");
}
private static void Verify(Type type, params string[] expected)
{
var body = AccessTools.DeclaredMethod(type, "OnUpdateBeforeDraw")?.GetMethodBody()?.GetILAsByteArray();
if (body == null) throw new NotSupportedException("Missing animation method body: " + type.Name);
using (var hash = SHA256.Create())
if (Array.IndexOf(expected, BitConverter.ToString(hash.ComputeHash(body)).Replace("-", "")) < 0)
throw new NotSupportedException("Unverified animation engine version: " + type.Name);
}
public static bool Prefix(MyRotationAnimator __instance)
{
if (__instance.CurrentSpeed != 0f || Increment(__instance) != 0f || !float.IsNaN(__instance.DesiredState)) return true;
if (__instance.Owner == null || (uint)__instance.RotationAxisOriginal > 3) return true;
var axis = __instance.CustomAxis;
if (float.IsNaN(axis.X) || float.IsNaN(axis.Y) || float.IsNaN(axis.Z)
|| float.IsInfinity(axis.X) || float.IsInfinity(axis.Y) || float.IsInfinity(axis.Z)) return true;
if (threadCounter == null) { threadCounter = new Counter(); counters.Add(threadCounter); }
threadCounter.Value++;
return false;
}
}
}
using System;
using System.Threading;
using System.Security.Cryptography;
using System.Collections.Concurrent;
using HarmonyLib;
using Sandbox.RenderDirect.ActorComponents;
namespace XFE.SeAgent.Plugin.Performance
{
// A stopped, unconstrained spinner has no speed update, target-angle event or
// rotation delta. The stock path still writes an identical transform and dirties
// the actor hierarchy/culling tree. Keep all moving and target-driven paths stock.
internal static class IdleRotation
{
private static readonly AccessTools.FieldRef<MySpinupAnimator, float> Increment =
AccessTools.FieldRefAccess<MySpinupAnimator, float>("m_speedIncrement");
private sealed class Counter { public long Value; }
[ThreadStatic] private static Counter threadCounter;
private static readonly ConcurrentBag<Counter> counters = new ConcurrentBag<Counter>();
// Avoid introducing a contended atomic write into the very parallel path
// being optimized. Only the owning thread writes each counter.
public static long Skipped
{
get { long sum = 0; foreach (var counter in counters) sum += Interlocked.Read(ref counter.Value); return sum; }
}
public static void ValidateEngine()
{
// Prefix reasoning depends on both original bodies. Reject unknown
// releases instead of silently assuming their side effects are unchanged.
Verify(typeof(MyRotationAnimator), "F1AF3896C695D1419DD97D5FE3E80CDDA174BF4C4C0D19A94A0619D90FD2C02B");
// 1.211.017 adds speed-reversal handling behind the unchanged nonzero
// increment guard. Stopped spinners still have no base-method work;
// the added helper changes the UpdateSpeed metadata token in this IL.
Verify(typeof(MySpinupAnimator), "61BD03E1130912A4B50CCE41A2F627A490E031258621F0AB79AEC601F84EE691",
"2698CEB5231E9D43BE8F772577415044CD36A80619A2EB517E2B029F4DC0394F");
}
private static void Verify(Type type, params string[] expected)
{
var body = AccessTools.DeclaredMethod(type, "OnUpdateBeforeDraw")?.GetMethodBody()?.GetILAsByteArray();
if (body == null) throw new NotSupportedException("Missing animation method body: " + type.Name);
using (var hash = SHA256.Create())
if (Array.IndexOf(expected, BitConverter.ToString(hash.ComputeHash(body)).Replace("-", "")) < 0)
throw new NotSupportedException("Unverified animation engine version: " + type.Name);
}
public static bool Prefix(MyRotationAnimator __instance)
{
if (__instance.CurrentSpeed != 0f || Increment(__instance) != 0f || !float.IsNaN(__instance.DesiredState)) return true;
if (__instance.Owner == null || (uint)__instance.RotationAxisOriginal > 3) return true;
var axis = __instance.CustomAxis;
if (float.IsNaN(axis.X) || float.IsNaN(axis.Y) || float.IsNaN(axis.Z)
|| float.IsInfinity(axis.X) || float.IsInfinity(axis.Y) || float.IsInfinity(axis.Z)) return true;
if (threadCounter == null) { threadCounter = new Counter(); counters.Add(threadCounter); }
threadCounter.Value++;
return false;
}
}
}