Arma Reforger Explorer 1.7.0.54
Arma Reforger Code Explorer by Zeroy - Thanks to MisterOutofTime
Loading...
Searching...
No Matches
SCR_WorkshopItemActionDownload.c
Go to the documentation of this file.
1
2
5
8
10{
11 protected ref Revision m_StartVersion; // Version which was when the download was started
13
14 protected bool m_bTargetVersionLatest; // When true, means we want to download latest version
15
17
18 protected float m_fSizeBytes; // Total size of this download.
19
20 // True if this action is waiting for details to start the download
21 protected bool m_bWaitingForRevisions;
22
23 // True when the actual download has been started
24 protected bool m_bDownloadStarted;
25
26 protected float m_fProgress;
27 protected float m_fProcessingProgress;
28 protected float m_fDownloadedSize;
29
30 // State of enabled state when this action was activated
31 protected bool m_bAddonEnabledAtStart;
32
33 // Pause and resume handling
35 protected bool m_bRunningImmediate; // Immediate change what is requested
36 protected bool m_bRunningAsync; // Changes after reponse
37
38 protected ref ScriptInvokerBase<ScriptInvoker_ActionDownloadProgress> m_OnDownloadProgress;
39 protected ref ScriptInvokerBase<ScriptInvoker_ActionDownloadFullStorage> m_OnFullStorageError;
40
41 //-----------------------------------------------------------------------------------------------
42 ScriptInvokerBase<ScriptInvoker_ActionDownloadProgress> GetOnDownloadProgress()
43 {
45 m_OnDownloadProgress = new ScriptInvokerBase<ScriptInvoker_ActionDownloadProgress>();
46
48 }
49
50 //-----------------------------------------------------------------------------------------------
51 ScriptInvokerBase<ScriptInvoker_ActionDownloadFullStorage> GetOnFullStorageError()
52 {
54 m_OnFullStorageError = new ScriptInvokerBase<ScriptInvoker_ActionDownloadFullStorage>();
55
57 }
58
59 //-----------------------------------------------------------------------------------------------
60 void SCR_WorkshopItemActionDownload(SCR_WorkshopItem wrapper, bool latestVersion, Revision revision = null)
61 {
62 m_bTargetVersionLatest = latestVersion;
63 m_TargetRevision = revision;
65
66 m_fSizeBytes = m_Wrapper.GetTargetRevisionPatchSize();
67 if (m_fSizeBytes <= 0)
68 {
69 revision.GetPatchSize(m_fSizeBytes);
70 }
71 }
72
73 //-----------------------------------------------------------------------------------------------
82
83
84 //-----------------------------------------------------------------------------------------------
90
91 //-----------------------------------------------------------------------------------------------
96
97 //-----------------------------------------------------------------------------------------------
99 bool GetUpdate()
100 {
101 return m_StartVersion;
102 }
103
104 //-----------------------------------------------------------------------------------------------
107 {
108 if (IsCompleted())
109 return 1.0;
110 return m_fProgress;
111 }
112
113 //-----------------------------------------------------------------------------------------------
115 {
117 }
118
119 //-----------------------------------------------------------------------------------------------
122 {
123 return m_fSizeBytes;
124 }
125
126 //-----------------------------------------------------------------------------------------------
127 protected override bool OnActivate()
128 {
129 m_bRunningImmediate = true;
130 m_bRunningAsync = true;
131
133
134 // Check if addon was enabled, when action is finished it will be restored
135 m_bAddonEnabledAtStart = m_Wrapper.GetEnabled();
136
137 // If we have versions already, we start downloading immediately
138 // Otherwise we load details and start waiting
140 }
141
142 //-----------------------------------------------------------------------------------------------
143 protected override bool OnReactivate()
144 {
145 return OnActivate();
146 }
147
148 //-----------------------------------------------------------------------------------------------
149 protected override bool OnCancel()
150 {
151 bool success = m_Wrapper.Internal_CancelDownload();
152
153 return success && !IsFailed();
154 }
155
156 //-----------------------------------------------------------------------------------------------
158 {
159 return m_bRunningAsync;
160 }
161
162 //-----------------------------------------------------------------------------------------------
167
168 //-----------------------------------------------------------------------------------------------
170 {
172 }
173
174 //-----------------------------------------------------------------------------------------------
175 protected override bool OnPause()
176 {
177 // Check
178 WorkshopItem item = m_Wrapper.GetWorkshopItem();
179 if (!item)
180 return false;
181
182 // Setup result
184 {
185 m_bRunningImmediate = false;
186
189 item.PauseDownload(m_PauseDownloadCallback);
190 }
191
192 // Result
193 return !IsFailed();
194 }
195
196 //-----------------------------------------------------------------------------------------------
197 protected void OnPauseResponse(BackendCallback callback)
198 {
199 // Success
200 m_bRunningAsync = false;
202 }
203
204 //-----------------------------------------------------------------------------------------------
205 protected void OnPauseError(BackendCallback callback)
206 {
207 // Get back to previsius reqeusted state
208 m_bRunningImmediate = true;
209 }
210
211 //-----------------------------------------------------------------------------------------------
212 protected override bool OnResume()
213 {
214 // Check
215 WorkshopItem item = m_Wrapper.GetWorkshopItem();
216 if (!item)
217 return false;
218
219 // Setup result
221 {
222 m_bRunningImmediate = true;
223
225 m_Callback.SetOnError(Callback_OnError);
226
227 m_bRunningAsync = true;
228 item.ResumeDownload(m_Callback);
229
230 // resume failed immediately - state changed in callback
231 if (m_State == STATE_FAILED)
232 return false;
233
235
236 return true;
237 }
238
239 // If download didn't actually start, we must not resume but start a download
241 }
242
243 //-----------------------------------------------------------------------------------------------
244 protected override void OnComplete()
245 {
246 // Restore 'enabled' state
248 }
249
250 //-----------------------------------------------------------------------------------------------
254 {
255 #ifdef WORKSHOP_DEBUG
256 _print("StartDownloadOrLoadDetails()");
257 #endif
258
259 #ifdef WORKSHOP_DEBUG
260 _print("StartDownloadOrLoadDetails(): Revisions are loaded, starting the download");
261 #endif
262
263 // Instant fail if mod is restricted
264 // We know that it's restricted only after we have loaded the details
265 if (m_Wrapper.GetRestricted())
266 {
267 Fail();
268 return false;
269 }
270
272 m_Callback.SetOnError(Callback_OnError);
273
275 {
276 m_TargetRevision = m_Wrapper.GetLatestRevision();
277 #ifdef WORKSHOP_DEBUG
278 _print("StartDownloadOrLoadDetails(): Download of latest version was requested");
279 #endif
280 }
281
282 #ifdef WORKSHOP_DEBUG
283 _print(string.Format("StartDownloadOrLoadDetails(): Target version: %1", m_TargetRevision.GetVersion()));
284 #endif
285
286 bool success = m_Wrapper.Internal_StartDownload(m_TargetRevision, m_Callback);
287
289 m_bDownloadStarted = true;
290
291 return success && !IsFailed(); // The workshop API might have called the Error handler by now
292 }
293
294 //-----------------------------------------------------------------------------------------------
295 override void Internal_Update(float timeSlice)
296 {
297 if (!m_Wrapper)
298 return;
299
300 // Check if comms have failed
302 {
303 if (m_Wrapper.GetRequestFailed())
304 Fail();
305 }
306
307 // Complete the action when the addon is at the target revisioon and is downloaded
308 if (m_State == STATE_ACTIVE)
309 {
310 bool offline = m_Wrapper.GetOffline();
311 Revision currentVersion = m_Wrapper.GetCurrentLocalRevision();
312
314 {
315 // Try to start the download again if we have loaded the details
316 if (m_Wrapper.GetRevisionsLoaded())
318 }
319 else if (m_Wrapper.GetWorkshopItem().GetPendingDownload())
320 {
321
322
323 //#ifdef WORKSHOP_DEBUG
324 //_print(string.Format("Internal_Update: Offline: %1, CurrentVersion: '%2', TargetVersion: '%3'", offline, currentVersion, m_sTargetVersion));
325 //#endif
326
327 float progress = m_Wrapper.Internal_GetDownloadProgress();
328 float processingProgress = m_Wrapper.Internal_GetProcessingProgress();
329
330 // Downloading
331 if (progress > m_fProgress)
332 {
333 float currentDownloadSize = m_Wrapper.GetTargetRevisionPatchSize() * progress;
334
336 m_OnDownloadProgress.Invoke(this, currentDownloadSize - m_fDownloadedSize);
337
338 m_fProgress = progress;
339 m_fDownloadedSize = currentDownloadSize;
340
342 }
343
344 // Processing (copying fragments)
345 else if (processingProgress > m_fProcessingProgress)
346 {
347 m_fProcessingProgress = processingProgress;
349 }
350 }
351 else if (offline && Revision.AreEqual(currentVersion, m_TargetRevision))
352 {
353 Complete();
354 }
355 }
356 }
357
358 //-----------------------------------------------------------------------------------------------
359 protected void Callback_OnError(BackendCallback callback)
360 {
361 int restCode = callback.GetRestResult();
362 int httpCode = callback.GetHttpCode();
363 //if (httpCode != EBackendError.EBERR_INVALID_STATE) // Ignore this one for now // EApiCode.EACODE_ERROR_OK
364
365 Fail(httpCode);
366
367 // Full storage issues
368 if (callback.GetHttpCode() == EBackendError.EBERR_STORAGE_IS_FULL)
369 {
371 }
372 //data are not available or validation failed
373 else if ((restCode >= 400 && restCode < 500) || (httpCode == EBackendError.EBERR_VALIDATION_FAILED))
374 {
375 m_Wrapper.DeleteDownloadProgress();
376 }
377 else
378 {
379 Fail();
380 }
381 }
382
383 //-----------------------------------------------------------------------------------------------
385 {
387 {
388 float size = m_Wrapper.GetItemTargetRevision().GetTotalSize();
389
390 if (size > 0)
391 m_OnFullStorageError.Invoke(this, size);
392 }
393 }
394
395
396 //-----------------------------------------------------------------------------------------------
399 {
400 // TODO: circular dependencies insanity: this m_Wrapper also has a reference to a SCR_WorkshopItemActionDownload which is not guaranteed to be this one
401 return m_Wrapper.RetryDownload(m_TargetRevision);
402 }
403
404 //-----------------------------------------------------------------------------------------------
406 {
407 Fail();
408 }
409
410 //-----------------------------------------------------------------------------------------------
412 {
413 return m_fDownloadedSize;
414 }
415
416 //-----------------------------------------------------------------------------------------------
418 {
419 return m_Wrapper && m_Wrapper.Internal_GetIsProcessing();
420 }
421};
int size
func ScriptInvoker_ActionDownloadProgress
func ScriptInvoker_ActionDownloadFullStorage
bool GetUpdate()
Returns true when this is an update, false when regular download.
override void Internal_Update(float timeSlice)
Revision GetStartRevision()
Returns the version which was at the moment when the download was started.
ScriptInvokerBase< ScriptInvoker_ActionDownloadProgress > GetOnDownloadProgress()
void OnPauseResponse(BackendCallback callback)
ref ScriptInvokerBase< ScriptInvoker_ActionDownloadFullStorage > m_OnFullStorageError
void Callback_OnError(BackendCallback callback)
void SCR_WorkshopItemActionDownload(SCR_WorkshopItem wrapper, bool latestVersion, Revision revision=null)
float GetSizeBytes()
Returns download size. Might return 0 until the actiion is activated.
ref ScriptInvokerBase< ScriptInvoker_ActionDownloadProgress > m_OnDownloadProgress
float GetProgress()
Returns current download progress if active or paused, 1 if complete, otherwise 0.
void OnPauseError(BackendCallback callback)
ScriptInvokerBase< ScriptInvoker_ActionDownloadFullStorage > GetOnFullStorageError()
SCR_WorkshopItemActionDownload RetryDownload()
Try redownload failed addon.
void Fail(int reason=-1)
Action must call this when it has failed.
void SCR_WorkshopItemAction(SCR_WorkshopItem wrapper)
void Complete()
Action must call this when it is completed.
void _print(string str, LogLevel logLevel=LogLevel.DEBUG)
void InvokeOnChanged()
Call this in inherited classes to invoke the OnChanged event handler.
Revision GetCurrentLocalRevision()
Returns the revision which we currently have on the local storage.
Workshop Item instance.
EBackendError
Backend error.