Page 1 of 1

Create directory

Posted: Tue May 10, 2022 3:34 pm
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?

Re: Create directory

Posted: Tue May 10, 2022 3:59 pm
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 :).

Re: Create directory

Posted: Tue May 10, 2022 4:13 pm
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.

Re: Create directory

Posted: Tue May 10, 2022 4:29 pm
by LeFiXER
It is a useful library that contains a lot of Windows-specific things that you may find useful.

Re: Create directory

Posted: Tue May 10, 2022 6:09 pm
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.

Re: Create directory

Posted: Wed May 11, 2022 8:11 am
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.

Re: Create directory

Posted: Wed May 11, 2022 3:49 pm
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.

Re: Create directory

Posted: Wed May 11, 2022 8:05 pm
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()

Re: Create directory

Posted: Thu May 12, 2022 2:29 am
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.

Re: Create directory

Posted: Thu May 12, 2022 10:39 am
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.