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 


Refresh recent files with Save button dialog

 
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: 202

PostPosted: Tue Sep 07, 2021 11:07 am    Post subject: Refresh recent files with Save button dialog Reply with quote

There is recent files extension for CE.
And everything works, but I want to update recent files using save dialogs as well. With the "Save File As" dialog box and clicking the Save button. And with overwrite file dialog and clicking Yes button. How to do it?
Here is the code written by mgr.inz.Player:
Code:

local recentFiles = {}

-- settings
recentFiles.FileName = 'recentFiles.txt'
recentFiles.NumberOfEntries = 25
recentFiles.IgnoreCETRAINER = false


-- vars
recentFiles.FileList = {}
recentFiles.FileList_Sorted = {}

local function insertRecentFileMenuItem(parent,position,caption)
  if parent==nil then return nil end
  local newItem = createMenuItem(parent)
  newItem.Caption = caption
  parent.insert(position,newItem)
  return newItem
end

function recentFiles.Show()

  if recentFiles.MenuMainItem==nil then
    local FileMenuItem = getMainForm().Menu.Items[0]
    local position = FileMenuItem.Count - 1
    insertRecentFileMenuItem(FileMenuItem,position,'-')
    insertRecentFileMenuItem(FileMenuItem,position,'Clear Recent File List').OnClick =
        function (sender)
          if messageDialog("Are you sure?", mtConfirmation, mbYes, mbNo)~=mrYes then return end
          recentFiles.FileList = {}
          recentFiles.Save()
        end
    recentFiles.MenuMainItem = insertRecentFileMenuItem(FileMenuItem,position,'Recent Files')
  end

  while recentFiles.MenuMainItem.Count > 0 do recentFiles.MenuMainItem[0].destroy() end

  local from = math.min(#recentFiles.FileList_Sorted,recentFiles.NumberOfEntries)

  recentFiles.MenuMainItem.Enabled = from > 0

  for i=from,1,-1 do
    insertRecentFileMenuItem(recentFiles.MenuMainItem,0,recentFiles.FileList_Sorted[i].k).OnClick =
        function (sender)
          local file = io.open( utf8ToAnsi(recentFiles.FileList_Sorted[i].k) ,'r')
          if file==nil then
            messageDialog("File "..recentFiles.FileList_Sorted[i].k.." doesn't exist. Entry will be removed from the list.", mtInformation,mbOK)
            recentFiles.FileList[recentFiles.FileList_Sorted[i].k] = nil
            recentFiles.Save()
            return
          else file:close() end

          local dir = recentFiles.FileList_Sorted[i].k:match([[.*%\]])
          local filename = recentFiles.FileList_Sorted[i].k:sub(#dir+1)

          getMainForm().OpenDialog1.Filename = filename
          getMainForm().OpenDialog1.InitialDir = dir
          getMainForm().LoadButton.doClick()
        end
  end
end

function recentFiles.Save()
  local file = io.open(getCheatEngineDir()..recentFiles.FileName,"w")

  if file then
    recentFiles.FileList_Sorted = {}
    for k,v in pairs(recentFiles.FileList) do
      recentFiles.FileList_Sorted[#recentFiles.FileList_Sorted+1] = {['k']=k,['v']=v}
    end
    table.sort(recentFiles.FileList_Sorted, function (a,b) return a.v < b.v end)

    if #recentFiles.FileList_Sorted==0 then file:write('') end
    for i=1,#recentFiles.FileList_Sorted do
      file:write(recentFiles.FileList_Sorted[i].k.."\n")
    end
    file:close()
  end

  recentFiles.Show()
end

function recentFiles.Load()
  local file = io.open(getCheatEngineDir()..recentFiles.FileName, "r")
  if file then
    local pos = 1
    for line in file:lines() do
      recentFiles.FileList[line] = pos
      pos = pos + 1
    end
    file:close()
  end

  recentFiles.FileList_Sorted = {}
  for k,v in pairs(recentFiles.FileList) do
    recentFiles.FileList_Sorted[v] = {['k']=k,['v']=v}
  end

  recentFiles.Show()
end

-- ONPOSTLOADTABLE
local function onPostLoadTable(filename,result)
  if (result == mrCancel) or (filename == '') then return end

  if recentFiles.IgnoreCETRAINER and filename:sub(-3):upper()~='.CT' then return end
  for k,v in pairs(recentFiles.FileList) do
    if k~=filename then recentFiles.FileList[k] = v + 1 end -- update positions
  end
  recentFiles.FileList[filename] = 1   -- bring to front
  recentFiles.Save()
end

local actOpenExecute = getMainForm().Load1.OnClick

local function openWithButtonOrMenu(sender)
  local dialogResultOffset = cheatEngineIs64Bit() and 0xB8 or 0x64

  if getCEVersion()>=7.0 then
    dialogResultOffset = cheatEngineIs64Bit() and 0xD8 or 0x70
  end

  actOpenExecute(sender)

  local dialogResult = readIntegerLocal(userDataToInteger(getMainForm().OpenDialog1)+dialogResultOffset) --mrOK, mrCancel
  local filename = getMainForm().OpenDialog1.Filename
  onPostLoadTable(filename,dialogResult)
end

getMainForm().LoadButton.OnClick = openWithButtonOrMenu  -- button
--
getMainForm().Load1.OnClick = openWithButtonOrMenu       -- menu

getMainForm().LoadButton.Action = nil
getMainForm().Load1.Action = nil

-- init and load recentFiles
recentFiles.Load()


Tried something like this, after "getMainForm().LoadButton.OnClick" line, but it is for mrOK, mrCancel buttons and it doesn't work.
Code:

getMainForm().LoadButton.OnClick = openWithButtonOrMenu  -- button

getMainForm().SaveButton.OnClick = onPostLoadTable(getMainForm().Savedialog1.Filename, readIntegerLocal(userDataToInteger(getMainForm().Savedialog1)+(cheatEngineIs64Bit() and 0xB8 or 0x64)))

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