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

XFEWarframe

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

公开
关注 0 Fork 0 Star 0
UTF-8
using static XFEWarframe.Bridge.GameTestLua;

namespace XFEWarframe.Bridge;

/// <summary>Use the active login movie's own callback and wait for its scene transition.</summary>
internal static class LauncherLoginUi
{
    private const string CallbackKey = "XFEWarframe.LauncherLoginDone";
    private sealed class LoginMovie(string requestId, nint movie, nint machine)
    {
        internal readonly string RequestId = requestId;
        internal readonly nint Movie = movie;
        internal readonly nint Machine = machine;
        internal readonly int Thread = Environment.CurrentManagedThreadId;
        internal readonly HashSet<nint> Inspected = [];
        internal bool Capturing, Captured, Invoked;
        internal long NextPoll;
    }
    private static LoginMovie? current;

    internal static bool Ready(string id, nint movie, nint machine)
    {
        if (movie == 0 || machine == 0) return false;
        if (current is not { } binding || binding.RequestId != id || binding.Movie != movie || binding.Machine != machine)
        {
            Reset(machine);
            Volatile.Write(ref current, new LoginMovie(id, movie, machine));
        }
        return current?.Captured == true;
    }

    internal static void Capture(nint state, nint main, int arguments)
    {
        var binding = Volatile.Read(ref current);
        if (binding is null || binding.Thread != Environment.CurrentManagedThreadId ||
            binding.Machine != main || binding.Captured || binding.Capturing || binding.Inspected.Count >= 512) return;
        var closure = LuaCallClosure(state, arguments);
        if (closure == 0 || !binding.Inspected.Add(closure)) return;
        binding.Capturing = true;
        try
        {
            using var lua = new Scope(state);
            var caller = lua.At(lua.Count - arguments);
            var getEnvironment = lua.Global("getfenv");
            var environment = lua.Call(getEnvironment, caller);
            if (lua.Kind(environment) != 7) return;
            var movie = lua.Field(environment, "mMovie", false);
            if (lua.Object(movie) == 0) movie = lua.Field(environment, "mMovie");
            if (lua.Object(movie) != binding.Movie) return;
            var callback = lua.Field(environment, "LoginDone", false);
            if (lua.Kind(callback) != 8) callback = lua.Field(environment, "LoginDone");
            if (lua.Kind(callback) != 8) return;
            var callbackEnvironment = lua.Call(getEnvironment, callback);
            if (lua.Kind(callbackEnvironment) != 7 || lua.Identity(callbackEnvironment) != lua.Identity(environment)) return;
            lua.Set(lua.Registry(), CallbackKey, callback);
            binding.Captured = true;
        }
        catch (Exception) { /* Missing script bindings leave the manual login intact. */ }
        finally { binding.Capturing = false; }
    }

    internal static bool Complete(string id, nint machine, Action<string> log)
    {
        if (current is not { Captured: true } binding || binding.RequestId != id || binding.Machine != machine)
            throw new InvalidOperationException("The native login callback is no longer available.");
        if (Environment.TickCount64 < binding.NextPoll) return false;
        binding.NextPoll = Environment.TickCount64 + 100;
        using var lua = new Scope();
        if (!binding.Invoked)
        {
            if (GameBranding.LoginMovie != binding.Movie) throw new InvalidOperationException("The login movie changed.");
            var registry = lua.Registry();
            var callback = lua.Field(registry, CallbackKey);
            if (lua.Kind(callback) != 8) throw new InvalidOperationException("The native login callback expired.");
            lua.Set(registry, CallbackKey, null);
            binding.Invoked = true;
            lua.CallAll(callback, true, "");
            log("Native main-menu LoginDone callback completed.");
            return false; // Observe the original scene transition on a later frame.
        }
        var globals = lua.Global("_T");
        if (lua.Kind(globals) != 7) return false;
        var showing = lua.Field(globals, "gShowingLoginScreen");
        if (lua.Kind(showing) != 1 || lua.Boolean(showing)) return false;
        var region = lua.Global("gRegion");
        return lua.Object(region) != 0 && lua.Object(lua.Method(region, "GetLocalPlayerAvatar")) != 0;
    }

    internal static void Reset(nint machine)
    {
        var binding = Interlocked.Exchange(ref current, null);
        if (binding is not { Captured: true, Invoked: false } || binding.Machine != machine) return;
        try
        {
            using var lua = new Scope();
            lua.Set(lua.Registry(), CallbackKey, null);
        }
        catch (Exception) { /* The Lua machine also owns the retained callback's lifetime. */ }
    }

    internal static void MachineClosed(nint machine)
    {
        var binding = Volatile.Read(ref current);
        if (binding is not null && binding.Machine == machine)
            Interlocked.CompareExchange(ref current, null, binding);
    }
}