Arma Reforger Explorer  1.1.0.42
Arma Reforger Code Explorer by Zeroy - Thanks to MisterOutofTime
SCR_WorkshopAddonManagerStorage.c
Go to the documentation of this file.
4 {
5  //----------------------------------------------------------
6  [Attribute()]
7  protected string m_sGuid;
8 
9  [Attribute()]
10  protected string m_sName;
11 
12 
13  string GetName()
14  {
15  return m_sName;
16  }
17 
18  string GetGuid()
19  {
20  return m_sGuid;
21  }
22 
23 
24  //---------------------------------------------------------------------------------------------------
26  {
28  return a;
29  }
30 
31 
32  //---------------------------------------------------------------------------------------------------
33  SCR_WorkshopAddonPresetAddonMeta Init(string guid, string name)
34  {
35  m_sGuid = guid;
36  m_sName = name;
37 
38  return this;
39  }
40 }
41 
42 
43 
46 class SCR_WorkshopAddonPreset
47 {
48  [Attribute()]
49  protected string m_sName; // Preset name
50 
51  [Attribute()]
52  protected ref array<ref SCR_WorkshopAddonPresetAddonMeta> m_aAddonsMeta; // Array with addon GUIDs
53 
54 
55  //---------------------------------------------------------------------------------------------------
56  string GetName()
57  {
58  return m_sName;
59  }
60 
61  //---------------------------------------------------------------------------------------------------
62  void SetName(string name)
63  {
64  m_sName = name;
65  }
66 
67 
68  //---------------------------------------------------------------------------------------------------
69  // We must keep constructor without arguments, otherwise JSON serializer has problems
70  SCR_WorkshopAddonPreset Init(string name, notnull array<ref SCR_WorkshopAddonPresetAddonMeta> addons)
71  {
72  m_sName = name;
73  m_aAddonsMeta = {};
74 
75  foreach (auto meta : addons)
76  m_aAddonsMeta.Insert(meta.Copy());
77 
78  return this;
79  }
80 
81 
82  //---------------------------------------------------------------------------------------------------
83  SCR_WorkshopAddonPreset Copy()
84  {
85  SCR_WorkshopAddonPreset a = (new SCR_WorkshopAddonPreset).Init(m_sName, m_aAddonsMeta);
86  return a;
87  }
88 
89 
90  //---------------------------------------------------------------------------------------------------
91  int GetAddonCount()
92  {
93  return m_aAddonsMeta.Count();
94  }
95 
96 
97  //---------------------------------------------------------------------------------------------------
98  array<ref SCR_WorkshopAddonPresetAddonMeta> GetAddons()
99  {
100  array<ref SCR_WorkshopAddonPresetAddonMeta> outArray = {};
101  foreach (auto m : m_aAddonsMeta)
102  outArray.Insert(m);
103 
104  return outArray;
105  }
106 }
107 
108 
109 // Class which manages storage of multiple addon presets
110 class SCR_WorkshopAddonManagerPresetStorage : ModuleGameSettings
111 {
112  //----------------------------------------------------------
113  // Serialized variables
114 
115  [Attribute()]
116  protected int m_iVersion; // Might be useful for future versioning
117 
118  [Attribute()]
119  protected ref array<ref SCR_WorkshopAddonPreset> m_aPresets;
120 
121  [Attribute()]
122  protected string m_sUsedPreset;
123 
124  const int VERSION = 1;
125  const string FILE_NAME = "$profile:.WorkshopPresets.json";
126 
127  protected ref ScriptInvoker<string, string> Event_OnUsedPresetChanged;
128 
129  //------------------------------------------------------------------------------------------------
130  protected void InvokeEventOnUsedPresetChanged(string arg0)
131  {
132  if (Event_OnUsedPresetChanged)
133  Event_OnUsedPresetChanged.Invoke(arg0);
134  }
135 
136  //------------------------------------------------------------------------------------------------
137  ScriptInvoker GetEventOnUsedPresetChanged()
138  {
139  if (!Event_OnUsedPresetChanged)
140  Event_OnUsedPresetChanged = new ScriptInvoker();
141 
142  return Event_OnUsedPresetChanged;
143  }
144 
145  //---------------------------------------------------------------------------------------------------
147  {
148  // When constructed, read all data from storage into RAM.
149  // When presets will be modified, or created, we will override whole SCR_WorkshopAddonManagerPresetStorage object in storage.
150  ReadPresetsFromStorage();
151  }
152 
153 
154  //-----------------------------------------------------------------------------------------
155  // PUBLIC
156 
157 
158  //---------------------------------------------------------------------------------------------------
159  array<ref SCR_WorkshopAddonPreset> GetAllPresets()
160  {
161  array<ref SCR_WorkshopAddonPreset> arrayOut = {};
162 
163  foreach (SCR_WorkshopAddonPreset preset : m_aPresets)
164  {
165  arrayOut.Insert(preset);
166  }
167 
168  return arrayOut;
169  }
170 
171 
172  //---------------------------------------------------------------------------------------------------
173  void SavePreset(notnull SCR_WorkshopAddonPreset preset, string prevName = "")
174  {
175  string name = preset.GetName();
176 
177  int id = GetPresetId(name);
178 
179  preset.SetName(name.Trim());
180 
181  // Make a copy of provided data,
182  // either append it to array or replace prev. preset with same name
183 
184  SCR_WorkshopAddonPreset presetCopy = preset.Copy();
185 
186  if (id != -1)
187  m_aPresets[id] = presetCopy;
188  else
189  m_aPresets.Insert(presetCopy);
190 
191  if (CanChangePresetName(id, name, prevName))
192  SetUsedPreset(name);
193 
194  // Save everything to storage
195  SavePresetsToStorage();
196  }
197 
198  //---------------------------------------------------------------------------------------------------
199  protected bool CanChangePresetName(int id, string name, string prevName)
200  {
201  // Brand new created
202  if (id == -1)
203  return true;
204 
205  // Created new overriding existing name
206  if (name == GetUsedPreset() || prevName == "")
207  return true;
208 
209  return false;
210  }
211 
212  //---------------------------------------------------------------------------------------------------
213  void SetUsedPreset(string name)
214  {
215  m_sUsedPreset = name;
216  SavePresetsToStorage();
217 
218  InvokeEventOnUsedPresetChanged(name);
219  }
220 
221  //---------------------------------------------------------------------------------------------------
222  void ClearUsedPreset()
223  {
224  SetUsedPreset("");
225  }
226 
227  //---------------------------------------------------------------------------------------------------
228  string GetUsedPreset()
229  {
230  return m_sUsedPreset;
231  }
232 
233  //---------------------------------------------------------------------------------------------------
234  void DeletePreset(string name)
235  {
236  int id = GetPresetId(name);
237 
238  if (id == -1)
239  return;
240 
241  m_aPresets.RemoveOrdered(id);
242 
243  SavePresetsToStorage();
244 
245  if (name == m_sUsedPreset)
246  ClearUsedPreset();
247  }
248 
249  //---------------------------------------------------------------------------------------------------
250  bool PresetExists(string name)
251  {
252  return GetPresetId(name) != -1;
253  }
254 
255 
256  //---------------------------------------------------------------------------------------------------
257  SCR_WorkshopAddonPreset GetPreset(string name)
258  {
259  int id = GetPresetId(name);
260 
261  if (id == -1)
262  return null;
263 
264  return m_aPresets[id];
265  }
266 
267  //---------------------------------------------------------------------------------------------------
268  array<ref SCR_WorkshopAddonPreset> GetPresets()
269  {
270  return m_aPresets;
271  }
272 
273  //-----------------------------------------------------------------------------------------
274  // PROTECTED
275 
276  //---------------------------------------------------------------------------------------------------
277  // Reads all presets from storage
278  protected bool ReadPresetsFromStorage()
279  {
280  BaseContainer allPresetsContainer = GetGame().GetGameUserSettings().GetModule("SCR_WorkshopAddonManagerPresetStorage");
281 
282  if (allPresetsContainer)
283  BaseContainerTools.WriteToInstance(this, allPresetsContainer);
284  else
285  {
286  m_aPresets = {};
287  m_iVersion = VERSION;
288  }
289 
290  return true;
291  }
292 
293  //---------------------------------------------------------------------------------------------------
295  protected void SavePresetsToStorage()
296  {
297  m_iVersion = VERSION;
298 
299  BaseContainer allPresetsContainer = GetGame().GetGameUserSettings().GetModule("SCR_WorkshopAddonManagerPresetStorage");
300  BaseContainerTools.ReadFromInstance(this, allPresetsContainer);
301  GetGame().UserSettingsChanged();
302  }
303 
304 
305  //---------------------------------------------------------------------------------------------------
306  protected int GetPresetId(string name)
307  {
308  foreach (int i, SCR_WorkshopAddonPreset preset : m_aPresets)
309  {
310  if (preset.GetName() == name)
311  return i;
312  }
313 
314  return -1;
315  }
316 }
m_sGuid
protected string m_sGuid
Definition: SCR_WorkshopAddonManagerStorage.c:4
GetName
string GetName()
Definition: SCR_WorkshopAddonManagerStorage.c:10
GetGame
ArmaReforgerScripted GetGame()
Definition: game.c:1424
SCR_WorkshopAddonPresetAddonMeta
Meta data of one addon.
Definition: SCR_WorkshopAddonManagerStorage.c:3
Copy
SCR_WorkshopAddonPresetAddonMeta Copy()
Definition: SCR_WorkshopAddonManagerStorage.c:22
Attribute
typedef Attribute
Post-process effect of scripted camera.
m_aPresets
ref array< ref SCR_ScriptedWidgetTooltipPreset > m_aPresets
Definition: SCR_ScriptedWidgetTooltip.c:407
m_sName
protected string m_sName
Definition: SCR_WorkshopAddonManagerStorage.c:7
Init
SCR_WorkshopAddonPresetAddonMeta Init(string guid, string name)
Definition: SCR_WorkshopAddonManagerStorage.c:30
BaseContainerProps
class SCR_WorkshopAddonPresetAddonMeta BaseContainerProps()] class SCR_WorkshopAddonPreset
Preset of addons.
Definition: SCR_WorkshopAddonManagerStorage.c:45
m_sName
protected LocalizedString m_sName
Definition: SCR_GroupIdentityComponent.c:19
SCR_WorkshopAddonManagerPresetStorage
Definition: SCR_WorkshopAddonManagerStorage.c:110