4
4
using Intersect . Editor . Core ;
5
5
using Intersect . Editor . General ;
6
6
using Intersect . Editor . Localization ;
7
+ using Intersect . Framework ;
7
8
using Intersect . Framework . Core . AssetManagement ;
8
9
using Intersect . Framework . Utilities ;
9
10
using Intersect . Web ;
@@ -22,7 +23,7 @@ public partial class FrmUpdate : Form
22
23
private long _nextUpdateAttempt ;
23
24
private Task ? _pendingManifestTask ;
24
25
private TokenResponse ? _tokenResponse ;
25
- private Updater _updater ;
26
+ private Updater ? _updater ;
26
27
private UpdaterStatus ? _updaterStatus ;
27
28
28
29
public FrmUpdate ( )
@@ -34,22 +35,28 @@ public FrmUpdate()
34
35
35
36
private void frmUpdate_Load ( object sender , EventArgs e )
36
37
{
37
- AppDomain . CurrentDomain . UnhandledException += Program . CurrentDomain_UnhandledException ;
38
38
try
39
39
{
40
40
Strings . Load ( ) ;
41
41
}
42
42
catch ( Exception exception )
43
43
{
44
- Intersect . Core . ApplicationContext . Context . Value ? . Logger . LogError ( exception , "Error loading strings" ) ;
44
+ ApplicationContext . Context . Value ? . Logger . LogError ( exception , "Error loading strings" ) ;
45
45
throw ;
46
46
}
47
+
47
48
GameContentManager . CheckForResources ( ) ;
48
49
Database . LoadOptions ( ) ;
49
50
InitLocalization ( ) ;
50
51
52
+
53
+ if ( ClientConfiguration . Instance . UpdateUrl is not { } updateUrl || string . IsNullOrWhiteSpace ( updateUrl ) )
54
+ {
55
+ return ;
56
+ }
57
+
51
58
_updater = new Updater (
52
- ClientConfiguration . Instance . UpdateUrl ,
59
+ updateUrl ,
53
60
"editor/update.json" ,
54
61
"version.editor.json" ,
55
62
7
@@ -81,39 +88,48 @@ private void InitLocalization()
81
88
lblVersion . Text = Strings . Login . version . ToString ( Application . ProductVersion ) ;
82
89
lblVersion . Location = new System . Drawing . Point (
83
90
( 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
85
93
) ;
86
94
lblStatus . Text = Strings . Update . Checking ;
87
95
}
88
96
89
97
protected override void OnClosed ( EventArgs e )
90
98
{
91
- _updater . Stop ( ) ;
99
+ _updater ? . Stop ( ) ;
92
100
base . OnClosed ( e ) ;
93
101
Application . Exit ( ) ;
94
102
}
95
103
96
104
protected override void OnShown ( EventArgs e )
97
105
{
98
106
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
+ }
100
116
}
101
117
102
- private void SwitchToLogin ( bool requiresAuthentication )
118
+ private void SwitchToLogin ( bool requiresAuthentication , bool deferHide = false )
103
119
{
104
120
lblFiles . Hide ( ) ;
105
121
lblSize . Hide ( ) ;
106
122
tmrUpdate . Enabled = false ;
107
123
108
124
var loginForm = Globals . LoginForm ??= new FrmLogin ( requiresAuthentication ) ;
109
125
110
- _pendingManifestTask = default ;
126
+ _pendingManifestTask = null ;
111
127
112
128
try
113
129
{
114
- Hide ( ) ;
115
-
116
130
loginForm . Show ( ) ;
131
+
132
+ Hide ( ) ;
117
133
}
118
134
catch
119
135
{
@@ -128,20 +144,23 @@ private void CheckForUpdate()
128
144
{
129
145
lock ( _manifestTaskLock )
130
146
{
131
- if ( _pendingManifestTask != default )
147
+ if ( _pendingManifestTask != null )
132
148
{
133
149
return ;
134
150
}
135
151
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
+ ( ) =>
140
154
{
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 ;
142
162
}
143
- _pendingManifestTask = default ;
144
- } ) ;
163
+ ) ;
145
164
}
146
165
}
147
166
@@ -150,9 +169,9 @@ internal void ShowWithToken(TokenResponse tokenResponse)
150
169
_tokenResponse = tokenResponse ?? throw new ArgumentNullException ( nameof ( tokenResponse ) ) ;
151
170
152
171
Preferences . SavePreference ( nameof ( TokenResponse ) , JsonConvert . SerializeObject ( _tokenResponse ) ) ;
153
- _updater . SetAuthorizationData ( _tokenResponse ) ;
172
+ _updater ? . SetAuthorizationData ( _tokenResponse ) ;
154
173
155
- _updaterStatus = default ;
174
+ _updaterStatus = null ;
156
175
157
176
lblFiles . Show ( ) ;
158
177
lblSize . Show ( ) ;
@@ -161,96 +180,96 @@ internal void ShowWithToken(TokenResponse tokenResponse)
161
180
Show ( ) ;
162
181
163
182
Globals . LoginForm ? . Close ( ) ;
164
- Globals . LoginForm = default ;
183
+ Globals . LoginForm = null ;
165
184
}
166
185
167
- private void tmrUpdate_Tick ( object sender , EventArgs e )
186
+ private void tmrUpdate_Tick ( object sender , EventArgs e )
187
+ {
188
+ if ( _updater == null )
168
189
{
169
- if ( _updater == null )
170
- {
171
- return ;
172
- }
190
+ return ;
191
+ }
173
192
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
+ }
192
211
193
- if ( _nextUpdateAttempt != long . MinValue )
212
+ if ( _nextUpdateAttempt != long . MinValue )
213
+ {
214
+ var now = Environment . TickCount64 ;
215
+ if ( now < _nextUpdateAttempt )
194
216
{
195
- var now = Environment . TickCount64 ;
196
- if ( now < _nextUpdateAttempt )
197
- {
198
- return ;
199
- }
200
-
201
- _nextUpdateAttempt = now + 10_000 ;
202
- CheckForUpdate ( ) ;
203
217
return ;
204
218
}
205
219
206
- progressBar . Style = _updater . Status == UpdateStatus . DownloadingManifest
207
- ? ProgressBarStyle . Marquee
208
- : ProgressBarStyle . Continuous ;
220
+ _nextUpdateAttempt = now + 10_000 ;
221
+ CheckForUpdate ( ) ;
222
+ return ;
223
+ }
209
224
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 ;
234
228
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 ( ) ;
255
273
}
274
+ }
256
275
}
0 commit comments