Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
cheat-engine committed Nov 28, 2023
2 parents 51bd207 + a2414fd commit 21f81ac
Show file tree
Hide file tree
Showing 8 changed files with 289 additions and 64 deletions.
56 changes: 41 additions & 15 deletions Cheat Engine/LuaManualModuleLoader.pas
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ procedure initializeLuaModuleLoader;
implementation

{$IFDEF windows}
uses ManualModuleLoader, lua, lauxlib, lualib, LuaClass, LuaHandler, LuaObject;
uses ManualModuleLoader, lua, lauxlib, lualib, LuaClass, LuaHandler, LuaObject, luafile;

function moduleloader_createModuleLoader(L: PLua_State): integer; cdecl;
var
Expand All @@ -27,36 +27,62 @@ function moduleloader_createModuleLoader(L: PLua_State): integer; cdecl;

usetimeout: boolean=false;
timeout: integer;

paramoffset: integer;

o: TObject;
ms: tmemorystream absolute o;
begin
result:=0;
paramoffset:=0;
if lua_gettop(L)>=1 then
begin
filename:=Lua_ToString(L,1);
try
ml:=TModuleLoader.create(filename);
ml.createSymbolListHandler;
except
on e: exception do
if lua_isstring(L,1) then
begin
filename:=Lua_ToString(L,1);
try
ml:=TModuleLoader.create(filename);
ml.createSymbolListHandler;
except
on e: exception do
begin
lua_pushnil(L);
lua_pushstring(L,e.message);
exit(2);
end;
end;
end
else
if lua_isuserdata(L,1) then
begin
o:=lua_toceuserdata(L, 1);
if o is TLuafile then
o:=tluafile(o).stream;

if o is TMemoryStream then //(memstream, filename, executeEntrypoint, timeout)
begin
lua_pushnil(L);
lua_pushstring(L,e.message);
exit(2);
if lua_gettop(L)>=2 then
filename:=Lua_ToString(L,2);

ml:=TModuleLoader.create(ms, filename);
ml.createSymbolListHandler;
paramoffset:=1;
end;
end;

if lua_gettop(L)>=2 then
executeEntryPoint:=lua_toboolean(L,2)
if lua_gettop(L)>=2+paramoffset then
executeEntryPoint:=lua_toboolean(L,2+paramoffset)
else
executeEntryPoint:=true;

if lua_gettop(L)>=3 then
if lua_gettop(L)>=3+paramoffset then
begin
if lua_isnil(L,3) then
if lua_isnil(L,3+paramoffset) then
useTimeout:=false
else
begin
useTimeout:=true;
timeout:=lua_tointeger(L,3);
timeout:=lua_tointeger(L,3+paramoffset);
end;
end
else
Expand Down
44 changes: 29 additions & 15 deletions Cheat Engine/ManualModuleLoader.pas
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,12 @@ type TModuleLoader=class
importlist: TStringHashList;
procedure cleanupExportList;
function FindKernelModuleExport(modulename: string; exportname: string): ptruint;
procedure createFromMemoryStream(memstream: tmemorystream);
public
Exporttable: TStringlist;
procedure createSymbolListHandler;
constructor create(filename: string);
constructor create(memstream: tmemorystream; filename: string='<memstream>.dll');
published
property BaseAddress: ptruint read destinationBase;
property Loaded: boolean read FLoaded;
Expand All @@ -67,6 +69,7 @@ procedure TModuleLoader.createSymbolListHandler;
begin
module:=ExtractFileName(filename);
fSymbolList:=TSymbolListHandler.create;
fSymbolList.name:=filename;

fSymbolList.AddModule(module,filename,destinationbase,modulesize,is64bit);

