XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet

XFE.SpaceEngineers.AgentBridge

【SpaceEngineer】AI调试插件

公开
关注 0 Fork 0 Star 0
UTF-8
using System;
using System.Security.Cryptography;
using HarmonyLib;
using VRage.Game.Components;
using VRageMath;

namespace XFE.SeAgent.Plugin.Performance
{
    // Keep recursive notifications and iteration order on the engine's calling
    // thread. Concrete reader enumerators avoid two interface boxes at every
    // level of a moving ship's hierarchy. No pose, callback or update is skipped.
    internal static class HierarchyUpdates
    {
        private static readonly AccessTools.FieldRef<MyPositionComponent, MyHierarchyComponentBase> Hierarchy =
            AccessTools.FieldRefAccess<MyPositionComponent, MyHierarchyComponentBase>("m_hierarchy");

        public static void ValidateEngine()
        {
            Verify("UpdateChildren", "6D88A7405C665579E18E3E5220E9CA78553A8F3C75C33B69D97F74BA8F73B77D");
            Verify("<UpdateChildren>g__UpdateChildren|14_0", "DA494F182A63E46D34E41C422A1CFD0EB0BCB9693AF8406B08A40B287EFA0FAB");
        }
        private static void Verify(string name,string expected)
        {
            var body=AccessTools.DeclaredMethod(typeof(MyPositionComponent),name)?.GetMethodBody()?.GetILAsByteArray();
            if(body==null) throw new NotSupportedException("Missing hierarchy method: "+name);
            using(var hash=SHA256.Create())
                if(BitConverter.ToString(hash.ComputeHash(body)).Replace("-","")!=expected)
                    throw new NotSupportedException("Unverified hierarchy engine version: "+name);
        }

        public static bool Prefix(MyPositionComponent __instance, object source, bool forceUpdateAllChildren)
        {
            MatrixD parent = __instance.WorldMatrixRef;
            var hierarchy = Hierarchy(__instance);
            if (forceUpdateAllChildren)
            {
                foreach (var child in hierarchy.Children)
                    child.Entity.PositionComp.UpdateWorldMatrix(ref parent, source, true, true);
            }
            else
            {
                foreach (var child in hierarchy.ChildrenNeedingWorldMatrix)
                    child.Entity.PositionComp.UpdateWorldMatrix(ref parent, source, true, false);
            }
            return false;
        }
    }
}