cheat-engine/Cheat Engine/ProcessHandlerUnit.pas

232 lines
5.3 KiB
ObjectPascal
Raw Permalink Normal View History

2011-07-04 19:52:53 +00:00
unit ProcessHandlerUnit;
{$MODE Delphi}
{
Will handle all process specific stuff like openening and closing a process
The ProcessHandler variable will be in cefuncproc, but a tabswitch to another
process will set it to the different tab's process
}
interface
uses
{$ifdef darwin}
macport,
{$endif}
2020-06-04 23:11:49 +02:00
{$ifdef windows}
windows,
{$endif}
{$ifndef jni}LCLIntf, {$endif}
newkernelhandler, classes, sysutils;
2011-07-04 19:52:53 +00:00
type
TSystemArchitecture=(archX86=0, archArm=1);
TOperatingsystemABI=(abiWindows=0, abiSystemV=1);
2011-07-04 19:52:53 +00:00
type TProcessHandler=class
private
fis64bit: boolean;
fIsAndroid: boolean;
2011-07-04 19:52:53 +00:00
fprocesshandle: THandle;
fpointersize: integer;
fSystemArchitecture: TSystemArchitecture;
fOSABI: TOperatingsystemABI; //for c-code
fHexDigitPreference: integer;
2011-07-04 19:52:53 +00:00
procedure setIs64bit(state: boolean);
procedure setProcessHandle(processhandle: THandle);
public
processid: dword;
2011-07-04 19:52:53 +00:00
procedure Open;
function isNetwork: boolean; //perhaps name it isLinux ?
procedure overridePointerSize(newsize: integer);
property is64Bit: boolean read fIs64Bit write setIs64bit;
property isAndroid: boolean read fIsAndroid;
2011-07-04 19:52:53 +00:00
property pointersize: integer read fPointersize;
property processhandle: THandle read fProcessHandle write setProcessHandle;
property SystemArchitecture: TSystemArchitecture read fSystemArchitecture write fSystemArchitecture;
property OSABI: TOperatingsystemABI read fOSABI;
property hexdigitpreference: integer read fHexDigitPreference;
2011-07-04 19:52:53 +00:00
end;
var processhandler: TProcessHandler;
function processhandle: THandle; inline;
function processid: dword; inline;
2011-07-04 19:52:53 +00:00
implementation
{$ifdef jni}
uses networkinterface, networkInterfaceApi;
{$else}
2023-11-13 01:47:23 +01:00
uses LuaHandler, mainunit, networkInterface, networkInterfaceApi, ProcessList, lua,
FileUtil, globals;
{$endif}
2011-07-04 19:52:53 +00:00
procedure TProcessHandler.overridePointerSize(newsize: integer);
begin
fpointersize:=newsize;
end;
function TProcessHandler.isNetwork: boolean;
begin
result:=(((processhandle shr 24) and $ff)=$ce) and (getConnection<>nil);
end;
2011-07-04 19:52:53 +00:00
procedure TProcessHandler.setIs64bit(state: boolean);
begin
fis64bit:=state;
if state then
fpointersize:=8
2011-07-04 19:52:53 +00:00
else
fpointersize:=4;
fhexdigitpreference:=fpointersize*2;
2011-07-04 19:52:53 +00:00
end;
procedure TProcessHandler.setProcessHandle(processhandle: THandle);
var
c: TCEConnection;
arch: integer;
abi: integer;
2011-07-04 19:52:53 +00:00
begin
2022-07-03 13:31:15 +02:00
//outputdebugstring('TProcessHandler.setProcessHandle');
if (fprocesshandle<>0) and (fprocesshandle<>getcurrentprocess) and (processhandle<>getcurrentprocess) then
begin
try
closehandle(fprocesshandle);
except //debugger issue
end;
fprocesshandle:=0;
end;
2011-07-04 19:52:53 +00:00
fprocesshandle:=processhandle;
c:=getConnection;
if c<>nil then
begin
fIsAndroid:=c.isAndroid;
arch:=c.getArchitecture(fprocesshandle);
case arch of
0: //i386
begin
fSystemArchitecture:=archX86;
2013-09-07 11:54:15 +00:00
setIs64Bit(false);
end;
1: //x86_64
begin
fSystemArchitecture:=archX86;
2013-09-07 11:54:15 +00:00
setIs64Bit(true);
end;
2: //arm
begin
fSystemArchitecture:=archArm;
2013-09-07 11:54:15 +00:00
setIs64Bit(false);
end;
3: //arm64
begin
fSystemArchitecture:=archArm;
2013-09-07 11:54:15 +00:00
setIs64Bit(true);
end;
end;
abi:=c.GetABI;
case abi of
0: fOSABI:=abiWindows;
1: fOSABI:=abiSystemV;
end;
end
else
begin
fIsAndroid:=false;
2022-07-03 13:31:15 +02:00
//outputdebugstring('setProcessHandle not windows');
{$ifdef darwin}
2023-11-22 16:59:38 +01:00
outputdebugstring('setProcessHandle');
if MacIsArm64 then //rosetta2 or I finally ported it to full armv8
2023-10-22 22:32:34 +02:00
begin
if isProcessTranslated(processid) then
2023-11-13 01:47:23 +01:00
begin
2023-11-22 16:59:38 +01:00
outputdebugstring('rosetta. So x86');
2023-11-13 01:47:23 +01:00
fSystemArchitecture:=archX86;
globals.SystemSupportsWritableExecutableMemory:=true;
end
2023-10-22 22:32:34 +02:00
else
2023-11-22 16:59:38 +01:00
begin
outputdebugstring('not rosetta. So pure ARM');
2023-10-22 22:32:34 +02:00
fSystemArchitecture:=archArm;
2023-11-22 16:59:38 +01:00
globals.SystemSupportsWritableExecutableMemory:=false;
end;
2023-10-22 22:32:34 +02:00
end;
{$else}
fSystemArchitecture:=archX86;
{$endif}
{$ifdef windows}
fOSABI:=abiWindows;
{$else}
fOSABI:=abiSystemV;
{$endif}
setIs64Bit(newkernelhandler.Is64BitProcess(fProcessHandle));
end;
2011-07-04 19:52:53 +00:00
2013-09-07 11:54:15 +00:00
{$ifdef ARMTEST}
fSystemArchitecture:=archArm;
fOSABI:=abiSystemV;
2013-09-07 11:54:15 +00:00
setIs64Bit(false);
{$endif}
if processhandle<>0 then
begin
outputdebugstring('calling open');
open;
end;
2011-07-04 19:52:53 +00:00
end;
procedure TProcessHandler.Open;
var mn: string;
2011-07-04 19:52:53 +00:00
begin
// outputdebugstring('TProcessHandler.Open');
//GetFirstModuleNa
{$ifndef jni}
if processid<>0 then
begin
mn:=GetFirstModuleName(processid);
lua_pushstring(luavm, pchar(extractfilename(mn)));
lua_setglobal(luavm, 'process');
end;
LUA_functioncall('onOpenProcess', [ptruint(processid)]); //todo: Change to a callback array/list
{$endif}
end;
function processhandle: THandle; inline;
begin
result:=processhandler.processhandle;
end;
function processid: dword; inline;
begin
result:=processhandler.processid;
2011-07-04 19:52:53 +00:00
end;
initialization
processhandler:=TProcessHandler.create;
2011-07-04 19:52:53 +00:00
end.