Skip to content

Commit

Permalink
finished async implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
cheat-engine committed Dec 16, 2016
1 parent f3d81e7 commit 97b69fd
Show file tree
Hide file tree
Showing 8 changed files with 194 additions and 27 deletions.
48 changes: 47 additions & 1 deletion Cheat Engine/LuaHandler.pas
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ interface
LuaVM: Plua_State;
LuaCS: Tcriticalsection;

threadvar
Thread_LuaVM: PLua_State;
Thread_LuaRef: integer;

function lua_strtofloat(s: string): double;
function lua_strtoint(s: string): integer;

Expand Down Expand Up @@ -63,6 +67,7 @@ function LuaValueToDescription(L: PLua_state; i: integer; recursivetablecount: i
function GetLuaState: PLUA_State; stdcall;



function lua_oldprintoutput:TStrings;
procedure lua_setPrintOutput(output: TStrings);

Expand Down Expand Up @@ -136,7 +141,19 @@ procedure lua_setPrintOutput(output: TStrings);

function GetLuaState: PLUA_State; stdcall;
begin
result:=LuaVM;
if Thread_LuaVM=nil then
begin
luacs.Enter;
try
Thread_LuaVM:=lua_newthread(luavm);
Thread_LuaRef:=luaL_ref(luavm, LUA_REGISTRYINDEX);
finally
luacs.leave;
end;
end;

result:=Thread_LuaVM;

end;

procedure lua_register(L: Plua_State; const n: PChar; f: lua_CFunction);
Expand Down Expand Up @@ -7713,9 +7730,14 @@ procedure InitializeLua;
s: tstringlist;
k32: THandle;
i: integer;


begin



LuaVM:=lua_open();
Thread_LuaVM:=LuaVM;

if LuaVM<>nil then
begin
Expand Down Expand Up @@ -8355,10 +8377,34 @@ procedure InitializeLua;

end;

var
tm: TThreadManager;
oldReleaseThreadVars: procedure;

procedure ReleaseLuaThreadVars;
begin
if Thread_LuaVM<>nil then
begin
if Thread_LuaRef<>0 then
lua_unref(Thread_LuaVM, Thread_LuaRef);

Thread_LuaVM:=nil;
end;

if assigned(oldReleaseThreadVars) then
oldReleaseThreadVars();
end;

initialization
LuaCS:=TCriticalSection.create;
luarefcs:=TCriticalSection.create;

GetThreadManager(tm);
oldReleaseThreadVars:=tm.ReleaseThreadVars;
tm.ReleaseThreadVars:=@ReleaseLuaThreadVars;
SetThreadManager(tm);


InitializeLua;


Expand Down
7 changes: 7 additions & 0 deletions Cheat Engine/MainUnit.lfm
Original file line number Diff line number Diff line change
Expand Up @@ -1852,6 +1852,13 @@ object MainForm: TMainForm
Visible = False
OnClick = Changescript1Click
end
object miAsyncScript: TMenuItem
AutoCheck = True
Caption = 'Execute asynchronious'
ShowAlwaysCheckable = True
Visible = False
OnClick = miAsyncScriptClick
end
object N5: TMenuItem
Caption = '-'
GroupIndex = 2
Expand Down
1 change: 1 addition & 0 deletions Cheat Engine/MainUnit.lrt
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ TMAINFORM.FREEZEALLADDRESSES2.CAPTION=Toggle Selected Records
TMAINFORM.MIFREEZEPOSITIVE.CAPTION=Freeze Positive
TMAINFORM.MIFREEZENEGATIVE.CAPTION=Freeze Negative
TMAINFORM.CHANGESCRIPT1.CAPTION=Change script
TMAINFORM.MIASYNCSCRIPT.CAPTION=Execute asynchronious
TMAINFORM.N5.CAPTION=-
TMAINFORM.MIGENERATEPOINTERMAP.CAPTION=Generate pointermap
TMAINFORM.POINTERSCANFORTHISADDRESS1.CAPTION=Pointer scan for this address
Expand Down
22 changes: 14 additions & 8 deletions Cheat Engine/MainUnit.pas
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ TMainForm = class(TForm)
FromAddress: TEdit;
andlabel: TLabel;
MenuItem12: TMenuItem;
miAsyncScript: TMenuItem;
miFlFindWhatAccesses: TMenuItem;
MenuItem13: TMenuItem;
miFlFindWhatWrites: TMenuItem;
Expand Down Expand Up @@ -476,6 +477,7 @@ TMainForm = class(TForm)
Selected: boolean);
procedure Label3Click(Sender: TObject);
procedure MenuItem12Click(Sender: TObject);
procedure miAsyncScriptClick(Sender: TObject);
procedure miFlFindWhatAccessesClick(Sender: TObject);
procedure miBindDeactivationClick(Sender: TObject);
procedure miDisassembleClick(Sender: TObject);
Expand Down Expand Up @@ -3051,16 +3053,8 @@ procedure TMainForm.MenuItem12Click(Sender: TObject);
end;




