Arma Reforger Explorer 1.7.0.54
Arma Reforger Code Explorer by Zeroy - Thanks to MisterOutofTime
Loading...
Searching...
No Matches
SCR_BaseContainerTools.c
Go to the documentation of this file.
1
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 PrintFormat("[SCR_BaseContainerTools.CreateInstanceFromPrefab] failed '%1' at the Resource.Load step!", prefab, level: LogLevel.ERROR);
17
18 return null;
19 }
20
21 BaseContainer baseContainer = resource.GetResource().ToBaseContainer();
22 if (!baseContainer)
23 {
24 if (printError)
25 PrintFormat("[SCR_BaseContainerTools.CreateInstanceFromPrefab] failed '%1' at the BaseContainer step!", prefab, level: LogLevel.ERROR);
26
27 return null;
28 }
29
30 Managed managed = BaseContainerTools.CreateInstanceFromContainer(baseContainer);
31 if (!managed)
32 {
33 if (printError)
34 PrintFormat("[SCR_BaseContainerTools.CreateInstanceFromPrefab] failed '%1' create instance step!", prefab, level: LogLevel.ERROR);
35
36 return null;
37 }
38
39 return managed;
40 }
41
42 //------------------------------------------------------------------------------------------------
45 static bool SaveInstanceToResourceName(notnull Managed instance, ResourceName resourceName, string filePath = "", bool printError = false)
46 {
47 if (!resourceName && !filePath) // .IsEmpty()
48 {
49 Print("[SCR_BaseContainerTools.SaveInstanceToPrefab] Provided resourceName is an empty ResourceName and filePath is an empty string", level: LogLevel.ERROR);
50 return false;
51 }
52
53 Resource resource = BaseContainerTools.CreateContainerFromInstance(instance);
54 if (!resource || !resource.IsValid())
55 {
56 if (printError)
57 Print("[SCR_BaseContainerTools.SaveInstanceToPrefab] Provided instance cannot be converted to container", LogLevel.ERROR);
58
59 return false;
60 }
61
62 if (!BaseContainerTools.SaveContainer(resource.GetResource().ToBaseContainer(), resourceName, filePath))
63 {
64 if (printError)
65 PrintFormat("[SCR_BaseContainerTools.SaveInstanceToPrefab] Cannot save instance to resourceName/filePath (%1/%2)", resourceName, filePath, level: LogLevel.ERROR);
66
67 return false;
68 }
69
70 return true;
71 }
72
73 //------------------------------------------------------------------------------------------------
78 static string GetContainerClassName(ResourceName prefab)
79 {
80 return GetContainerClassName(Resource.Load(prefab));
81 }
82
83 //------------------------------------------------------------------------------------------------
88 static string GetContainerClassName(Resource prefabResource)
89 {
90 if (!prefabResource || !prefabResource.IsValid())
91 return string.Empty;
92
93 BaseResourceObject prefabContainer = prefabResource.GetResource();
94 if (!prefabContainer)
95 return string.Empty;
96
97 BaseContainer prefabBase = prefabContainer.ToBaseContainer();
98 if (!prefabBase)
99 return string.Empty;
100
101 return prefabBase.GetClassName();
102 }
103
104 //------------------------------------------------------------------------------------------------
108 static IEntitySource FindEntitySource(Resource prefabResource)
109 {
110 if (!prefabResource || !prefabResource.IsValid())
111 return null;
112
113 BaseResourceObject prefabBase = prefabResource.GetResource();
114 if (!prefabBase)
115 return null;
116
117 return prefabBase.ToEntitySource();
118 }
119
120 //------------------------------------------------------------------------------------------------
125 static IEntityComponentSource FindComponentSource(Resource prefabResource, string componentClassName)
126 {
127 if (!prefabResource || !prefabResource.IsValid())
128 return null;
129
130 IEntitySource prefabEntity = FindEntitySource(prefabResource);
131 if (!prefabEntity)
132 return null;
133
134 return FindComponentSource(prefabEntity, componentClassName);
135 }
136
137 //------------------------------------------------------------------------------------------------
142 static IEntityComponentSource FindComponentSource(Resource prefabResource, typename componentClass)
143 {
144 if (!prefabResource || !prefabResource.IsValid())
145 return null;
146
147 IEntitySource prefabEntity = FindEntitySource(prefabResource);
148 if (!prefabEntity)
149 return null;
150
151 return FindComponentSource(prefabEntity, componentClass);
152 }
153
154 //------------------------------------------------------------------------------------------------
159 static int FindComponentIndex(IEntitySource entitySource, string componentClassName)
160 {
161 if (!entitySource)
162 return -1;
163
164 int componentsCount = entitySource.GetComponentCount();
165 for (int i; i < componentsCount; i++)
166 {
167 IEntityComponentSource componentSource = entitySource.GetComponent(i);
168 if (componentSource.GetClassName() == componentClassName)
169 return i;
170 }
171
172 return -1;
173 }
174
175 //------------------------------------------------------------------------------------------------
180 static int FindComponentIndex(IEntitySource entitySource, typename componentClass)
181 {
182 if (!entitySource)
183 return -1;
184
185 int componentsCount = entitySource.GetComponentCount();
186 IEntityComponentSource componentSource;
187 for (int i; i < componentsCount; i++)
188 {
189 componentSource = entitySource.GetComponent(i);
190 if (componentSource.GetClassName().ToType() &&
191 componentSource.GetClassName().ToType().IsInherited(componentClass))
192 return i;
193 }
194
195 return -1;
196 }
197
198 //------------------------------------------------------------------------------------------------
203 static IEntityComponentSource FindComponentSource(IEntitySource prefabEntity, string componentClassName)
204 {
205 if (!prefabEntity)
206 return null;
207
208 IEntityComponentSource componentSource;
209 for (int i, componentsCount = prefabEntity.GetComponentCount(); i < componentsCount; i++)
210 {
211 componentSource = prefabEntity.GetComponent(i);
212 if (componentSource.GetClassName() == componentClassName)
213 return componentSource;
214 }
215
216 return null;
217 }
218
219 //------------------------------------------------------------------------------------------------
224 static IEntityComponentSource FindComponentSource(IEntitySource prefabEntity, typename componentClass)
225 {
226 if (!prefabEntity)
227 return null;
228
229 IEntityComponentSource componentSource;
230 for (int i, componentsCount = prefabEntity.GetComponentCount(); i < componentsCount; i++)
231 {
232 componentSource = prefabEntity.GetComponent(i);
233 if (componentSource.GetClassName().ToType() &&
234 componentSource.GetClassName().ToType().IsInherited(componentClass))
235 return componentSource;
236 }
237
238 return null;
239 }
240
241 //------------------------------------------------------------------------------------------------
247 static int FindComponentSources(Resource prefabResource, notnull array<string> componentClassNames, notnull out array<ref array<IEntityComponentSource>> componentSources)
248 {
249 int classNamesCount = componentClassNames.Count();
250 componentSources.Clear();
251 componentSources.Resize(classNamesCount);
252
253 if (!prefabResource || !prefabResource.IsValid())
254 return classNamesCount;
255
256 IEntitySource prefabEntity = FindEntitySource(prefabResource);
257 if (!prefabEntity)
258 return classNamesCount;
259
260 return FindComponentSources(prefabEntity, componentClassNames, componentSources);
261 }
262
263 //------------------------------------------------------------------------------------------------
269 static int FindComponentSources(IEntitySource prefabEntity, notnull array<string> componentClassNames, notnull out array<ref array<IEntityComponentSource>> componentSources)
270 {
271 int classNamesCount = componentClassNames.Count();
272 componentSources.Clear();
273 componentSources.Resize(classNamesCount);
274
275 if (!prefabEntity)
276 return classNamesCount;
277
278 IEntityComponentSource componentSource;
279 array<IEntityComponentSource> components;
280 for (int i, componentsCount = prefabEntity.GetComponentCount(); i < componentsCount; i++)
281 {
282 componentSource = prefabEntity.GetComponent(i);
283 string componentClassName = componentSource.GetClassName();
284 for (int j = 0; j < classNamesCount; j++)
285 {
286 if (componentClassName == componentClassNames[j])
287 {
288 components = componentSources[j];
289 if (!components)
290 {
291 components = {};
292 componentSources.Set(j, components);
293 }
294
295 components.Insert(componentSource);
296 break;
297 }
298 }
299 }
300
301 return classNamesCount;
302 }
303
304 //------------------------------------------------------------------------------------------------
311 static int FindComponentSourcesOfClass(IEntitySource prefabEntity, typename componentClass, bool GetChildComponentsOfComponents, notnull out array<IEntityComponentSource> componentSources)
312 {
313 componentSources.Clear();
314 if (!prefabEntity)
315 return 0;
316
317 IEntityComponentSource componentSource;
318 array<IEntityComponentSource> components;
319 for (int i, componentsCount = prefabEntity.GetComponentCount(); i < componentsCount; i++)
320 {
321 componentSource = prefabEntity.GetComponent(i);
322
323 if (componentSource.GetClassName().ToType().IsInherited(componentClass))
324 componentSources.Insert(componentSource);
325
326 //~ Search children of components and the children of components children
327 if (GetChildComponentsOfComponents)
328 {
329 array<IEntityComponentSource> componentSourceChildren = {};
330 array<IEntityComponentSource> componentSourceChildChildren = {};
331
332 //~ Search children of components
333 if (GetComponentSourceChildren(componentSource, componentSourceChildren) > 0)
334 {
335 foreach (IEntityComponentSource componentSourceChild : componentSourceChildren)
336 {
337 if (componentSourceChild.GetClassName().ToType().IsInherited(componentClass))
338 componentSources.Insert(componentSourceChild);
339
340 //~ Search children of components children
341 if (GetComponentSourceChildren(componentSourceChild, componentSourceChildChildren) > 0)
342 {
343 foreach (IEntityComponentSource componentSourceChildChild : componentSourceChildChildren)
344 {
345 if (componentSourceChildChild.GetClassName().ToType().IsInherited(componentClass))
346 componentSources.Insert(componentSourceChildChild);
347 }
348 }
349 }
350 }
351 }
352 }
353
354 return componentSources.Count();
355 }
356
357 //------------------------------------------------------------------------------------------------
362 static int GetComponentSourceChildren(notnull IEntityComponentSource componentSource, notnull out array<IEntityComponentSource> componentSources)
363 {
364 componentSources.Clear();
365
366 BaseContainerList containerList = componentSource.GetObjectArray("components");
367 if (!containerList)
368 return 0;
369
370 for (int i, count = containerList.Count(); i < count; i++)
371 {
372 componentSource = containerList.Get(i);
373 if (!componentSource)
374 continue;
375
376 componentSources.Insert(componentSource);
377 }
378
379 return componentSources.Count();
380 }
381
382// //------------------------------------------------------------------------------------------------
383// //! Check if the container contains any changes as opposed to its ancestor
384// //! \param[in] container Queried container
385// //! \return True if the container has been modified
386// static bool IsChanged(BaseContainer container)
387// {
388// string varName;
389// BaseContainerList objectArray;
390// for (int i = 0, varsCount = container.GetNumVars(); i < varsCount; i++)
391// {
392// varName = container.GetVarName(i);
393// if (container.GetObject(varName))
394// {
395// if (IsChanged(container.GetObject(varName)))
396// return true;
397// }
398// else if (container.GetObjectArray(varName))
399// {
400// objectArray = container.GetObjectArray(varName);
401// for (int a = 0, arrayCount = objectArray.Count(); a < arrayCount; a++)
402// {
403// if (IsChanged(objectArray.Get(a)))
404// return true;
405// }
406// }
407// else
408// {
409// //PrintFormat("%1: %2: %3", container.GetClassName(), varName, container.IsVariableSetDirectly(varName));
410// if (container.IsVariableSetDirectly(varName))
411// return true;
412// }
413// }
414// return false;
415// }
416
417 //------------------------------------------------------------------------------------------------
421 static BaseContainer GetPrefabContainer(BaseContainer container)
422 {
423 BaseContainer ancestor = container;
424 while (ancestor)
425 {
426 if (container.GetResourceName().Contains("/"))
427 return ancestor;
428
429 ancestor = ancestor.GetAncestor();
430 }
431
432 return container;
433 }
434
435 //------------------------------------------------------------------------------------------------
439 static ResourceName GetPrefabResourceName(BaseContainer container)
440 {
441 while (container)
442 {
443 if (container.GetResourceName().Contains("/"))
444 return container.GetResourceName();
445
446 container = container.GetAncestor();
447 }
448
449 return ResourceName.Empty;
450 }
451
452 //------------------------------------------------------------------------------------------------
456 static array<string> GetPrefabSetValueNames(notnull BaseContainer baseContainer)
457 {
458 array<string> result = {};
459 for (int i, count = baseContainer.GetNumVars(); i < count; i++)
460 {
461 string varName = baseContainer.GetVarName(i);
462 if (baseContainer.IsVariableSetDirectly(varName))
463 result.Insert(varName);
464 }
465
466 return result;
467 }
468
469 //------------------------------------------------------------------------------------------------
474 static BaseContainer GetTopMostAncestor(notnull BaseContainer baseContainer)
475 {
476 BaseContainer ancestorContainer = baseContainer.GetAncestor();
477 if (!ancestorContainer)
478 return baseContainer;
479
480 while (ancestorContainer.GetAncestor())
481 {
482 ancestorContainer = ancestorContainer.GetAncestor();
483 }
484
485 return ancestorContainer;
486 }
487
488 //------------------------------------------------------------------------------------------------
493 static bool IsKindOf(notnull BaseContainer container, ResourceName resourceName)
494 {
495 if (resourceName.IsEmpty())
496 return false;
497
498 while (container)
499 {
500 if (container.GetResourceName() == resourceName)
501 return true;
502
503 container = container.GetAncestor();
504 }
505
506 return false;
507 }
508
509 //------------------------------------------------------------------------------------------------
514 static bool IsKindOf(ResourceName checkedResourceName, ResourceName supposedAncestor)
515 {
516 if (checkedResourceName.IsEmpty())
517 return false;
518
519 Resource resource = Resource.Load(checkedResourceName);
520 if (!resource.IsValid())
521 return string.Empty;
522
523 BaseResourceObject container = resource.GetResource();
524 if (!container)
525 return string.Empty;
526
527 BaseContainer prefabBase = container.ToBaseContainer();
528 if (!prefabBase)
529 return string.Empty;
530
531 return IsKindOf(prefabBase, supposedAncestor);
532 }
533
534 //------------------------------------------------------------------------------------------------
539 static vector GetWorldCoords(IEntitySource entitySource, vector coords)
540 {
541 vector coordsEntity;
542 while (entitySource)
543 {
544 if (entitySource.Get("coords", coordsEntity))
545 coords += coordsEntity;
546
547 entitySource = entitySource.GetParent();
548 }
549
550 return coords;
551 }
552
553 //------------------------------------------------------------------------------------------------
558 static vector GetLocalCoords(IEntitySource entitySource, vector coords)
559 {
560 vector coordsEntity;
561 while (entitySource)
562 {
563 if (entitySource.Get("coords", coordsEntity))
564 coords -= coordsEntity;
565
566 entitySource = entitySource.GetParent();
567 }
568
569 return coords;
570 }
571
572 //------------------------------------------------------------------------------------------------
576 static string GetArrayValue(notnull array<int> values)
577 {
578 string result;
579 foreach (int i, int value : values)
580 {
581 if (i > 0)
582 result += ",";
583
584 result += value.ToString();
585 }
586
587 return result;
588 }
589}
ref array< string > coords
ResourceName resourceName
Definition SCR_AIGroup.c:66
Object holding reference to resource. In destructor release the resource.
Definition Resource.c:25
proto void Print(void var, LogLevel level=LogLevel.NORMAL)
Prints content of variable to console/log.
LogLevel
Enum with severity of the logging message.
Definition LogLevel.c:14
proto void PrintFormat(string fmt, void param1=NULL, void param2=NULL, void param3=NULL, void param4=NULL, void param5=NULL, void param6=NULL, void param7=NULL, void param8=NULL, void param9=NULL, LogLevel level=LogLevel.NORMAL)