make the speedhack threadsafe

This commit is contained in:
cheatengine@gmail.com 2012-12-14 12:07:04 +00:00
parent 3722aa6e4e
commit d343944437
4 changed files with 216 additions and 76 deletions

View File

@ -30,6 +30,7 @@
<RunParams>
<local>
<FormatVersion Value="1"/>
<HostApplicationFilename Value="F:\svn\Cheat Engine\bin\speedhacktest-x86_64.exe"/>
<LaunchingApplication PathPlusParams="/usr/X11R6/bin/xterm -T 'Lazarus Run Output' -e $(LazarusDir)/tools/runwait.sh $(TargetCmdLine)"/>
</local>
</RunParams>
@ -38,15 +39,15 @@
<PackageName Value="LCL"/>
</Item1>
</RequiredPackages>
<Units Count="4">
<Units Count="6">
<Unit0>
<Filename Value="speedhack.lpr"/>
<IsPartOfProject Value="True"/>
<EditorIndex Value="2"/>
<EditorIndex Value="4"/>
<WindowIndex Value="0"/>
<TopLine Value="5"/>
<CursorPos X="18" Y="29"/>
<UsageCount Value="39"/>
<TopLine Value="1"/>
<CursorPos X="18" Y="26"/>
<UsageCount Value="43"/>
<Loaded Value="True"/>
<LoadedDesigner Value="True"/>
<DefaultSyntaxHighlighter Value="Delphi"/>
@ -58,9 +59,9 @@
<IsVisibleTab Value="True"/>
<EditorIndex Value="0"/>
<WindowIndex Value="0"/>
<TopLine Value="49"/>
<CursorPos X="47" Y="70"/>
<UsageCount Value="39"/>
<TopLine Value="76"/>
<CursorPos X="36" Y="95"/>
<UsageCount Value="43"/>
<Loaded Value="True"/>
<DefaultSyntaxHighlighter Value="Delphi"/>
</Unit1>
@ -73,107 +74,154 @@
</Unit2>
<Unit3>
<Filename Value="C:\lazarus\fpc\2.6.1\source\rtl\win\wininc\func.inc"/>
<EditorIndex Value="1"/>
<EditorIndex Value="3"/>
<WindowIndex Value="0"/>
<TopLine Value="244"/>
<CursorPos X="10" Y="258"/>
<UsageCount Value="19"/>
<TopLine Value="115"/>
<CursorPos X="34" Y="129"/>
<UsageCount Value="21"/>
<Loaded Value="True"/>
</Unit3>
<Unit4>
<Filename Value="C:\lazarus\fpc\2.6.1\source\rtl\win\wininc\redef.inc"/>
<EditorIndex Value="2"/>
<WindowIndex Value="0"/>
<TopLine Value="998"/>
<CursorPos X="10" Y="1012"/>
<UsageCount Value="12"/>
<Loaded Value="True"/>
</Unit4>
<Unit5>
<Filename Value="C:\lazarus\fpc\2.6.1\source\packages\fcl-base\src\syncobjs.pp"/>
<UnitName Value="syncobjs"/>
<EditorIndex Value="1"/>
<WindowIndex Value="0"/>
<TopLine Value="152"/>
<CursorPos X="1" Y="1"/>
<UsageCount Value="11"/>
<Loaded Value="True"/>
</Unit5>
</Units>
<JumpHistory Count="23" HistoryIndex="21">
<JumpHistory Count="30" HistoryIndex="29">
<Position1>
<Filename Value="speedhack.lpr"/>
<Caret Line="10" Column="31" TopLine="4"/>
<Caret Line="16" Column="10" TopLine="8"/>
</Position1>
<Position2>
<Filename Value="speedhack.lpr"/>
<Caret Line="19" Column="12" TopLine="4"/>
<Filename Value="speedhackmain.pas"/>
<Caret Line="85" Column="23" TopLine="87"/>
</Position2>
<Position3>
<Filename Value="speedhack.lpr"/>
<Caret Line="36" Column="1" TopLine="5"/>
<Caret Line="31" Column="14" TopLine="8"/>
</Position3>
<Position4>
<Filename Value="speedhack.lpr"/>
<Caret Line="18" Column="11" TopLine="4"/>
<Caret Line="36" Column="1" TopLine="8"/>
</Position4>
<Position5>
<Filename Value="speedhack.lpr"/>
<Caret Line="34" Column="5" TopLine="4"/>
<Filename Value="speedhackmain.pas"/>
<Caret Line="66" Column="17" TopLine="55"/>
</Position5>
<Position6>
<Filename Value="speedhack.lpr"/>
<Caret Line="29" Column="21" TopLine="1"/>
<Filename Value="speedhackmain.pas"/>
<Caret Line="7" Column="42" TopLine="1"/>
</Position6>
<Position7>
<Filename Value="speedhack.lpr"/>
<Caret Line="35" Column="1" TopLine="4"/>
<Filename Value="speedhackmain.pas"/>
<Caret Line="155" Column="19" TopLine="126"/>
</Position7>
<Position8>
<Filename Value="speedhackmain.pas"/>
<Caret Line="7" Column="6" TopLine="1"/>
<Caret Line="76" Column="10" TopLine="59"/>
</Position8>
<Position9>
<Filename Value="speedhackmain.pas"/>
<Caret Line="63" Column="21" TopLine="47"/>
<Caret Line="131" Column="25" TopLine="118"/>
</Position9>
<Position10>
<Filename Value="speedhackmain.pas"/>
<Caret Line="12" Column="43" TopLine="1"/>
<Caret Line="92" Column="17" TopLine="73"/>
</Position10>
<Position11>
<Filename Value="speedhackmain.pas"/>
<Caret Line="82" Column="15" TopLine="59"/>
<Caret Line="155" Column="50" TopLine="139"/>
</Position11>
<Position12>
<Filename Value="speedhackmain.pas"/>
<Caret Line="62" Column="8" TopLine="55"/>
<Caret Line="73" Column="19" TopLine="67"/>
</Position12>
<Position13>
<Filename Value="speedhack.lpr"/>
<Caret Line="34" Column="3" TopLine="2"/>
<Filename Value="speedhackmain.pas"/>
<Caret Line="86" Column="1" TopLine="72"/>
</Position13>
<Position14>
<Filename Value="speedhack.lpr"/>
<Caret Line="29" Column="11" TopLine="1"/>
<Filename Value="speedhackmain.pas"/>
<Caret Line="99" Column="1" TopLine="85"/>
</Position14>
<Position15>
<Filename Value="speedhackmain.pas"/>
<Caret Line="37" Column="56" TopLine="28"/>
<Caret Line="122" Column="11" TopLine="117"/>
</Position15>
<Position16>
<Filename Value="speedhackmain.pas"/>
<Caret Line="38" Column="20" TopLine="28"/>
<Caret Line="125" Column="140" TopLine="108"/>
</Position16>
<Position17>
<Filename Value="speedhackmain.pas"/>
<Caret Line="55" Column="48" TopLine="40"/>
<Caret Line="135" Column="49" TopLine="111"/>
</Position17>
<Position18>
<Filename Value="speedhackmain.pas"/>
<Caret Line="69" Column="45" TopLine="51"/>
<Caret Line="69" Column="20" TopLine="55"/>
</Position18>
<Position19>
<Filename Value="speedhack.lpr"/>
<Caret Line="30" Column="14" TopLine="1"/>
<Filename Value="speedhackmain.pas"/>
<Caret Line="33" Column="11" TopLine="16"/>
</Position19>
<Position20>
<Filename Value="speedhackmain.pas"/>
<Caret Line="82" Column="27" TopLine="61"/>
<Caret Line="146" Column="5" TopLine="125"/>
</Position20>
<Position21>
<Filename Value="speedhack.lpr"/>
<Caret Line="29" Column="12" TopLine="1"/>
<Caret Line="29" Column="12" TopLine="8"/>
</Position21>
<Position22>
<Filename Value="speedhackmain.pas"/>
<Caret Line="82" Column="23" TopLine="65"/>
<Filename Value="speedhack.lpr"/>
<Caret Line="34" Column="45" TopLine="8"/>
</Position22>
<Position23>
<Filename Value="C:\lazarus\fpc\2.6.1\source\rtl\win\wininc\func.inc"/>
<Caret Line="258" Column="10" TopLine="244"/>
<Filename Value="speedhackmain.pas"/>
<Caret Line="75" Column="10" TopLine="61"/>
</Position23>
<Position24>
<Filename Value="speedhackmain.pas"/>
<Caret Line="7" Column="45" TopLine="1"/>
</Position24>
<Position25>
<Filename Value="speedhackmain.pas"/>
<Caret Line="166" Column="26" TopLine="143"/>
</Position25>
<Position26>
<Filename Value="speedhackmain.pas"/>
<Caret Line="70" Column="16" TopLine="144"/>
</Position26>
<Position27>
<Filename Value="speedhackmain.pas"/>
<Caret Line="172" Column="1" TopLine="1"/>
</Position27>
<Position28>
<Filename Value="speedhack.lpr"/>
<Caret Line="42" Column="39" TopLine="11"/>
</Position28>
<Position29>
<Filename Value="speedhack.lpr"/>
<Caret Line="43" Column="37" TopLine="18"/>
</Position29>
<Position30>
<Filename Value="speedhack.lpr"/>
<Caret Line="44" Column="4" TopLine="18"/>
</Position30>
</JumpHistory>
</ProjectOptions>
<CompilerOptions>
@ -183,6 +231,7 @@
<Filename Value="..\bin\speedhack-$(TargetCPU)"/>
</Target>
<SearchPaths>
<IncludeFiles Value="$(ProjOutDir)"/>
<UnitOutputDirectory Value="unitbin"/>
</SearchPaths>
<Parsing>
@ -211,6 +260,7 @@
<CompilerMessages>
<UseMsgFile Value="True"/>
</CompilerMessages>
<CustomOptions Value="-dUSECS"/>
<CompilerPath Value="$(CompPath)"/>
</Other>
</CompilerOptions>

