Create directory

Want Cheat Engine to do something specific and no idea how to do that, ask here. (From simple scripts to full trainers and extensions)
Post Reply
User avatar
Horse4Horse
Expert Cheater
Expert Cheater
Posts: 76
Joined: Thu Aug 02, 2018 7:54 pm
Reputation: 29

Create directory

Post by Horse4Horse »

Hi. I'm now digging in some chinese program and want to create some directory paths.
Everything is working as intended, I can create files with chinese name.
But when it comes to creating folder with chinese chars in it's name - I'm totally stuck.

I'm using windows "mkdir" command with the full and correct path

Code: Select all

Destination = "C:\\Some\\Folders\\格力2-ABC\\"
os.execute("mkdir \""..Destination.."\"")
os.execute is returning "true" and path is created indeed . But all chinese symbols are messed up and this leads to creating path
"C:\Some\Folders\ж јеЉ›2-ABC" and now "writeRegionToFile" cant find "Destination" folder.

I tried using "lfs.mkdir" command, but seems that it uses the same method as previous and path is messed up in the same way.

So I want to create folder with correct name, or at least just check if folder is created correctly. Is it somehow possible without sanitizing "Destination" string?

User avatar
LeFiXER
LeFixer
LeFixer
Posts: 482
Joined: Wed Mar 24, 2021 9:35 am
Reputation: 243

Re: Create directory

Post by LeFiXER »

This is because Lua is ANSI C based and Chinese characters are unicode. You will need an external library to handle such strings which you can find at [Link]. At least that's the only way I know of. If someone else knows a better way that I am more than welcome to listen :).

User avatar
Horse4Horse
Expert Cheater
Expert Cheater
Posts: 76
Joined: Thu Aug 02, 2018 7:54 pm
Reputation: 29

Re: Create directory

Post by Horse4Horse »

LeFiXER wrote:
Tue May 10, 2022 3:59 pm
This is because Lua is ANSI C based and Chinese characters are unicode. You will need an external library to handle such strings which you can find at [Link]. At least that's the only way I know of. If someone else knows a better way that I am more than welcome to listen :).
But it's more over-engineered than just checking path to contain only english letters, numbers and symbols.
Anyway, Thanks, I'll look into it - library seems very useful at first glance.

User avatar
LeFiXER
LeFixer
LeFixer
Posts: 482
Joined: Wed Mar 24, 2021 9:35 am
Reputation: 243

Re: Create directory

Post by LeFiXER »

It is a useful library that contains a lot of Windows-specific things that you may find useful.

Paul44
Table Makers
Table Makers
Posts: 752
Joined: Thu Jul 27, 2017 9:02 am
Reputation: 437

Re: Create directory

Post by Paul44 »

^ you should also check this out: [ [Link] ]. This library is integrated in CE (using it myself in my tables), but "obviously" i'm not aware if it supports multi_char sets (no mentioning @ github).
If it does, please report back.

User avatar
Horse4Horse
Expert Cheater
Expert Cheater
Posts: 76
Joined: Thu Aug 02, 2018 7:54 pm
Reputation: 29

Re: Create directory

Post by Horse4Horse »

Paul44 wrote:
Tue May 10, 2022 6:09 pm
^ you should also check this out: [ [Link] ]. This library is integrated in CE (using it myself in my tables), but "obviously" i'm not aware if it supports multi_char sets (no mentioning @ github).
If it does, please report back.
As I said in the topic - lfs.mkdir not helped - it created the same dir as native windows "mkdir".
And with default settings - generated trainers does not include it.

Also, I just found in celua one useful thing called "ansiToUtf8(string)" - thanks LeFiXER for mentioning ANSI.
So I can just use

Code: Select all

