Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


Check if file exists, file name in memory?
Goto page 1, 2  Next
 
Post new topic   Reply to topic    Cheat Engine Forum Index -> Cheat Engine Lua Scripting
View previous topic :: View next topic  
Author Message
thethiny
Cheater
Reputation: 0

Joined: 01 Apr 2012
Posts: 38
Location: earth

PostPosted: Sat Jul 16, 2016 12:37 pm    Post subject: Check if file exists, file name in memory? Reply with quote

Hi,
I'm building a MOD Loader for MKX, and I need to check for a file that exists, I'll tell you what I'm doing and correct me if I'm wrong.

I have a string @ Location [rdx-80], let's assume it is Asset\File1.
Now the game will load Asset\File1 as Player1. What I want to do is write a script that loads Asset\File1 from the memory, and then change it to Loadr\File1 and check if the file exists or not, if it exists then I will copy the new string (Loadr\File1) into [rdx-80], if it doesn't exist then I'm gonna jmp to end.

Is there a way to do this using the Auto Assembler? (Note that I actually can do this manually by manually writing the value byte by byte, so I know for sure that I can load Loadr\File1).

Thanks in advance, Hitler.
Back to top
View user's profile Send private message
akumakuja28
Master Cheater
Reputation: 16

Joined: 28 Jun 2015
Posts: 432

PostPosted: Sat Jul 16, 2016 1:07 pm    Post subject: Reply with quote

IM a little confused at what your asking but this how you check if a file exists in LUA.





Code:


FileToCheck = [[C:\Program Files\WinRAR\WinRar.exe]]


FileToOpen = io.open (FileToCheck)

if FileToOpen ~= nil
then
print('File is On Disk')
else
print('No File Exist')
end


_________________
Back to top
View user's profile Send private message
thethiny
Cheater
Reputation: 0

Joined: 01 Apr 2012
Posts: 38
Location: earth

PostPosted: Sat Jul 16, 2016 1:22 pm    Post subject: Reply with quote

Thanks for the reply,
What I want exactly is how to load the file name from memory. Is there a way to do that? through ASM.
Back to top
View user's profile Send private message
ParkourPenguin
I post too much
Reputation: 140

Joined: 06 Jul 2014
Posts: 4297

PostPosted: Sat Jul 16, 2016 1:44 pm    Post subject: Reply with quote

Load it into what? A 32-bit register can only hold 32 bits (i.e. 4 chars). If you want to copy it to another memory region, you can do it 4 bytes at a time through registers, use rep stos, or some library function (e.g. msvcrt.strcpy).

To check if a file exists or not, you can use PathFileExists.

_________________
I don't know where I'm going, but I'll figure it out when I get there.
Back to top
View user's profile Send private message
thethiny
Cheater
Reputation: 0

Joined: 01 Apr 2012
Posts: 38
Location: earth

PostPosted: Sat Jul 16, 2016 2:32 pm    Post subject: Reply with quote

Apparently I'm not explaining it well.

Okay listen guys.

Currently the memory has this @ Address 500 for example: Asset\File1
I want to check if file Loadr\File1 exists.

if the memory has Asset\File7 @ Address 500, then I want to check if Loadr\File7 exists. This is what I'm asking for. Asset\File1 exists in the memory and I know where, but Loadr\File1 may or may not exist.
Back to top
View user's profile Send private message
ParkourPenguin
I post too much
Reputation: 140

Joined: 06 Jul 2014
Posts: 4297

PostPosted: Sat Jul 16, 2016 2:43 pm    Post subject: Reply with quote

Quote:
I want to check if file Loadr\File1 exists.
Do you know where that string would be stored? If so, just compare it either 4 bytes at a time or via strcmp. If not, find it. If it's not a string and is an actual file on disk, see my previous post.
Quote:
if the memory has Asset\File7 @ Address 500...
You just said Asset\File1 is at that address.
Quote:
I want to check if Loadr\File7 exists.
Same thing for checking Loadr\File1.
_________________
I don't know where I'm going, but I'll figure it out when I get there.
Back to top
View user's profile Send private message
thethiny
Cheater
Reputation: 0

Joined: 01 Apr 2012
Posts: 38
Location: earth

PostPosted: Sat Jul 16, 2016 2:54 pm    Post subject: Reply with quote

ParkourPenguin wrote:
Quote:
I want to check if file Loadr\File1 exists.
Do you know where that string would be stored? If so, just compare it either 4 bytes at a time or via strcmp. If not, find it. If it's not a string and is an actual file on disk, see my previous post.
Quote:
if the memory has Asset\File7 @ Address 500...
You just said Asset\File1 is at that address.
Quote:
I want to check if Loadr\File7 exists.
Same thing for checking Loadr\File1.


Address 500 stores the file to be loaded inside. So when you hover at the first player, it will load file1, if you have at the second player, it will load file2. All of these loads happen inside the folder Asset, All I want to do is FORCE the game to load it from the folder Loadr instead of the folder Asset ONLY IF Loadr\File is found.
Address 500 is shared between all players, it changes dynamically.
Back to top
View user's profile Send private message
ParkourPenguin
I post too much
Reputation: 140

