Skip to content

Commit

Permalink
change the pointerscan result reader to make use of file mapping inst…
Browse files Browse the repository at this point in the history
…ead of manually reading
  • Loading branch information
[email protected] committed Dec 21, 2013
1 parent 2481554 commit b8a5643
Showing 1 changed file with 87 additions and 18 deletions.
105 changes: 87 additions & 18 deletions Cheat Engine/PointerscanresultReader.pas
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@
}
interface

uses LCLIntf, sysutils, classes, CEFuncProc, NewKernelHandler, symbolhandler, math;
uses windows, LCLIntf, sysutils, classes, CEFuncProc, NewKernelHandler, symbolhandler, math;

function GetFileSizeEx(hFile:HANDLE; FileSize:PQWord):DWORD; external 'kernel32.dll' name 'GetFileSizeEx';

const maxcachecount=2560;

type TPointerscanResult=record
modulenr: integer;
Expand All @@ -32,12 +33,19 @@ TPointerscanresultReader=class
files: array of record
startindex: qword;
lastindex: qword;
f: TFileStream;
filename: string;
filesize: qword;
f,fm: Thandle;
end;

cacheStart: integer;
cacheSize: integer;
cache: pointer;

cacheStart2: integer;
cacheSize2: integer;
cache2: pointer;

fExternalScanners: integer;
fGeneratedByWorkerID: integer;

Expand Down Expand Up @@ -137,22 +145,58 @@ procedure TPointerscanresultReader.saveModuleListToResults(s: TStream);
function TPointerscanresultReader.InitializeCache(i: qword): boolean;
var j: integer;
actualread: dword;

wantedoffset: qword;
offset: qword;
begin
result:=false;
if i>=fcount then exit;



//find which file to use
for j:=0 to length(files)-1 do
begin
if InRangeQ(i, files[j].startindex, files[j].lastindex) then
begin
files[j].f.Position:=sizeOfEntry*(i-files[j].startindex);

actualread:=files[j].f.Read(cache^, sizeofentry*maxcachecount);
{
FileSeek(files[j].f, int64(sizeOfEntry*(i-files[j].startindex)), fsFromBeginning);
// files[j].f.Position:=sizeOfEntry*(i-files[j].startindex);
actualread:=0;
ReadFile(files[j].f, cache^, sizeofentry*maxcachecount, actualread, nil);
// actualread:=files[j].f.Read(cache^, sizeofentry*maxcachecount);
cachesize:=actualread div sizeofentry;
if cachesize>0 then
result:=true;
result:=true; }
wantedoffset:=int64(sizeOfEntry*(i-files[j].startindex));


if (wantedoffset mod systeminfo.dwAllocationGranularity)<>0 then
begin
//bring offset down to a location matching systeminfo.dwAllocationGranularity
offset:=wantedoffset-(wantedoffset mod systeminfo.dwAllocationGranularity);
end
else
offset:=wantedoffset;


cachesize:=min(files[j].filesize-offset, systeminfo.dwAllocationGranularity*32); //normally 2MB
if cache2<>nil then
unmapviewoffile(cache2);



cache2:=MapViewOfFile(files[j].fm, FILE_MAP_READ, offset shr 32, offset and $ffffffff, cachesize );

//point cache to the start of the wanted offset
cache:=pointer(ptruint(cache2)+(wantedoffset-offset));
cachesize:=(cachesize-(wantedoffset-offset)) div sizeofentry;

result:=cache2<>nil;
exit;
end;
end;
Expand Down Expand Up @@ -258,7 +302,7 @@ procedure TPointerscanresultReader.getFileList(list: TStrings);
var i: integer;
begin
for i:=0 to length(files)-1 do
list.add(files[i].f.FileName);
list.add(files[i].FileName);
end;

constructor TPointerscanresultReader.create(filename: string; original: TPointerscanresultReader=nil);
Expand All @@ -274,6 +318,8 @@ constructor TPointerscanresultReader.create(filename: string; original: TPointer
filenamecount: integer;
error: boolean;
a: ptruint;

fn: string;
begin
FFilename:=filename;
configfile:=TFileStream.Create(filename, fmOpenRead or fmShareDenyWrite);
Expand Down Expand Up @@ -348,14 +394,31 @@ constructor TPointerscanresultReader.create(filename: string; original: TPointer

try
if pos(PathDelim, temppchar)=0 then
files[j].f:=TFileStream.Create(ExtractFilePath(filename)+temppchar, fmOpenRead or fmShareDenyWrite)
fn:=ExtractFilePath(filename)+temppchar
else
files[j].f:=TFileStream.Create(temppchar, fmOpenRead or fmShareDenyWrite);
fn:=temppchar;

files[j].filename:=fn;


files[j].f:=CreateFile(pchar(fn), GENERIC_READ, FILE_SHARE_READ, nil, OPEN_EXISTING, FILE_FLAG_SEQUENTIAL_SCAN, 0);

if (files[j].f<>0) and (files[j].f<>INVALID_HANDLE_VALUE) then
begin
files[j].startindex:=fcount;

files[j].startindex:=fcount;
fcount:=fcount+uint64(files[j].f.Size div uint64(sizeofentry));
files[j].lastindex:=fcount-1;
inc(j);
GetFileSizeEx(files[j].f, @files[j].filesize);

fcount:=fcount+uint64(files[j].filesize div uint64(sizeofentry));
files[j].lastindex:=fcount-1;

files[j].fm:=CreateFileMapping(files[j].f, nil,PAGE_READONLY, 0,0,nil);

if (files[j].fm=0) then
closehandle(files[j].f)
else
inc(j);
end;
except
end;

Expand Down Expand Up @@ -384,7 +447,8 @@ constructor TPointerscanresultReader.create(filename: string; original: TPointer
end;


getmem(cache, sizeofEntry*maxcachecount);
// getmem(cache, sizeofEntry*maxcachecount);
// getmem(cache2, sizeofEntry*maxcachecount);
InitializeCache(0);

freemem(temppchar);
Expand All @@ -397,12 +461,17 @@ destructor TPointerscanresultReader.destroy;
if modulelist<>nil then
modulelist.free;

if cache<>nil then
freemem(cache);
if cache2<>nil then
UnmapViewOfFile(cache2);

for i:=0 to length(files)-1 do
if files[i].f<>nil then
files[i].f.Free;
if files[i].f<>0 then
begin
if files[i].fm<>0 then
closehandle(files[i].fm);

closehandle(files[i].f);
end;
end;

end.

0 comments on commit b8a5643

Please sign in to comment.