View File

@ -16,6 +16,9 @@ uses
windows,
SysUtils,
Classes,
{$ifdef USECS}
syncobjs,
{$endif}
speedhackmain in 'speedhackmain.pas';
exports InitializeSpeedhack;
@ -28,6 +31,14 @@ exports speedhackversion_QueryPerformanceCounter;
begin
speedmultiplier:=1;
confighaschanged:=0; //not changed, speed is 1
initialoffset:=0;
initialoffset:=gettickcount;
initialtime:=initialoffset;
QueryPerformanceCounter(initialoffset64);
initialtime64:=initialoffset64;
loadlibrary('winmm.dll');
{$ifdef USECS}
QPCLock.cs:=TCriticalsection.create;
GTCLock.cs:=TCriticalSection.create;
{$endif}
end.

View File

@ -4,7 +4,7 @@ unit speedhackmain;
interface
uses windows, classes;
uses windows, classes, sysutils{$ifdef USECS},syncobjs{$endif};
procedure InitializeSpeedhack(speed: single); stdcall;
@ -14,6 +14,19 @@ type TQueryPerformanceCounter=function(var x: int64): BOOL; stdcall;
function speedhackversion_GetTickCount: DWORD; stdcall;
function speedhackversion_QueryPerformanceCounter(var x: int64): BOOL; stdcall;
type TSimpleLock=record
{$ifdef USECS}
cs: TCriticalSection;
{$else}
count: integer;
owner: dword;
{$endif}
end;
procedure lock(var l: TSimpleLock);
procedure unlock(var l: TSimpleLock);
//function GetTime:dword; stdcall;
//function NewQueryPerformanceCounter(var output: int64):BOOl; stdcall;
var CETick: dword;
@ -44,50 +57,116 @@ var CETick: dword;
initialoffset64: int64;
initialtime64: int64;
GTCLock: TSimpleLock;
QPCLock: TSimpleLock;
implementation
function speedhackversion_GetTickCount: DWORD; stdcall;
var x: dword;
procedure lock(var l: TSimpleLock);
var tid: dword;
begin
//also used for timeGetTime
x:=TGetTickCount(realgettickcount);
//time past since activation, mulitplied by speed multiplier
result:=trunc((x-initialtime)*speedmultiplier)+initialoffset;
{$ifdef USECS}
l.cs.enter;
{$else}
tid:=GetCurrentThreadId;
if l.owner<>tid then //check if it's already locked
begin
while InterLockedExchange(l.count, 1)<>0 do sleep(0);
l.owner:=tid;
end
else
InterLockedIncrement(l.count);
{$endif}
end;
procedure unlock(var l: TSimpleLock);
var c: integer;
begin
{$ifdef USECS}
l.cs.leave;
{$else}
if l.count=1 then //disable optimization when compiling if you wish to use this code
l.owner:=0;
InterLockedDecrement(l.count);
if l.count<0 then OutputDebugString('error -1');
{$endif}
end;
function speedhackversion_GetTickCount: DWORD; stdcall;
var currentTime: dword;
begin
//also used for timeGetTime
lock(GTCLock);
currentTime:=TGetTickCount(realgettickcount);
//time past since activation, mulitplied by speed multiplier
result:=trunc((currentTime-initialtime)*speedmultiplier)+initialoffset;
unlock(GTCLock);
end;
function speedhackversion_QueryPerformanceCounter(var x: int64): BOOL; stdcall;
var currentTime64: int64;
newx: int64;
begin
x:=0;
y:=0;
lock(QPCLock);
newx:=0;
currentTime64:=0;
//also used for timeGetTime
result:=TQueryPerformanceCounter(realQueryPerformanceCounter)(currentTime64);
//time past since activation, multiplied by speed multiplier
x:=trunc((currentTime64-initialtime64)*speedmultiplier)+initialoffset64;
newx:=trunc((currentTime64-initialtime64)*speedmultiplier)+initialoffset64;
unlock(QPCLock);
x:=newx; //access violation possible here
end;
procedure InitializeSpeedhack(speed: single); stdcall;
{
Called by createremotethread
}
var x: int64;
begin
x:=0;
lock(QPCLock);
lock(GTCLock);
// outputdebugstring(pchar(format('locked: tid=%d, lockQPC=%d (%d), lockGTC=%d (%d)',[GetThreadID, QPCLock.owner, QPCLock.count, GTCLock.owner, GTCLock.count])));
//sleep(5000);
// messagebox(0,'called','called',mb_ok);
initialoffset:=gettickcount; //get the time the process currently sees (could be fake)
initialtime:=TGetTickCount(realgettickcount); //get the real time
QueryPerformanceCounter(x);
initialoffset64:=x;
TQueryPerformanceCounter(realQueryPerformanceCounter)(x);
initialtime64:=x;
// OutputDebugString('c1');
QueryPerformanceCounter(initialoffset64);
TQueryPerformanceCounter(realQueryPerformanceCounter)(initialtime64);
// OutputDebugString('c2');
speedmultiplier:=speed;
// OutputDebugString('d');
// sleep(100);
//OutputDebugString('Going to unlock');
// outputdebugstring(pchar(format('before unlock: tid=%d, lockQPC=%d (%d), lockGTC=%d (%d)',[GetThreadID, QPCLock.owner, QPCLock.count, GTCLock.owner, GTCLock.count])));
unlock(GTCLock);
unlock(QPCLock);
// outputdebugstring(pchar(format('unlocked: tid=%d, lockQPC=%d (%d), lockGTC=%d (%d)',[GetThreadID, QPCLock.owner, QPCLock.count, GTCLock.owner, GTCLock.count])));
// OutputDebugString('f');
end;
end.

