using System.Collections.ObjectModel;
using System.ComponentModel;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using Microsoft.UI.Xaml;
using XFEWarframe.Client.Runtime;
namespace XFEWarframe.Player.ViewModels;
public sealed record RuntimeTarget(RuntimeSession Session)
{
public string Label => $"进程 {Session.ProcessId} · {Session.StartedAt.LocalDateTime:HH:mm:ss} 启动";
}
public sealed record RuntimeLogRow(string Time, string Message);
public partial class RuntimePageViewModel : PlayerViewModelBase
{
private readonly DispatcherTimer timer = new() { Interval = TimeSpan.FromSeconds(2) };
private CancellationTokenSource? pageLifetime;
private RuntimeControlClient? client;
private bool polling;
private long cursor;
public ObservableCollection<RuntimeTarget> Targets { get; } = [];
public ObservableCollection<RuntimeLogRow> Logs { get; } = [];
[ObservableProperty] public partial RuntimeTarget? Target { get; set; }
[ObservableProperty] public partial string ConnectionStatus { get; set; } = "尚未连接游戏";
[ObservableProperty] public partial string Details { get; set; } = "启动游戏后,选择正在运行的实例。";
[ObservableProperty] public partial bool ShowBranding { get; set; } = true;
[ObservableProperty] public partial bool LogRequests { get; set; }
[ObservableProperty, NotifyPropertyChangedFor(nameof(ConnectedVisibility))] public partial bool IsConnected { get; set; }
[ObservableProperty] public partial Visibility BrandingVisibility { get; set; } = Visibility.Collapsed;
[ObservableProperty] public partial Visibility RequestLoggingVisibility { get; set; } = Visibility.Collapsed;
public Visibility ConnectedVisibility => IsConnected ? Visibility.Visible : Visibility.Collapsed;
public Visibility LoadingVisibility => IsBusy ? Visibility.Visible : Visibility.Collapsed;
public RuntimePageViewModel()
{
timer.Tick += Tick;
PropertyChanged += (_, e) => { if (e.PropertyName == nameof(IsBusy)) OnPropertyChanged(nameof(LoadingVisibility)); };
}
public void Attach()
{
Detach(); pageLifetime = CancellationTokenSource.CreateLinkedTokenSource(Lifetime); State.PropertyChanged += StateChanged;
}
public void Detach()
{
State.PropertyChanged -= StateChanged; pageLifetime?.Cancel(); pageLifetime?.Dispose(); pageLifetime = null;
Disconnect();
}
private void StateChanged(object? sender, PropertyChangedEventArgs e)
{
if (e.PropertyName != nameof(State.GameDirectory)) return;
Disconnect(); Targets.Clear(); Target = null; Logs.Clear(); ConnectionStatus = "游戏目录已更改,请重新检测。";
}
private void Disconnect() { timer.Stop(); client?.Dispose(); client = null; IsConnected = false; BrandingVisibility = RequestLoggingVisibility = Visibility.Collapsed; }
protected override async Task LoadAsync(object? parameter) { await base.LoadAsync(parameter); await DetectAsync(); }
private async Task DetectAsync()
{
var token = pageLifetime?.Token ?? Lifetime; var game = State.GameDirectory;
Disconnect(); Targets.Clear(); Target = null; Logs.Clear(); cursor = 0;
if (string.IsNullOrWhiteSpace(game)) { ConnectionStatus = "请先在安装与维护中选择游戏目录。"; return; }
var sessions = await Task.Run(() => RuntimeDiscovery.FindAsync(game, token), token);
token.ThrowIfCancellationRequested(); if (game != State.GameDirectory) return;
foreach (var session in sessions) Targets.Add(new(session));
Target = Targets.FirstOrDefault();
if (Target is null) { ConnectionStatus = "未发现此目录的 XFEWarframe 运行时。"; Details = "请使用更新后的兼容组件启动游戏,再检测连接。"; return; }
await ConnectCoreAsync();
}
[RelayCommand] private Task DetectRuntimeAsync() => RunAsync(DetectAsync);
[RelayCommand] private Task ConnectAsync() => RunAsync(ConnectCoreAsync);
private async Task ConnectCoreAsync()
{
Disconnect(); var target = Target ?? throw new InvalidOperationException("请先选择运行中的游戏。");
var candidate = new RuntimeControlClient(target.Session); var token = pageLifetime?.Token ?? Lifetime;
try
{
var status = await candidate.StatusAsync(token); token.ThrowIfCancellationRequested();
if (target != Target) { candidate.Dispose(); return; }
client = candidate; IsConnected = true; cursor = 0; Logs.Clear(); ShowStatus(status);
ShowBranding = !status.Settings.DisableOverlay; LogRequests = status.Settings.ClientHttpLogging;
BrandingVisibility = status.Capabilities.Any(x => x.Key == "disable_overlay" && x.Available) ? Visibility.Visible : Visibility.Collapsed;
RequestLoggingVisibility = status.Capabilities.Any(x => x.Key == "client_http_logging" && x.Available) ? Visibility.Visible : Visibility.Collapsed;
Append(await candidate.LogsAsync(cursor, token)); timer.Start();
}
catch { candidate.Dispose(); if (ReferenceEquals(client, candidate)) Disconnect(); ConnectionStatus = "无法连接此游戏实例,请重新检测。"; throw; }
}
private void ShowStatus(RuntimeStatus status)
{
ConnectionStatus = "已连接 · " + status.ServerName;
Details = $"运行时 {status.Version} · 游戏 {status.GameVersion} · 内存 {status.WorkingSet / 1048576:N0} MB\n{status.ServerUrl}";
Details += status.Capabilities.Any(x => x.Key == "experimental_multi_abilities" && x.Available)
? "\n多技能实验:兼容组件已就绪。"
: "\n多技能实验:当前游戏或兼容组件不支持。";
}
private void Append(RuntimeLogs result)
{
if (result.Reset) Logs.Clear();
foreach (var log in result.Items) Logs.Add(new(log.Time.LocalDateTime.ToString("HH:mm:ss"), log.Message));
while (Logs.Count > 200) Logs.RemoveAt(0); cursor = result.Cursor;
}
private async void Tick(object? sender, object args)
{
if (polling || IsBusy || client is null || pageLifetime is null) return;
polling = true; var current = client; var token = pageLifetime.Token;
try
{
var status = await current.StatusAsync(token); var logs = await current.LogsAsync(cursor, token);
if (!ReferenceEquals(client, current) || token.IsCancellationRequested) return;
ShowStatus(status); Append(logs);
}
catch (OperationCanceledException) when (token.IsCancellationRequested) { }
catch
{
if (ReferenceEquals(client, current)) { Disconnect(); ConnectionStatus = "游戏连接已断开,请在游戏启动后重新连接。"; }
}
finally { polling = false; }
}
[RelayCommand] private Task ApplyAsync() => RunAsync(async () => { await ApplySettingsAsync(); Notify("运行时设置已应用"); });
private Task<RuntimeSettings> ApplySettingsAsync() => (client ?? throw new InvalidOperationException("请先连接游戏。"))
.SettingsAsync(new(!ShowBranding, LogRequests), pageLifetime?.Token ?? Lifetime);
[RelayCommand] private Task SaveAsync() => RunAsync(async () =>
{
await ApplySettingsAsync(); await client!.SaveAsync(pageLifetime?.Token ?? Lifetime); Notify("设置已保存,下次启动时继续使用");
});
[RelayCommand] private Task ClearLogsAsync() => RunAsync(async () =>
{
if (client is null) return; await client.ClearLogsAsync(pageLifetime?.Token ?? Lifetime); Logs.Clear();
Append(await client.LogsAsync(0, pageLifetime?.Token ?? Lifetime));
});
}
using System.Collections.ObjectModel;
using System.ComponentModel;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using Microsoft.UI.Xaml;
using XFEWarframe.Client.Runtime;
namespace XFEWarframe.Player.ViewModels;
public sealed record RuntimeTarget(RuntimeSession Session)
{
public string Label => $"进程 {Session.ProcessId} · {Session.StartedAt.LocalDateTime:HH:mm:ss} 启动";
}
public sealed record RuntimeLogRow(string Time, string Message);
public partial class RuntimePageViewModel : PlayerViewModelBase
{
private readonly DispatcherTimer timer = new() { Interval = TimeSpan.FromSeconds(2) };
private CancellationTokenSource? pageLifetime;
private RuntimeControlClient? client;
private bool polling;
private long cursor;
public ObservableCollection<RuntimeTarget> Targets { get; } = [];
public ObservableCollection<RuntimeLogRow> Logs { get; } = [];
[ObservableProperty] public partial RuntimeTarget? Target { get; set; }
[ObservableProperty] public partial string ConnectionStatus { get; set; } = "尚未连接游戏";
[ObservableProperty] public partial string Details { get; set; } = "启动游戏后,选择正在运行的实例。";
[ObservableProperty] public partial bool ShowBranding { get; set; } = true;
[ObservableProperty] public partial bool LogRequests { get; set; }
[ObservableProperty, NotifyPropertyChangedFor(nameof(ConnectedVisibility))] public partial bool IsConnected { get; set; }
[ObservableProperty] public partial Visibility BrandingVisibility { get; set; } = Visibility.Collapsed;
[ObservableProperty] public partial Visibility RequestLoggingVisibility { get; set; } = Visibility.Collapsed;
public Visibility ConnectedVisibility => IsConnected ? Visibility.Visible : Visibility.Collapsed;
public Visibility LoadingVisibility => IsBusy ? Visibility.Visible : Visibility.Collapsed;
public RuntimePageViewModel()
{
timer.Tick += Tick;
PropertyChanged += (_, e) => { if (e.PropertyName == nameof(IsBusy)) OnPropertyChanged(nameof(LoadingVisibility)); };
}
public void Attach()
{
Detach(); pageLifetime = CancellationTokenSource.CreateLinkedTokenSource(Lifetime); State.PropertyChanged += StateChanged;
}
public void Detach()
{
State.PropertyChanged -= StateChanged; pageLifetime?.Cancel(); pageLifetime?.Dispose(); pageLifetime = null;
Disconnect();
}
private void StateChanged(object? sender, PropertyChangedEventArgs e)
{
if (e.PropertyName != nameof(State.GameDirectory)) return;
Disconnect(); Targets.Clear(); Target = null; Logs.Clear(); ConnectionStatus = "游戏目录已更改,请重新检测。";
}
private void Disconnect() { timer.Stop(); client?.Dispose(); client = null; IsConnected = false; BrandingVisibility = RequestLoggingVisibility = Visibility.Collapsed; }
protected override async Task LoadAsync(object? parameter) { await base.LoadAsync(parameter); await DetectAsync(); }
private async Task DetectAsync()
{
var token = pageLifetime?.Token ?? Lifetime; var game = State.GameDirectory;
Disconnect(); Targets.Clear(); Target = null; Logs.Clear(); cursor = 0;
if (string.IsNullOrWhiteSpace(game)) { ConnectionStatus = "请先在安装与维护中选择游戏目录。"; return; }
var sessions = await Task.Run(() => RuntimeDiscovery.FindAsync(game, token), token);
token.ThrowIfCancellationRequested(); if (game != State.GameDirectory) return;
foreach (var session in sessions) Targets.Add(new(session));
Target = Targets.FirstOrDefault();
if (Target is null) { ConnectionStatus = "未发现此目录的 XFEWarframe 运行时。"; Details = "请使用更新后的兼容组件启动游戏,再检测连接。"; return; }
await ConnectCoreAsync();
}
[RelayCommand] private Task DetectRuntimeAsync() => RunAsync(DetectAsync);
[RelayCommand] private Task ConnectAsync() => RunAsync(ConnectCoreAsync);
private async Task ConnectCoreAsync()
{
Disconnect(); var target = Target ?? throw new InvalidOperationException("请先选择运行中的游戏。");
var candidate = new RuntimeControlClient(target.Session); var token = pageLifetime?.Token ?? Lifetime;
try
{
var status = await candidate.StatusAsync(token); token.ThrowIfCancellationRequested();
if (target != Target) { candidate.Dispose(); return; }
client = candidate; IsConnected = true; cursor = 0; Logs.Clear(); ShowStatus(status);
ShowBranding = !status.Settings.DisableOverlay; LogRequests = status.Settings.ClientHttpLogging;
BrandingVisibility = status.Capabilities.Any(x => x.Key == "disable_overlay" && x.Available) ? Visibility.Visible : Visibility.Collapsed;
RequestLoggingVisibility = status.Capabilities.Any(x => x.Key == "client_http_logging" && x.Available) ? Visibility.Visible : Visibility.Collapsed;
Append(await candidate.LogsAsync(cursor, token)); timer.Start();
}
catch { candidate.Dispose(); if (ReferenceEquals(client, candidate)) Disconnect(); ConnectionStatus = "无法连接此游戏实例,请重新检测。"; throw; }
}
private void ShowStatus(RuntimeStatus status)
{
ConnectionStatus = "已连接 · " + status.ServerName;
Details = $"运行时 {status.Version} · 游戏 {status.GameVersion} · 内存 {status.WorkingSet / 1048576:N0} MB\n{status.ServerUrl}";
Details += status.Capabilities.Any(x => x.Key == "experimental_multi_abilities" && x.Available)
? "\n多技能实验:兼容组件已就绪。"
: "\n多技能实验:当前游戏或兼容组件不支持。";
}
private void Append(RuntimeLogs result)
{
if (result.Reset) Logs.Clear();
foreach (var log in result.Items) Logs.Add(new(log.Time.LocalDateTime.ToString("HH:mm:ss"), log.Message));
while (Logs.Count > 200) Logs.RemoveAt(0); cursor = result.Cursor;
}
private async void Tick(object? sender, object args)
{
if (polling || IsBusy || client is null || pageLifetime is null) return;
polling = true; var current = client; var token = pageLifetime.Token;
try
{
var status = await current.StatusAsync(token); var logs = await current.LogsAsync(cursor, token);
if (!ReferenceEquals(client, current) || token.IsCancellationRequested) return;
ShowStatus(status); Append(logs);
}
catch (OperationCanceledException) when (token.IsCancellationRequested) { }
catch
{
if (ReferenceEquals(client, current)) { Disconnect(); ConnectionStatus = "游戏连接已断开,请在游戏启动后重新连接。"; }
}
finally { polling = false; }
}
[RelayCommand] private Task ApplyAsync() => RunAsync(async () => { await ApplySettingsAsync(); Notify("运行时设置已应用"); });
private Task<RuntimeSettings> ApplySettingsAsync() => (client ?? throw new InvalidOperationException("请先连接游戏。"))
.SettingsAsync(new(!ShowBranding, LogRequests), pageLifetime?.Token ?? Lifetime);
[RelayCommand] private Task SaveAsync() => RunAsync(async () =>
{
await ApplySettingsAsync(); await client!.SaveAsync(pageLifetime?.Token ?? Lifetime); Notify("设置已保存,下次启动时继续使用");
});
[RelayCommand] private Task ClearLogsAsync() => RunAsync(async () =>
{
if (client is null) return; await client.ClearLogsAsync(pageLifetime?.Token ?? Lifetime); Logs.Clear();
Append(await client.LogsAsync(0, pageLifetime?.Token ?? Lifetime));
});
}