Joined: 06 Jul 2014
Posts: 4297

PostPosted: Sat Jul 16, 2016 3:16 pm    Post subject: Reply with quote

So get the string of the file to be loaded from that address (I'd recommend simply mov instructions), append it to the string "Loadr\" (don't forget to zero terminate it), and call PathFileExists to check if it exists. If it returns true (1), replace "Asset" with "Loadr" in the address of the file to be loaded via mov instructions (recommended), rep stos, or strcpy.
_________________
I don't know where I'm going, but I'll figure it out when I get there.
Back to top
View user's profile Send private message
akumakuja28
Master Cheater
Reputation: 16

Joined: 28 Jun 2015
Posts: 432

PostPosted: Sat Jul 16, 2016 3:17 pm    Post subject: Reply with quote

I remember a question like this for this game before.


Is it a string load?

Meaning if you change the Asset string(File Location) in memory it loads a different asset?

_________________
Back to top
View user's profile Send private message
thethiny
Cheater
Reputation: 0

Joined: 01 Apr 2012
Posts: 38
Location: earth

PostPosted: Sat Jul 16, 2016 4:02 pm    Post subject: Reply with quote

ParkourPenguin wrote:
So get the string of the file to be loaded from that address (I'd recommend simply mov instructions), append it to the string "Loadr\" (don't forget to zero terminate it), and call PathFileExists to check if it exists. If it returns true (1), replace "Asset" with "Loadr" in the address of the file to be loaded via mov instructions (recommended), rep stos, or strcpy.

Can you show me an example of PathFileExists? I did what you told me (the code will be below) and it works fine without the If check.



akumakuja28 wrote:
I remember a question like this for this game before.


Is it a string load?

Meaning if you change the Asset string(File Location) in memory it loads a different asset?

Yes EXACTLY!



Here's the code that works now but still missing the File Check:
Code:

alloc(newmem,2048,"MK10.exe"+13FF5C4)
alloc(DATA,128)
label(returnhere)
label(originalcode)
label(exit)


newmem: //this is allocated memory, you have read,write,execute access
//place your code here
movaps [DATA],xmm0
movaps xmm1,[rax-70]
movaps [DATA+10],xmm1
cmp Byte Ptr [DATA],2E
jnz originalcode
cmp Byte Ptr [DATA+1],2E
jnz originalcode
cmp Byte Ptr [DATA+2],5C
jnz originalcode
cmp Byte Ptr [DATA+3],41
jnz originalcode
cmp Byte Ptr [DATA+4],73
jnz originalcode
cmp Byte Ptr [DATA+5],73
jnz originalcode
cmp Byte Ptr [DATA+6],65
jnz originalcode
cmp Byte Ptr [DATA+7],74
jnz originalcode
cmp Byte Ptr [DATA+8],5C
jnz originalcode
mov Byte Ptr [DATA+3], 4C
mov Byte Ptr [DATA+4], 6F
mov Byte Ptr [DATA+5], 61
mov Byte Ptr [DATA+6], 64
mov Byte Ptr [DATA+7], 72
mov Byte Ptr [DATA+8], 5C
movaps xmm0,[DATA]
movaps xmm1,[DATA+10]
movaps [rdx-80],xmm0
jmp returnhere

originalcode:
movaps [rdx-80],xmm0
movaps xmm1,[rax-70]

exit:
jmp returnhere

"MK10.exe"+13FF5C4:
jmp newmem
nop
nop
nop
returnhere:
Back to top
View user's profile Send private message
ParkourPenguin
I post too much
Reputation: 140

Joined: 06 Jul 2014
Posts: 4297

PostPosted: Sat Jul 16, 2016 4:27 pm    Post subject: This post has 1 review(s) Reply with quote

You can move and compare 4 bytes at a time, as well as reference characters directly (CE will convert them to ASCII hex).
Code:
alloc(newmem,1024,"MK10.exe"+13FF5C4)
alloc(DATA,128)
label(returnhere)
label(exit)

newmem:
  movaps [rdx-80],xmm0
  movaps xmm1,[rax-70]
  cmp [rdx-80],'..\A'
  jne short exit
  cmp [rdx-84],'sset'
  jne short exit
  cmp byte ptr[rdx-88],'\'
  jne short exit
  mov [rdx-80],'..\L'
  mov [rdx-84],'oadr'
  // last char already '\'
  movaps xmm0,[rdx-80] // only required if game reads from xmm0 again
exit:
  jmp returnhere

"MK10.exe"+13FF5C4:
  jmp newmem
  nop
  nop
  nop
returnhere:


Example using PathFileExists (64-bit):
Code:
globalalloc(derp,2048)
label(examplePath)
label(returnedValue)

createthread(derp)

derp:
  sub rsp,20
  lea rcx,[examplePath]
  call SHLWAPI.PathFileExistsA
  mov [returnedValue],eax
  add rsp,20
  ret