View File

@ -42,7 +42,7 @@
<WindowIndex Value="0"/>
<TopLine Value="1"/>
<CursorPos X="8" Y="11"/>
<UsageCount Value="54"/>
<UsageCount Value="88"/>
<Loaded Value="True"/>
<LoadedDesigner Value="True"/>
<DefaultSyntaxHighlighter Value="Delphi"/>
@ -57,9 +57,9 @@
<IsVisibleTab Value="True"/>
<EditorIndex Value="0"/>
<WindowIndex Value="0"/>
<TopLine Value="87"/>
<CursorPos X="16" Y="107"/>
<UsageCount Value="54"/>
<TopLine Value="88"/>
<CursorPos X="39" Y="108"/>
<UsageCount Value="88"/>
<Loaded Value="True"/>
<LoadedDesigner Value="True"/>
<DefaultSyntaxHighlighter Value="Delphi"/>
@ -69,26 +69,26 @@
<WindowIndex Value="0"/>
<TopLine Value="23"/>
<CursorPos X="17" Y="27"/>
<UsageCount Value="26"/>
<UsageCount Value="23"/>
</Unit2>
<Unit3>
<Filename Value="C:\lazarus\fpc\2.4.1\source\rtl\inc\resh.inc"/>
<TopLine Value="40"/>
<CursorPos X="19" Y="56"/>
<UsageCount Value="12"/>
<UsageCount Value="9"/>
</Unit3>
<Unit4>
<Filename Value="C:\lazarus\fpc\2.4.1\source\rtl\win\wininc\func.inc"/>
<TopLine Value="45"/>
<CursorPos X="49" Y="59"/>
<UsageCount Value="12"/>
<UsageCount Value="9"/>
</Unit4>
<Unit5>
<Filename Value="C:\lazarus\fpc\2.4.1\source\rtl\win\wininc\base.inc"/>
<WindowIndex Value="0"/>
<TopLine Value="83"/>
<CursorPos X="25" Y="99"/>
<UsageCount Value="26"/>
<UsageCount Value="23"/>
</Unit5>
<Unit6>
<Filename Value="C:\lazarus\fpc\2.4.1\source\rtl\win64\classes.pp"/>
@ -96,21 +96,21 @@
<WindowIndex Value="0"/>
<TopLine Value="1"/>
<CursorPos X="1" Y="1"/>
<UsageCount Value="22"/>
<UsageCount Value="19"/>
</Unit6>
<Unit7>
<Filename Value="C:\lazarus\fpc\2.4.1\source\rtl\win\sysosh.inc"/>
<WindowIndex Value="0"/>
<TopLine Value="47"/>
<CursorPos X="3" Y="19"/>
<UsageCount Value="22"/>
<UsageCount Value="19"/>
</Unit7>
<Unit8>
<Filename Value="C:\lazarus\fpc\2.4.1\source\rtl\win\wininc\redef.inc"/>
<WindowIndex Value="0"/>
<TopLine Value="1"/>
<CursorPos X="10" Y="752"/>
<UsageCount Value="10"/>
<UsageCount Value="7"/>
</Unit8>
<Unit9>
<Filename Value="C:\lazarus\fpc\2.6.1\source\rtl\win\wininc\redef.inc"/>
@ -118,7 +118,7 @@
<WindowIndex Value="0"/>
<TopLine Value="734"/>
<CursorPos X="10" Y="748"/>
<UsageCount Value="10"/>
<UsageCount Value="27"/>
<Loaded Value="True"/>
</Unit9>
<Unit10>
@ -127,7 +127,7 @@
<WindowIndex Value="0"/>
<TopLine Value="2164"/>
<CursorPos X="6" Y="2178"/>
<UsageCount Value="10"/>
<UsageCount Value="27"/>
<Loaded Value="True"/>
</Unit10>
</Units>