Arma Reforger Explorer  1.1.0.42
Arma Reforger Code Explorer by Zeroy - Thanks to MisterOutofTime
SCR_BaseContainerTools.c
Go to the documentation of this file.
4 {
5  //------------------------------------------------------------------------------------------------
10  static Managed CreateInstanceFromPrefab(ResourceName prefab, bool printError = false)
11  {
12  Resource resource = Resource.Load(prefab);
13  if (!resource.IsValid())
14  {
15  if (printError)
16  Print(string.Format("'SCR_BaseContainerTools', method 'CreateInstanceFromPrefab': failed '%1' at the Resource.Load step!", prefab), LogLevel.ERROR);
17 
18  return null;
19  }
20 
21  BaseContainer baseContainer = resource.GetResource().ToBaseContainer();
22  if (!baseContainer)
23  {
24  if (printError)
25  Print(string.Format("'SCR_BaseContainerTools', method 'CreateInstanceFromPrefab': failed '%1' at the BaseContainer step!", prefab), LogLevel.ERROR);
26 
27  return null;
28  }
29 
30  Managed managed = BaseContainerTools.CreateInstanceFromContainer(baseContainer);
31  if (!managed)
32  {
33  if (printError)
34  Print(string.Format("'SCR_BaseContainerTools', method 'CreateInstanceFromPrefab': failed '%1' create instance step!", prefab), LogLevel.ERROR);
35 
36  return null;
37  }
38 
39  return managed;
40  }
41 
42  //------------------------------------------------------------------------------------------------
47  static string GetContainerClassName(ResourceName prefab)
48  {
49  return GetContainerClassName(Resource.Load(prefab));
50  }
51 
52  //------------------------------------------------------------------------------------------------
57  static string GetContainerClassName(Resource prefabResource)
58  {
59  if (!prefabResource || !prefabResource.IsValid())
60  return string.Empty;
61 
62  BaseResourceObject prefabContainer = prefabResource.GetResource();
63  if (!prefabContainer)
64  return string.Empty;
65 
66  BaseContainer prefabBase = prefabContainer.ToBaseContainer();
67  if (!prefabBase)
68  return string.Empty;
69 
70  return prefabBase.GetClassName();
71  }
72 
73  //------------------------------------------------------------------------------------------------
77  static IEntitySource FindEntitySource(Resource prefabResource)
78  {
79  if (!prefabResource || !prefabResource.IsValid())
80  return null;
81 
82  BaseResourceObject prefabBase = prefabResource.GetResource();
83  if (!prefabBase)
84  return null;
85 
86  return prefabBase.ToEntitySource();
87  }
88 
89  //------------------------------------------------------------------------------------------------
94  static IEntityComponentSource FindComponentSource(Resource prefabResource, string componentClassName)
95  {
96  if (!prefabResource || !prefabResource.IsValid())
97  return null;
98 
99  IEntitySource prefabEntity = FindEntitySource(prefabResource);
100  if (!prefabEntity)
101  return null;
102 
103  return FindComponentSource(prefabEntity, componentClassName);
104  }
105 
106  //------------------------------------------------------------------------------------------------
111  static IEntityComponentSource FindComponentSource(Resource prefabResource, typename componentClass)
112  {
113  if (!prefabResource || !prefabResource.IsValid())
114  return null;
115 
116  IEntitySource prefabEntity = FindEntitySource(prefabResource);
117  if (!prefabEntity)
118  return null;
119 
120  return FindComponentSource(prefabEntity, componentClass);
121  }
122 
123  //------------------------------------------------------------------------------------------------
128  static IEntityComponentSource FindComponentSource(IEntitySource prefabEntity, string componentClassName)
129  {
130  if (!prefabEntity)
131  return null;
132 
133  IEntityComponentSource componentSource;
134  for (int i, componentsCount = prefabEntity.GetComponentCount(); i < componentsCount; i++)
135  {
136  componentSource = prefabEntity.GetComponent(i);
137  if (componentSource.GetClassName() == componentClassName)
138  return componentSource;
139  }
140 
141  return null;
142  }
143 
144  //------------------------------------------------------------------------------------------------
149  static IEntityComponentSource FindComponentSource(IEntitySource prefabEntity, typename componentClass)
150  {
151  if (!prefabEntity)
152  return null;
153 
154  IEntityComponentSource componentSource
155  for (int i, componentsCount = prefabEntity.GetComponentCount(); i < componentsCount; i++)
156  {
157  componentSource = prefabEntity.GetComponent(i);
158  if (componentSource.GetClassName().ToType().IsInherited(componentClass))
159  return componentSource;
160  }
161 
162  return null;
163  }
164 
165  //------------------------------------------------------------------------------------------------
171  static int FindComponentSources(Resource prefabResource, notnull array<string> componentClassNames, notnull out array<ref array<IEntityComponentSource>> componentSources)
172  {
173  int classNamesCount = componentClassNames.Count();
174  componentSources.Clear();
175  componentSources.Resize(classNamesCount);
176 
177  if (!prefabResource || !prefabResource.IsValid())
178  return classNamesCount;
179 
180  IEntitySource prefabEntity = FindEntitySource(prefabResource);
181  if (!prefabEntity)
182  return classNamesCount;
183 
184  return FindComponentSources(prefabEntity, componentClassNames, componentSources);
185  }
186 
187  //------------------------------------------------------------------------------------------------
193  static int FindComponentSources(IEntitySource prefabEntity, notnull array<string> componentClassNames, notnull out array<ref array<IEntityComponentSource>> componentSources)
194  {
195  int classNamesCount = componentClassNames.Count();
196  componentSources.Clear();
197  componentSources.Resize(classNamesCount);
198 
199  if (!prefabEntity)
200  return classNamesCount;
201 
202  IEntityComponentSource componentSource;
203  array<IEntityComponentSource> components;
204  for (int i, componentsCount = prefabEntity.GetComponentCount(); i < componentsCount; i++)
205  {
206  componentSource = prefabEntity.GetComponent(i);
207  string componentClassName = componentSource.GetClassName();
208  for (int j = 0; j < classNamesCount; j++)
209  {
210  if (componentClassName == componentClassNames[j])
211  {
212  components = componentSources[j];
213  if (!components)
214  {
215  components = {};
216  componentSources.Set(j, components);
217  }
218 
219  components.Insert(componentSource);
220  break;
221  }
222  }
223  }
224 
225  return classNamesCount;
226  }
227 
228 // //------------------------------------------------------------------------------------------------
229 // //! Check if the container contains any changes as opposed to its ancestor
230 // //! \param[in] container Queried container
231 // //! \return True if the container has been modified
232 // static bool IsChanged(BaseContainer container)
233 // {
234 // string varName;
235 // BaseContainerList objectArray;
236 // for (int i = 0, varsCount = container.GetNumVars(); i < varsCount; i++)
237 // {
238 // varName = container.GetVarName(i);
239 // if (container.GetObject(varName))
240 // {
241 // if (IsChanged(container.GetObject(varName)))
242 // return true;
243 // }
244 // else if (container.GetObjectArray(varName))
245 // {
246 // objectArray = container.GetObjectArray(varName);
247 // for (int a = 0, arrayCount = objectArray.Count(); a < arrayCount; a++)
248 // {
249 // if (IsChanged(objectArray.Get(a)))
250 // return true;
251 // }
252 // }
253 // else
254 // {
255 // //PrintFormat("%1: %2: %3", container.GetClassName(), varName, container.IsVariableSetDirectly(varName));
256 // if (container.IsVariableSetDirectly(varName))
257 // return true;
258 // }
259 // }
260 // return false;
261 // }
262 
263  //------------------------------------------------------------------------------------------------
267  static BaseContainer GetPrefabContainer(BaseContainer container)
268  {
269  BaseContainer ancestor = container;
270  while (ancestor)
271  {
272  if (container.GetResourceName().Contains("/"))
273  return ancestor;
274 
275  ancestor = ancestor.GetAncestor();
276  }
277 
278  return container;
279  }
280 
281  //------------------------------------------------------------------------------------------------
285  static ResourceName GetPrefabResourceName(BaseContainer container)
286  {
287  while (container)
288  {
289  if (container.GetResourceName().Contains("/"))
290  return container.GetResourceName();
291 
292  container = container.GetAncestor();
293  }
294 
295  return ResourceName.Empty;
296  }
297 
298  //------------------------------------------------------------------------------------------------
302  static array<string> GetPrefabSetValueNames(notnull BaseContainer baseContainer)
303  {
304  array<string> result = {};
305  for (int i, count = baseContainer.GetNumVars(); i < count; i++)
306  {
307  string varName = baseContainer.GetVarName(i);
308  if (baseContainer.IsVariableSetDirectly(varName))
309  result.Insert(varName);
310  }
311 
312  return result;
313  }
314 
315  //------------------------------------------------------------------------------------------------
320  static BaseContainer GetTopMostAncestor(notnull BaseContainer baseContainer)
321  {
322  BaseContainer ancestorContainer = baseContainer.GetAncestor();
323  if (!ancestorContainer)
324  return baseContainer;
325 
326  while (ancestorContainer.GetAncestor())
327  {
328  ancestorContainer = ancestorContainer.GetAncestor();
329  }
330 
331  return ancestorContainer;
332  }
333 
334  //------------------------------------------------------------------------------------------------
339  static bool IsKindOf(notnull BaseContainer container, ResourceName resourceName)
340  {
341  if (resourceName.IsEmpty())
342  return false;
343 
344  while (container)
345  {
346  if (container.GetResourceName() == resourceName)
347  return true;
348 
349  container = container.GetAncestor();
350  }
351 
352  return false;
353  }
354 
355  //------------------------------------------------------------------------------------------------
360  static bool IsKindOf(ResourceName checkedResourceName, ResourceName supposedAncestor)
361  {
362  if (checkedResourceName.IsEmpty())
363  return false;
364 
365  Resource resource = Resource.Load(checkedResourceName);
366  if (!resource.IsValid())
367  return string.Empty;
368 
369  BaseResourceObject container = resource.GetResource();
370  if (!container)
371  return string.Empty;
372 
373  BaseContainer prefabBase = container.ToBaseContainer();
374  if (!prefabBase)
375  return string.Empty;
376 
377  return IsKindOf(prefabBase, supposedAncestor);
378  }
379 
380  //------------------------------------------------------------------------------------------------
385  static vector GetWorldCoords(IEntitySource entitySource, vector coords)
386  {
387  vector coordsEntity;
388  while (entitySource)
389  {
390  if (entitySource.Get("coords", coordsEntity))
391  coords += coordsEntity;
392 
393  entitySource = entitySource.GetParent();
394  }
395 
396  return coords;
397  }
398 
399  //------------------------------------------------------------------------------------------------
404  static vector GetLocalCoords(IEntitySource entitySource, vector coords)
405  {
406  vector coordsEntity;
407  while (entitySource)
408  {
409  if (entitySource.Get("coords", coordsEntity))
410  coords -= coordsEntity;
411 
412  entitySource = entitySource.GetParent();
413  }
414 
415  return coords;
416  }
417 
418  //------------------------------------------------------------------------------------------------
422  static string GetArrayValue(notnull array<int> values)
423  {
424  string result;
425  foreach (int i, int value : values)
426  {
427  if (i > 0)
428  result += ",";
429 
430  result += value.ToString();
431  }
432 
433  return result;
434  }
435 }
SCR_BaseContainerTools
Definition: SCR_BaseContainerTools.c:3