File size: 9,426 Bytes
8739cbb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
if getTranslationFolder()~='' then
  loadPOFile(getTranslationFolder()..'autosave.po')
end

require("lfs")

autosave={} --todo make local

local AutoSaveSettings=getSettings('Auto Save')
local AutoSaveVersion=1

autosave.getPath=function()
  local path=AutoSaveSettings['SavePath']
  if (path==nil) or (path=='') then
  
    path=os.getenv("LOCALAPPDATA")
    if (path==nil) or (path=='') then
      path=getCheatEngineDir() --last attempt  
    end
  end
  
  if string.sub(path,#path)~='\\' then
    path=path..'\\'
  end
  
  return path
end

function autosave.saveState()

  
  local pid=AutoSaveSettings['ProcessID']
  if pid and pid~='' then
    pid=tonumber(pid)
    if pid~=getCheatEngineProcessID() then
      --another CE has done an autosave
      if getProcessList()[pid]==nil then
        --it doesn't exist anymore.
        messageDialog(translate('Another instance of Cheat Engine has crashed and it created an autosave.  Autosave disabled until you go to settings and click ok'), mtWarning, mbOk)
        return false
      end
    end
  end

  AutoSaveSettings['ProcessID']=getCheatEngineProcessID()
  local path, err=autosave.getPath()
  if path==nil then
    messageDialog(translate('Failure to obtain a location to save the autosave data'), mtError, mbOk)  
    return false
  end
    
  path=path..'Cheat Engine AutoSave';
  lfs.mkdir(path)
  
  
  local r,err=saveTable(path..[[\table.ct]])
  if (r==nil) or (r==false) then
    if err==nil then
      err=translate('Unknown Reason')    
    end      
    messageDialog(translate('Failure to autosave table : ')..err, mtError, mbOk)
    return false
  end

  --save the rest (open luascripts and aa scripts, excluding aa scripts belonging to memrecs)
  --feel free to add more if you like, keep in mind to apply the same to loadState
  local cestate,err=createFileStream(path..[[\state.dat]], fmCreate)
  if cestate==nil then
    messageDialog(translate('Failure to autosave state : ')..err, mtError, mbOk)
    return false
  end
  
  local i
  local luaforms={}
  local aaforms={}
  for i=getFormCount()-1,0,-1 do --negative order so the creation order will be correct (the form order is the z-order)
    local f=getForm(i)
   
    if (f.ClassName=='TfrmLuaEngine') and f.Visible then      
      local e={}
      e.history=f.mOutput.Lines.Text
      e.script=f.mScript.Lines.Text
      e.filename=f.savedialog1.FileName
      e.Left=f.Left
      e.Top=f.Top
      e.Width=f.Width
      e.Height=f.Height      
      
      
      if f.MiView.Visible then --first one
        local temp=luaforms[1]
        luaforms[1]=e
        temp=e
      end
      
      if e then      
        table.insert(luaforms,e)
      end
      
    elseif (f.ClassName=='TfrmAutoInject') and f.Visible then
      if (f.IsEditing==false) and (f.ScriptMode~='smLua') then --Editing means it's editing a memoryrecord saved in the table, not saving that
        --standalone AA window, not the tablescript        
        e={}
        e.Filename=f.savedialog1.FileName
        e.Left=f.Left
        e.Top=f.Top
        e.Width=f.Width
        e.Height=f.Height        
        e.ScriptCount=f.TabCount
        e.Scripts={}
        local j
        for j=0,f.TabCount-1 do
          e.Scripts[j+1]=f.TabScript[j]
        end

        table.insert(aaforms,e)
      end    
    end       
  end 
  
  cestate.writeDword(AutoSaveVersion)
  cestate.writeDword(#luaforms)
  for i=1,#luaforms do
    cestate.writeAnsiString(luaforms[i].history)
    cestate.writeAnsiString(luaforms[i].script)
    cestate.writeAnsiString(luaforms[i].filename)
    cestate.writeDword(luaforms[i].Left)
    cestate.writeDword(luaforms[i].Top)
    cestate.writeDword(luaforms[i].Width)
    cestate.writeDword(luaforms[i].Height)    
  end 
  
  cestate.writeDword(#aaforms)
  for i=1,#aaforms do
    cestate.writeDword(aaforms[i].ScriptCount)
    for j=1,#aaforms[i].Scripts do
      cestate.writeAnsiString(aaforms[i].Scripts[j])
    end
    cestate.writeAnsiString(aaforms[i].Filename)
    cestate.writeDword(aaforms[i].Left)
    cestate.writeDword(aaforms[i].Top)
    cestate.writeDword(aaforms[i].Width)
    cestate.writeDword(aaforms[i].Height)        
  end  

  cestate.destroy()
  
  return true
end

function autosave.loadState()
 -- print("loadState()")
  
  AutoSaveSettings['ProcessID']=getCheatEngineProcessID()
  local path=autosave.getPath()..'Cheat Engine AutoSave\\';  
  
  r,err=loadTable(path..'table.ct')
  if (r==nil) or (r==false) then
    if err==nil then
      err=translate('Unknown Reason')    
    end      
    messageDialog(string.format(translate('Failure to load autosave at %s  . Error: %s'), path, err), mtError, mbOk)
    return false
  end
  
  --print("Table loaded. Loading rest")
  
  --load the rest
  local cestate,err=createFileStream(path..[[state.dat]], fmOpenRead)
  if cestate then  
    local i
    local version=cestate.readDword()
    if version~=AutoSaveVersion then
      --check how to load older versions, and if not supported then:
      messageDialog(translate('The saved state belongs to a different autosave version that is not currently implemented'), mtError, mbOk)  
      return false
    end
    local luaformcount=cestate.readDword()
    --print("luaformcount="..luaformcount)
    for i=1,luaformcount do
      local f=createLuaEngine()
      f.mOutput.Lines.Text=cestate.readAnsiString()
      f.mScript.Lines.Text=cestate.readAnsiString()
      f.SaveDialog1.FileName=cestate.readAnsiString()
      f.Left=cestate.readDword()
      f.Top=cestate.readDword()
      f.Width=cestate.readDword()
      f.Height=cestate.readDword()
      
      f.show()     
    end
    
    local aacount=cestate.readDword()
    --print("aacount="..aacount)
    for i=1,aacount do
      f=createAutoAssemblerForm()
      
      local count=cestate.readDword()
     
      if count>0 then --should be      
        f.TabCount=count      
        local j
        for j=0,count-1 do          
          f.TabScript[j]=cestate.readAnsiString()
        end
      end
      
      f.SaveDialog1.FileName=cestate.readAnsiString()
      f.Left=cestate.readDword()
      f.Top=cestate.readDword()
      f.Width=cestate.readDword()
      f.Height=cestate.readDword()    
      
      f.show()
    end
    
    --print("Done loading")
    
    
    cestate.destroy()
    return true
  else
    --print("error opening state.dat because of "..err)
    return false
  end
  

end

autosave.applySettings=function()
  local interval=tonumber(AutoSaveSettings['Interval'])
    
  if autosave.Timer then
    --print("killing old timer")
    autosave.Timer.destroy()
    autosave.Timer=nil
  end
    
  if interval and (interval>0) then
   -- print("creating new timer with interval "..interval*1000)
    autosave.Timer=createTimer(nil)
    autosave.Timer.Interval=interval*1000
    autosave.Timer.OnTimer=function(t)
      --print("autosave")
      if autosave.saveState()~=true then
        --something happened
        autosave.Timer.destroy()
        autosave.Timer=nil
      end
    end
    autosave.Enabled=true  
  else
    --print("no autosave")
  end
end

MainForm.registerFirstShowCallback(function()
  --only activate on proper load

  local oldMainFormOnClose=MainForm.OnClose
  MainForm.OnClose=function(s, ca)
    AutoSaveSettings['ProcessID']=nil --CE was properly closed
    if oldMainFormOnClose then
      return oldMainFormOnClose(s, ca)
    else 
      return ca
    end
  end


  --add settings menu
  local sf=getSettingsForm()
  autosave.settingsTab=sf.SettingsPageControl.addTab()

  autosave.settingsEdtInterval=createEdit(autosave.settingsTab)
  autosave.settingsEdtInterval.Text=AutoSaveSettings['Interval']
  autosave.settingsEdtInterval.Align='alTop'

  autosave.settingsLblInterval=createLabel(autosave.settingsTab)
  autosave.settingsLblInterval.Caption=translate('Auto Save Interval (In seconds. 0 is disabled)')
  autosave.settingsLblInterval.Align='alTop'

  local insertNode=sf.SettingsTreeView.Items[3]
  local node=sf.SettingsTreeView.Items.insert(insertNode, translate("Auto Save"))
  node.data=userDataToInteger(autosave.settingsTab)

  local originalSettingsCloseEvent=sf.OnClose
  sf.OnClose=function(s, closeAction)
   
    local r=closeAction
    if originalSettingsCloseEvent then
      r=originalSettingsCloseEvent(s, closeAction)
    end
    
    if s.ModalResult==mrOK then
      --apply change
      AutoSaveSettings['Interval']=autosave.settingsEdtInterval.Text
      
      autosave.applySettings()
    end
    return r 
  end


  --check if there is an autosave waiting to be loaded

  pid=AutoSaveSettings['ProcessID']
  if (pid) and (pid ~= '') and (getProcessList()[tonumber(pid)]==nil) then
    --The noted down processid does not exist anymore. CE crashed.  Ask to load the autosave
    AutoSaveSettings['ProcessID']=nil
    
    if messageDialog(translate('Cheat Engine did not properly close last time. Do you wish to restore your work?'), mtInformation, mbYes, mbNo)==mrYes then
      autosave.loadState()    
    end
  end


  --start the autosave timer(if needed)
  autosave.applySettings()
end)