Arma Reforger Explorer  1.1.0.42
Arma Reforger Code Explorer by Zeroy - Thanks to MisterOutofTime
SCR_WorkshopItemActionDownload.c
Go to the documentation of this file.
1 
5 
8 
10 {
11  protected ref Revision m_StartVersion; // Version which was when the download was started
12  protected ref Revision m_TargetRevision;
13 
14  protected bool m_bTargetVersionLatest; // When true, means we want to download latest version
15 
16  protected ref SCR_WorkshopCallbackBase m_Callback;
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
34  protected ref SCR_BackendCallback m_PauseDownloadCallback = new SCR_BackendCallback();
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  {
44  if (!m_OnDownloadProgress)
45  m_OnDownloadProgress = new ScriptInvokerBase<ScriptInvoker_ActionDownloadProgress>();
46 
47  return m_OnDownloadProgress;
48  }
49 
50  //-----------------------------------------------------------------------------------------------
51  ScriptInvokerBase<ScriptInvoker_ActionDownloadFullStorage> GetOnFullStorageError()
52  {
53  if (!m_OnFullStorageError)
54  m_OnFullStorageError = new ScriptInvokerBase<ScriptInvoker_ActionDownloadFullStorage>();
55 
56  return m_OnFullStorageError;
57  }
58 
59  //-----------------------------------------------------------------------------------------------
60  void SCR_WorkshopItemActionDownload(SCR_WorkshopItem wrapper, bool latestVersion, Revision revision = null)
61  {
62  m_bTargetVersionLatest = latestVersion;
63  m_TargetRevision = revision;
64  m_StartVersion = wrapper.GetCurrentLocalRevision();
65 
66  m_fSizeBytes = m_Wrapper.GetTargetRevisionPatchSize();
67  }
68 
69  //-----------------------------------------------------------------------------------------------
74  Revision GetTargetRevision()
75  {
76  return m_TargetRevision;
77  }
78 
79 
80  //-----------------------------------------------------------------------------------------------
82  Revision GetStartRevision()
83  {
84  return m_StartVersion;
85  }
86 
87  //-----------------------------------------------------------------------------------------------
88  bool GetTargetedAtLatestVersion()
89  {
90  return m_bTargetVersionLatest;
91  }
92 
93  //-----------------------------------------------------------------------------------------------
95  bool GetUpdate()
96  {
97  return m_StartVersion;
98  }
99 
100  //-----------------------------------------------------------------------------------------------
102  float GetProgress()
103  {
104  if (IsCompleted())
105  return 1.0;
106  return m_fProgress;
107  }
108 
109  //-----------------------------------------------------------------------------------------------
110  float GetProcessingProgress()
111  {
112  return m_fProcessingProgress;
113  }
114 
115  //-----------------------------------------------------------------------------------------------
117  float GetSizeBytes()
118  {
119  return m_fSizeBytes;
120  }
121 
122  //-----------------------------------------------------------------------------------------------
123  protected override bool OnActivate()
124  {
125  m_bRunningImmediate = true;
126  m_bRunningAsync = true;
127 
128  m_fDownloadedSize = 0;
129 
130  // Check if addon was enabled, when action is finished it will be restored
131  m_bAddonEnabledAtStart = m_Wrapper.GetEnabled();
132 
133  // If we have versions already, we start downloading immediately
134  // Otherwise we load details and start waiting
135  return StartDownloadOrLoadDetails();
136  }
137 
138  //-----------------------------------------------------------------------------------------------
139  protected override bool OnReactivate()
140  {
141  return OnActivate();
142  }
143 
144  //-----------------------------------------------------------------------------------------------
145  protected override bool OnCancel()
146  {
147  bool success = m_Wrapper.Internal_CancelDownload();
148 
149  return success && !IsFailed();
150  }
151 
152  //-----------------------------------------------------------------------------------------------
153  bool GetRunningAsync()
154  {
155  return m_bRunningAsync;
156  }
157 
158  //-----------------------------------------------------------------------------------------------
159  bool IsRunningAsyncSolved()
160  {
161  return m_bRunningImmediate && m_bRunningAsync;
162  }
163 
164  //-----------------------------------------------------------------------------------------------
165  bool IsPauseAsyncSolved()
166  {
167  return !m_bRunningImmediate && !m_bRunningAsync;
168  }
169 
170  //-----------------------------------------------------------------------------------------------
171  protected override bool OnPause()
172  {
173  // Check
174  WorkshopItem item = m_Wrapper.GetWorkshopItem();
175  if (!item)
176  return false;
177 
178  // Setup result
179  if (m_bDownloadStarted)
180  {
181  m_bRunningImmediate = false;
182 
183  m_PauseDownloadCallback.GetEventOnResponse().Insert(OnPauseResponse);
184  item.PauseDownload(m_PauseDownloadCallback);
185  }
186 
187  // Result
188  return !IsFailed();
189  }
190 
191  //-----------------------------------------------------------------------------------------------
192  protected void OnPauseResponse(SCR_BackendCallback callback)
193  {
194  callback.GetEventOnResponse().Remove(OnPauseResponse);
195 
196  // Fail
197  if (callback.GetResponseType() != EBackendCallbackResponse.SUCCESS)
198  {
199  // Get back to previsius reqeusted state
200  m_bRunningImmediate = true;
201  return;
202  }
203 
204  // Success
205  m_bRunningAsync = false;
206  InvokeOnChanged();
207  }
208 
209  //-----------------------------------------------------------------------------------------------
210  protected override bool OnResume()
211  {
212  // Check
213  WorkshopItem item = m_Wrapper.GetWorkshopItem();
214  if (!item)
215  return false;
216 
217  // Setup result
218  if (m_bDownloadStarted)
219  {
220  m_bRunningImmediate = true;
221 
222  //m_PauseDownloadCallback.GetEventOnResponse().Insert(OnResumeResponse);
223 
224  m_bRunningAsync = true;
225  item.ResumeDownload(null);
226  InvokeOnChanged();
227 
228  return true;
229  }
230 
231  // If download didn't actually start, we must not resume but start a download
232  return StartDownloadOrLoadDetails();
233  }
234 
235  //-----------------------------------------------------------------------------------------------
236  protected void OnResumeResponse(SCR_BackendCallback callback)
237  {
238  callback.GetEventOnResponse().Remove(OnResumeResponse);
239 
240  // Fail
241  if (callback.GetResponseType() != EBackendCallbackResponse.SUCCESS)
242  {
243  // Get back to previsius reqeusted state
244  m_bRunningImmediate = false;
245  return;
246  }
247 
248  // Success
249  m_bRunningAsync = true;
250 
251  InvokeOnChanged();
252  }
253 
254  //-----------------------------------------------------------------------------------------------
255  protected override void OnComplete()
256  {
257  // Restore 'enabled' state
258  m_Wrapper.SetEnabled(m_bAddonEnabledAtStart);
259  }
260 
261  //-----------------------------------------------------------------------------------------------
264  protected bool StartDownloadOrLoadDetails()
265  {
266  #ifdef WORKSHOP_DEBUG
267  _print("StartDownloadOrLoadDetails()");
268  #endif
269 
270  #ifdef WORKSHOP_DEBUG
271  _print("StartDownloadOrLoadDetails(): Revisions are loaded, starting the download");
272  #endif
273 
274  // Instant fail if mod is restricted
275  // We know that it's restricted only after we have loaded the details
276  if (m_Wrapper.GetRestricted())
277  {
278  Fail();
279  return false;
280  }
281 
283  m_Callback.m_OnError.Insert(Callback_OnError);
284  m_Callback.m_OnTimeout.Insert(Callback_OnTimeout);
285 
286  if (m_bTargetVersionLatest)
287  {
288  m_TargetRevision = m_Wrapper.GetLatestRevision();
289  #ifdef WORKSHOP_DEBUG
290  _print("StartDownloadOrLoadDetails(): Download of latest version was requested");
291  #endif
292  }
293 
294  #ifdef WORKSHOP_DEBUG
295  _print(string.Format("StartDownloadOrLoadDetails(): Target version: %1", m_TargetRevision.GetVersion()));
296  #endif
297 
298  bool success = m_Wrapper.Internal_StartDownload(m_TargetRevision, m_Callback);
299 
300  m_bWaitingForRevisions = false;
301  m_bDownloadStarted = true;
302 
303  return success && !IsFailed(); // The workshop API might have called the Error handler by now
304  }
305 
306  //-----------------------------------------------------------------------------------------------
307  override void Internal_Update(float timeSlice)
308  {
309  if (!m_Wrapper)
310  return;
311 
312  // Check if comms have failed
313  if (m_State != STATE_COMPLETED && m_State != STATE_FAILED)
314  {
315  if (m_Wrapper.GetRequestFailed())
316  Fail();
317  }
318 
319  // Complete the action when the addon is at the target revisioon and is downloaded
320  if (m_State == STATE_ACTIVE)
321  {
322  if (m_bWaitingForRevisions)
323  {
324  // Try to start the download again if we have loaded the details
325  if (m_Wrapper.GetRevisionsLoaded())
326  StartDownloadOrLoadDetails();
327  }
328  else
329  {
330  bool offline = m_Wrapper.GetOffline();
331  Revision currentVersion = m_Wrapper.GetCurrentLocalRevision();
332 
333  //#ifdef WORKSHOP_DEBUG
334  //_print(string.Format("Internal_Update: Offline: %1, CurrentVersion: '%2', TargetVersion: '%3'", offline, currentVersion, m_sTargetVersion));
335  //#endif
336 
337  float progress = m_Wrapper.Internal_GetDownloadProgress();
338  float processingProgress = m_Wrapper.Internal_GetProcessingProgress();
339 
340  // Downloading
341  if (progress > m_fProgress)
342  {
343  float currentDownloadSize = m_Wrapper.GetTargetRevisionPatchSize() * progress;
344 
345  if (m_OnDownloadProgress)
346  m_OnDownloadProgress.Invoke(this, currentDownloadSize - m_fDownloadedSize);
347 
348  m_fProgress = progress;
349  m_fDownloadedSize = currentDownloadSize;
350 
351  InvokeOnChanged();
352  }
353 
354  // Processing (copying fragments)
355  else if (processingProgress > m_fProcessingProgress)
356  {
357  m_fProcessingProgress = processingProgress;
358  InvokeOnChanged();
359  }
360 
361  if (offline && Revision.AreEqual(currentVersion, m_TargetRevision))
362  {
363  Complete();
364  }
365  }
366  }
367  }
368 
369  //-----------------------------------------------------------------------------------------------
370  protected void Callback_OnError(SCR_WorkshopCallbackBase callback, int code, int restCode, int apiCode)
371  {
372  //if (code != EBackendError.EBERR_INVALID_STATE) // Ignore this one for now // EApiCode.EACODE_ERROR_OK
373 
374  callback.m_OnError.Remove(Callback_OnError);
375  callback.m_OnTimeout.Remove(Callback_OnTimeout);
376 
377  Fail(code);
378 
379  // Full storage issues
380  if (code == EBackendError.EBERR_STORAGE_IS_FULL)
381  {
382  FullStorageFail();
383  }
384  //data are not available or validation failed
385  else if ((restCode >= 400 && restCode < 500) || (code == EBackendError.EBERR_VALIDATION_FAILED))
386  {
387  m_Wrapper.DeleteDownloadProgress();
388  }
389  //else network error
390  }
391 
392  //-----------------------------------------------------------------------------------------------
393  void FullStorageFail()
394  {
395  if (m_OnFullStorageError)
396  {
397  float size = m_Wrapper.GetItemTargetRevision().GetTotalSize();
398 
399  if (size > 0)
400  m_OnFullStorageError.Invoke(this, size);
401  }
402  }
403 
404  //-----------------------------------------------------------------------------------------------
405  protected void Callback_OnTimeout(SCR_WorkshopCallbackBase callback)
406  {
407  Fail();
408  }
409 
410  //-----------------------------------------------------------------------------------------------
412  void RetryDownload()
413  {
414  m_Wrapper.RetryDownload(m_TargetRevision);
415  }
416 
417  //-----------------------------------------------------------------------------------------------
418  void ForceFail()
419  {
420  Fail();
421  }
422 
423  //-----------------------------------------------------------------------------------------------
424  float GetDownloadSize()
425  {
426  return m_fDownloadedSize;
427  }
428 
429  //-----------------------------------------------------------------------------------------------
430  bool IsProcessing()
431  {
432  return m_Wrapper && m_Wrapper.Internal_GetIsProcessing();
433  }
434 };
ScriptInvoker_ActionDownloadFullStorage
func ScriptInvoker_ActionDownloadFullStorage
Definition: SCR_WorkshopItemActionDownload.c:7
_print
void _print(string s)
Definition: SCR_ModularButtonComponent.c:665
ScriptInvoker_ActionDownloadProgress
func ScriptInvoker_ActionDownloadProgress
Definition: SCR_WorkshopItemActionDownload.c:4
SCR_WorkshopItemActionDownload
Definition: SCR_WorkshopItemActionDownload.c:9
m_Callback
protected ref CampaignCallback m_Callback
Definition: SCR_PlayerProfileManagerComponent.c:25
func
func
Definition: SCR_AIThreatSystem.c:5
SCR_WorkshopItem
Definition: SCR_WorkshopItem.c:21
SCR_BackendCallback
Scripted backend callback class unifying backend response.
Definition: SCR_BackendCallback.c:21
SCR_WorkshopCallbackBase
Definition: SCR_OnlineServiceWorkshop.c:101
m_fSizeBytes
SCR_HostScenarioWhileDownloadingDialog m_fSizeBytes
There is not enough storage on your hard drive. The space required is at least %1.
SCR_WorkshopItemAction
Definition: SCR_WorkshopItemAction.c:16
m_State
private EEditableEntityState m_State
Definition: SCR_BaseEntitiesEditorUIEffect.c:3
EBackendCallbackResponse
EBackendCallbackResponse
Basic callback responses.
Definition: SCR_BackendCallback.c:12
callback
DownloadConfigCallback callback