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 


Open dialog question

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> Cheat Engine Lua Scripting
View previous topic :: View next topic  
Author Message
Razi
Expert Cheater
Reputation: 1

Joined: 17 Jan 2018
Posts: 205

PostPosted: Mon Aug 03, 2020 2:06 pm    Post subject: Open dialog question Reply with quote

I have a questions about the open dialog. In the following code, when the code calls the open file dialog window, and if the cancel button was pressed, then error appears that the value is nil. What to do to prevent code from being executed further in menuitem[1].OnClick function after openFile(sender) if the cancel button was pressed? When I paste: if load_dialog.execute() == false then return end in the menuitem[1].OnClick function, then the error will not show, but the open file dialog will open twice.

Code:
menuitem = {}
for x = 0, 5 do
  menuitem[x] = UDF1["MenuItem"..x+2]
end

function openFile(sender)

  load_dialog = createOpenDialog(self)
  load_dialog.InitalDir = os.getenv('%USERPROFILE%')
  load_dialog.Filter =
'Exe and MNU files|*.EXE;*.MNU|MNU files (*.MNU)|*.MNU|EXE files (*.exe)|*.EXE|All files (*.*)|*'
  if sender == menuitem[1] then
  load_dialog.FileName = 'SHOPMENU.MNU'
  load_dialog.FilterIndex=2
  elseif sender == menuitem[0] then
  load_dialog.FileName = 'ff7_en.exe'
  load_dialog.FilterIndex=3
  end
  if load_dialog.execute() == false then return end
  local path = load_dialog.FileName
  path = utf8ToAnsi(path)

  fileSize = fsize(path)
  fileSize = tonumber(fileSize)
  if fileSize ~= nil then
  listbox[0].Enabled = true
  listbox[1].Enabled = true
  load_dialog.destroy()
  openFileAsProcess(path, r)
  else
   --showMessage('Wrong file')
  end
end

menuitem[1].OnClick = function(sender)
openFile(sender)
local res = AOBScan("25 51 55 49 50 50 45 44 FF 00 00 00", "+W")--, "+W"
  if res == nil then
  --showMessage('Wrong file')
  return end
for i=0, res.Count-1, 1 do
  local a = tonumber(res[0], 16)
  baseaddr = a - 0x20B4
  baseaddr2 = baseaddr + 0x2140
end
res.destroy()

--if load_dialog.execute() == false then return end

for x = 0, 1 do
listbox[x].OnClick()
end
UDF1.TabSheet2.OnShow()
end

listbox[0].OnClick = function(sender)
local a = listbox[0].ItemIndex
names_prices[0].Text = listbox[0].Items[a]
shopitems[0].ItemIndex = readBytes(baseaddr + listbox[0].ItemIndex*84)
shopitems[1].ItemIndex = readBytes(baseaddr + 2 + listbox[0].ItemIndex*84)
end


2) How to open a file in the same folder as the trainer exe file?

3) What to do if the file path does not contain English letters?
Back to top
View user's profile Send private message
panraven
Grandmaster Cheater
Reputation: 61

Joined: 01 Oct 2008
Posts: 958

PostPosted: Mon Aug 03, 2020 4:30 pm    Post subject: Reply with quote

In general, as Lua is dynamic type, it would be a good practice to consider check type of variable before process it.

1) there is an OnCanClose event method for dialog class, which seemly only trigger when the dialog close without cancel, this is a test (RIGHT-CLICK a file to select, then OK or CANCEL):
Code:

--

local dia = createOpenDialog()

dia.OnShow = function(me)
  print('onShow FileName:',me.FileName)
end

dia.OnCanClose = function(me)
  print('onCanClose FileName:',me.FileName)
  return true   -- this seem to allow continue to Close event, eg. it can block the Close event on a failed validation doing here
end

dia.OnClose = function(me)
  print('onClose FileName:',me.FileName)
end

dia.Execute()

--- sample result:

-- cancelled
onShow FileName: 
onClose FileName: C:\*\_49_d4.CT

-- ok
onShow FileName: 
onCanClose FileName: C:\*\_49_d3.CT
onClose FileName: C:\*\_49_d3.CT


ADDED:
The openFile should return something in case an actual file is selected and process ok within, eg
Code:

function openFile(sender)

  load_dialog = createOpenDialog(self)
  load_dialog.InitalDir = os.getenv('%USERPROFILE%')
...
  if load_dialog.execute() == false then return end
  local path = load_dialog.FileName
...
  openFileAsProcess(path, r)
  else
   --showMessage('Wrong file')
  end
  return true
end

Then in menuitem[1].OnClick check what openFile return
Code:

menuitem[1].OnClick = function(sender)
if not openFile(sender) then ... [[show error and abort etc.]] end
... [[ok to process ]]


2. set property 'InitialDir', with TrainerOrigin, or GetCheatEngineDir()
-- TrainerOrigin is the directory where *.ct *.cetrainer *.exe (tiny) run/click from explorer, there seems not defined in *.exe (gigantic)
-- GetCheatEngineDir() will be the exploded temporary directory of where *.exe (gigantic) running, probably the answer. There will be other files/folder included in trainer generator.

3. depend on how/where the return path to be use, if it will be use in command prompt for example, it may need to quoted.

ps:

Code:

function openFile(sender)
  load_dialog = createOpenDialog(self)    --- this 'self' is likely refer to a global, as the function name is not of this form "objectPath : methodName (a,b,c)", where 1st parameter is a local variable (of the function) 'self'.
...