Expand Down Expand Up @@ -104,7 +107,31 @@ function TModuleLoader.FindKernelModuleExport(modulename: string; exportname: st
result:=0;
end;

constructor TModuleLoader.create(memstream: tmemorystream; filename: string='<memstream>.dll');
begin
inherited create;

self.filename:=filename;
createFromMemoryStream(memstream);
end;

constructor TModuleLoader.create(filename: string);
var m: TMemoryStream;
begin
inherited create;

self.filename:=filename;

m:=tmemorystream.create;
try
m.LoadFromFile(filename);
createFromMemoryStream(m);
finally
m.free;
end;
end;

procedure TModuleLoader.createFromMemoryStream(memstream: tmemorystream);
var
i,j,k: integer;
filemap: TMemorystream;
Expand Down Expand Up @@ -134,11 +161,6 @@ constructor TModuleLoader.create(filename: string);
processhandle: thandle;
mi: TModuleInfo;
begin
inherited create;


self.filename:=filename;

exporttable:=tstringlist.create;

pid:=processid;
Expand All @@ -148,15 +170,9 @@ constructor TModuleLoader.create(filename: string);

processhandle:=dbk32functions.OP(ifthen<dword>(GetSystemType<=6,$1f0fff, process_all_access), true, pid);

filemap:=tmemorystream.Create;
filemap:=memstream;
filemap.Position:=0;
try
//showmessage('Loading '+filename);


//todo: add a filesearch if no patch is given

filemap.LoadFromFile(filename);

if PImageDosHeader(filemap.Memory)^.e_magic<>IMAGE_DOS_SIGNATURE then
raise exception.create(rsMMLNotAValidFile);

Expand Down Expand Up @@ -422,8 +438,6 @@ constructor TModuleLoader.create(filename: string);
tempmap.free;
end;
finally
filemap.free;

cleanupExportList;
end;
end;
Expand Down
2 changes: 2 additions & 0 deletions Cheat Engine/bin/celua.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1349,6 +1349,7 @@ properties
CanUndo: boolean
CharWidth: integer READONLY
LineHeight: integer READONLY
CaretX, CaretY: integer

methods
CopyToClipboard()
Expand Down Expand Up @@ -3736,6 +3737,7 @@ methods

ModuleLoader(Inheritance: -)
loadModule(pathtodll, executeEntryPoint OPTIONAL default=true, timeout OPTIONAL default=nil=infinite)
loadModule(memorystream or tablefile, internalfilename, executeEntryPoint OPTIONAL default=true, timeout OPTIONAL default=nil=infinite)

properties:
loaded: boolean - true if successfuly mapped
Expand Down
5 changes: 4 additions & 1 deletion Cheat Engine/ceguicomponents.pas
Original file line number Diff line number Diff line change
Expand Up @@ -982,7 +982,7 @@ type TCEButton=class(TButton);

implementation

uses luahandler,luacaller, formdesignerunit, CheckLst, colorbox;
uses luahandler,luacaller, formdesignerunit, CheckLst, colorbox, SynCompletion;

resourcestring
rsInvalidFormData = 'Invalid formdata';
Expand Down Expand Up @@ -1645,6 +1645,9 @@ initialization
registerclass(TColorListBox);
registerclass(TLazVirtualStringTree);
registerclass(TSynEdit);
registerclass(TSynCompletion);
registerclass(TSynAutoComplete);




Expand Down
11 changes: 11 additions & 0 deletions Cheat Engine/disassembler.pas
Original file line number Diff line number Diff line change
Expand Up @@ -15717,6 +15717,17 @@ function TDisassembler.getLastBytestring: string;
VA,PA: qword;
begin
result:='';
if processhandler.SystemArchitecture=archArm then
begin
if length(LastDisassembleData.Bytes)=2 then
result:=pword(@LastDisassembleData.Bytes[0])^.ToHexString(4);

if length(LastDisassembleData.Bytes)=4 then
result:=pdword(@LastDisassembleData.Bytes[0])^.ToHexString(8);

exit;
end;

for i:=0 to length(LastDisassembleData.Bytes)-1 do
begin
if syntaxhighlighting then
Expand Down
15 changes: 13 additions & 2 deletions Cheat Engine/disassemblerarm64.pas
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ EInvalidInstruction=class(Exception);
private
address: qword;
opcode: uint32;
syntaxcheck: boolean;

procedure InitARM64Support;

Expand Down Expand Up @@ -3906,7 +3907,7 @@ function TArm64Instructionset.GuessTypes(param: string): TArm64ParameterTypes;

{$ifndef armdev}
symhandler.getAddressFromName(param,false,r);
if r then
if not r then
result:=result+[pt_label, pt_addrlabel];

{$endif}
Expand Down Expand Up @@ -4545,6 +4546,9 @@ function TArm64Instructionset.ParseParameterForAssembler(param:TAParameters; par
qv:=symhandler.getAddressFromName(paramstr);
{$endif}

if syntaxcheck then qv:=address and $fffffffff0;


outputdebugstring(pchar(format('assembling pt_label. origin=%.8x target destination=%.8x',[address, qv])));
qv:=qv-address;

Expand All @@ -4568,6 +4572,7 @@ function TArm64Instructionset.ParseParameterForAssembler(param:TAParameters; par
{$else}
qv:=symhandler.getAddressFromName(paramstr);
{$endif}
if syntaxcheck then qv:=address and $fffffffff0;


if param.extra=0 then
Expand Down Expand Up @@ -5423,7 +5428,13 @@ function TArm64Instructionset.assemble(_address: ptruint; instruction: string):
match: boolean;
begin
InitARM64Support;
outputdebugstring(pchar('Assembling ARM64 instruction '+instruction+' at '+inttohex(_address,8)));

syntaxcheck:=_address=0;

if syntaxcheck then
outputdebugstring(pchar('Syntaxcheck: Assembling ARM64 instruction '+instruction+' at '+inttohex(_address,8)))
else
outputdebugstring(pchar('Assembling ARM64 instruction '+instruction+' at '+inttohex(_address,8)));
result:=0;
parameters:=[];

Expand Down
Loading

0 comments on commit 21f81ac

Please sign in to comment.