change offsets to class objects (for future use)

This commit is contained in:
Dark Byte 2016-07-22 03:24:20 +02:00
parent cc633cf0b1
commit b5ddc12c58
8 changed files with 271 additions and 60 deletions

View File

@ -4816,7 +4816,7 @@ begin
exit;
end;
if opcodes[j].opcode1=eo_reg then //probably imul reg,imm32
if opcodes[j].opcode1=eo_reg then //probably imul r32,imm8
begin
addopcode(bytes,j);
createmodrm(bytes,getreg(parameter1),parameter1);

View File

@ -69,9 +69,9 @@ begin
CleanupLuaCall(tmethod(control.onClose));
control.onClose:=nil;
if lua_isfunction(L,-1) then
if lua_isfunction(L,1) then
begin
routine:=Lua_ToString(L,-1);
routine:=Lua_ToString(L,1);
f:=luaL_ref(L,LUA_REGISTRYINDEX);
lc:=TLuaCaller.create;
@ -79,9 +79,9 @@ begin
control.OnClose:=lc.CloseEvent;
end
else
if lua_isstring(L,-1) then
if lua_isstring(L,1) then
begin
routine:=lua_tostring(L,-1);
routine:=lua_tostring(L,1);
lc:=TLuaCaller.create;
lc.luaroutine:=routine;
control.OnClose:=lc.CloseEvent;

View File

@ -19,7 +19,7 @@ var
memrec: TMemoryRecord;
begin
memrec:=luaclass_getClassObject(L);
lua_pushinteger(L, length(memrec.pointeroffsets));
lua_pushinteger(L, memrec.offsetCount);
result:=1;
end;
@ -30,7 +30,7 @@ begin
result:=0;
memrec:=luaclass_getClassObject(L);
if lua_gettop(L)=1 then
setlength(memrec.pointeroffsets, lua_tointeger(L, 1));
memrec.offsetCount:=lua_tointeger(L, 1);
end;
function memoryrecord_getOffset(L: PLua_State): integer; cdecl;
@ -43,7 +43,7 @@ begin
if lua_gettop(L)=1 then
begin
index:=lua_toInteger(L,1);
lua_pushinteger(L, memrec.pointeroffsets[index]);
lua_pushinteger(L, memrec.offsets[index].offset);
result:=1;
end;
end;
@ -58,7 +58,7 @@ begin
if lua_gettop(L)=2 then
begin
index:=lua_toInteger(L,1);
memrec.pointeroffsets[index]:=lua_tointeger(L, 2);
memrec.offsets[index].offset:=lua_tointeger(L, 2);
end;
end;
@ -120,10 +120,10 @@ begin
lua_newtable(L);
tabletop:=lua_gettop(L);
for i:=0 to length(memrec.pointeroffsets)-1 do
for i:=0 to memrec.offsetCount-1 do
begin
lua_pushinteger(L,i+1);
lua_pushinteger(L, memrec.pointeroffsets[i]);
lua_pushinteger(L, memrec.offsets[i].offset);
lua_settable(L, tabletop);
end;
result:=2;
@ -143,7 +143,7 @@ begin
//address
memrec.interpretableaddress:=Lua_ToString(L, 1);
memrec.ReinterpretAddress(true);
setlength(memrec.pointeroffsets, 0);
memrec.offsetCount:=0;
if lua_gettop(L)>=2 then
begin
@ -153,12 +153,12 @@ begin
i:=lua_objlen(L,2);
if i>512 then exit; //FY
setlength(memrec.pointeroffsets, i);
for i:=0 to length(memrec.pointeroffsets)-1 do
memrec.offsetCount:=i;
for i:=0 to memrec.offsetCount-1 do
begin
lua_pushinteger(L, i+1); //get the offset
lua_gettable(L, 2); //from the table (table[i+1])
memrec.pointeroffsets[i]:=lua_tointeger(L,-1);
memrec.offsets[i].offset:=lua_tointeger(L,-1);
lua_pop(L,1);
end;
end;

View File

