File size: 7,714 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
--version check update script for cheat engine
--Don't like it? Just delete this file. Easy as that

--For the translators:
if getTranslationFolder()~='' then
  loadPOFile(getTranslationFolder()..'VersionCheck.po')
end

local vsettings=getSettings("VersionCheck")

local VersionCheckThread

function CheckVersion(automatic)
  --create a thread that will get the latest version and buildnumber
  if versionCheckThread==nil then
    versionCheckThread=createThread(function(t)
        local i=getInternet('CEVersionCheck')
        local r=i.getURL('https://cheatengine.org/latestversion.txt')

        if r then
          local sl=createStringlist()
          local newerVersion=false
          local latestVersionCompleteBuildNumber
          local latestVersionNumber
          local latestVersionString --separate for crap like 6.5.1 (can't show 6.51 to the user)
          sl.Text=r

          if sl.Count<3 then
            t.synchronize(function()
              if automatic then
                messageDialog(translate('Unable to check version (Invalid content, not enough lines)'), mtError,mbOK)
              end
              versionCheckThread=nil
            end)
            sl.destroy()
            i.destroy()
            return
          end

          latestVersionCompleteBuildNumber=tonumber(sl[0])
          latestVersionNumber=tonumber(sl[1])
          latestVersionString=sl[2]
          
          sl.destroy()
          sl=nil

          if (latestVersionCompleteBuildNumber==nil) or (latestVersionNumber==nil) then
            t.synchronize(function()
              if automatic then
                messageDialog(translate('Unable to check version (Invalid content)'), mtError, mbOK)
              end
              versionCheckThread=nil
            end)
            i.destroy()
            return
          end

          local fv=getCheatEngineFileVersion()

          if fv then
            if latestVersionCompleteBuildNumber>fv then
              newerVersion=true
            end
          else
            --failed getting the file version (filesystem issues...)
            if latestVersionNumber>getCEVersion() then
              newerVersion=true
            end
          end


          t.synchronize(function()
            if newerVersion then
              if messageDialog(string.format(translate('Cheat Engine %s is available at www.cheatengine.org. Go there now?'),latestVersionString), mtConfirmation, mbYes, mbNo)==mrYes then
                shellExecute('https://cheatengine.org/?versioncheck=1')
              else
                if automatic then --the user clicked away, so probably not interested
                  local NewInterval=(tonumber(vsettings.Value['CheckInterval']) or 1)*2 --just show a default of two times the current skip time
                  local r=inputQuery(string.format(translate('Update to %s'),latestVersionString),translate('In how many days should I notify you again?'), NewInterval)
                  NewInterval=tonumber(r)
                  if NewInterval then
                    vsettings.Value['CheckInterval']=NewInterval
                  else
                    --the user clicked cancel, so seems to be in a hurry. Let's ask in 2*days again
                    vsettings.Value['CheckInterval']=(tonumber(vsettings.Value['CheckInterval']) or 1)*2
                  end
                end
              end
            else
              if not automatic then
                showMessage(string.format(translate('You are up to date. The latest version is %s'),latestVersionString))
              end
            end

            versionCheckThread=nil
          end)

          vsettings.Value['LastCheck']=os.time() --last successful check
        else
          t.synchronize(function()
            if not automatic then
              messageDialog(translate("Unable to check version (Can't connect)"), mtError, mbOK)
            end
            versionCheckThread=nil
          end)
        end

        i.destroy()
    end)
  end


end


if vsettings then
  if vsettings.Value['CheckOnLaunch']=='1' then
    local LastCheck=tonumber(vsettings.Value['LastCheck']) or 0 --get the time of the last successful check
    local CheckInterval=tonumber(vsettings.Value['CheckInterval']) or 1
    if (LastCheck+CheckInterval*60*60*24)<os.time() then
      CheckVersion(true)
    end
  end
end


local mi=createMenuItem(MainForm.MainMenu)
mi.Caption=translate('Check for new version')
mi.ImageIndex=15
-- also works: mi.Caption=translateID('VC-CFNV')
mi.OnClick=function(mi) CheckVersion(false) end

MainForm.miHelp.insert(MainForm.miAbout.MenuIndex,mi)

--add setting to the main ce config screen
local sf=getSettingsForm()
--I want it in front of the show undo button
--that means, take on the top and left anchor of the undo button, and change the anchor of the undo button to my item

local cbCheckForUpdatesOnLaunch=createCheckBox(sf)
local lblInterval=createLabel(sf)
local edtInterval=createEdit(sf)
local parent=sf.cbAlwaysAttemptToLaunchAsAdmin.Parent --put it inside the same control as the "undo button checkbox" (the scrollbox)

cbCheckForUpdatesOnLaunch.Checked=vsettings.Value['CheckOnLaunch']=='1'
cbCheckForUpdatesOnLaunch.Caption=translate('Check for updates when Cheat Engine starts')..'.'
cbCheckForUpdatesOnLaunch.Parent=parent
cbCheckForUpdatesOnLaunch.AnchorSideTop.Control=edtInterval
cbCheckForUpdatesOnLaunch.AnchorSideTop.Side=asrCenter
cbCheckForUpdatesOnLaunch.AnchorSideLeft=sf.cbAlwaysAttemptToLaunchAsAdmin.AnchorSideLeft

cbCheckForUpdatesOnLaunch.OnChange=function()
  lblInterval.Enabled=cbCheckForUpdatesOnLaunch.Checked
  edtInterval.Enabled=cbCheckForUpdatesOnLaunch.Checked
end

cbCheckForUpdatesOnLaunch.OnChange(cbCheckForUpdatesOnLaunch)

lblInterval.Caption=translate('Interval(days):')
lblInterval.Parent=parent
lblInterval.AnchorSideTop.Control=cbCheckForUpdatesOnLaunch
lblInterval.AnchorSideTop.Side=asrCenter
lblInterval.AnchorSideLeft.Control=cbCheckForUpdatesOnLaunch
lblInterval.AnchorSideLeft.Side=asrRight


if vsettings then
  edtInterval.Text=tonumber(vsettings.Value['CheckInterval']) or '1'
else
  edtInterval.Text='1'
end

edtInterval.ClientWidth=sf.Canvas.getTextWidth(' XX ');
edtInterval.Parent=parent
edtInterval.AnchorSideTop = sf.cbAlwaysAttemptToLaunchAsAdmin.AnchorSideTop
edtInterval.AnchorSideLeft.Control=lblInterval
edtInterval.AnchorSideLeft.Side=asrRight


sf.cbAlwaysAttemptToLaunchAsAdmin.AnchorSideTop.Control=edtInterval
sf.cbAlwaysAttemptToLaunchAsAdmin.AnchorSideTop.Side=asrBottom --put the top of the "undo button checkbox" to the bottom of the new edtInterval (so below it)

sf.cbAlwaysAttemptToLaunchAsAdmin.AnchorSideLeft.Control=cbCheckForUpdatesOnLaunch
sf.cbAlwaysAttemptToLaunchAsAdmin.AnchorSideLeft.Side=asrLeft --put the left of the "undo button checkbox" to the left side of the new checkbox (so same start)




--now capture when the action is applied. I could hijack the button, but exceptions can sometimes cause issues. (though this button should not give exceptions in 6.7+ anymore)
local oldSettingsFormClose=sf.OnClose

sf.OnClose=function(f, closeAction)
  local result=closeAction
  if sf.ModalResult==mrOK then --the user clicked OK and all checks passed
    vsettings.Value['CheckOnLaunch']=cbCheckForUpdatesOnLaunch.Checked
    vsettings.Value['CheckInterval']=edtInterval.Text
  end

  --call the original OnClose of the settings form
  if oldSettingsFormClose then
    return oldSettingsFormClose(f, closeAction);
  end
end