if ansiToUtf8(FilePath) ~= FilePath then print("Failed.") end
To see, if path will be broken and use exe's root dir instead.
LeFiXER wrote:
Tue May 10, 2022 4:29 pm
It is a useful library that contains a lot of Windows-specific things that you may find useful.
Yup, I think it's better to include one library to bypass all the problems I can encounter in the future. Thanks again!
Now I just need to figure out, how to compile it and include in the CT and EXE(As I think - include it in cheat table lua script and then add in exe's "extra files") . Never did that before.

User avatar
Horse4Horse
Expert Cheater
Expert Cheater
Posts: 76
Joined: Thu Aug 02, 2018 7:54 pm
Reputation: 29

Re: Create directory

Post by Horse4Horse »

Nwm, figured it out, but still without 32bit version
Well, seems that I just can't compile "winapi" library - tried gcc, msvc, even luarocks.
Only gcc succeeded - but when it comes to loading dll with CE - it throws an error like

Code: Select all

main.lua error:error loading module "winapi" from file
"C:\ProgramFiles\CheatEngine 7.2\winapi.dll": ?? ?????? ????????? ??????.
Or last part is changing to "%1 ?? ???????? ??????????? Win32" if I'm trying to load it with 32bit CE - because I cant compile dll with 32bit lua5.1.dll
I'm sorry for the dumb question, but can someone send me working dll? I can't find any.[/i]
Well, just used "lua53-64.dll" and it's headers from Cheat Engine folder, as Dark Byte said [Link].
Now library loads and it's commands are executed correctly.
But anyway - winapi.make_dir() is creating folder with the same broken name. So, full day spent for nothing.

User avatar
LeFiXER
LeFixer
LeFixer
Posts: 482
Joined: Wed Mar 24, 2021 9:35 am
Reputation: 243

Re: Create directory

Post by LeFiXER »

Horse4Horse wrote:
Wed May 11, 2022 3:49 pm
Nwm, figured it out, but still without 32bit version
Well, seems that I just can't compile "winapi" library - tried gcc, msvc, even luarocks.
Only gcc succeeded - but when it comes to loading dll with CE - it throws an error like

Code: Select all

main.lua error:error loading module "winapi" from file
"C:\ProgramFiles\CheatEngine 7.2\winapi.dll": ?? ?????? ????????? ??????.
Or last part is changing to "%1 ?? ???????? ??????????? Win32" if I'm trying to load it with 32bit CE - because I cant compile dll with 32bit lua5.1.dll
I'm sorry for the dumb question, but can someone send me working dll? I can't find any.[/i]
Well, just used "lua53-64.dll" and it's headers from Cheat Engine folder, as Dark Byte said [Link].
Now library loads and it's commands are executed correctly.
But anyway - winapi.make_dir() is creating folder with the same broken name. So, full day spent for nothing.
Use winapi.short_path()

panraven
Table Makers
Table Makers
Posts: 124
Joined: Fri Mar 03, 2017 12:03 am
Reputation: 112

Re: Create directory

Post by panraven »

May try this. The function should be able to make for other usage, eg. rmdir etc.

Code: Select all

function cmd_utf8(cmd_tmplate, debugCheck)
  return function(...)-- string parameter to be put in template
    local tmp, fms = os.getenv'TEMP', string.format
    local name = os.tmpname():match[[([^\]*)$]]
    local cmd = fms([[%s\%s.cmd]],tmp,name)
    if debugCheck then print('path:',cmd)end-- for debug
    -- sending utf txt by a cmd file instead via command-line
    -- chcp 65001 -> utf8 encoding
    local cmdtxt = fms([[
    @echo off
    chcp 65001
    ]]..cmd_tmplate, ... )--  string params from input, each %s in template should match count of ... (varargs)
    if debugCheck then print('cmd--\n',cmdtxt,'\n--') end
    local f = io.open(cmd,'wb')
    if not f then return nil,'fail open cmd'else
      local ok = f:write(cmdtxt),f:close()
      if not ok then return nil,'fail write cmd'else
        shellExecute('cmd',fms('/c call "%s" && del /Q "%s"',cmd, cmd),nil,false)
        return true
      end
    end
  end
end
-- test
local mkdir_utf8 = cmd_utf8([[
    mkdir "%s"
 ]],true)--<-- remove true for no debug print
mkdir_utf8[[d:\00exe\逆転裁判]]-- it may need to be a full path, for it executed from a cmd at temp dir.

User avatar
Horse4Horse
Expert Cheater
Expert Cheater
Posts: 76
Joined: Thu Aug 02, 2018 7:54 pm
Reputation: 29

Re: Create directory

Post by Horse4Horse »

LeFiXER wrote:
Wed May 11, 2022 8:05 pm
Use winapi.short_path()
I tried, but it does not create folders, only files in existing folders

Code: Select all

testfolder = winapi.short_path("testfolder\\testfile")
testfile = winapi.short_path("testfile")

print(tostring(testfolder))
print(testfile)

nil 
testfile 
Maybe I just don't understand something, but api's example file "testshort.lua" does not create folders too.
panraven wrote:
Thu May 12, 2022 2:29 am
May try this. The function should be able to make for other usage, eg. rmdir etc.
Many thanks, sir panraven, you're always a savior.
Firstly I tried creating a pipe to cmd and then passing commands, but it didn't helped.
Then I thought about creating batch file with mkdir and path string, but stuck at the moment of writing to file the string I wanted. Totally forgot about lua's io.open.
Your code is working great, even does not blink with unnecessary CMD window. But for me - sometimes it does not delete cmd file if command is executed repeatedly, and with random names - there's a bunch of old cmd's left in temp folder. Not a big deal - I can just hardcode file name.

Post Reply

Who is online

Users browsing this forum: No registered users