10 static Managed CreateInstanceFromPrefab(
ResourceName prefab,
bool printError =
false)
13 if (!resource.IsValid())
16 PrintFormat(
"[SCR_BaseContainerTools.CreateInstanceFromPrefab] failed '%1' at the Resource.Load step!", prefab, level:
LogLevel.ERROR);
21 BaseContainer baseContainer = resource.GetResource().ToBaseContainer();
25 PrintFormat(
"[SCR_BaseContainerTools.CreateInstanceFromPrefab] failed '%1' at the BaseContainer step!", prefab, level:
LogLevel.ERROR);
34 PrintFormat(
"[SCR_BaseContainerTools.CreateInstanceFromPrefab] failed '%1' create instance step!", prefab, level:
LogLevel.ERROR);
45 static bool SaveInstanceToResourceName(notnull Managed instance,
ResourceName resourceName,
string filePath =
"",
bool printError =
false)
49 Print(
"[SCR_BaseContainerTools.SaveInstanceToPrefab] Provided resourceName is an empty ResourceName and filePath is an empty string", level:
LogLevel.ERROR);
54 if (!resource || !resource.IsValid())
57 Print(
"[SCR_BaseContainerTools.SaveInstanceToPrefab] Provided instance cannot be converted to container",
LogLevel.ERROR);
65 PrintFormat(
"[SCR_BaseContainerTools.SaveInstanceToPrefab] Cannot save instance to resourceName/filePath (%1/%2)",
resourceName, filePath, level:
LogLevel.ERROR);
80 return GetContainerClassName(
Resource.Load(prefab));
88 static string GetContainerClassName(
Resource prefabResource)
90 if (!prefabResource || !prefabResource.IsValid())
101 return prefabBase.GetClassName();
110 if (!prefabResource || !prefabResource.IsValid())
117 return prefabBase.ToEntitySource();
127 if (!prefabResource || !prefabResource.IsValid())
130 IEntitySource prefabEntity = FindEntitySource(prefabResource);
134 return FindComponentSource(prefabEntity, componentClassName);
144 if (!prefabResource || !prefabResource.IsValid())
147 IEntitySource prefabEntity = FindEntitySource(prefabResource);
151 return FindComponentSource(prefabEntity, componentClass);
159 static int FindComponentIndex(
IEntitySource entitySource,
string componentClassName)
164 int componentsCount = entitySource.GetComponentCount();
165 for (
int i; i < componentsCount; i++)
168 if (componentSource.GetClassName() == componentClassName)
180 static int FindComponentIndex(
IEntitySource entitySource,
typename componentClass)
185 int componentsCount = entitySource.GetComponentCount();
187 for (
int i; i < componentsCount; i++)
189 componentSource = entitySource.GetComponent(i);
190 if (componentSource.GetClassName().ToType() &&
191 componentSource.GetClassName().ToType().IsInherited(componentClass))
209 for (
int i, componentsCount = prefabEntity.GetComponentCount(); i < componentsCount; i++)
211 componentSource = prefabEntity.GetComponent(i);
212 if (componentSource.GetClassName() == componentClassName)
213 return componentSource;
230 for (
int i, componentsCount = prefabEntity.GetComponentCount(); i < componentsCount; i++)
232 componentSource = prefabEntity.GetComponent(i);
233 if (componentSource.GetClassName().ToType() &&
234 componentSource.GetClassName().ToType().IsInherited(componentClass))
235 return componentSource;
247 static int FindComponentSources(
Resource prefabResource, notnull array<string> componentClassNames, notnull out array<ref array<IEntityComponentSource>> componentSources)
249 int classNamesCount = componentClassNames.Count();
250 componentSources.Clear();
251 componentSources.Resize(classNamesCount);
253 if (!prefabResource || !prefabResource.IsValid())
254 return classNamesCount;
256 IEntitySource prefabEntity = FindEntitySource(prefabResource);
258 return classNamesCount;
260 return FindComponentSources(prefabEntity, componentClassNames, componentSources);
269 static int FindComponentSources(
IEntitySource prefabEntity, notnull array<string> componentClassNames, notnull out array<ref array<IEntityComponentSource>> componentSources)
271 int classNamesCount = componentClassNames.Count();
272 componentSources.Clear();
273 componentSources.Resize(classNamesCount);
276 return classNamesCount;
279 array<IEntityComponentSource> components;
280 for (
int i, componentsCount = prefabEntity.GetComponentCount(); i < componentsCount; i++)
282 componentSource = prefabEntity.GetComponent(i);
283 string componentClassName = componentSource.GetClassName();
284 for (
int j = 0; j < classNamesCount; j++)
286 if (componentClassName == componentClassNames[j])
288 components = componentSources[j];
292 componentSources.Set(j, components);
295 components.Insert(componentSource);
301 return classNamesCount;
311 static int FindComponentSourcesOfClass(
IEntitySource prefabEntity,
typename componentClass,
bool GetChildComponentsOfComponents, notnull out array<IEntityComponentSource> componentSources)
313 componentSources.Clear();
318 array<IEntityComponentSource> components;
319 for (
int i, componentsCount = prefabEntity.GetComponentCount(); i < componentsCount; i++)
321 componentSource = prefabEntity.GetComponent(i);
323 if (componentSource.GetClassName().ToType().IsInherited(componentClass))
324 componentSources.Insert(componentSource);
327 if (GetChildComponentsOfComponents)
329 array<IEntityComponentSource> componentSourceChildren = {};
330 array<IEntityComponentSource> componentSourceChildChildren = {};
333 if (GetComponentSourceChildren(componentSource, componentSourceChildren) > 0)
337 if (componentSourceChild.GetClassName().ToType().IsInherited(componentClass))
338 componentSources.Insert(componentSourceChild);
341 if (GetComponentSourceChildren(componentSourceChild, componentSourceChildChildren) > 0)
345 if (componentSourceChildChild.GetClassName().ToType().IsInherited(componentClass))
346 componentSources.Insert(componentSourceChildChild);
354 return componentSources.Count();
362 static int GetComponentSourceChildren(notnull
IEntityComponentSource componentSource, notnull out array<IEntityComponentSource> componentSources)
364 componentSources.Clear();
370 for (
int i, count = containerList.Count(); i < count; i++)
372 componentSource = containerList.Get(i);
373 if (!componentSource)
376 componentSources.Insert(componentSource);
379 return componentSources.Count();
426 if (container.GetResourceName().Contains(
"/"))
429 ancestor = ancestor.GetAncestor();
443 if (container.GetResourceName().Contains(
"/"))
444 return container.GetResourceName();
446 container = container.GetAncestor();
456 static array<string> GetPrefabSetValueNames(notnull
BaseContainer baseContainer)
458 array<string> result = {};
459 for (
int i, count = baseContainer.GetNumVars(); i < count; i++)
461 string varName = baseContainer.GetVarName(i);
462 if (baseContainer.IsVariableSetDirectly(varName))
463 result.Insert(varName);
476 BaseContainer ancestorContainer = baseContainer.GetAncestor();
477 if (!ancestorContainer)
478 return baseContainer;
480 while (ancestorContainer.GetAncestor())
482 ancestorContainer = ancestorContainer.GetAncestor();
485 return ancestorContainer;
503 container = container.GetAncestor();
516 if (checkedResourceName.IsEmpty())
520 if (!resource.IsValid())
531 return IsKindOf(prefabBase, supposedAncestor);
544 if (entitySource.Get(
"coords", coordsEntity))
547 entitySource = entitySource.GetParent();
563 if (entitySource.Get(
"coords", coordsEntity))
566 entitySource = entitySource.GetParent();
576 static string GetArrayValue(notnull array<int> values)
579 foreach (
int i,
int value : values)
584 result += value.ToString();