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

XFEWarframe

【WinUI】基于Warframe游戏本体的公开服务器以及安装器

公开
关注 0 Fork 0 Star 0
UTF-8
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text.Json.Nodes;
using XFEWarframe.Client;
using static XFEWarframe.Bridge.GameTestLua;

namespace XFEWarframe.Bridge;

internal static class GameBranding
{
    private const string IdentityClip = "BuildLabel";
    private static string buildLabel = "";
    private static string serverName = "XFEWarframe";
    private static nint game, loginMovie;
    private static long nextUpdate;
    private static bool loginVisible, overlayDisabled;
    private static string appliedText = "";
    internal static nint LoginMovie => loginMovie;
    internal static bool OverlayDisabled { get => Volatile.Read(ref overlayDisabled); set => Volatile.Write(ref overlayDisabled, value); }

    internal static void Start(ClientConfiguration configuration)
    {
        Volatile.Write(ref serverName, configuration.ServerName);
        buildLabel = configuration.BuildLabel;
        new Thread(UpdateWindowTitle) { IsBackground = true, Name = "XFEWarframe window title" }.Start();
        _ = RefreshName(configuration.ServerUrl);
    }

    private static async Task RefreshName(string origin)
    {
        while (true)
        {
            try { Volatile.Write(ref serverName, (await ServerConnection.ReadAsync(origin)).ServerName); }
            catch { /* Keep the last known name during an outage. */ }
            await Task.Delay(TimeSpan.FromSeconds(30));
        }
    }

    // The movie and its text field belong to the game's native login UI. All movie
    // operations run on the engine thread; no separate window or overlay is created.
    internal static void UpdateLoginUi(bool loggedIn)
    {
        if (Environment.TickCount64 < nextUpdate) return;
        nextUpdate = Environment.TickCount64 + 100;
        try
        {
            using var lua = new Scope();
            var movie = lua.Method(lua.Global("gFlashMgr"), "FindMovie", lua.Load("/Lotus/Interface/ThemedMainMenu.swf"));
            var instance = lua.Object(movie);
            if (instance == 0) { loginMovie = 0; loginVisible = false; appliedText = ""; return; }
            var visible = !loggedIn && !OverlayDisabled;
            if (loginMovie != instance)
            {
                if (!lua.Boolean(lua.Method(movie, "0x1DD8A236", IdentityClip))) return;
                loginMovie = instance; appliedText = "";
            }
            lua.Method(movie, "0xD9713943", IdentityClip, 13, visible);
            loginVisible = visible;
            var text = "XFEWarframe · " + Volatile.Read(ref serverName) + " · " + buildLabel;
            if (visible && text != appliedText)
            {
                lua.Method(movie, "0xD8A2BED1", IdentityClip, 36, text);
                appliedText = text;
            }
        }
        catch { loginVisible = false; /* Identity must never interrupt a game session. */ }
    }

    internal static JsonObject Snapshot() => new() { ["serverName"] = Volatile.Read(ref serverName),
        ["loginVisible"] = loginVisible, ["nativeLoginMovie"] = loginMovie != 0, ["independentOverlay"] = false };

    private static unsafe void UpdateWindowTitle()
    {
        try
        {
            string applied = "";
            while (true)
            {
                if (game == 0 || !IsWindow(game))
                {
                    game = 0;
                    EnumWindows((nint)(delegate* unmanaged[Stdcall]<nint, nint, int>)&FindGame, 0);
                }
                var name = Volatile.Read(ref serverName);
                if (game != 0 && name != applied && SetWindowText(game, name)) applied = name;
                Thread.Sleep(500);
            }
        }
        catch { /* A title update must never prevent a game session. */ }
    }

    [UnmanagedCallersOnly(CallConvs = [typeof(CallConvStdcall)])]
    private static int FindGame(nint window, nint unused)
    {
        GetWindowThreadProcessId(window, out var pid);
        if (pid == Environment.ProcessId && GetWindow(window, 4) == 0 && IsWindowVisible(window) &&
            GetClientRect(window, out var bounds) && bounds.Right >= 640 && bounds.Bottom >= 360)
        { game = window; return 0; }
        return 1;
    }

    [StructLayout(LayoutKind.Sequential)] private struct Rect { public int Left, Top, Right, Bottom; }
    [DllImport("user32")] private static extern bool EnumWindows(nint callback, nint parameter);
    [DllImport("user32")] private static extern uint GetWindowThreadProcessId(nint window, out uint pid);
    [DllImport("user32")] private static extern nint GetWindow(nint window, uint command);
    [DllImport("user32")] private static extern bool IsWindowVisible(nint window);
    [DllImport("user32")] private static extern bool IsWindow(nint window);
    [DllImport("user32")] private static extern bool GetClientRect(nint window, out Rect rect);
    [DllImport("user32", EntryPoint = "SetWindowTextW", CharSet = CharSet.Unicode)] private static extern bool SetWindowText(nint window, string text);
}