Arma Reforger Explorer  1.1.0.42
Arma Reforger Code Explorer by Zeroy - Thanks to MisterOutofTime
SCR_WorkshopItemAction.c
Go to the documentation of this file.
1 /*
2 SCR_WorkshopItemAction adds a concept of an asynchronous operation which can be
3 started, paused, canceled, resumed, failed, restarted.
4 
5 It also provides event handlers for state changes.
6 
7 The action can be interacted with through public API.
8 
9 
10 From this class other classes are derived which represent specific operation types.
11 
12 
13 !!! Don't create an action yourself! Use the action returned by SCR_WorkshopItem instead.
14 */
15 
17 {
18  ref ScriptInvoker m_OnActivated = new ScriptInvoker;
19  ref ScriptInvoker m_OnCompleted = new ScriptInvoker;
20  ref ScriptInvoker m_OnCanceled = new ScriptInvoker;
21  ref ScriptInvoker m_OnResumed = new ScriptInvoker;
22  ref ScriptInvoker m_OnPaused = new ScriptInvoker;
23  ref ScriptInvoker m_OnFailed = new ScriptInvoker;
24  ref ScriptInvoker m_OnChanged = new ScriptInvoker;
25 
26  // ---- States ----
27  protected const int STATE_INACTIVE = 0;
28  protected const int STATE_ACTIVE = 1;
29  protected const int STATE_COMPLETED = 2;
30  protected const int STATE_FAILED = 3;
31  protected const int STATE_CANCELED = 4;
32  protected const int STATE_PAUSED = 5;
33 
34 
35  // ---- Protected ----
36  protected int m_State;
37  SCR_WorkshopItem m_Wrapper;
38  protected bool m_bAttached;
39  protected ref Managed m_UserData; // Arbitrary user data, use GetUserData() SetUserData().
40 
41  // Cached basic data about the addon. Because action might outlive the WorkshopItem object.
42  protected string m_sAddonName;
43  protected string m_sAddonId;
44 
45  //-----------------------------------------------------------------------------------------------
46  // !!! Don't create an action yourself! Use API in SCR_WorkshopItem instead.
48  {
49  #ifdef WORKSHOP_DEBUG
50  _print("NEW");
51  #endif
52 
53  m_Wrapper = wrapper;
54  m_bAttached = true;
55 
56  // Cache basic addon data
57  m_sAddonName = wrapper.GetName();
58  m_sAddonId = wrapper.GetId();
59  }
60 
61 
62  //-----------------------------------------------------------------------------------------------
64  {
65  #ifdef WORKSHOP_DEBUG
66  _print("DELETE");
67  #endif
68  }
69 
70  // ---- Public API ----
71 
72  //-----------------------------------------------------------------------------------------------
73  // Public getters for internal state
74  bool IsInactive() { return m_State == STATE_INACTIVE; }
75  bool IsActive() { return m_State == STATE_ACTIVE; }
76  bool IsCompleted() { return m_State == STATE_COMPLETED; }
77  bool IsCanceled() { return m_State == STATE_CANCELED; }
78  bool IsPaused() { return m_State == STATE_PAUSED; }
79  bool IsFailed() { return m_State == STATE_FAILED; }
80 
81  //-----------------------------------------------------------------------------------------------
85  bool IsAttached() { return m_bAttached; }
86 
87  //-----------------------------------------------------------------------------------------------
88  SCR_WorkshopItem GetWorkshopItem() { return m_Wrapper; }
89 
90  //-----------------------------------------------------------------------------------------------
91  void SetUserData(Managed data) { m_UserData = data; }
92 
93  //-----------------------------------------------------------------------------------------------
94  Managed GetUserData(Managed data) { return m_UserData; }
95 
96  //-----------------------------------------------------------------------------------------------
98  string GetAddonName()
99  {
100  if (m_Wrapper)
101  return m_Wrapper.GetName();
102  else
103  return m_sAddonName;
104  }
105 
106  //-----------------------------------------------------------------------------------------------
108  string GetAddonId()
109  {
110  if (m_Wrapper)
111  return m_Wrapper.GetId();
112  else
113  return m_sAddonId;
114  }
115 
116  //-----------------------------------------------------------------------------------------------
120  //-----------------------------------------------------------------------------------------------
121 
122  //-----------------------------------------------------------------------------------------------
124  bool Activate()
125  {
126  #ifdef WORKSHOP_DEBUG
127  _print("Activate()");
128  #endif
129 
130  if (!m_Wrapper || !m_bAttached)
131  return false;
132 
133  if (m_State == STATE_INACTIVE)
134  {
135  #ifdef WORKSHOP_DEBUG
136  _print("OnActivate()");
137  #endif
138  bool success = OnActivate();
139  if (success)
140  {
141  m_State = STATE_ACTIVE;
142  m_OnActivated.Invoke(this);
143  InvokeOnChanged();
144  m_Wrapper.Internal_OnChanged();
145  }
146  else
147  {
148  Fail();
149  }
150  return success;
151  }
152  if (m_State == STATE_PAUSED)
153  {
154  return this.Resume();
155  }
156  else if (m_State == STATE_ACTIVE)
157  {
158  return true;
159  }
160 
161  return false;
162  }
163 
164 
165  //-----------------------------------------------------------------------------------------------
167  bool Cancel()
168  {
169  if (!m_Wrapper || !m_bAttached)
170  return false;
171 
172  #ifdef WORKSHOP_DEBUG
173  _print("Cancel()");
174  #endif
175 
176  if (m_State == STATE_ACTIVE || m_State == STATE_INACTIVE || m_State == STATE_PAUSED)
177  {
178  #ifdef WORKSHOP_DEBUG
179  _print("OnCancel()");
180  #endif
181 
182  bool success = OnCancel();
183  if (success)
184  {
185  m_State = STATE_CANCELED;
186  m_OnCanceled.Invoke(this);
187  InvokeOnChanged();
188  m_Wrapper.Internal_OnChanged();
189  }
190  else
191  {
192  Fail();
193  }
194  return success;
195  }
196  else if (m_State == STATE_CANCELED)
197  {
198  return true;
199  }
200 
201  return false;
202  }
203 
204 
205  //-----------------------------------------------------------------------------------------------
207  bool Pause()
208  {
209  if (!m_Wrapper || !m_bAttached)
210  return false;
211 
212  #ifdef WORKSHOP_DEBUG
213  _print("Pause()");
214  #endif
215 
216  if (m_State == STATE_ACTIVE)
217  {
218  #ifdef WORKSHOP_DEBUG
219  _print("OnPause()");
220  #endif
221 
222  bool success = OnPause();
223  if (success)
224  {
225  m_State = STATE_PAUSED;
226  m_OnPaused.Invoke(this);
227  InvokeOnChanged();
228  m_Wrapper.Internal_OnChanged();
229  }
230  else
231  {
232  Fail();
233  }
234  return success;
235  }
236  else if (m_State == STATE_PAUSED)
237  {
238  return true;
239  }
240 
241  return false;
242  }
243 
244 
245  //-----------------------------------------------------------------------------------------------
247  bool Resume()
248  {
249  if (!m_Wrapper || !m_bAttached)
250  return false;
251 
252  #ifdef WORKSHOP_DEBUG
253  _print("Resume()");
254  #endif
255 
256  if (m_State == STATE_PAUSED)
257  {
258  #ifdef WORKSHOP_DEBUG
259  _print("OnResume()");
260  #endif
261 
262  bool success = OnResume();
263  if (success)
264  {
265  m_State = STATE_ACTIVE;
266  m_OnResumed.Invoke(this);
267  InvokeOnChanged();
268  m_Wrapper.Internal_OnChanged();
269  }
270  else
271  {
272  Fail();
273  }
274  return success;
275  }
276  else if (m_State == STATE_INACTIVE)
277  {
278  Activate();
279  }
280  else if (m_State == STATE_ACTIVE)
281  {
282  return true; // Resuming an active action doesn't mean an error
283  }
284 
285  return false;
286  }
287 
288  //-----------------------------------------------------------------------------------------------
290  bool Reactivate()
291  {
292  if (!m_Wrapper || !m_bAttached)
293  return false;
294 
295  #ifdef WORKSHOP_DEBUG
296  _print("Reactivate()");
297  #endif
298 
299  if (m_State == STATE_FAILED)
300  {
301  #ifdef WORKSHOP_DEBUG
302  _print("OnReactivate()");
303  #endif
304 
305  bool success = OnReactivate();
306  if (success)
307  {
308  m_State = STATE_ACTIVE;
309  m_OnActivated.Invoke(this);
310  InvokeOnChanged();
311  m_Wrapper.Internal_OnChanged();
312  }
313  return success;
314  }
315  else if (m_State == STATE_ACTIVE)
316  {
317  return true;
318  }
319 
320  return false;
321  }
322 
323 
324  // ---- Protected / private ----
325 
326 
327  //-----------------------------------------------------------------------------------------------
329  protected void Fail(int reason = -1)
330  {
331  if (!m_Wrapper || !m_bAttached)
332  return;
333 
334  #ifdef WORKSHOP_DEBUG
335  _print("Fail()");
336  #endif
337 
338  if (m_State != STATE_FAILED)
339  {
340  m_State = STATE_FAILED;
341  OnFail(reason);
342  m_OnFailed.Invoke(this, reason);
343  InvokeOnChanged();
344  m_Wrapper.Internal_OnChanged();
345  }
346  }
347 
348  //-----------------------------------------------------------------------------------------------
350  protected void Complete()
351  {
352  if (!m_Wrapper || !m_bAttached)
353  return;
354 
355  #ifdef WORKSHOP_DEBUG
356  _print("Complete()");
357  #endif
358 
359  if (m_State != STATE_COMPLETED)
360  {
361  m_State = STATE_COMPLETED;
362  OnComplete();
363  m_OnCompleted.Invoke(this);
364  InvokeOnChanged();
365  m_Wrapper.Internal_OnChanged();
366  }
367  }
368 
369  //-----------------------------------------------------------------------------------------------
371  protected void InvokeOnChanged()
372  {
373  m_OnChanged.Invoke(this);
374  }
375 
376 
377  //------------------------------------------------------------------------------------------------
380  protected bool OnActivate() { return false; }
381  protected bool OnCancel() { return false; }
382  protected bool OnPause() { return false; }
383  protected bool OnResume() { return false; }
384  protected bool OnReactivate() { return false; }
385  protected void OnFail(int reason = -1);
386  protected void OnComplete();
387 
388  //------------------------------------------------------------------------------------------------
391  void Internal_Update(float timeSlice);
392 
393  //------------------------------------------------------------------------------------------------
394  void Internal_Detach()
395  {
396  m_bAttached = false;
397  }
398 
399  //------------------------------------------------------------------------------------------------
400  void _print(string str, LogLevel logLevel = LogLevel.DEBUG)
401  {
402  Print(string.Format("[SCR_AddonManager] %1 %2", this, str), logLevel);
403  }
404 };
405 
406 
409 {
410  ref array<ref SCR_WorkshopItemAction> m_aActions = new array<ref SCR_WorkshopItemAction>;
411 
412  //-----------------------------------------------------------------------------------------------
414  array<ref SCR_WorkshopItemAction> GetActions()
415  {
416  array<ref SCR_WorkshopItemAction> actions = new array<ref SCR_WorkshopItemAction>;
417 
418  foreach (auto a : m_aActions)
419  actions.Insert(a);
420 
421  return actions;
422  }
423 
424  //-----------------------------------------------------------------------------------------------
425  protected void RegisterAction(SCR_WorkshopItemAction action)
426  {
427  m_aActions.Insert(action);
428  }
429 
430  //-----------------------------------------------------------------------------------------------
431  protected void UnregisterAction(SCR_WorkshopItemAction action)
432  {
433  m_aActions.RemoveItem(action);
434  }
435 
436  //-----------------------------------------------------------------------------------------------
437  protected void UnregisterActions(array<SCR_WorkshopItemAction> actions)
438  {
439  foreach (auto i : actions)
440  UnregisterAction(i);
441  }
442 
443  //-----------------------------------------------------------------------------------------------
444  protected bool AllCompleted()
445  {
446  int n = 0;
447 
448  foreach (auto a : m_aActions)
449  if (a.IsCompleted())
450  n++;
451 
452  return n == m_aActions.Count();
453  }
454 
455  //-----------------------------------------------------------------------------------------------
456  protected bool AnyFailed()
457  {
458  foreach (auto a : m_aActions)
459  if (a.IsFailed())
460  return true;
461 
462  return false;
463  }
464 
465  //-----------------------------------------------------------------------------------------------
466  protected void CancelAll()
467  {
468  foreach (auto a : m_aActions)
469  a.Cancel();
470  }
471 
472  //-----------------------------------------------------------------------------------------------
473  protected void PauseAll()
474  {
475  foreach (auto a : m_aActions)
476  a.Pause();
477  }
478 
479  //-----------------------------------------------------------------------------------------------
480  protected void ResumeAll()
481  {
482  foreach (auto a : m_aActions)
483  a.Resume();
484  }
485 
486  //-----------------------------------------------------------------------------------------------
487  protected void ActivateAll()
488  {
489  foreach (auto a : m_aActions)
490  a.Activate();
491  }
492 };
m_aActions
ref SCR_SortedArray< SCR_BaseEditorAction > m_aActions
Definition: SCR_BaseActionsEditorComponent.c:8
SCR_WorkshopItem
Definition: SCR_WorkshopItem.c:21
m_OnChanged
protected ref ScriptInvokerTabViewIndex m_OnChanged
Definition: SCR_TabViewComponent.c:64
SCR_WorkshopItemAction
Definition: SCR_WorkshopItemAction.c:16
m_State
private EEditableEntityState m_State
Definition: SCR_BaseEntitiesEditorUIEffect.c:3
data
Get all prefabs that have the spawner data
Definition: SCR_EntityCatalogManagerComponent.c:305
m_UserData
protected ref Managed m_UserData
Definition: SCR_ModularButtonComponent.c:80
SCR_WorkshopItemActionComposite
Composite action which includes multiple subactions.
Definition: SCR_WorkshopItemAction.c:408