44using Intersect . Editor . Core ;
55using Intersect . Editor . General ;
66using Intersect . Editor . Localization ;
7+ using Intersect . Framework ;
78using Intersect . Framework . Core . AssetManagement ;
89using Intersect . Framework . Utilities ;
910using Intersect . Web ;
@@ -22,7 +23,7 @@ public partial class FrmUpdate : Form
2223 private long _nextUpdateAttempt ;
2324 private Task ? _pendingManifestTask ;
2425 private TokenResponse ? _tokenResponse ;
25- private Updater _updater ;
26+ private Updater ? _updater ;
2627 private UpdaterStatus ? _updaterStatus ;
2728
2829 public FrmUpdate ( )
@@ -34,22 +35,28 @@ public FrmUpdate()
3435
3536 private void frmUpdate_Load ( object sender , EventArgs e )
3637 {
37- AppDomain . CurrentDomain . UnhandledException += Program . CurrentDomain_UnhandledException ;
3838 try
3939 {
4040 Strings . Load ( ) ;
4141 }
4242 catch ( Exception exception )
4343 {
44- Intersect . Core . ApplicationContext . Context . Value ? . Logger . LogError ( exception , "Error loading strings" ) ;
44+ ApplicationContext . Context . Value ? . Logger . LogError ( exception , "Error loading strings" ) ;
4545 throw ;
4646 }
47+
4748 GameContentManager . CheckForResources ( ) ;
4849 Database . LoadOptions ( ) ;
4950 InitLocalization ( ) ;
5051
52+
53+ if ( ClientConfiguration . Instance . UpdateUrl is not { } updateUrl || string . IsNullOrWhiteSpace ( updateUrl ) )
54+ {
55+ return ;
56+ }
57+
5158 _updater = new Updater (
52- ClientConfiguration . Instance . UpdateUrl ,
59+ updateUrl ,
5360 "editor/update.json" ,
5461 "version.editor.json" ,
5562 7
@@ -81,39 +88,48 @@ private void InitLocalization()
8188 lblVersion . Text = Strings . Login . version . ToString ( Application . ProductVersion ) ;
8289 lblVersion . Location = new System . Drawing . Point (
8390 ( lblVersion . Parent ? . ClientRectangle . Right - ( lblVersion . Parent ? . Padding . Right + lblVersion . Width + 4 ) ) ?? 0 ,
84- ( lblVersion . Parent ? . ClientRectangle . Bottom - ( lblVersion . Parent ? . Padding . Bottom + lblVersion . Height + 4 ) ) ?? 0
91+ ( lblVersion . Parent ? . ClientRectangle . Bottom - ( lblVersion . Parent ? . Padding . Bottom + lblVersion . Height + 4 ) ) ??
92+ 0
8593 ) ;
8694 lblStatus . Text = Strings . Update . Checking ;
8795 }
8896
8997 protected override void OnClosed ( EventArgs e )
9098 {
91- _updater . Stop ( ) ;
99+ _updater ? . Stop ( ) ;
92100 base . OnClosed ( e ) ;
93101 Application . Exit ( ) ;
94102 }
95103
96104 protected override void OnShown ( EventArgs e )
97105 {
98106 base . OnShown ( e ) ;
99- tmrUpdate . Enabled = true ;
107+
108+ if ( _updater is null )
109+ {
110+ SwitchToLogin ( requiresAuthentication : false , deferHide : true ) ;
111+ }
112+ else
113+ {
114+ tmrUpdate . Enabled = true ;
115+ }
100116 }
101117
102- private void SwitchToLogin ( bool requiresAuthentication )
118+ private void SwitchToLogin ( bool requiresAuthentication , bool deferHide = false )
103119 {
104120 lblFiles . Hide ( ) ;
105121 lblSize . Hide ( ) ;
106122 tmrUpdate . Enabled = false ;
107123
108124 var loginForm = Globals . LoginForm ??= new FrmLogin ( requiresAuthentication ) ;
109125
110- _pendingManifestTask = default ;
126+ _pendingManifestTask = null ;
111127
112128 try
113129 {
114- Hide ( ) ;
115-
116130 loginForm . Show ( ) ;
131+
132+ Hide ( ) ;
117133 }
118134 catch
119135 {
@@ -128,20 +144,23 @@ private void CheckForUpdate()
128144 {
129145 lock ( _manifestTaskLock )
130146 {
131- if ( _pendingManifestTask != default )
147+ if ( _pendingManifestTask != null )
132148 {
133149 return ;
134150 }
135151
136- _pendingManifestTask = Task . Run ( ( ) =>
137- {
138- _updaterStatus = _updater . TryGetManifest ( out var manifest , force : _tokenResponse != default ) ;
139- if ( _updaterStatus == UpdaterStatus . Offline )
152+ _pendingManifestTask = Task . Run (
153+ ( ) =>
140154 {
141- _nextUpdateAttempt = Environment . TickCount64 + 10_000 ;
155+ _updaterStatus = _updater ? . TryGetManifest ( out _ , force : _tokenResponse != null ) ;
156+ if ( _updaterStatus == UpdaterStatus . Offline )
157+ {
158+ _nextUpdateAttempt = Environment . TickCount64 + 10_000 ;
159+ }
160+
161+ _pendingManifestTask = null ;
142162 }
143- _pendingManifestTask = default ;
144- } ) ;
163+ ) ;
145164 }
146165 }
147166
@@ -150,9 +169,9 @@ internal void ShowWithToken(TokenResponse tokenResponse)
150169 _tokenResponse = tokenResponse ?? throw new ArgumentNullException ( nameof ( tokenResponse ) ) ;
151170
152171 Preferences . SavePreference ( nameof ( TokenResponse ) , JsonConvert . SerializeObject ( _tokenResponse ) ) ;
153- _updater . SetAuthorizationData ( _tokenResponse ) ;
172+ _updater ? . SetAuthorizationData ( _tokenResponse ) ;
154173
155- _updaterStatus = default ;
174+ _updaterStatus = null ;
156175
157176 lblFiles . Show ( ) ;
158177 lblSize . Show ( ) ;
@@ -161,96 +180,96 @@ internal void ShowWithToken(TokenResponse tokenResponse)
161180 Show ( ) ;
162181
163182 Globals . LoginForm ? . Close ( ) ;
164- Globals . LoginForm = default ;
183+ Globals . LoginForm = null ;
165184 }
166185
167- private void tmrUpdate_Tick ( object sender , EventArgs e )
186+ private void tmrUpdate_Tick ( object sender , EventArgs e )
187+ {
188+ if ( _updater == null )
168189 {
169- if ( _updater == null )
170- {
171- return ;
172- }
190+ return ;
191+ }
173192
174- switch ( _updaterStatus )
175- {
176- case UpdaterStatus . NoUpdateNeeded :
177- SwitchToLogin ( false ) ;
178- return ;
179- case UpdaterStatus . NeedsAuthentication :
180- SwitchToLogin ( true ) ;
181- return ;
182- case UpdaterStatus . Ready :
183- _nextUpdateAttempt = long . MinValue ;
184- _updaterStatus = default ;
185- _updater . Start ( ) ;
186- break ;
187- case UpdaterStatus . Offline :
188- break ;
189- default :
190- break ;
191- }
193+ switch ( _updaterStatus )
194+ {
195+ case UpdaterStatus . NoUpdateNeeded :
196+ SwitchToLogin ( false ) ;
197+ return ;
198+ case UpdaterStatus . NeedsAuthentication :
199+ SwitchToLogin ( true ) ;
200+ return ;
201+ case UpdaterStatus . Ready :
202+ _nextUpdateAttempt = long . MinValue ;
203+ _updaterStatus = null ;
204+ _updater . Start ( ) ;
205+ break ;
206+ case UpdaterStatus . Offline :
207+ break ;
208+ default :
209+ throw Exceptions . UnreachableInvalidEnum ( _updaterStatus ?? default ) ;
210+ }
192211
193- if ( _nextUpdateAttempt != long . MinValue )
212+ if ( _nextUpdateAttempt != long . MinValue )
213+ {
214+ var now = Environment . TickCount64 ;
215+ if ( now < _nextUpdateAttempt )
194216 {
195- var now = Environment . TickCount64 ;
196- if ( now < _nextUpdateAttempt )
197- {
198- return ;
199- }
200-
201- _nextUpdateAttempt = now + 10_000 ;
202- CheckForUpdate ( ) ;
203217 return ;
204218 }
205219
206- progressBar . Style = _updater . Status == UpdateStatus . DownloadingManifest
207- ? ProgressBarStyle . Marquee
208- : ProgressBarStyle . Continuous ;
220+ _nextUpdateAttempt = now + 10_000 ;
221+ CheckForUpdate ( ) ;
222+ return ;
223+ }
209224
210- switch ( _updater . Status )
211- {
212- case UpdateStatus . DownloadingManifest :
213- lblStatus . Text = Strings . Update . Checking ;
214- break ;
215- case UpdateStatus . UpdateInProgress :
216- lblFiles . Show ( ) ;
217- lblSize . Show ( ) ;
218- lblFiles . Text = Strings . Update . Files . ToString ( _updater . FilesRemaining ) ;
219- lblSize . Text = Strings . Update . Size . ToString ( Updater . GetHumanReadableFileSize ( _updater . SizeRemaining ) ) ;
220- lblStatus . Text = Strings . Update . Updating . ToString ( ( int ) _updater . Progress ) ;
221- progressBar . Value = Math . Min ( 100 , ( int ) _updater . Progress ) ;
222- break ;
223- case UpdateStatus . Restart :
224- lblFiles . Hide ( ) ;
225- lblSize . Hide ( ) ;
226- progressBar . Value = 100 ;
227- lblStatus . Text = Strings . Update . Restart . ToString ( ) ;
228- tmrUpdate . Enabled = false ;
229-
230- if ( ! ProcessHelper . TryRelaunch ( ) )
231- {
232- ApplicationContext . CurrentContext . Logger . LogWarning ( "Failed to restart automatically" ) ;
233- }
225+ progressBar . Style = _updater . Status == UpdateStatus . DownloadingManifest
226+ ? ProgressBarStyle . Marquee
227+ : ProgressBarStyle . Continuous ;
234228
235- this . Close ( ) ;
236-
237- break ;
238- case UpdateStatus . UpdateCompleted :
239- progressBar . Value = 100 ;
240- lblStatus . Text = Strings . Update . Done ;
241- SwitchToLogin ( false ) ;
242- break ;
243- case UpdateStatus . Error :
244- lblFiles . Hide ( ) ;
245- lblSize . Hide ( ) ;
246- progressBar . Value = 100 ;
247- lblStatus . Text = Strings . Update . Error . ToString ( _updater . Exception ? . Message ?? "" ) ;
248- break ;
249- case UpdateStatus . None :
250- SwitchToLogin ( false ) ;
251- break ;
252- default :
253- throw new ArgumentOutOfRangeException ( ) ;
254- }
229+ switch ( _updater . Status )
230+ {
231+ case UpdateStatus . DownloadingManifest :
232+ lblStatus . Text = Strings . Update . Checking ;
233+ break ;
234+ case UpdateStatus . UpdateInProgress :
235+ lblFiles . Show ( ) ;
236+ lblSize . Show ( ) ;
237+ lblFiles . Text = Strings . Update . Files . ToString ( _updater . FilesRemaining ) ;
238+ lblSize . Text = Strings . Update . Size . ToString ( Updater . GetHumanReadableFileSize ( _updater . SizeRemaining ) ) ;
239+ lblStatus . Text = Strings . Update . Updating . ToString ( ( int ) _updater . Progress ) ;
240+ progressBar . Value = Math . Min ( 100 , ( int ) _updater . Progress ) ;
241+ break ;
242+ case UpdateStatus . Restart :
243+ lblFiles . Hide ( ) ;
244+ lblSize . Hide ( ) ;
245+ progressBar . Value = 100 ;
246+ lblStatus . Text = Strings . Update . Restart . ToString ( ) ;
247+ tmrUpdate . Enabled = false ;
248+
249+ if ( ! ProcessHelper . TryRelaunch ( ) )
250+ {
251+ ApplicationContext . CurrentContext . Logger . LogWarning ( "Failed to restart automatically" ) ;
252+ }
253+
254+ this . Close ( ) ;
255+
256+ break ;
257+ case UpdateStatus . UpdateCompleted :
258+ progressBar . Value = 100 ;
259+ lblStatus . Text = Strings . Update . Done ;
260+ SwitchToLogin ( false ) ;
261+ break ;
262+ case UpdateStatus . Error :
263+ lblFiles . Hide ( ) ;
264+ lblSize . Hide ( ) ;
265+ progressBar . Value = 100 ;
266+ lblStatus . Text = Strings . Update . Error . ToString ( _updater . Exception ? . Message ?? "" ) ;
267+ break ;
268+ case UpdateStatus . None :
269+ SwitchToLogin ( false ) ;
270+ break ;
271+ default :
272+ throw new ArgumentOutOfRangeException ( ) ;
255273 }
274+ }
256275}
0 commit comments