Razi Expert Cheater Reputation: 1
Joined: 17 Jan 2018 Posts: 202
|
Posted: Tue Sep 07, 2021 11:07 am Post subject: Refresh recent files with Save button dialog |
|
|
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)))
|
|
|