using System.ComponentModel;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
using Microsoft.Win32.SafeHandles;
namespace XFEEDToolBox.Core.Native;
public interface IReadOnlyGameMemory
{
ulong ImageBase{get;}
ulong ImageSize{get;}
byte[] Read(ulong address,int length);
}
/// <summary>Bounded QUERY_INFORMATION | VM_READ access. No game calls, hooks or writable handle.</summary>
internal sealed class ReadOnlyPlayerMemory: IReadOnlyGameMemory,IDisposable
{
private readonly Process process;
private readonly SafeProcessHandle handle;
private readonly long started;
public ulong ImageBase{get;}
public ulong ImageSize{get;}
public ReadOnlyPlayerMemory(int pid,long expectedStarted,string hash)
{
if(!OperatingSystem.IsWindows())throw new PlatformNotSupportedException();
process=Process.GetProcessById(pid);
try
{
started=process.StartTime.ToUniversalTime().Ticks;
if(started!=expectedStarted||!process.ProcessName.Equals("EliteDangerous64",StringComparison.OrdinalIgnoreCase))throw new InvalidOperationException("游戏实例已变更。");
var module=process.MainModule??throw new InvalidOperationException("游戏映像不可读取。");
using(var file=File.OpenRead(module.FileName))
if(!Convert.ToHexStringLower(SHA256.HashData(file)).Equals(hash,StringComparison.Ordinal))throw new InvalidDataException("当前游戏构建的星系内布局尚未验证。");
ImageBase=(ulong)module.BaseAddress;ImageSize=(ulong)module.ModuleMemorySize;
handle=OpenProcess(0x410,false,pid);
if(handle.IsInvalid){handle.Dispose();throw new Win32Exception(Marshal.GetLastWin32Error());}
}
catch{process.Dispose();throw;}
}
public byte[] Read(ulong address,int length)
{
if(address<0x10000||address>0x7fffffffffff||length is <1 or >8192||address+(ulong)length>0x800000000000)throw new InvalidDataException("星系内读取超出校验范围。");
var bytes=new byte[length];
if(!ReadProcessMemory(handle,(nint)address,bytes,(nuint)length,out var count)||count!=(nuint)length)throw new Win32Exception(Marshal.GetLastWin32Error());
return bytes;
}
public void Validate(){process.Refresh();if(process.HasExited||process.StartTime.ToUniversalTime().Ticks!=started)throw new InvalidOperationException("游戏实例已变更。");}
public void Dispose(){handle.Dispose();process.Dispose();}
[DllImport("kernel32.dll",SetLastError=true)] private static extern SafeProcessHandle OpenProcess(uint access,bool inherit,int pid);
[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 count);
}
internal static class GameMemoryValues
{
public static ulong U64(this IReadOnlyGameMemory m,ulong at)=>BitConverter.ToUInt64(m.Read(at,8));
public static uint U32(this IReadOnlyGameMemory m,ulong at)=>BitConverter.ToUInt32(m.Read(at,4));
public static int I32(this IReadOnlyGameMemory m,ulong at)=>BitConverter.ToInt32(m.Read(at,4));
public static double F64(this IReadOnlyGameMemory m,ulong at)=>BitConverter.ToDouble(m.Read(at,8));
public static bool InImage(this IReadOnlyGameMemory m,ulong at)=>at>=m.ImageBase&&at<m.ImageBase+m.ImageSize;
public static ulong Component(this IReadOnlyGameMemory m,ulong root,uint type)
{
if(type is 0 or >99999)throw new InvalidDataException("游戏接口编号无效。");
var metadata=m.U64(root+0x10);var count=m.U64(metadata+0x18);
if(count is 0 or >1024)throw new InvalidDataException("游戏组件表超出上限。");
var slots=m.U64(metadata+0x30);var slot=m.U64(slots+type*16UL);
if(slot>=count)throw new InvalidDataException("当前飞船未提供所需接口。");
return m.U64(m.U64(root+0x18)+slot*8);
}
public static ulong LeafReference(this IReadOnlyGameMemory m,ulong owner,ulong method)
{
if(!m.InImage(method))return 0;
var code=m.Read(method,12);
if(code[0]==0x48&&code[1]==0x8D&&code[2]==0x81&&code[7]==0xC3)return checked((ulong)((long)owner+BitConverter.ToInt32(code,3)));
if(code[0]==0x48&&code[1]==0x8D&&code[2]==0x41&&code[4]==0xC3)return checked((ulong)((long)owner+(sbyte)code[3]));
if(code[0]==0x48&&code[1]==0x8B&&code[2]==0x81&&code[7]==0xC3)return m.U64(checked((ulong)((long)owner+BitConverter.ToInt32(code,3))));
if(code[0]==0x48&&code[1]==0x8B&&code[2]==0x41&&code[4]==0xC3)return m.U64(checked((ulong)((long)owner+(sbyte)code[3])));
return 0;
}
public static (ulong Fsd,ulong Life,ulong Root) Player(this IReadOnlyGameMemory m,ulong expectedEntity)
{
var pool=m.ImageBase+0x5F2F040;
if(expectedEntity==0||m.U64(pool+0x68)!=0xEC0||m.U64(pool+0x90)>128)throw new InvalidDataException("飞船对象池未就绪。");
var visited=new HashSet<ulong>();var block=m.U64(pool+0x30);(ulong Fsd,ulong Life,ulong Root)? found=null;
while(block!=0&&visited.Add(block)&&visited.Count<=128)
{
var count=m.U64(block+0x38);if(count is 0 or >128)throw new InvalidDataException("飞船对象池超出校验范围。");
for(ulong index=0;index<count;index++)
{
var fsd=block+0x80+index*0xEC0;
if(m.U64(fsd)!=m.ImageBase+0x5316CC8||m.U64(fsd+0x58)!=m.ImageBase+0x5316DF8)continue;
var owner=m.U64(fsd+0x20);var table=m.U64(owner);
if(!m.InImage(table))continue;
var reference=m.LeafReference(owner,m.U64(table+0x20));if(reference==0)continue;
var life=m.U64(reference);
if(m.U32(life+0xC0)!=8||m.U64(life+0xB0)!=expectedEntity)continue;
if(found is not null)throw new InvalidDataException("受控飞船身份不唯一。");
found=(fsd,life,m.U64(life+0xC8)-0x30);
}
block=m.U64(block+0x20);
}
if(block!=0||found is null)throw new InvalidDataException("等待当前飞船服务。");
return found.Value;
}
}
using System.ComponentModel;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
using Microsoft.Win32.SafeHandles;
namespace XFEEDToolBox.Core.Native;
public interface IReadOnlyGameMemory
{
ulong ImageBase{get;}
ulong ImageSize{get;}
byte[] Read(ulong address,int length);
}
/// <summary>Bounded QUERY_INFORMATION | VM_READ access. No game calls, hooks or writable handle.</summary>
internal sealed class ReadOnlyPlayerMemory: IReadOnlyGameMemory,IDisposable
{
private readonly Process process;
private readonly SafeProcessHandle handle;
private readonly long started;
public ulong ImageBase{get;}
public ulong ImageSize{get;}
public ReadOnlyPlayerMemory(int pid,long expectedStarted,string hash)
{
if(!OperatingSystem.IsWindows())throw new PlatformNotSupportedException();
process=Process.GetProcessById(pid);
try
{
started=process.StartTime.ToUniversalTime().Ticks;
if(started!=expectedStarted||!process.ProcessName.Equals("EliteDangerous64",StringComparison.OrdinalIgnoreCase))throw new InvalidOperationException("游戏实例已变更。");
var module=process.MainModule??throw new InvalidOperationException("游戏映像不可读取。");
using(var file=File.OpenRead(module.FileName))
if(!Convert.ToHexStringLower(SHA256.HashData(file)).Equals(hash,StringComparison.Ordinal))throw new InvalidDataException("当前游戏构建的星系内布局尚未验证。");
ImageBase=(ulong)module.BaseAddress;ImageSize=(ulong)module.ModuleMemorySize;
handle=OpenProcess(0x410,false,pid);
if(handle.IsInvalid){handle.Dispose();throw new Win32Exception(Marshal.GetLastWin32Error());}
}
catch{process.Dispose();throw;}
}
public byte[] Read(ulong address,int length)
{
if(address<0x10000||address>0x7fffffffffff||length is <1 or >8192||address+(ulong)length>0x800000000000)throw new InvalidDataException("星系内读取超出校验范围。");
var bytes=new byte[length];
if(!ReadProcessMemory(handle,(nint)address,bytes,(nuint)length,out var count)||count!=(nuint)length)throw new Win32Exception(Marshal.GetLastWin32Error());
return bytes;
}
public void Validate(){process.Refresh();if(process.HasExited||process.StartTime.ToUniversalTime().Ticks!=started)throw new InvalidOperationException("游戏实例已变更。");}
public void Dispose(){handle.Dispose();process.Dispose();}
[DllImport("kernel32.dll",SetLastError=true)] private static extern SafeProcessHandle OpenProcess(uint access,bool inherit,int pid);
[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 count);
}
internal static class GameMemoryValues
{
public static ulong U64(this IReadOnlyGameMemory m,ulong at)=>BitConverter.ToUInt64(m.Read(at,8));
public static uint U32(this IReadOnlyGameMemory m,ulong at)=>BitConverter.ToUInt32(m.Read(at,4));
public static int I32(this IReadOnlyGameMemory m,ulong at)=>BitConverter.ToInt32(m.Read(at,4));
public static double F64(this IReadOnlyGameMemory m,ulong at)=>BitConverter.ToDouble(m.Read(at,8));
public static bool InImage(this IReadOnlyGameMemory m,ulong at)=>at>=m.ImageBase&&at<m.ImageBase+m.ImageSize;
public static ulong Component(this IReadOnlyGameMemory m,ulong root,uint type)
{
if(type is 0 or >99999)throw new InvalidDataException("游戏接口编号无效。");
var metadata=m.U64(root+0x10);var count=m.U64(metadata+0x18);
if(count is 0 or >1024)throw new InvalidDataException("游戏组件表超出上限。");
var slots=m.U64(metadata+0x30);var slot=m.U64(slots+type*16UL);
if(slot>=count)throw new InvalidDataException("当前飞船未提供所需接口。");
return m.U64(m.U64(root+0x18)+slot*8);
}
public static ulong LeafReference(this IReadOnlyGameMemory m,ulong owner,ulong method)
{
if(!m.InImage(method))return 0;
var code=m.Read(method,12);
if(code[0]==0x48&&code[1]==0x8D&&code[2]==0x81&&code[7]==0xC3)return checked((ulong)((long)owner+BitConverter.ToInt32(code,3)));
if(code[0]==0x48&&code[1]==0x8D&&code[2]==0x41&&code[4]==0xC3)return checked((ulong)((long)owner+(sbyte)code[3]));
if(code[0]==0x48&&code[1]==0x8B&&code[2]==0x81&&code[7]==0xC3)return m.U64(checked((ulong)((long)owner+BitConverter.ToInt32(code,3))));
if(code[0]==0x48&&code[1]==0x8B&&code[2]==0x41&&code[4]==0xC3)return m.U64(checked((ulong)((long)owner+(sbyte)code[3])));
return 0;
}
public static (ulong Fsd,ulong Life,ulong Root) Player(this IReadOnlyGameMemory m,ulong expectedEntity)
{
var pool=m.ImageBase+0x5F2F040;
if(expectedEntity==0||m.U64(pool+0x68)!=0xEC0||m.U64(pool+0x90)>128)throw new InvalidDataException("飞船对象池未就绪。");
var visited=new HashSet<ulong>();var block=m.U64(pool+0x30);(ulong Fsd,ulong Life,ulong Root)? found=null;
while(block!=0&&visited.Add(block)&&visited.Count<=128)
{
var count=m.U64(block+0x38);if(count is 0 or >128)throw new InvalidDataException("飞船对象池超出校验范围。");
for(ulong index=0;index<count;index++)
{
var fsd=block+0x80+index*0xEC0;
if(m.U64(fsd)!=m.ImageBase+0x5316CC8||m.U64(fsd+0x58)!=m.ImageBase+0x5316DF8)continue;
var owner=m.U64(fsd+0x20);var table=m.U64(owner);
if(!m.InImage(table))continue;
var reference=m.LeafReference(owner,m.U64(table+0x20));if(reference==0)continue;
var life=m.U64(reference);
if(m.U32(life+0xC0)!=8||m.U64(life+0xB0)!=expectedEntity)continue;
if(found is not null)throw new InvalidDataException("受控飞船身份不唯一。");
found=(fsd,life,m.U64(life+0xC8)-0x30);
}
block=m.U64(block+0x20);
}
if(block!=0||found is null)throw new InvalidDataException("等待当前飞船服务。");
return found.Value;
}
}