_________________
- Retarded.
Back to top
View user's profile Send private message
Corroder
Grandmaster Cheater Supreme
Reputation: 75

Joined: 10 Apr 2015
Posts: 1668

PostPosted: Tue Aug 04, 2020 6:27 am    Post subject: Reply with quote

Simple example:

Code:
local file_dialog = createOpenDialog()
file_dialog.InitialDir = 'C:\\'  --os.getenv('%USERPROFILE%')  -- or set to dir/folder which exe file placed
file_dialog.Filter = 'Text files|*.TXT;*.txt|All files (*.*)|*'


--- avoid error when cancel open the file
if file_dialog.execute() then
   local file_name = file_dialog.FileName
   print(file_name)
   return
else
   return nil
end

_________________
Stealing Code From Stolen Code...
And Admit It.. Hmmm....Typically LOL
Back to top
View user's profile Send private message
Razi
Expert Cheater
Reputation: 1

Joined: 17 Jan 2018
Posts: 205

PostPosted: Tue Aug 04, 2020 12:51 pm    Post subject: Reply with quote

Thanks for the answers, they were very helpful.
About the third question. For example if file name contains non-English characters, then
Code:
local filename = load_dialog.FileName
filename = utf8ToAnsi(filename)

and trainer can open files with non-English characters. So I need to take the path to the file and convert utf8ToAnsi. How to do it?
Back to top
View user's profile Send private message
Corroder
Grandmaster Cheater Supreme
Reputation: 75

Joined: 10 Apr 2015
Posts: 1668

PostPosted: Tue Aug 04, 2020 1:06 pm    Post subject: Reply with quote

I didn't try this because there are no non-English characters on my machine.

Code:
function GetFileName(f)
 local str = f
 local temp = ""
 local result = ""
 for i = str:len(), 1, -1 do
 if str:sub(i,i) ~= "/"  then
 temp = temp..str:sub(i,i)
 else
 break
 end
 end
 for j = temp:len(), 1, -1 do
 result = result..temp:sub(j,j)
 end
 return result
end

local file_dialog = createOpenDialog()
file_dialog.InitialDir = 'C:\\'  --os.getenv('%USERPROFILE%')  -- or set to dir/folder which exe file placed
file_dialog.Filter = 'Text files|*.TXT;*.txt|All files (*.*)|*'

--- avoid error when cancel open the file
if file_dialog.execute() then
  local file_name = file_dialog.FileName
  file = utf8ToAnsi(GetFileName(file_name))
  print(file)
  return
else
  return nil
end


Anyhow, what language for non-English character on your machine?

_________________
Stealing Code From Stolen Code...
And Admit It.. Hmmm....Typically LOL
Back to top
View user's profile Send private message
Razi
Expert Cheater
Reputation: 1

Joined: 17 Jan 2018
Posts: 205

PostPosted: Tue Aug 04, 2020 1:37 pm    Post subject: Reply with quote

Corroder wrote:
Anyhow, what language for non-English character on your machine?

In general, I want to have support for Cyrillic languages ​​as well.
There is one more thing. If the file name contains a space, then the trainer cannot open this file. The trainer can open this file: КопSHOPMENU-pal.MNU, but this one cannot: Коп SHOPMENU-pal.MNU.


Last edited by Razi on Sun Aug 09, 2020 4:10 am; edited 1 time in total
Back to top
View user's profile Send private message
Corroder
Grandmaster Cheater Supreme
Reputation: 75

Joined: 10 Apr 2015
Posts: 1668

PostPosted: Wed Aug 05, 2020 1:24 am    Post subject: Reply with quote

Not sure how to solve the filename characters. Maybe by change your windows code page to 1251. Also, you can check this:

https://gist.github.com/Egor-Skriptunoff/2458547aa3b9210a8b5f686ac08ecbf0

_________________
Stealing Code From Stolen Code...
And Admit It.. Hmmm....Typically LOL
Back to top
View user's profile Send private message
Razi
Expert Cheater
Reputation: 1

Joined: 17 Jan 2018
Posts: 205

PostPosted: Wed Aug 05, 2020 2:39 pm    Post subject: Reply with quote

Corroder wrote:
Not sure how to solve the filename characters.


It looks like the issue was solved even before the beginning.
The following code opens files with English and non-English characters. Also opens files if the path contains both English and non-English characters:
Code:
function openFile(sender)

  load_dialog = createOpenDialog(self)
  load_dialog.InitialDir = os.getenv('%USERPROFILE%')
  load_dialog.Filter =
'Exe and MNU files|*.EXE;*.MNU|MNU files (*.MNU)|*.MNU|EXE files (*.exe)|*.EXE|All files (*.*)|*'
  if sender == menuitem[1] then
  load_dialog.FileName = 'SHOPMENU.MNU'
  load_dialog.FilterIndex=2
  elseif sender == menuitem[0] then
  load_dialog.FileName = 'ff7_en.exe'--'ff7.exe'
  load_dialog.FilterIndex=3
  end

  if load_dialog.execute() then
    local file_name = load_dialog.FileName
    listbox[0].Enabled = true
    listbox[1].Enabled = true
    openFileAsProcess(file_name, r)
    return
  else
    return nil
  end
end

The issue was in the utf8ToAnsi: file_name = utf8ToAnsi(file_name). With utf8ToAnsi, the trainer cannot open files or paths correctly with non-English characters. Code tested with CE 6.7-7.1, now it can open any file in any path.
Thanks everyone for the help.
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
Page 1 of 1

 
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