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

XFEWarframe

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

公开
关注 0 Fork 0 Star 0
UTF-8
using System.Diagnostics;
using System.Text.Json;

namespace XFEWarframe.Client;

public sealed record GameExitRecord(int ProcessId, DateTimeOffset StartedAt, DateTimeOffset ExitedAt, int ExitCode, string ExitCodeHex, string BuildLabel);

internal static class GameExitDiagnostics
{
    internal static void Observe(int processId, string game, string build)
    {
        try
        {
            var process = Process.GetProcessById(processId);
            _ = Task.Run(async () =>
            {
                using (process)
                {
                    try
                    {
                        var started = new DateTimeOffset(process.StartTime.ToUniversalTime());
                        await process.WaitForExitAsync().ConfigureAwait(false);
                        var record = new GameExitRecord(processId, started, DateTimeOffset.UtcNow, process.ExitCode,
                            $"0x{unchecked((uint)process.ExitCode):X8}", build);
                        var path = ClientInstaller.Managed(game, $"logs/exit-{DateTime.UtcNow:yyyyMMdd-HHmmss}-{processId}.json");
                        Directory.CreateDirectory(Path.GetDirectoryName(path)!);
                        await File.WriteAllTextAsync(path, JsonSerializer.Serialize(record, ClientJson.Default.GameExitRecord)).ConfigureAwait(false);
                    }
                    catch { /* Best effort: diagnostics never terminate the launcher or the game. */ }
                }
            });
        }
        catch (ArgumentException) { /* Process already exited before observation began. */ }
    }
}