@ -24,7 +24,7 @@ uses
groupscancommandparser, GraphType, IntfGraphics, RemoteMemoryManager,
DBK64SecondaryLoader, savedscanhandler, debuggertypedefinitions, networkInterface,
FrmMemoryRecordDropdownSettingsUnit, xmlutils, zstream, zstreamext, commonTypeDefs,
VirtualQueryExCache, LazLogger;
VirtualQueryExCache, LazLogger, LazUTF8;
//the following are just for compatibility
@ -7696,12 +7696,12 @@ begin
//create and fill in the offset list
frmpointerscannersettings.cbMustEndWithSpecificOffset.Checked := True;
TOffsetEntry(frmpointerscannersettings.offsetlist[0]).offset := memrec.pointeroffsets[0];
TOffsetEntry(frmpointerscannersettings.offsetlist[0]).offset := memrec.offsets[0].offset;
for i := 1 to length(memrec.pointeroffsets) - 1 do
for i := 1 to memrec.offsetcount - 1 do
begin
frmpointerscannersettings.btnAddOffset.Click;
TOffsetEntry(frmpointerscannersettings.offsetlist[i]).offset := memrec.pointeroffsets[i];
TOffsetEntry(frmpointerscannersettings.offsetlist[i]).offset := memrec.offsets[i].offset;
end;
end;
@ -7766,12 +7766,12 @@ begin
selectedrecord.address := addresslist.selectedRecord.getrealAddress;
selectedrecord.ispointer := addresslist.selectedRecord.IsPointer;
selectedrecord.countoffsets := length(addresslist.selectedRecord.pointeroffsets);
selectedrecord.countoffsets := addresslist.selectedRecord.offsetCount;
getmem(offsets, selectedrecord.countoffsets * 4); //don't forget to free
selectedrecord.offsets := offsets;
for i := 0 to selectedrecord.countoffsets - 1 do
selectedrecord.offsets[i] := addresslist.selectedRecord.pointeroffsets[i];
selectedrecord.offsets[i] := addresslist.selectedRecord.offsets[i].offset;
description := addresslist.selectedRecord.Description;
selectedrecord.description := @description[1];
@ -7851,7 +7851,7 @@ begin
address := foundlist.GetAddress(item.Index, extra, Value);
AddressString:=IntToHex(address,8);
part:=1;
Value := AnsiToUtf8(Value);
Value := WinCPToUTF8(Value);
part:=2;
hexadecimal:=foundlist.isHexadecimal;
@ -8320,7 +8320,7 @@ begin
memscan.floatscanWithoutExponents:=cbFloatSimple.checked;
memscan.firstscan(GetScanType2, getVarType2, roundingtype,
utf8toansi(scanvalue.Text), utf8toansi(svalue2), scanStart, scanStop,
UTF8ToWinCP(scanvalue.Text), UTF8ToWinCP(svalue2), scanStart, scanStop,
cbHexadecimal.Checked, rbdec.Checked, cbunicode.Checked, cbCaseSensitive.Checked,
fastscanmethod, edtAlignment.Text,
TCustomType(vartype.items.objects[vartype.ItemIndex]));
@ -8525,8 +8525,8 @@ begin
memscan.floatscanWithoutExponents:=cbFloatSimple.checked;
memscan.nextscan(GetScanType2, roundingtype, utf8toansi(scanvalue.Text),
utf8toansi(svalue2), cbHexadecimal.Checked, rbdec.Checked,
memscan.nextscan(GetScanType2, roundingtype, UTF8ToWinCP(scanvalue.Text),
UTF8ToWinCP(svalue2), cbHexadecimal.Checked, rbdec.Checked,
cbunicode.Checked, cbCaseSensitive.Checked, percentage, compareToSavedScan,
currentlySelectedSavedResultname);
DisableGui;

View File

@ -8,7 +8,7 @@ interface
uses
Windows, forms, graphics, Classes, SysUtils, controls, stdctrls, comctrls,symbolhandler,
cefuncproc,newkernelhandler, autoassembler, hotkeyhandler, dom, XMLRead,XMLWrite,
customtypehandler, fileutil, LCLProc, commonTypeDefs, pointerparser, LazUTF8;
customtypehandler, fileutil, LCLProc, commonTypeDefs, pointerparser, LazUTF8, LuaClass;
{$endif}
{$ifdef unix}
@ -63,11 +63,35 @@ type TMemRecExtraData=record
end;
type
TMemoryRecordActivateEvent=function (sender: TObject; before, currentstate: boolean): boolean of object;
TMemoryRecordHotkey=class;
TMemoryRecord=class;
TMemrecOffset=class
private
fowner: TMemoryRecord;
foffset: integer;
special: boolean; //if set, look at luaref or text, else just keep it to offset (also, update offset to the latest value while at it)
text: string; //symhandler interpretable value, or a luastatement
luaref: integer; //if lua, this contains a reference to the function (so it doesn't have to be parsed each time)
function getOffsetNoBase: integer;
procedure cleanupluaref;
public
function getOffset(currentBase: ptruint): integer;
procedure setOffset(o: integer);
function setOffsetText(s: string): boolean;
constructor create(owner: TMemoryRecord);
destructor destroy; override;
published
property offset: integer read getOffsetNoBase write setOffset;
end;
TMemrecOffsetList=array of TMemrecOffset;
TMemoryRecordActivateEvent=function (sender: TObject; before, currentstate: boolean): boolean of object;
TMemoryRecord=class
private
fID: integer;
@ -117,8 +141,14 @@ type
fDontSave: boolean;
luaref: integer; //luaclass object to this instance
fonactivate, fondeactivate: TMemoryRecordActivateEvent;
fOnDestroy: TNotifyEvent;
fpointeroffsets: array of TMemrecOffset; //if longer than 0, this is a pointer
function getPointerOffset(index: integer): TMemrecOffset;
function getByteSize: integer;
function BinaryToString(b: pbytearray; bufsize: integer): string;
function getAddressString: string;
@ -162,8 +192,6 @@ type
interpretableaddress: string;
pointeroffsets: array of integer; //if set this is an pointer
Extra: TMemRecExtraData;
@ -187,6 +215,8 @@ type
procedure beginEdit;
procedure endEdit;
procedure setOffsetCount(c: integer);
function getoffsetCount: integer;
function isPointer: boolean;
function isOffset: boolean;
procedure ApplyFreeze;
@ -224,6 +254,9 @@ type
procedure SetVisibleChildrenState;
procedure cleanupPointerOffsets;
function getLuaRef: integer;
constructor Create(AOwner: TObject);
destructor destroy; override;
@ -269,7 +302,8 @@ type
property OnActivate: TMemoryRecordActivateEvent read fOnActivate write fOnActivate;
property OnDeactivate: TMemoryRecordActivateEvent read fOnDeActivate write fOndeactivate;
property OnDestroy: TNotifyEvent read fOnDestroy write fOnDestroy;
property offsetCount: integer read getoffsetCount write setOffsetCount;
property offsets[index: integer]: TMemrecOffset read getPointerOffset;
end;
TMemoryRecordHotkey=class
@ -316,6 +350,117 @@ uses mainunit, addresslist, formsettingsunit, LuaHandler, lua, lauxlib, lualib,
uses processhandlerunit, Parsers;
{$endif}
{-----------------------------TMemrecOffset---------------------------------}
function TMemrecOffset.getOffsetNoBase: integer;
begin
result:=getOffset(0);
end;
function TMemrecOffset.getOffset(currentBase: ptruint): integer;
var
e: boolean;
memrecluaobjectref: integer;
stack: integer;
begin
if special then
begin
foffset:=0;
//parse it/call the lua function
if luaref=-1 then
foffset:=symhandler.getAddressFromName(text, false, e)
else
begin
memrecluaobjectref:=fowner.getLuaRef;
lua_rawgeti(Luavm, LUA_REGISTRYINDEX, memrecluaobjectref);
lua_pushinteger(luavm, currentBase);
LUACS.Enter;
try
stack:=lua_Gettop(luavm);
if lua_pcall(Luavm, 2, 1,0)=0 then
foffset:=lua_tointeger(Luavm, -1);
finally
lua_settop(luavm, stack);
luacs.Leave;
end;
end;
end;
result:=foffset;
end;
procedure TMemrecOffset.cleanupluaref;
begin
if luaref<>-1 then //dereference this lua function
begin
luaL_unref(LuaVM, LUA_REGISTRYINDEX, luaref);
luaref:=-1;
end;
end;
procedure TMemrecOffset.setOffset(o: integer);
begin
special:=false;
foffset:=o;
end;
function TMemrecOffset.setOffsetText(s: string): boolean;
var
e: boolean;
ft: tstringlist;
stack: integer;
begin
cleanupluaref;
special:=not TryStrToInt('$'+s,foffset);
result:=special;
if special then
begin
text:=s;
//parse it as a symbolhandler text, if that fails, try lua
foffset:=symhandler.getAddressFromName(s, false, e);
if e then
begin
//try lua
ft:=tstringlist.create;
ft.add('memrec, address=...');
ft.add('return '+s);
LUACS.Enter;
try
stack:=lua_Gettop(luavm);
if luaL_loadstring(luavm, pchar(ft.text))=0 then
if lua_isfunction(luavm,-1) then //store a reference to this function
luaref:=luaL_ref(luavm, LUA_REGISTRYINDEX);
finally
lua_settop(luavm, stack);
LuaCS.Leave;
end;
result:=luaref<>-1;
end;
end;
end;
constructor TMemrecOffset.create(owner: TMemoryRecord);
begin
fOwner:=owner;
luaref:=-1;
end;
destructor TMemrecOffset.destroy;
begin
cleanupluaref;
end;
{-----------------------------TMemoryRecordHotkey------------------------------}
constructor TMemoryRecordHotkey.create(AnOwner: TMemoryRecord);
@ -435,6 +580,27 @@ begin
result:=TMemoryRecordHotkey(hotkeylist[index]);
end;
procedure TMemoryRecord.cleanupPointerOffsets;
var i: integer;
begin
for i:=0 to length(fpointeroffsets)-1 do
if fpointeroffsets[i]<>nil then
freeandnil(fpointeroffsets[i]);
setlength(fpointeroffsets,0);
end;
function TMemoryRecord.getLuaRef: integer;
begin
if luaref=-1 then
begin
luaclass_newClass(luavm, self);
luaref:=luaL_ref(luavm, LUA_REGISTRYINDEX);
end;
result:=luaref;
end;
constructor TMemoryRecord.create(AOwner: TObject);
begin
fVisible:=true;
@ -447,6 +613,8 @@ begin
foptions:=[];
luaref:=-1;
inherited create;
end;
@ -486,6 +654,9 @@ begin
if fDropDownList<>nil then
freeandnil(fDropDownList);
if luaref<>-1 then
luaL_unref(LuaVM, LUA_REGISTRYINDEX, luaref);
inherited Destroy;
end;
@ -782,19 +953,19 @@ begin
tempnode:=CheatEntry.FindNode('Offsets');
if tempnode<>nil then
begin
setlength(pointeroffsets,tempnode.ChildNodes.Count);
offsetCount:=tempnode.ChildNodes.Count;
j:=0;
for i:=0 to tempnode.ChildNodes.Count-1 do
begin
if tempnode.ChildNodes[i].NodeName='Offset' then
begin
pointeroffsets[j]:=strtoint('$'+tempnode.ChildNodes[i].TextContent);
fpointeroffsets[j].offset:=strtoint('$'+tempnode.ChildNodes[i].TextContent);
inc(j);
end;
end;
setlength(pointeroffsets,j); //set to the proper size
offsetcount:=j; //set to the proper size
end;
tempnode:=CheatEntry.FindNode('Hotkeys');
@ -1128,8 +1299,8 @@ begin
begin
Offsets:=cheatEntry.AppendChild(doc.CreateElement('Offsets'));
for i:=0 to length(pointeroffsets)-1 do
Offsets.AppendChild(doc.CreateElement('Offset')).TextContent:=inttohex(pointeroffsets[i],1);
for i:=0 to offsetCount-1 do
Offsets.AppendChild(doc.CreateElement('Offset')).TextContent:=inttohex(fpointeroffsets[i].offset,1);
cheatEntry.AppendChild(Offsets);
end;
@ -1234,9 +1405,34 @@ begin
dec(editcount);
end;
function TMemoryRecord.getPointerOffset(index: integer): TMemrecOffset;
begin
result:=fpointeroffsets[index];
end;
procedure TMemoryRecord.setOffsetCount(c: integer);
var
oldc: integer;
i: integer;
begin
oldc:=offsetcount;
for i:=oldc-1 downto c do
freeandnil(fpointeroffsets[i]);
setlength(fpointeroffsets, c);
for i:=oldc to c-1 do
fpointeroffsets[i]:=TMemrecOffset.create(self);
end;
function TMemoryRecord.getOffsetCount: integer;
begin
result:=length(fpointeroffsets);
end;
function TMemoryRecord.isPointer: boolean;
begin
result:=length(pointeroffsets)>0;
result:=offsetcount>0;
end;
function TMemoryRecord.isOffset: boolean;
@ -1828,7 +2024,7 @@ function TMemoryRecord.getAddressString: string;
begin
GetRealAddress;
if length(pointeroffsets)>0 then
if isPointer then
begin
if UnreadablePointer then
result:=rsPqqqqqqqq
@ -2337,14 +2533,19 @@ var
realaddress, realaddress2: PtrUInt;
i: integer;
count: dword;
list: array of integer;
begin
realAddress:=0;
realAddress2:=0;
if length(pointeroffsets)>0 then //it's a pointer
if isPointer then //it's a pointer
begin
setlength(list, offsetCount);
for i:=0 to offsetCount-1 do
list[i]:=offsets[i].offset;
//find the address this pointer points to
result:=getPointerAddress(getBaseAddress, pointeroffsets, UnreadablePointer);
result:=getPointerAddress(getBaseAddress, list, UnreadablePointer);
if UnreadablePointer then
begin
realAddress:=0;

View File

@ -751,9 +751,9 @@ begin
memrec.VarType:=vartype;
memrec.CustomTypeName:=customtypename;
setlength(memrec.pointeroffsets,offsetcount);
memrec.offsetCount:=offsetcount;
for i:=0 to offsetcount-1 do
memrec.pointeroffsets[i]:=offsets[i];
memrec.offsets[i].offset:=offsets[i];
case vartype of
vtString:

View File

@ -173,8 +173,8 @@ type
function getUnicode: boolean;
procedure setDescription(s: string);
function getDescription: string;
procedure setAddress(var address: string; var offsets: Toffsetlist);
function getAddress(var address: string; var offsets: ToffsetList): boolean;
procedure setAddress(var address: string; var offsets: TMemrecOffsetList);
function getAddress(var address: string; var offsets: TIntegerDynArray): boolean;
public
{ Public declarations }
index: integer;
@ -745,7 +745,7 @@ end;
{ Tformaddresschange }
procedure Tformaddresschange.setAddress(var address: string; var offsets: Toffsetlist);
procedure Tformaddresschange.setAddress(var address: string; var offsets: TMemrecOffsetList);
var i: integer;
begin
if system.length(offsets)=0 then
@ -767,14 +767,14 @@ begin
pointerinfo.setupPositionsAndSizes;
for i:=0 to system.length(offsets)-1 do
pointerinfo.offset[i].offset:=offsets[i];
pointerinfo.offset[i].offset:=offsets[i].offset;
pointerinfo.processAddress;
end;
end;
function Tformaddresschange.getAddress(var address: string; var offsets: ToffsetList): boolean;
function Tformaddresschange.getAddress(var address: string; var offsets: TIntegerDynArray): boolean;
var
i: integer;
begin
@ -1059,13 +1059,19 @@ end;
procedure TformAddressChange.setMemoryRecord(rec: TMemoryRecord);
var i: integer;
tmp:string;
list: TMemrecOffsetList;
begin
fMemoryRecord:=rec;
description:=rec.Description;
vartype:=rec.VarType;
setAddress(rec.interpretableaddress, rec.pointeroffsets);
setlength(list, rec.offsetCount);
for i:=0 to rec.offsetCount-1 do
list[i]:=rec.offsets[i];
setAddress(rec.interpretableaddress, list);
case fMemoryRecord.vartype of
vtBinary:
@ -1102,7 +1108,7 @@ var bit: integer;
err:integer;
paddress: dword;
offsets: array of integer;
offsets: TIntegerDynArray;
i: integer;
begin
@ -1134,9 +1140,9 @@ begin
getAddress(address, offsets);
memoryrecord.interpretableaddress:=address;
setlength(memoryrecord.pointeroffsets, system.length(offsets));
memoryrecord.offsetCount:=system.length(offsets);
for i:=0 to system.length(offsets)-1 do
memoryrecord.pointeroffsets[i]:=offsets[system.length(offsets)-1-i];
memoryrecord.offsets[i].offset:=offsets[system.length(offsets)-1-i];
modalresult:=mrok;

View File

@ -929,15 +929,19 @@ begin
begin
if neededoffsets<>nil then
neededOffsets^:=length(m.pointeroffsets);
if offsets<>nil then
for i:=0 to maxoffsets-1 do
offsets[i]:=m.pointeroffsets[i];
neededOffsets^:=m.offsetCount;
if address<>nil then
address^:=m.GetRealAddress;
if offsets<>nil then
begin
for i:=0 to maxoffsets-1 do
offsets[i]:=m.offsets[i].offset;
end;
result:=true;
end;
except
@ -962,11 +966,11 @@ begin
p:=params;
if (p.memrec is TMemoryRecord) then
begin
setlength(p.memrec.pointeroffsets, p.offsetcount);
p.memrec.offsetCount:=p.offsetcount;
p.memrec.interpretableaddress:=p.address;
for i:=0 to p.offsetcount-1 do
p.memrec.pointeroffsets[i]:=p.offsets[i];
p.memrec.offsets[i].offset:=p.offsets[i];
result:=pointer(1);