derp+100:
returnedValue:
  dd 0

derp+104:
examplePath:
  db 'C:\Program Files (x86)\Cheat Engine 6.5.1\Cheat Engine.exe',0

_________________
I don't know where I'm going, but I'll figure it out when I get there.
Back to top
View user's profile Send private message
thethiny
Cheater
Reputation: 0

Joined: 01 Apr 2012
Posts: 38
Location: earth

PostPosted: Sat Jul 16, 2016 4:48 pm    Post subject: Reply with quote

Thank you very much for the help, now this is my final code, but I'm getting an "offset too big" error. Do you think you can tell where the issue resides?

Code:

alloc(newmem,2048,"MK10.exe"+13FF5C4)
alloc(DATA,128)
label(returnhere)
label(originalcode)
label(file)
label(exit)

label(BOOL)
createthread(file)

jmp newmem

file:
  push rcx
  push rax
  sub rsp,20
  lea rcx,[DATA]
  call SHLWAPI.PathFileExistsA
  mov [BOOL],eax
  add rsp,20
  pop rcx
  pop rax
  ret

file+100:
BOOL:
  dd 0




newmem: //this is allocated memory, you have read,write,execute access
//place your code here
movaps [DATA],xmm0
movaps xmm1,[rax-70]
movaps [DATA+10],xmm1
cmp [DATA],'..\A'
jnz originalcode
cmp [DATA+4],'sset'
jnz originalcode
cmp Byte Ptr [DATA+8],'\'
jnz originalcode
mov [DATA+3],'Load'
mov Word Ptr [DATA+7],'r\'

call file //Try to open ..\Loadr\YourFile
cmp [BOOL],1
jnz originalcode

movaps xmm0,[DATA]
movaps xmm1,[DATA+10]
movaps [rdx-80],xmm0
jmp returnhere

originalcode:
movaps [rdx-80],xmm0
movaps xmm1,[rax-70]

exit:
jmp returnhere

"MK10.exe"+13FF5C4:
jmp newmem
nop
nop
nop
returnhere:
Back to top
View user's profile Send private message
ParkourPenguin
I post too much
Reputation: 140

Joined: 06 Jul 2014
Posts: 4297

PostPosted: Sat Jul 16, 2016 5:21 pm    Post subject: Reply with quote

When do you get that error? Does it tell you a line number? Does the game crash?

It might be because you're not backing up a register that's being modified by the call (see x64 calling conventions register usage). You probably don't have to worry about the xmm or FPU registers, but RAX, RCX, RDX, and R8-R11 should be backed up if you don't know if the game is using them or not.

I didn't expect you to just copy and paste it. At least remove the createthread. Unless you plan on calling that from some other script, it would be best to put it inline with everything else.

Also make sure the file path string is zero-terminated.

_________________
I don't know where I'm going, but I'll figure it out when I get there.
Back to top
View user's profile Send private message
thethiny
Cheater
Reputation: 0

Joined: 01 Apr 2012
Posts: 38
Location: earth

PostPosted: Sat Jul 16, 2016 5:30 pm    Post subject: Reply with quote

I'm sorry but I don't know what the createthread does, I'm fairly new to Cheat Engine modding.

Anyways, It just tells me that error with no line whatsoever, I noticed that commenting the BOOL: DD 0 line will get rid of that error but instead gives me another error which is BOOL was not declared. I did some googling and they told me that the distance between the FileCreateA and the Game must not be greater than 2GB, maybe that's it?
About the registers, well if I don't backup them all up, then the game simply crashes.


Edit:
Okay I moved BOOL to DATA+7C and I no longer have the offset issue. But the game now freezes.

Edit2:
Okay so the FileCreate sets XMM0 to 0, I had to backup that too. This fixed the crash, but there's a new issue. The function call ALWAYS returns 0! Why is that?
Back to top
View user's profile Send private message
ParkourPenguin
I post too much
Reputation: 140

Joined: 06 Jul 2014
Posts: 4297

PostPosted: Sat Jul 16, 2016 6:23 pm    Post subject: Reply with quote

https://en.wikipedia.org/wiki/Thread_%28computing%29 (there's also a simple wiki page if you don't care for the details)

Now that you mention it, file+100 is probably more than 2GB away from newmem which causes the instruction cmp [BOOL],1 to fail (it's not encodable). Pass it back via a register (i.e. eax) instead, or just put it inline like I suggested.

I also just now noticed you're swapping rax and rcx by popping them in the wrong order. Elements are accessed from the stack on a last-in-first-out basis.

If PathFileExists always returns 0, then it's always failing to find the file specified. Try executing my example script in the game but instead of CE's path use '..\Loadr\FileName.txt',0 (replace FileName.txt with a file you know is there). Add the address "derp+100" (4-byte) to your cheat table manually to see the result. Fiddle around with it until it returns 1.

_________________
I don't know where I'm going, but I'll figure it out when I get there.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> Cheat Engine Lua Scripting All times are GMT - 6 Hours
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites