  | 
				
				Cheat Engine The Official Site of Cheat Engine   
				
 
				 | 
			 
		 
		 
	
		| View previous topic :: View next topic   | 
	 
	
	
		| Author | 
		Message | 
	 
	
		Beginner999 Cheater
  Reputation: 0
  Joined: 27 Jul 2018 Posts: 25
 
  | 
		
			
				 Posted: Tue Aug 13, 2024 11:35 am    Post subject: Get Process path | 
				       | 
			 
			
				
  | 
			 
			
				I am trying to use Lua to edit some files from the game directly.
 
The target file is player.json which stay in the same directory folder with game.exe
 
 	  | Code: | 	 		  | enumModules()[1].PathToFile | 	  
 
 
I tried this to get the path to game.exe but it kept return to error
 
when I tried to modify it
 
 	  | Code: | 	 		  | enumModules("game.exe")[1].PathToFile | 	  
 
 
it return to 
 
C:\Program Files\Cheat Engine 7.5\
 
 
Any idea what's wrong?   
 | 
			 
		  | 
	 
	
		| Back to top | 
		 | 
	 
	
		  | 
	 
	
		ParkourPenguin I post too much
  Reputation: 152
  Joined: 06 Jul 2014 Posts: 4706
 
  | 
		
			
				 Posted: Tue Aug 13, 2024 12:52 pm    Post subject:  | 
				       | 
			 
			
				
  | 
			 
			
				 	  | Beginner999 wrote: | 	 		   	  | Code: | 	 		  | enumModules()[1].PathToFile | 	 
  | 	  Works fine for me
 
 
Post your code
 _________________
 I don't know where I'm going, but I'll figure it out when I get there.  | 
			 
		  | 
	 
	
		| Back to top | 
		 | 
	 
	
		  | 
	 
	
		Beginner999 Cheater
  Reputation: 0
  Joined: 27 Jul 2018 Posts: 25
 
  | 
		
			
				 Posted: Tue Aug 13, 2024 12:55 pm    Post subject:  | 
				       | 
			 
			
				
  | 
			 
			
				Here is my code
 
 	  | Code: | 	 		  -- Get the executable path using enumModules
 
local exepath = enumModules("Game.exe")[1].PathToFile
 
 
-- Extract the directory path from the executable path
 
local gameDir = exepath:match("(.*[/\\])")
 
local filePath = gameDir .. "player.json"
 
 
print("File path: " .. filePath)
 
 
local searchPattern = '"misc/ms/":2'
 
local replacePattern = '"misc/ms/":4'
 
 
-- Function to read the contents of the file
 
local function readFile(filePath)
 
    local file = io.open(filePath, "r")
 
    if not file then return nil end
 
    local content = file:read("*all")
 
    file:close()
 
    return content
 
end
 
 
-- Function to write content back to the file
 
local function writeFile(filePath, content)
 
    local file = io.open(filePath, "w")
 
    if not file then return false end
 
    file:write(content)
 
    file:close()
 
    return true
 
end
 
 
-- Read the original content
 
local content = readFile(filePath)
 
if content then
 
    -- Replace the specific line
 
    content = content:gsub(searchPattern, replacePattern)
 
 
    -- Write the modified content back to the file
 
    local success = writeFile(filePath, content)
 
 
    if success then
 
        print("File updated successfully.")
 
    else
 
        print("Failed to write to the file.")
 
    end
 
else
 
    print("Failed to read the file.")
 
end | 	  
 | 
			 
		  | 
	 
	
		| Back to top | 
		 | 
	 
	
		  | 
	 
	
		AylinCE Grandmaster Cheater Supreme
  Reputation: 37
  Joined: 16 Feb 2017 Posts: 1530
 
  | 
		
			
				 Posted: Tue Aug 13, 2024 2:35 pm    Post subject:  | 
				       | 
			 
			
				
  | 
			 
			
				This will replace all matching directories.
 
 
If you want it to be a single directory or a specific order, tell us what it should be.
 
 
 	  | Code: | 	 		  function lineReplace(pth,src,rpl)
 
local filePath = pth
 
local content1 = ""
 
local file1 = io.open(filePath, "r")
 
    if not file1 then return nil end
 
    for line in file1:lines() do
 
      if string.find(line, src) then
 
        newline = string.gsub(line, src,rpl)
 
        content1 = content1 .. newline .. "\n"
 
      else
 
        content1 = content1 .. line .. "\n"
 
      end
 
    end
 
    file1:close()
 
    local file2 = io.open(filePath, "w")
 
    file2:write(content1)
 
    file2:close()
 
end
 
 
proc_fileandpath = enumModules()[1].PathToFile
 
gameDir = extractFilePath(proc_fileandpath)
 
--print(gameDir)
 
 
local searchPattern = '"misc/ms/":2'
 
local replacePattern = '"misc/ms/":4'
 
local fileDir = gameDir .. 'player.json'
 
 
lineReplace(fileDir,searchPattern,replacePattern) | 	  
 _________________
  | 
			 
		  | 
	 
	
		| Back to top | 
		 | 
	 
	
		  | 
	 
	
		ParkourPenguin I post too much
  Reputation: 152
  Joined: 06 Jul 2014 Posts: 4706
 
  | 
		
			
				 Posted: Tue Aug 13, 2024 4:03 pm    Post subject:  | 
				       | 
			 
			
				
  | 
			 
			
				I'd make minor changes, but overall, it works fine for me. What about it doesn't work for you?
 
 	  | Code: | 	 		  local exepath = enumModules("Game.exe")[1].PathToFile
 
 
local gameDir = extractFilePath(exepath)
 
local filePath = gameDir .. "player.json"
 
 
print('EXE path:', exepath)
 
print('File path:', filePath)
 
 
local searchPattern = '"misc/ms/":2'
 
local replacePattern = '"misc/ms/":4'
 
 
 
local function readFile(filePath)
 
  local file, err = io.open(filePath, "r")
 
  if not file then
 
    return file, err
 
  end
 
 
  local content, err2 = file:read'a'
 
  local closed, err3 = file:close()
 
 
  if not content then
 
    return content, err2
 
  end
 
  if not closed then
 
    print('Warning:', err3)
 
  end
 
 
  return content
 
end
 
 
local function writeFile(filePath, content)
 
  local file, err = io.open(filePath, "w")
 
  if not file then
 
    return file, err
 
  end
 
 
  local written, err2 = file:write(content)
 
  local closed, err3 = file:close()
 
 
  if not written then
 
    return written, err2
 
  end
 
  if not closed then
 
    print('Warning:', err3)
 
  end
 
  return true
 
end
 
 
 
local content, n_matches = assert(readFile(filePath)):gsub(searchPattern, replacePattern)
 
 
if n_matches < 1 then
 
  print'Warning: pattern not found'
 
else
 
  assert(writeFile(filePath, content))
 
end | 	  
 _________________
 I don't know where I'm going, but I'll figure it out when I get there.  | 
			 
		  | 
	 
	
		| Back to top | 
		 | 
	 
	
		  | 
	 
	
		 | 
	 
 
  
	 
	    
	   | 
	
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
  | 
   
 
		 |