3 name:
"Validate Prefabs",
4 wbModules: {
"ResourceManager" },
6 awesomeFontCode: 0xF5BF)]
7class SCR_PrefabValidatorPlugin : WorkbenchPlugin
9 protected static const ref array<string> UNIQUE_COMPONENTS = {
"SCR_DestructionMultiPhaseComponent",
"MeshObject",
"RigidBody",
"RplComponent",
"Hierarchy" };
10 protected static const ref array<string> REPLICATED_COMPONENTS = {
"RplComponent",
"Hierarchy" };
13 protected void ValidatePrefabs(notnull array<ResourceName> prefabs)
15 ResourceManager resourceManager = Workbench.GetModule(ResourceManager);
16 WBProgressDialog progress =
new WBProgressDialog(
"Validating Prefabs...", resourceManager);
18 int count = prefabs.Count();
27 message =
string.Format(
"@\"%1\"", prefab.GetPath());
29 prefabResource =
Resource.Load(prefab);
30 if (prefabResource.IsValid())
32 prefabEntity = prefabResource.GetResource().ToEntitySource();
35 if (!ValidateEntitySource(prefabEntity, message))
40 currProgress = i / count;
41 if (currProgress - prevProgress >= 0.01)
43 progress.SetProgress(currProgress);
44 prevProgress = currProgress;
48 Print(
string.Format(
"%1 prefab(s) validated, %2 invalid ones found.", count, countInvalid),
LogLevel.NORMAL);
52 protected bool ValidateEntitySource(
IEntitySource entitySource,
string message,
int isReplicated = -1)
57 array<int> componentCounts = {};
58 for (
int i, count = UNIQUE_COMPONENTS.Count(); i < count; i++)
60 componentCounts.Insert(0);
63 bool isReplicatedCompatible;
65 string componentSourceClassName;
66 for (
int i, count = entitySource.GetComponentCount(); i < count; i++)
68 componentSource = entitySource.GetComponent(i);
69 componentSourceClassName = componentSource.GetClassName();
72 if (isReplicated == -1 && componentSourceClassName ==
"RplComponent")
75 componentSource.Get(
"Enabled", enabled);
81 if (isReplicated == 1 && REPLICATED_COMPONENTS.Contains(componentSourceClassName))
82 isReplicatedCompatible =
true;
85 int uniqueIndex = UNIQUE_COMPONENTS.Find(componentSourceClassName);
89 componentSource.Get(
"Enabled", enabled);
91 componentCounts[uniqueIndex] = componentCounts[uniqueIndex] + 1;
96 if (isReplicated == -1)
100 foreach (
int componentIndex,
int componentCount : componentCounts)
102 if (componentCount > 1)
104 Print(
string.Format(
"Duplicate component '%1' in prefab %2!", UNIQUE_COMPONENTS[componentIndex], message),
LogLevel.ERROR);
110 if (isReplicated && !isReplicatedCompatible)
112 Print(
string.Format(
"Entity %1 is in replicated prefab, but is missing 'RplComponent' or 'Hierarchy'!", message),
LogLevel.ERROR);
118 for (
int i, count = entitySource.GetNumChildren(); i < count; i++)
120 child = entitySource.GetChild(i);
121 valid &= ValidateEntitySource(child, message +
string.Format(
"[%1]", i, child.GetClassName()), isReplicated);
129 ResourceManager resourceManager = Workbench.GetModule(ResourceManager);
131 array<ResourceName> selection = {};
132 resourceManager.GetResourceBrowserSelection(selection.Insert,
true);
134 ValidatePrefabs(selection);
141 ResourceManager resourceManager = Workbench.GetModule(ResourceManager);
143 resourceManager.GetCmdLine(
"-validatePaths", pathsCLI);
145 array<string> paths = {};
146 pathsCLI.Split(
",", paths,
true);
148 array<ResourceName> prefabs = {};
150 filter.fileExtensions = {
"et" };
151 foreach (
string path : paths)
153 filter.rootPath =
path;
157 ValidatePrefabs(prefabs);
Object holding reference to resource. In destructor release the resource.
Object used for holding filtering params for ResourceDatabase.SearchResources() method.
proto void Print(void var, LogLevel level=LogLevel.NORMAL)
Prints content of variable to console/log.
LogLevel
Enum with severity of the logging message.