[Lua] 纯文本查看 复制代码
local CONFIG_FILE=getCheatEngineDir()..'\\AutoSaveCT.ini'
local interval,path,prefix=60,getCheatEngineDir(),'自动保存'
local timer,settingsForm=nil,nil
local closeSaveEnabled=true
local mainForm=getMainForm()
local closeHandlerInstalled=false
local function trim(s)return(tostring(s or ''):gsub('^%s+',''):gsub('%s+$',''))end
local function loadConfig()
local f=io.open(CONFIG_FILE,'r');if not f then return end
for l in f:lines() do
local k,v=l:match('^%s*([%w_]+)%s*=%s*(.-)%s*$')
if k=='Interval'then interval=tonumber(v)or interval elseif k=='Path'and v~=''then path=v elseif k=='FileNamePrefix'and v~=''then prefix=v elseif k=='CloseSaveEnabled'then closeSaveEnabled=(v=='1'or v:lower()=='true')end
end;f:close()
end
local function saveConfig()
local f,e=io.open(CONFIG_FILE,'w');if not f then showMessage('配置保存失败:\n'..tostring(e));return end
f:write('[Settings]\nInterval=',interval,'\nPath=',path,'\nFileNamePrefix=',prefix,'\nCloseSaveEnabled=',closeSaveEnabled and '1'or'0','\n');f:close()
end
local function saveCT(name,notice)
if trim(path)==''then if notice then showMessage('请先设置保存路径。')end;return false end
local file=string.format('%s\\%s_%s.CT',path,name,os.date('%H_%M_%S'));local ok,e=pcall(function()saveTable(file)end)
if notice then showMessage(ok and('已保存:\n'..file)or('保存失败:\n'..tostring(e)))end;return ok
end
local function stopTimer()if timer then timer.destroy();timer=nil end end
local function startTimer()stopTimer();timer=createTimer(nil);timer.Interval=interval*1000;timer.OnTimer=function()saveCT(prefix,false)end end
local function toggleAutoSave()
if timer then stopTimer();showMessage('自动保存已关闭。');return end
if trim(path)==''then showMessage('请先设置保存路径。');return end
startTimer();showMessage(string.format('自动保存已开启。\n间隔:%d 秒\n路径:%s',interval,path))
end
local function openSettings()
if not settingsForm then
settingsForm=createForm(false);settingsForm.Caption,settingsForm.Width,settingsForm.Height='自动保存CT基础设置',430,230;settingsForm.Position,settingsForm.BorderStyle=poScreenCenter,bsSingle
-- 用户点击窗口右上角 X 时,CE 会释放窗体;清空引用以便下次重新创建。
settingsForm.OnClose=function()
settingsForm=nil
return caFree
end
local function lab(t,y)local c=createLabel(settingsForm);c.Caption,c.Left,c.Top,c.Width=t,12,y+4,400 end
local function edt(y,v)local c=createEdit(settingsForm);c.Left,c.Top,c.Width,c.Text=12,y,390,v;return c end
lab('保存间隔(秒):',10);local ie=edt(32,tostring(interval));lab('保存路径(默认为 CE 目录):',66);local pe=edt(88,path);lab('文件名前缀(格式:前缀_小时_分钟_秒.CT):',122);local fe=edt(144,prefix)
local b=createButton(settingsForm);b.Caption,b.Left,b.Top,b.Width='保存设置',95,180,110;b.OnClick=function()
local n=tonumber(trim(ie.Text));if not n or n<1 then showMessage('请输入有效的保存间隔(至少 1 秒)。');return end
path=trim(pe.Text);if path==''then showMessage('请输入保存路径。');return end
interval=math.floor(n);prefix=trim(fe.Text);if prefix==''then prefix='自动保存'end;saveConfig();if timer then startTimer()end;settingsForm.hide();showMessage('设置已保存。')
end
local c=createButton(settingsForm);c.Caption,c.Left,c.Top,c.Width='取消',225,180,110;c.OnClick=function()settingsForm.hide()end
end;settingsForm.show()
end
local function installCloseHandler()
if closeHandlerInstalled then return end
mainForm.OnClose=function(sender)
mainForm.OnClose=nil;closeHandlerInstalled=false;stopTimer()
if closeSaveEnabled then saveCT('关闭前保存',false)end
return caFree
end
closeHandlerInstalled=true
end
local function uninstallCloseHandler()
if closeHandlerInstalled then mainForm.OnClose=nil;closeHandlerInstalled=false end
end
loadConfig();local menu=mainForm.Menu
local root=createMenuItem(menu);root.Caption='自动保存CT';menu.Items.add(root)
local a=createMenuItem(root);a.Caption='基础设置';a.OnClick=openSettings;root.add(a)
local b=createMenuItem(root);b.Caption='开启/关闭自动保存';b.OnClick=toggleAutoSave;root.add(b)
local c=createMenuItem(root);c.Caption='手动保存';c.OnClick=function()saveCT(prefix,true)end;root.add(c)
local d=createMenuItem(root);d.Caption='关闭前自动保存';d.AutoCheck=true;d.Checked=closeSaveEnabled;d.OnClick=function()
closeSaveEnabled=d.Checked;saveConfig();if closeSaveEnabled then installCloseHandler()else uninstallCloseHandler()end
end;root.add(d)
if closeSaveEnabled then installCloseHandler()end