10 static Managed CreateInstanceFromPrefab(ResourceName prefab,
bool printError =
false)
12 Resource resource = Resource.Load(prefab);
13 if (!resource.IsValid())
16 Print(
string.Format(
"'SCR_BaseContainerTools', method 'CreateInstanceFromPrefab': failed '%1' at the Resource.Load step!", prefab), LogLevel.ERROR);
21 BaseContainer baseContainer = resource.GetResource().ToBaseContainer();
25 Print(
string.Format(
"'SCR_BaseContainerTools', method 'CreateInstanceFromPrefab': failed '%1' at the BaseContainer step!", prefab), LogLevel.ERROR);
30 Managed managed = BaseContainerTools.CreateInstanceFromContainer(baseContainer);
34 Print(
string.Format(
"'SCR_BaseContainerTools', method 'CreateInstanceFromPrefab': failed '%1' create instance step!", prefab), LogLevel.ERROR);
47 static string GetContainerClassName(ResourceName prefab)
49 return GetContainerClassName(Resource.Load(prefab));
57 static string GetContainerClassName(Resource prefabResource)
59 if (!prefabResource || !prefabResource.IsValid())
62 BaseResourceObject prefabContainer = prefabResource.GetResource();
66 BaseContainer prefabBase = prefabContainer.ToBaseContainer();
70 return prefabBase.GetClassName();
77 static IEntitySource FindEntitySource(Resource prefabResource)
79 if (!prefabResource || !prefabResource.IsValid())
82 BaseResourceObject prefabBase = prefabResource.GetResource();
86 return prefabBase.ToEntitySource();
94 static IEntityComponentSource FindComponentSource(Resource prefabResource,
string componentClassName)
96 if (!prefabResource || !prefabResource.IsValid())
99 IEntitySource prefabEntity = FindEntitySource(prefabResource);
103 return FindComponentSource(prefabEntity, componentClassName);
111 static IEntityComponentSource FindComponentSource(Resource prefabResource,
typename componentClass)
113 if (!prefabResource || !prefabResource.IsValid())
116 IEntitySource prefabEntity = FindEntitySource(prefabResource);
120 return FindComponentSource(prefabEntity, componentClass);
128 static IEntityComponentSource FindComponentSource(IEntitySource prefabEntity,
string componentClassName)
133 IEntityComponentSource componentSource;
134 for (
int i, componentsCount = prefabEntity.GetComponentCount(); i < componentsCount; i++)
136 componentSource = prefabEntity.GetComponent(i);
137 if (componentSource.GetClassName() == componentClassName)
138 return componentSource;
149 static IEntityComponentSource FindComponentSource(IEntitySource prefabEntity,
typename componentClass)
154 IEntityComponentSource componentSource
155 for (
int i, componentsCount = prefabEntity.GetComponentCount(); i < componentsCount; i++)
157 componentSource = prefabEntity.GetComponent(i);
158 if (componentSource.GetClassName().ToType().IsInherited(componentClass))
159 return componentSource;
171 static int FindComponentSources(Resource prefabResource, notnull array<string> componentClassNames, notnull out array<ref array<IEntityComponentSource>> componentSources)
173 int classNamesCount = componentClassNames.Count();
174 componentSources.Clear();
175 componentSources.Resize(classNamesCount);
177 if (!prefabResource || !prefabResource.IsValid())
178 return classNamesCount;
180 IEntitySource prefabEntity = FindEntitySource(prefabResource);
182 return classNamesCount;
184 return FindComponentSources(prefabEntity, componentClassNames, componentSources);
193 static int FindComponentSources(IEntitySource prefabEntity, notnull array<string> componentClassNames, notnull out array<ref array<IEntityComponentSource>> componentSources)
195 int classNamesCount = componentClassNames.Count();
196 componentSources.Clear();
197 componentSources.Resize(classNamesCount);
200 return classNamesCount;
202 IEntityComponentSource componentSource;
203 array<IEntityComponentSource> components;
204 for (
int i, componentsCount = prefabEntity.GetComponentCount(); i < componentsCount; i++)
206 componentSource = prefabEntity.GetComponent(i);
207 string componentClassName = componentSource.GetClassName();
208 for (
int j = 0; j < classNamesCount; j++)
210 if (componentClassName == componentClassNames[j])
212 components = componentSources[j];
216 componentSources.Set(j, components);
219 components.Insert(componentSource);
225 return classNamesCount;
267 static BaseContainer GetPrefabContainer(BaseContainer container)
269 BaseContainer ancestor = container;
272 if (container.GetResourceName().Contains(
"/"))
275 ancestor = ancestor.GetAncestor();
285 static ResourceName GetPrefabResourceName(BaseContainer container)
289 if (container.GetResourceName().Contains(
"/"))
290 return container.GetResourceName();
292 container = container.GetAncestor();
295 return ResourceName.Empty;
302 static array<string> GetPrefabSetValueNames(notnull BaseContainer baseContainer)
304 array<string> result = {};
305 for (
int i, count = baseContainer.GetNumVars(); i < count; i++)
307 string varName = baseContainer.GetVarName(i);
308 if (baseContainer.IsVariableSetDirectly(varName))
309 result.Insert(varName);
320 static BaseContainer GetTopMostAncestor(notnull BaseContainer baseContainer)
322 BaseContainer ancestorContainer = baseContainer.GetAncestor();
323 if (!ancestorContainer)
324 return baseContainer;
326 while (ancestorContainer.GetAncestor())
328 ancestorContainer = ancestorContainer.GetAncestor();
331 return ancestorContainer;
339 static bool IsKindOf(notnull BaseContainer container, ResourceName resourceName)
341 if (resourceName.IsEmpty())
346 if (container.GetResourceName() == resourceName)
349 container = container.GetAncestor();
360 static bool IsKindOf(ResourceName checkedResourceName, ResourceName supposedAncestor)
362 if (checkedResourceName.IsEmpty())
365 Resource resource = Resource.Load(checkedResourceName);
366 if (!resource.IsValid())
369 BaseResourceObject container = resource.GetResource();
373 BaseContainer prefabBase = container.ToBaseContainer();
377 return IsKindOf(prefabBase, supposedAncestor);
385 static vector GetWorldCoords(IEntitySource entitySource, vector coords)
390 if (entitySource.Get(
"coords", coordsEntity))
391 coords += coordsEntity;
393 entitySource = entitySource.GetParent();
404 static vector GetLocalCoords(IEntitySource entitySource, vector coords)
409 if (entitySource.Get(
"coords", coordsEntity))
410 coords -= coordsEntity;
412 entitySource = entitySource.GetParent();
422 static string GetArrayValue(notnull array<int> values)
425 foreach (
int i,
int value : values)
430 result += value.ToString();