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

XFE.SpaceEngineers.AgentBridge

【SpaceEngineer】AI调试插件

公开
关注 0 Fork 0 Star 0
UTF-8
using System;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Remoting.Messaging;
using System.Runtime.Remoting.Proxies;
using HarmonyLib;
using Sandbox.RenderDirect.ActorComponents;
using VRage.Render.Scene;
using VRageMath;
using XFE.SeAgent.Plugin.Performance;

// Runs the user's installed animation implementation against an isolated actor spy.
// It never starts/attaches to the game or reads/writes a world; no game code is shipped.
internal static class Program
{
    private static int Main(string[] args)
    {
        string bin = args.Length == 1 ? Path.GetFullPath(args[0]) : throw new ArgumentException("Supply the installed game Bin64 directory.");
        AppDomain.CurrentDomain.AssemblyResolve += (_, e) => {
            string file = Path.Combine(bin, new AssemblyName(e.Name).Name + ".dll");
            return File.Exists(file) ? Assembly.LoadFrom(file) : null;
        };
        try { Run(); return 0; } catch (Exception e) { Console.Error.WriteLine(e); return 1; }
    }
    [MethodImpl(MethodImplOptions.NoInlining)]
    private static void Run()
    {
        using (var hash = System.Security.Cryptography.SHA256.Create())
            foreach (var type in new[] { typeof(MyRotationAnimator), typeof(MySpinupAnimator) })
                Console.WriteLine("IL " + type.Name + " " + BitConverter.ToString(hash.ComputeHash(AccessTools.DeclaredMethod(type, "OnUpdateBeforeDraw").GetMethodBody().GetILAsByteArray())).Replace("-", ""));
        IdleRotation.ValidateEngine();
        var cases = new[] {
            new Case("stopped free spinner", 0, 0, float.NaN, true),
            new Case("moving free spinner", 2, 0, float.NaN, false),
            new Case("starting from rest", 0, 1, float.NaN, false),
            new Case("decelerating", 1, -1, float.NaN, false),
            new Case("target angle while stopped", 0, 0, .5f, false),
            new Case("moving target angle", 1, 0, .5f, false)
        };
        var original = new ActorSpy[cases.Length];
        for (int i = 0; i < cases.Length; i++) original[i] = Execute(cases[i]);
        var patch = new Harmony("xfe.tests.idle-rotation");
        patch.Patch(AccessTools.Method(typeof(MyRotationAnimator), "OnUpdateBeforeDraw"), prefix: new HarmonyMethod(typeof(IdleRotation), "Prefix"));
        try {
            for (int i = 0; i < cases.Length; i++) {
                var changed = Execute(cases[i]); var before = original[i];
                if (before.Transform != changed.Transform || before.Reached != changed.Reached || before.Changed != changed.Changed
                    || before.LastAngle != changed.LastAngle || before.FinalSpeed != changed.FinalSpeed)
                    throw new Exception("Animation behavior changed: " + cases[i].Name);
                if (cases[i].Skips ? changed.Writes != 0 || before.Writes != 8 : changed.Writes != before.Writes)
                    throw new Exception("Unexpected matrix-write count: " + cases[i].Name);
                Console.WriteLine("PASS " + cases[i].Name + ": pose, events, speed match; writes " + before.Writes + " -> " + changed.Writes);
            }
        } finally { patch.UnpatchAll(patch.Id); }
        var priorMode = System.Runtime.GCSettings.LatencyMode;
        using (var tuning = new GcLatencyTuning())
        {
            try
            {
                tuning.Set(true); tuning.Update(false);
                if (System.Runtime.GCSettings.LatencyMode != priorMode) throw new Exception("GC changed outside gameplay");
                tuning.Update(true);
                if (tuning.Applied && System.Runtime.GCSettings.LatencyMode != System.Runtime.GCLatencyMode.SustainedLowLatency) throw new Exception("GC mode not applied");
                tuning.Update(false);
                if (System.Runtime.GCSettings.LatencyMode != priorMode) throw new Exception("GC mode not restored after gameplay");
                Console.WriteLine("PASS GC latency restores previous mode outside gameplay");
            }
            finally { System.Runtime.GCSettings.LatencyMode = priorMode; }
        }
        HierarchyChecks();
        AffinityChecks();
        ParallelBatchChecks();

    }
    private static void ParallelBatchChecks()
    {
        ParallelTasks.Parallel.Scheduler = new ParallelTasks.PrioritizedScheduler(4, true, null);
        foreach (int count in new[] { 0, 1, 7, 8, 9, 10003 })
        {
            var items = new System.Collections.Generic.List<int>();
            var visits = new int[count];
            for (int i = 0; i < count; i++) items.Add(i);
            EntityBatching.Run(items, i => System.Threading.Interlocked.Increment(ref visits[i]), ParallelTasks.WorkPriority.VeryHigh, null, true);
            for (int i = 0; i < count; i++) if (visits[i] != 1) throw new Exception("Parallel callback lost or repeated");
            Console.WriteLine("PASS entity batch joins all " + count + " callbacks exactly once");
        }
        int active = 0, peak = 0;
        var limited = new int[32];
        EntityBatching.Run(limited, _ => {
            int concurrent = System.Threading.Interlocked.Increment(ref active);
            int before;
            do { before = peak; if (before >= concurrent) break; }
            while (System.Threading.Interlocked.CompareExchange(ref peak, concurrent, before) != before);
            System.Threading.Thread.Sleep(1);
            System.Threading.Interlocked.Decrement(ref active);
        }, ParallelTasks.WorkPriority.Normal, ParallelTasks.Parallel.DefaultOptions.WithMaxThreads(1), true);
        if (peak != 1 || active != 0) throw new Exception("Entity batching ignored explicit worker cap");
        int nested = 0;
        EntityBatching.Run(new[] { 1 }, _ => EntityBatching.Run(new[] { 1, 2, 3 }, __ => System.Threading.Interlocked.Increment(ref nested), ParallelTasks.WorkPriority.Normal, null, true), ParallelTasks.WorkPriority.Normal, null, true);
        if (nested != 3) throw new Exception("Nested entity batch reused active storage");
        Console.WriteLine("PASS entity batch explicit worker limit and reentrant snapshot lifetime");
        var actors = new System.Collections.Generic.List<VRage.Render.Scene.Components.MyActorComponent>();
        for (int i = 0; i < 129; i++) actors.Add(new VRage.Render.Scene.Components.MyActorComponent());
        foreach (int cap in new[] { 1, 2 })
        {
            active = peak = 0; int calls = 0;
            ActorBatching.Workers = cap; ActorBatching.ChunkSize = 16;
            ActorBatching.Run(actors, _ => {
                int concurrent = System.Threading.Interlocked.Increment(ref active), before;
                do { before = peak; if (before >= concurrent) break; }
                while (System.Threading.Interlocked.CompareExchange(ref peak, concurrent, before) != before);
                System.Threading.Thread.Sleep(1);System.Threading.Interlocked.Increment(ref calls);
                System.Threading.Interlocked.Decrement(ref active);
            }, ParallelTasks.WorkPriority.Normal, ParallelTasks.Parallel.DefaultOptions.WithMaxThreads(int.MaxValue), true);
            if (peak < 1 || peak > cap || active != 0 || calls != actors.Count) throw new Exception("Actor worker cap or join violated");
            Console.WriteLine("PASS actor worker cap " + cap + " with " + calls + " joined callbacks, peak " + peak);
        }
        ActorBatching.Workers = 0;ActorBatching.ChunkSize = 256;EntityBatching.ChunkSize = 128;
        var manyActors = new VRage.Render.Scene.Components.MyActorComponent[16384];
        var manyEntities = new int[16384];
        Action<VRage.Render.Scene.Components.MyActorComponent> visitActor = _ => { };
        Action<int> visitEntity = _ => { };
        long actorBytes = CallerAllocations(() => ActorBatching.Run(manyActors, visitActor, ParallelTasks.WorkPriority.Normal, ParallelTasks.Parallel.DefaultOptions.WithMaxThreads(int.MaxValue), true));
        long entityBytes = CallerAllocations(() => EntityBatching.Run(manyEntities, visitEntity, ParallelTasks.WorkPriority.Normal, null, true));
        Console.WriteLine("Batch snapshot caller allocations over 32 warmed passes: actors=" + actorBytes + ", entities=" + entityBytes);
        // On .NET Framework, List.AddRange(ICollection) allocates an intermediate
        // array even when the destination has capacity. 32 passes would allocate
        // 4 / 2 MiB here. Allow task bookkeeping, but reject per-pass snapshots.
        if (actorBytes >= 1024 * 1024 || entityBytes >= 1024 * 1024) throw new Exception("Batch snapshot still allocates per-item storage each pass");
        Console.WriteLine("PASS warmed actor/entity snapshots avoid per-pass item arrays");
        ActorBatching.ChunkSize = 32;EntityBatching.ChunkSize = 8;
    }
    private static long CallerAllocations(Action run)
    {
        for (int i = 0; i < 8; i++) run();
        long start = GC.GetAllocatedBytesForCurrentThread();
        for (int i = 0; i < 32; i++) run();
        return GC.GetAllocatedBytesForCurrentThread() - start;
    }
    private sealed class PositionSpy : VRage.Game.Components.MyPositionComponent
    {
        public readonly System.Collections.Generic.List<string> Calls=new System.Collections.Generic.List<string>();
        public int Id; public object Source;
        public void InvokeChildren(object source,bool force) { base.UpdateChildren(source,force); }
        public override void UpdateWorldMatrix(ref MatrixD matrix,object source=null,bool updateChildren=true,bool forceUpdateAllChildren=false)
        {
            Calls.Add(Id+":"+matrix.M41+":"+updateChildren+":"+forceUpdateAllChildren+":"+ReferenceEquals(source,Source));
            matrix.M41+=Id; // The stock method passes the same local copy through the entire enumeration.
        }
    }
    private static void HierarchyChecks()
    {
        HierarchyUpdates.ValidateEngine();
        var method=AccessTools.DeclaredMethod(typeof(VRage.Game.Components.MyPositionComponent),"UpdateChildren");
        using(var hash=System.Security.Cryptography.SHA256.Create())
            Console.WriteLine("IL Hierarchy "+BitConverter.ToString(hash.ComputeHash(method.GetMethodBody().GetILAsByteArray())).Replace("-",""));
        foreach(var helper in AccessTools.GetDeclaredMethods(typeof(VRage.Game.Components.MyPositionComponent)))
            if(helper.Name.Contains("<UpdateChildren>")) using(var hash=System.Security.Cryptography.SHA256.Create())
                Console.WriteLine("IL Hierarchy helper "+helper.Name+" "+BitConverter.ToString(hash.ComputeHash(helper.GetMethodBody().GetILAsByteArray())).Replace("-",""));
        var parent=new PositionSpy();var hierarchy=new VRage.Game.Components.MyHierarchyComponentBase();
        AccessTools.Field(typeof(VRage.Game.Components.MyPositionComponent),"m_hierarchy").SetValue(parent,hierarchy);
        var children=(System.Collections.Generic.List<VRage.Game.Components.MyHierarchyComponentBase>)AccessTools.Field(hierarchy.GetType(),"m_children").GetValue(hierarchy);
        var needing=(System.Collections.Generic.HashSet<VRage.Game.Components.MyHierarchyComponentBase>)AccessTools.Field(hierarchy.GetType(),"m_childrenNeedingWorldMatrix").GetValue(hierarchy);
        var spies=new System.Collections.Generic.List<PositionSpy>();object source=new object();
        for(int i=0;i<3;i++) {
            var spy=new PositionSpy{Id=i+1,Source=source};
            var entity=(VRage.ModAPI.IMyEntity)new EntitySpy(spy).GetTransparentProxy();
            var container=new VRage.Game.Components.MyEntityComponentContainer(entity);
            var child=new VRage.Game.Components.MyHierarchyComponentBase();
            AccessTools.Field(typeof(VRage.Game.Components.MyComponentBase),"m_container").SetValue(child,container);
            children.Add(child);if(i!=1) needing.Add(child);spies.Add(spy);
        }
        foreach(bool force in new[]{false,true}) {
            parent.InvokeChildren(source,force);
            string expected=string.Join("|",spies.ConvertAll(s=>string.Join(",",s.Calls)));
            foreach(var spy in spies) spy.Calls.Clear();
            HierarchyUpdates.Prefix(parent,source,force);
            string actual=string.Join("|",spies.ConvertAll(s=>string.Join(",",s.Calls)));
            if(actual!=expected) throw new Exception("Hierarchy callbacks changed: "+expected+" != "+actual);
            foreach(var spy in spies) spy.Calls.Clear();
            Console.WriteLine("PASS hierarchy "+(force?"all":"requested")+" children: order, matrix, source and flags match");
        }
        children.Clear();needing.Clear();
        parent.InvokeChildren(source,false);HierarchyUpdates.Prefix(parent,source,false);
        long start=GC.GetAllocatedBytesForCurrentThread();
        for(int i=0;i<10000;i++) parent.InvokeChildren(source,false);
        long originalBytes=GC.GetAllocatedBytesForCurrentThread()-start;
        start=GC.GetAllocatedBytesForCurrentThread();
        for(int i=0;i<10000;i++) HierarchyUpdates.Prefix(parent,source,false);
        long optimizedBytes=GC.GetAllocatedBytesForCurrentThread()-start;
        if(optimizedBytes!=0 || originalBytes<=0) throw new Exception("Hierarchy enumeration allocation regression");
        Console.WriteLine("PASS 10000 empty hierarchy updates allocate "+originalBytes+" -> "+optimizedBytes+" bytes");
    }
    private sealed class EntitySpy : RealProxy
    {
        private readonly PositionSpy position;
        public EntitySpy(PositionSpy value):base(typeof(VRage.ModAPI.IMyEntity)) {position=value;}
        public override IMessage Invoke(IMessage message)
        {
            var call=(IMethodCallMessage)message;
            if(call.MethodName!="get_PositionComp") throw new Exception("Unexpected entity access: "+call.MethodName);
            return new ReturnMessage(position,null,0,call.LogicalCallContext,call);
        }
    }
    private sealed class Case
    {
        public readonly string Name; public readonly float Speed, Increment, Target; public readonly bool Skips;
        public Case(string name,float speed,float increment,float target,bool skips) { Name=name;Speed=speed;Increment=increment;Target=target;Skips=skips; }
    }
    [System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential)]
    private struct TestGroupAffinity {public UIntPtr Mask;public ushort Group,R0,R1,R2;}
    [System.Runtime.InteropServices.DllImport("kernel32.dll")]
    private static extern bool GetThreadGroupAffinity(IntPtr handle,out TestGroupAffinity affinity);
    private static ulong CurrentThreadMask()
    {
        if(!GetThreadGroupAffinity(new IntPtr(-2),out var value)) throw new Exception("Cannot inspect test thread affinity");
        return value.Mask.ToUInt64();
    }
    private static void AffinityChecks()
    {
        using(var tuning=new CacheCoreAffinity())
        using(var pinned=new System.Threading.ManualResetEvent(false))
        using(var released=new System.Threading.ManualResetEvent(false)) {
            string process=tuning.CurrentMask;ulong main=CurrentThreadMask(),before=0,after=0,restored=0;Exception workerError=null;
            try {tuning.Set(true,true,true);} catch(NotSupportedException e){Console.WriteLine("SKIP heterogeneous L3 affinity: "+e.Message);return;}
            var worker=new System.Threading.Thread(()=> {
                try {before=CurrentThreadMask();tuning.MarkRenderThread();after=CurrentThreadMask();pinned.Set();released.WaitOne();restored=CurrentThreadMask();}
                catch(Exception e){workerError=e;pinned.Set();}
            });
            try {
                worker.Start();if(!pinned.WaitOne(5000)) throw new Exception("Affinity worker timed out");
                if(workerError!=null) throw workerError;
                if(tuning.CurrentMask!=process || after!=CurrentThreadMask() || after==before || tuning.ThreadError!=null)
                    throw new Exception("Main/render affinity did not preserve process-wide worker availability");
                tuning.Set(false);released.Set();worker.Join();
                if(workerError!=null) throw workerError;
                if(restored!=before || CurrentThreadMask()!=main || tuning.CurrentMask!=process)
                    throw new Exception("Affinity restoration failed");
                Console.WriteLine("PASS main/render threads pin together; other cores remain available; both thread masks restore");
            } finally {released.Set();if(worker.IsAlive) worker.Join(5000);}
        }
    }
    private static ActorSpy Execute(Case test)
    {
        var spy = new ActorSpy(); var actor = (IMyActor)spy.GetTransparentProxy();
        var animator = new MyRotationAnimator(); animator.Construct(); animator.Assign(actor);
        animator.CurrentSpeed = test.Speed;
        AccessTools.Field(typeof(MySpinupAnimator), "m_speedIncrement").SetValue(animator, test.Increment);
        AccessTools.Field(typeof(MySpinupAnimator), "m_targetSpeed").SetValue(animator, test.Increment > 0 ? 2f : 0f);
        AccessTools.Field(typeof(MyRotationAnimator), "m_desiredState").SetValue(animator, test.Target);
        for (int i = 0; i < 8; i++) animator.OnUpdateBeforeDraw();
        spy.FinalSpeed = animator.CurrentSpeed; return spy;
    }
    private sealed class ActorSpy : RealProxy
    {
        public Matrix Transform = Matrix.CreateTranslation(2,3,4);
        public int Writes, Reached, Changed; public float LastAngle, FinalSpeed;
        public ActorSpy() : base(typeof(IMyActor)) { }
        public override IMessage Invoke(IMessage message)
        {
            var call=(IMethodCallMessage)message; object value=null;
            switch(call.MethodName) {
                case "get_Scene": break;
                case "get_RelativeTransform": value=Transform; break;
                case "SetRelativeTransform": Transform=(Matrix)call.Args[0]; Writes++; break;
                case "RotationReached": Reached++; break;
                case "RotationChanged": Changed++; LastAngle=(float)call.Args[0]; break;
                default: throw new NotSupportedException(call.MethodName);
            }
            return new ReturnMessage(value,call.Args,call.ArgCount,call.LogicalCallContext,call);
        }
    }
}