File size: 10,948 Bytes
8739cbb | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 | --Copyright Cheat Engine
--local
UnityEngineTimeSetTimeScaleMethod=nil
function getOriginalCodeAndFiller(address, farjmp)
local original,filler
if type(address)~='number' then
address=getAddressSafe(address)
end
if address==nil then
return nil, 'invalid address'
end
local sl=createStringList()
local d=createDisassembler()
local size=0
local jmpsize=not farjmp and 5 or 14
while size<jmpsize do
d.disassemble(address)
local ldd=d.LastDisassembleData
local inst=ldd.opcode..' '..ldd.parameters
sl.add(inst)
size=size+#ldd.bytes
address=address+#ldd.bytes
end
original=sl.Text
if size-jmpsize>0 then
filler=string.format("nop %x", size-jmpsize)
else
filler=''
end
sl.destroy()
d.destroy()
return original,filler
end
function hookSpeedFunctions()
--print("hookSpeedFunctions")
if getAddressSafe("speedhack_wantedspeed")~=nil then
-- print("already hooked")
return true
end
UnityEngineTimeSetTimeScaleMethod=nil
local r,r2=injectCEHelperLib()
if not r then
messageDialog('error in injectCEHelperLib(): '..r2, mtError,mbOK)
return false
end
local result, data=autoAssemble([[
globalalloc(speedhack_wantedspeed,4)
speedhack_wantedspeed:
dd (float)1
globalalloc(speedhack_unityspeedhackmethod,4)
speedhack_unityspeedhackmethod:
dd 1
{$asm}
]])
if not result then
messageDialog(data)
return
end
-- print("allocated speedhack_wantedspeed")
if monoSettings.Value["SkipMonoSpeedhack"]~='1' then
local hasMono=getAddressSafe('mono_thread_attach',false,true) or getAddressSafe('il2cpp_thread_attach',false,true)
if hasMono then
LaunchMonoDataCollector()
local r=mono_findClass("UnityEngine","Time")
if r then
UnityEngineTimeSetTimeScaleMethod=mono_class_findMethod(r,"set_timeScale")
if UnityEngineTimeSetTimeScaleMethod then
local set_timeScale
if mono_isil2cpp() then
set_timeScale=mono_compile_method(UnityEngineTimeSetTimeScaleMethod)
else
set_timeScale=getAddressSafe("UnityEngine.Time:set_timeScale")
end
if set_timeScale then
local originalcode,filler=getOriginalCodeAndFiller(set_timeScale, true)
if originalcode then
local s=string.format([[
{$c}
extern int speedhack_unityspeedhackmethod;
extern float speedhack_wantedspeed;
float getNewSpeed(float newspeed)
{
float wanted=speedhack_wantedspeed; //bug in tcc needs local first
int method=speedhack_unityspeedhackmethod;
if (method==0) //method 0 : If speed==0, return 0, else return the speedhack speed
{
if (newspeed>0)
newspeed=wanted;
}
else //method 1: multiply the wanted speed by the speedhack speed. 0*wanted==0, 1*wanted=speedhack speed, 2*wanted=double speedhack speed. Only issue is if it where to read the old speed and apply it's own multiplication
newspeed=newspeed*wanted;
return newspeed;
}
{$asm}
alloc(set_timeScaleEntryHook,128)
registersymbol(set_timeScaleEntryHook)
label(returnhere)
set_timeScaleEntryHook:
sub rsp,20
call getNewSpeed //will change xmm0
add rsp,20
//previous version's implementation
// mov rax,speedhack_wantedspeed
// movss xmm0,[rax]
%s
jmp returnhere
%x:
jmp far set_timeScaleEntryHook
%s
returnhere:]], originalcode, set_timeScale, filler)
local r,err=autoAssemble(s)
if r then return true end --just this should be enough
--print(s)
return true
end
end
else
print("no UnityEngineTimeSetTimeScaleMethod")
end
end
end
end
local gtcaddress=getAddressSafe('kernel32.gettickcount64')
if gtcaddress==nil then
waitforExports()
gtcaddress=getAddressSafe('kernel32.gettickcount64')
if (gtcaddress==nil) then
reinitializeSymbolhandler()
gtcaddress=getAddressSafe('kernel32.gettickcount64')
if (gtcaddress==nil) then
messageDialog('Failure finding kernel32.gettickcount64', mtError, mbOK)
return false
end
end
end
local originalcode,filler=getOriginalCodeAndFiller(gtcaddress)
if originalcode then
local s=string.format([[
alloc(gtc_originalcode,64,"kernel32.gettickcount64")
label(gtc_returnhere)
label(gtchook_exit)
{$c}
#include <stdint.h>
#include <stddef.h>
#include <celib.h>
#include <windowslite.h>
__stdcall uint64_t gtc_originalcode(void);
float gtc_speed=1.0f;
uint64_t gtc_initialtime=0;
uint64_t gtc_initialoffset=0;
CRITICAL_SECTION gtc_cs;
void *gtc_initonce=(void*)0;
int gtc_intialized=0;
extern float speedhack_wantedspeed;
__stdcall int InitOnceExecuteOnce(void *InitOnce, void* InitFn, void* Parameter, void *Context);
__stdcall int initgtc_cs(void *InitOnce, void *Parameter, void *lpContext) {
InitializeCriticalSection(>c_cs);
gtc_intialized=1;
return TRUE;
}
__stdcall uint64_t new_gettickcount(void)
{
uint64_t newtime;
uint64_t currenttime;
float wantedspeed; //small issue with tcc where you can not compare against extern directly
currenttime=gtc_originalcode();
if (gtc_intialized==0) //only set to true once the cs has been initialized
InitOnceExecuteOnce(>c_initonce, initgtc_cs, NULL, NULL);
//after here, gtc_cs is initialized
EnterCriticalSection(>c_cs);
//csenter(>c_cs);
wantedspeed=speedhack_wantedspeed;
if (gtc_initialtime==0)
{
gtc_initialtime=currenttime;
gtc_initialoffset=currenttime;
}
newtime=(currenttime-gtc_initialtime)*gtc_speed;
newtime=newtime+gtc_initialoffset; //don't put in in the calculation above, as it gets converted to float, and truncated
if (gtc_speed!=wantedspeed)
{
//the user wants to change the speed
gtc_initialoffset=newtime;
gtc_initialtime=currenttime;
gtc_speed=speedhack_wantedspeed;
}
LeaveCriticalSection(>c_cs);
return newtime;
}
{$asm}
gtc_originalcode:
%s
gtchook_exit:
jmp gtc_returnhere
kernel32.gettickcount64:
jmp new_gettickcount
%s
gtc_returnhere:
{$ifdef kernel32.timegettime}
kernel32.timeGetTime:
jmp new_gettickcount
{$endif}
kernel32.getTickCount:
jmp new_gettickcount
]],originalcode, filler)
local result, data=autoAssemble(s)
if not result then
if data==nil then
data=' (no reason)'
end
messageDialog('Failure hooking kernel32.gettickcount64:'..data, mtError, mbOK)
end
end;
--queryPerformanceCounter
local qpcaddress=getAddressSafe('ntdll.RtlQueryPerformanceCounter')
if qpcaddress==nil then
waitforExports()
qpcaddress=getAddressSafe('ntdll.RtlQueryPerformanceCounter')
if (qpcaddress==nil) then
reinitializeSymbolhandler()
qpcaddress=getAddressSafe('ntdll.RtlQueryPerformanceCounter')
if (qpcaddress==nil) then
messageDialog('Failure finding kernel32.gettickcount64', mtError, mbOK)
return false
end
end
end
local originalcode,filler=getOriginalCodeAndFiller(qpcaddress)
if originalcode then
--speedhack does not disable. Just sets speed to 1 when done
local s=string.format([[
alloc(qpc_originalcode,64,"ntdll.RtlQueryPerformanceCounter")
label(qpc_returnhere)
label(qpchook_exit)
{$c}
#include <stdint.h>
#include <stddef.h>
#include <celib.h>
#include <windowslite.h>
__stdcall int qpc_originalcode(uint64_t *count);
float qpc_speed=1.0f;
uint64_t qpc_initialtime=0;
uint64_t qpc_initialoffset=0;
CRITICAL_SECTION qpc_cs;
void *qpc_initonce=(void*)0;
int qpc_intialized=0;
uint64_t qpc_lastresult=0;
extern float speedhack_wantedspeed;
__stdcall int InitOnceExecuteOnce(void *InitOnce, void* InitFn, void* Parameter, void *Context);
__stdcall int initqpc_cs(void *InitOnce, void *Parameter, void *lpContext) {
InitializeCriticalSection(&qpc_cs);
qpc_intialized=1;
return TRUE;
}
__stdcall int new_RtlQueryPerformanceCounter(uint64_t *count)
{
uint64_t newtime;
uint64_t currenttime;
uint64_t newwantedspeed;
float wantedspeed; //small issue with tcc where you can not compare against extern directly
if (qpc_intialized==0) //only set to true once the cs has been initialized
InitOnceExecuteOnce(&qpc_initonce, initqpc_cs, NULL, NULL);
//after here, gtc_cs is initialized
EnterCriticalSection(&qpc_cs);
int result=qpc_originalcode(¤ttime);
wantedspeed=speedhack_wantedspeed;
if (qpc_initialtime==0)
{
qpc_initialtime=currenttime;
qpc_initialoffset=currenttime;
}
newtime=(currenttime-qpc_initialtime)*qpc_speed;
newtime=newtime+qpc_initialoffset;
if (qpc_speed!=wantedspeed)
{
//the user wants to change the speed
qpc_initialoffset=newtime;
qpc_initialtime=currenttime;
qpc_speed=speedhack_wantedspeed;
}
LeaveCriticalSection(&qpc_cs);
*count=newtime;
return result;
}
{$asm}
qpc_originalcode:
%s
qpchook_exit:
jmp qpc_returnhere
ntdll.RtlQueryPerformanceCounter:
jmp new_RtlQueryPerformanceCounter
%s
qpc_returnhere:
]],originalcode, filler)
local result2, data2=autoAssemble(s)
if not result2 then
if data2==nil then
data2=' (no reason)'
end
messageDialog('Failure hooking ntdll.RtlQueryPerformanceCounter:'..data2, mtError, mbOK)
end
end;
return result or result2
end
registerSpeedhackCallbacks(function() --OnActivate
if (not isConnectedToCEServer()) and targetIsX86() then
local result, errormsg
if getAddressSafe("speedhack_wantedspeed")==nil then
--still needs hooking
result,errormsg=hookSpeedFunctions()
else
result=true
end
return true, result, errormsg
else
return false
end
end,
function(speed) --OnSetSpeed(speed)
if (not isConnectedToCEServer()) and targetIsX86() then
local result, errormsg
if getAddressSafe("new_gettickcount")==nil or getAddressSafe("speedhack_wantedspeed")==nil then
result,errormsg=hookSpeedFunctions()
if not result then return true, false, errormsg end
end
writeFloat("speedhack_wantedspeed", speed)
if UnityEngineTimeSetTimeScaleMethod then
mono_invoke_method(nil,UnityEngineTimeSetTimeScaleMethod,nil,{speed})
end
result=true
return true, true
else
return false
end
end)
|