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

XFEEDToolBox

【WinUI】XFE精英危险工具箱

公开
关注 0 Fork 0 Star 0
UTF-8
using System.ComponentModel;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
using Microsoft.Win32.SafeHandles;
using XFEEDToolBox.Core.Rendering;
namespace XFEEDToolBox.Core.Native;

public sealed record GameWalletSnapshot(long Arx,DateTimeOffset ReadAt,int ProcessId,long StartedUtcTicks,string GameSha256);

/// <summary>Copies the game's own cached ARX balance; never calls game functions, sends HTTP or requests write access.</summary>
public sealed class ReadOnlyWalletReader
{
    private string verifiedImage="";
    private DateTime verifiedWrite;
    private long verifiedLength;
    public GameWalletSnapshot Read()
    {
        if(!OperatingSystem.IsWindows())throw new PlatformNotSupportedException();
        var candidates=Process.GetProcessesByName("EliteDangerous64");
        try
        {
            if(candidates.Length!=1)throw new InvalidOperationException("等待唯一游戏实例");
            var process=candidates[0];var module=process.MainModule??throw new InvalidOperationException("无法读取游戏映像");
            var image=new FileInfo(module.FileName);
            if(image.FullName!=verifiedImage||image.LastWriteTimeUtc!=verifiedWrite||image.Length!=verifiedLength)
            {
                using var stream=image.OpenRead();
                if(!Convert.ToHexString(SHA256.HashData(stream)).Equals(EliteLocalModels.Reference.GameSha256,StringComparison.OrdinalIgnoreCase))throw new InvalidDataException("当前游戏版本尚未验证钱包读取布局");
                verifiedImage=image.FullName;verifiedWrite=image.LastWriteTimeUtc;verifiedLength=image.Length;
            }
            using var handle=OpenProcess(0x410,false,process.Id);
            if(handle.IsInvalid)throw new Win32Exception(Marshal.GetLastWin32Error());
            var start=process.StartTime.ToUniversalTime().Ticks;var baseAddress=(ulong)module.BaseAddress;
            ulong ReadValue(ulong address,int bytes=8)
            {
                if(address<0x10000||bytes is <1 or >8)throw new InvalidDataException("钱包读取越界");
                var data=new byte[8];
                if(!ReadProcessMemory(handle,(nint)address,data,(nuint)bytes,out var read)||read!=(nuint)bytes)throw new Win32Exception(Marshal.GetLastWin32Error());
                return BitConverter.ToUInt64(data);
            }
            var pool=baseAddress+0x5F93B50;
            if(ReadValue(baseAddress+0x5F93C10,1)!=1||ReadValue(pool+0x68)!=0x1E0||ReadValue(pool+0x90)>32)throw new InvalidDataException("游戏钱包尚未初始化");
            var block=ReadValue(pool+0x30);var visited=new HashSet<ulong>();var values=new List<(ulong Address,long Value)>();
            while(block!=0&&visited.Add(block)&&visited.Count<=32)
            {
                var count=ReadValue(block+0x38);if(count>32)throw new InvalidDataException("钱包对象池超出校验范围");
                for(ulong index=0;index<count;index++)
                {
                    var wallet=block+0x80+index*0x1E0;
                    if(ReadValue(wallet)!=baseAddress+0x5299BE8||ReadValue(wallet+8)!=baseAddress+0x5299C18||ReadValue(wallet+0x58)!=baseAddress+0x5299D18)continue;
                    if(ReadValue(wallet+0x140)==0||ReadValue(wallet+0x1B8)==0)continue;
                    var balance=ReadValue(wallet+0x78,4);
                    if(balance>1_000_000_000||ReadValue(wallet+0x78,4)!=balance||ReadValue(wallet)!=baseAddress+0x5299BE8)throw new InvalidDataException("游戏钱包正在切换状态");
                    values.Add((wallet,(long)balance));
                }
                block=ReadValue(block+0x20);
            }
            if(block!=0||values.Count!=1||process.HasExited||process.StartTime.ToUniversalTime().Ticks!=start)throw new InvalidDataException("未识别到唯一有效钱包");
            return new(values[0].Value,DateTimeOffset.UtcNow,process.Id,start,EliteLocalModels.Reference.GameSha256);
        }
        finally{foreach(var process in candidates)process.Dispose();}
    }
    [DllImport("kernel32.dll",SetLastError=true)] private static extern SafeProcessHandle OpenProcess(uint access,bool inherit,int processId);
    [DllImport("kernel32.dll",SetLastError=true)] [return:MarshalAs(UnmanagedType.Bool)] private static extern bool ReadProcessMemory(SafeProcessHandle process,nint address,[Out] byte[] buffer,nuint size,out nuint read);
}