var t: TRemoteMemoryManager;







procedure TMainForm.miScanDirtyOnlyClick(Sender: TObject);
begin
scan_dirtyonly:=miScanDirtyOnly.checked;
Expand Down Expand Up @@ -6394,6 +6388,10 @@ procedure TMainForm.PopupMenu2Popup(Sender: TObject);
Changescript1.Visible := (addresslist.selectedRecord <> nil) and
(addresslist.selectedrecord.VarType = vtAutoAssembler);

miAsyncScript.visible:=Changescript1.Visible;
if miAsyncScript.visible then
miAsyncScript.Checked:=addresslist.selectedrecord.Async;

n5.Visible := (addresslist.selectedRecord <> nil);

Pointerscanforthisaddress1.Visible :=
Expand Down Expand Up @@ -7955,6 +7953,14 @@ procedure TMainForm.AddressListAutoAssemblerEdit(Sender: TObject; memrec: TMemor

end;

procedure TMainForm.miAsyncScriptClick(Sender: TObject);
begin
if (addresslist.selectedRecord <> nil) and
(addresslist.selectedRecord.VarType = vtAutoAssembler) then
addresslist.selectedRecord.Async:=miAsyncScript.Checked;
end;


procedure TMainForm.Changescript1Click(Sender: TObject);
begin
if (addresslist.selectedRecord <> nil) and
Expand Down
28 changes: 25 additions & 3 deletions Cheat Engine/MemoryRecordUnit.pas
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ TMemoryRecord=class

fAsync: boolean;
processingThread: TMemoryRecordProcessingThread; //not nil when doing work
processingTimeStart: qword;
wantedstate: boolean; //last state the user wanted to set it to


Expand Down Expand Up @@ -294,6 +295,7 @@ TMemoryRecord=class
procedure cleanupPointerOffsets;
function getLuaRef: integer;
function isProcessing: boolean;
function getProcessingTime: qword;

constructor Create(AOwner: TObject);
destructor destroy; override;
Expand Down Expand Up @@ -343,6 +345,7 @@ TMemoryRecord=class
property OffsetCount: integer read getoffsetCount write setOffsetCount;
property Async: Boolean read fAsync write fAsync;
property AsyncProcessing: Boolean read isProcessing;
property AsyncProcessingTime: qword read getProcessingTime;
end;

THKSoundFlag=(hksPlaySound=0, hksSpeakText=1, hksSpeakTextEnglish=2); //playSound excludes speakText
Expand Down Expand Up @@ -1147,6 +1150,8 @@ procedure TMemoryRecord.setXMLnode(CheatEntry: TDOMNode);
AutoAssemblerData.script:=tstringlist.Create;
AutoAssemblerData.script.text:=tempnode.TextContent;

a:=tempnode.Attributes.GetNamedItem('Async');
if (a<>nil) then fAsync:=a.TextContent='1';
end;
end;

Expand Down Expand Up @@ -1350,6 +1355,8 @@ procedure TMemoryRecord.getXMLNode(node: TDOMNode; selectedOnly: boolean);
laststate: TDOMNode;
soundentry: TDOMNode;

n: TDOMNode;

tn: TTreenode;
i,j: integer;
a:TDOMAttr;
Expand Down Expand Up @@ -1430,8 +1437,6 @@ procedure TMemoryRecord.getXMLNode(node: TDOMNode; selectedOnly: boolean);
end;




end;

if DropDownList.Count>0 then
Expand Down Expand Up @@ -1539,7 +1544,15 @@ procedure TMemoryRecord.getXMLNode(node: TDOMNode; selectedOnly: boolean);

vtAutoAssembler:
begin
cheatEntry.AppendChild(doc.CreateElement('AssemblerScript')).TextContent:=AutoAssemblerData.script.Text;
n:=cheatEntry.AppendChild(doc.CreateElement('AssemblerScript'));
if fAsync then
begin
a:=doc.CreateAttribute('Async');
a.TextContent:='1';
n.Attributes.SetNamedItem(a);
end;

n.TextContent:=AutoAssemblerData.script.Text;
end;
end;

Expand Down Expand Up @@ -2020,6 +2033,14 @@ function TMemoryRecord.isProcessing: boolean;
result:=processingThread<>nil;
end;

function TMemoryRecord.getProcessingTime: qword;
begin
if isProcessing then
result:=GetTickCount64-processingTimeStart
else
result:=0;
end;

procedure TMemoryRecord.processingDone;
//called after an aa script has finished processing
var i: integer;
Expand Down Expand Up @@ -2118,6 +2139,7 @@ procedure TMemoryRecord.setActive(state: boolean);
begin
//spawn a thread to activate this entry.
//set the state to "Activating"
processingTimeStart:=gettickcount64;
processingThread:=TMemoryRecordProcessingThread.Create(self,state);
treenode.update;
exit;
Expand Down
85 changes: 84 additions & 1 deletion Cheat Engine/addresslist.pas
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ TAddresslist=class(TPanel)
fDecreaseArrowColor: TColor;
fIncreaseArrowColor: TColor;

animationtimer: TTimer;

procedure doAnimation(sender: TObject);

function getTreeNodes: TTreenodes;
procedure setTreeNodes(t: TTreenodes);

Expand Down Expand Up @@ -1667,6 +1671,37 @@ procedure TAddresslist.SelectionUpdate(sender: TObject);
end;
end;

procedure TAddresslist.doAnimation(sender: TObject);
var
i: integer;
updated: boolean;
start,stop: integer;
begin
updated:=false;

if treeview.TopItem<>nil then
start:=treeview.TopItem.Index
else
start:=0;

if treeview.BottomItem<>nil then
stop:=treeview.BottomItem.Index
else
stop:=count-1;

for i:=start to stop do
begin
if TMemoryRecord(Treeview.items[i].data).isProcessing then
begin
memrecitems[i].treenode.Update;
updated:=true;
end;
end;

if not updated then
animationtimer.enabled:=false;
end;

procedure TAddresslist.AdvancedCustomDrawItem(Sender: TCustomTreeView; Node: TTreeNode; State: TCustomDrawState; Stage: TCustomDrawStage; var PaintImages, DefaultDraw: Boolean);
var
textrect: trect;
Expand All @@ -1689,6 +1724,10 @@ procedure TAddresslist.AdvancedCustomDrawItem(Sender: TCustomTreeView; Node: TTr
expandsignlineborderspace: integer;

n: Ttreenode;
t:integer;
cx,cy: integer;
x,y: single;
r: single;
begin
//multiselect implementation

Expand Down Expand Up @@ -1871,7 +1910,51 @@ procedure TAddresslist.AdvancedCustomDrawItem(Sender: TCustomTreeView; Node: TTr
end
else
begin
//todo: animate something nice
//draw a clock
if memrec.isSelected then
sender.canvas.pen.color:=checkboxSelectedColor
else
sender.canvas.pen.color:=checkboxColor;

sender.Canvas.Ellipse(checkbox);

r:=(checkbox.right-checkbox.left) div 2;
cx:=trunc(checkbox.left+r);
cy:=trunc(checkbox.top+r);

t:=memrec.AsyncProcessingTime mod 1000; //every time t=0 the line should be up (value 0)
t:=trunc(t*0.36); //every second is a full rotation

x:=cx+cos(pi*(270+t mod 360)/180)*r;
y:=cy+sin(pi*(270+t mod 360)/180)*r;

sender.Canvas.Line(cx,cy,trunc(x),trunc(y));


if memrec.isSelected then
sender.canvas.pen.color:=IncreaseArrowColor
else
sender.canvas.pen.color:=DecreaseArrowColor;

t:=(memrec.AsyncProcessingTime div 1000) mod 60; //every 60 seconds (t=0) the second handle should be up
t:=t*6;

x:=cx+cos(pi*(270+t mod 360)/180)*r;
y:=cy+sin(pi*(270+t mod 360)/180)*r;

sender.Canvas.Line(cx,cy,trunc(x),trunc(y));


sender.canvas.pen.color:=oldpencolor;

if animationtimer=nil then
begin
animationtimer:=TTimer.Create(self);
animationtimer.interval:=16;
animationtimer.OnTimer:=DoAnimation;
end;

animationtimer.enabled:=true;
end;
descriptionstart:=max(checkbox.right+10,header.Sections[1].Left);

Expand Down
Loading

0 comments on commit 97b69fd

Please sign in to comment.