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

XFE.SpaceEngineers.AgentBridge

【SpaceEngineer】AI调试插件

公开
关注 0 Fork 0 Star 0
UTF-8
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.InteropServices;
using HarmonyLib;
using Newtonsoft.Json.Linq;
using Sandbox;
using VRageMath;

namespace XFE.SeAgent.Plugin.Performance
{
    internal static class GraphicsDiagnostics
    {
        [DllImport("shcore.dll")] private static extern int GetProcessDpiAwareness(IntPtr process, out int awareness);
        [DllImport("user32.dll")] private static extern uint GetDpiForWindow(IntPtr window);
        [DllImport("user32.dll")] private static extern IntPtr GetWindowDpiAwarenessContext(IntPtr window);
        [DllImport("user32.dll")] private static extern int GetAwarenessFromDpiAwarenessContext(IntPtr context);
        [DllImport("user32.dll")] private static extern bool GetClientRect(IntPtr window, out Rect rect);
        [DllImport("dwmapi.dll")] private static extern int DwmGetWindowAttribute(IntPtr window, int attribute, out Rect rect, int size);
        [StructLayout(LayoutKind.Sequential)] private struct Rect { public int Left, Top, Right, Bottom; }
        private static readonly Type Renderer = AccessTools.TypeByName("VRageRender.MyRender11");
        private const BindingFlags Flags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance;
        private static object Member(object owner, string name)
        {
            if (owner == null) return null;
            var type = owner as Type ?? owner.GetType(); var instance = owner is Type ? null : owner;
            return type.GetField(name, Flags)?.GetValue(instance) ?? type.GetProperty(name, Flags)?.GetValue(instance, null);
        }
        private static JToken Value(object value)
        {
            if (value == null) return JValue.CreateNull();
            if (value is Vector2I size) return new JObject { ["width"] = size.X, ["height"] = size.Y };
            return value is Enum ? new JValue(value.ToString()) : JToken.FromObject(value);
        }
        public static JObject Snapshot()
        {
            var result = new JObject();
            try
            {
                int awareness; int error = GetProcessDpiAwareness(IntPtr.Zero, out awareness);
                result["processDpiAwareness"] = error == 0 ? (JToken)awareness : null;
                using (var process = Process.GetCurrentProcess())
                {
                    IntPtr window = process.MainWindowHandle;
                    result["windowDpi"] = GetDpiForWindow(window);
                    result["windowDpiAwareness"] = GetAwarenessFromDpiAwarenessContext(GetWindowDpiAwarenessContext(window));
                    if (GetClientRect(window, out var client)) result["windowClient"] = new JObject { ["width"] = client.Right, ["height"] = client.Bottom };
                    if (DwmGetWindowAttribute(window, 9, out var bounds, 16) == 0)
                        result["physicalWindow"] = new JObject { ["width"] = bounds.Right - bounds.Left, ["height"] = bounds.Bottom - bounds.Top };
                }
                result["renderResolution"] = Value(Member(Renderer, "m_resolution"));
                result["viewportResolution"] = Value(Member(Renderer, "ViewportResolution"));
                result["backBufferResolution"] = Value(Member(Renderer, "BackBufferResolution"));
                var user = Member(Member(Renderer, "Settings"), "User");
                result["dynamicResolutionSetting"] = Value(Member(user, "DRScaling"));
                result["dynamicResolutionOverride"] = Value(Member(Member(Renderer, "DebugOverrides"), "EnableDRS"));
                result["dynamicResolutionFraction"] = Value(Member(Renderer, "CurrentResolutionFraction"));
                var quality = new JObject();
                foreach (string field in new[] { "AntialiasingMode", "TextureQuality", "VoxelTextureQuality", "ModelQuality",
                    "VoxelShaderQuality", "AlphaMaskedShaderQuality", "AtmosphereShaderQuality", "ShadowQuality" })
                    quality[field] = Value(Member(user, field));
                result["quality"] = quality;
                result["configuredWidth"] = Value(Member(MySandboxGame.Config, "ScreenWidth"));
                result["configuredHeight"] = Value(Member(MySandboxGame.Config, "ScreenHeight"));
                result["note"] = "Renderer and window snapshots; DRS fraction is not an applied scale when DRS is disabled. DPI awareness 0 allows Windows bitmap scaling.";
            }
            catch (Exception error) { result["unavailable"] = error.Message; }
            return result;
        }
    }
}