2[WorkbenchToolAttribute(
3 name:
"Prefab Management Tool",
4 description:
"This Tool allows to:\n" +
5 "- clone existing Prefabs\n" +
6 "- create existing Prefabs' children\n" +
7 "- import XOBs as Prefabs\n" +
8 "Resulting Prefabs are created in the source's directory if a save path is not defined",
9 awesomeFontCode: 0xF49E)]
10class SCR_PrefabManagementTool : WorldEditorTool
16 [
Attribute(defvalue:
"1", uiwidget: UIWidgets.ComboBox,
desc:
"File system where new prefabs will be created", enums: SCR_ParamEnumArray.FromAddons(),
category:
"General")]
17 protected int m_iAddon;
19 [
Attribute(defvalue:
"1",
desc:
"If a destination file already exists, it is overwritten",
category:
"General")]
20 protected bool m_bOverwriteFiles;
26 [
Attribute(
desc:
"suffixes to name clones, e.g _red, _blue, _US, etc; the underscore is added automatically if needed. Accepted chars: [a-zA-Z0 - 9_-]",
category:
"Suffixes")]
27 protected ref array<string> m_aSuffixes1;
29 [
Attribute(
desc:
"suffixes to name clones, e.g _red, _blue, _US, etc; the underscore is added automatically if needed. Accepted chars: [a-zA-Z0 - 9_-]",
category:
"Suffixes")]
30 protected ref array<string> m_aSuffixes2;
32 [
Attribute(
desc:
"suffixes to name clones, e.g _red, _blue, _US, etc; the underscore is added automatically if needed. Accepted chars: [a-zA-Z0 - 9_-]",
category:
"Suffixes")]
33 protected ref array<string> m_aSuffixes3;
41 [
Attribute(defvalue:
"0",
desc:
"Which Suffixes array will be used for Clones - 0 = no suffixes",
params:
"0 3 1",
category:
"Clone")]
42 protected int m_iCloneSuffixes;
45 protected ResourceName m_sCloneSavePath;
51 [
Attribute(defvalue:
"0",
desc:
"Which Suffixes array will be used for creating Children - 0 = no suffixes",
params:
"0 3 1",
category:
"Children")]
52 protected int m_iChildrenSuffixes;
54 [
Attribute(defvalue:
"{C502C0D612FB13CE}Prefabs",
desc:
"Path to save the prefabs' children",
params:
"unregFolders",
category:
"Children")]
55 protected ResourceName m_sChildrenSavePath;
61 [
Attribute(defvalue:
"{C502C0D612FB13CE}Prefabs",
desc:
"Path to save the XOB prefabs",
params:
"unregFolders",
category:
"XOB Import")]
62 protected ResourceName m_sXOBSavePath;
64 [
Attribute(defvalue:
"0",
desc:
"if ONE directory is selected and XOB Save Path is defined, " +
65 "relative paths will be used to mimic that directory's structure\n" +
66 "e.g Assets/Rocks/Granite/Granite_01.xob saves to XOBSavePath/Rocks/Granite/Granite_01.et",
category:
"XOB Import")]
67 protected bool m_bKeepSingleDirectoryStructure;
69 [
Attribute(
desc:
"Parent prefab to created prefabs", uiwidget: UIWidgets.ResourcePickerThumbnail,
params: PREFAB_EXTENSION,
category:
"XOB Import")]
70 protected ResourceName m_sParentPrefab;
73 protected bool m_bCreateBasePrefab;
75 protected static const string PREFAB_EXTENSION =
"et";
76 protected static const string DEFAULT_CLONE_SUFFIX =
"_clone";
77 protected static const string DEFAULT_CHILD_SUFFIX =
"_child";
81 protected void BtnClonePrefabs()
83 array<string> suffixes = GetSelectedSuffixes(m_iCloneSuffixes);
86 Print(
"No such suffixes defined - check the index",
LogLevel.ERROR);
90 CreateClonesOrChildren(suffixes,
true);
95 protected void BtnCreateChildren()
97 array<string> suffixes = GetSelectedSuffixes(m_iChildrenSuffixes);
100 Print(
"No such suffixes defined - check the index",
LogLevel.ERROR);
104 CreateClonesOrChildren(suffixes,
false);
109 protected void BtnImportXOBs()
111 array<ResourceName> foundXOBs;
116 if (m_bKeepSingleDirectoryStructure)
118 array<ResourceName> dirs = SCR_WorldEditorToolHelper.GetSelectedDirectories();
119 if (dirs && dirs.Count() == 1)
121 sourceDir = dirs[0].GetPath();
122 targetDir = m_sXOBSavePath.GetPath();
127 foundXOBs = SCR_WorldEditorToolHelper.GetSelectedOrOpenedResources(
"xob");
129 if (foundXOBs.IsEmpty())
131 Print(
"No models are opened or selected in the Resource Browser",
LogLevel.WARNING);
137 Debug.BeginTimeMeasure();
139 foreach (ResourceName foundXOB : foundXOBs)
141 string fileNameWithoutExtension = FilePath.StripExtension(FilePath.StripPath(foundXOB.GetPath()));
143 string relativeSaveDirectory;
144 if (m_sXOBSavePath.IsEmpty())
145 relativeSaveDirectory = FilePath.StripFileName(foundXOB.GetPath());
146 else if (sourceDir.IsEmpty())
147 relativeSaveDirectory = m_sXOBSavePath.GetPath();
149 relativeSaveDirectory = SCR_PrefabHelper.GetRelativeParentDirectory(FilePath.StripFileName(foundXOB.GetPath()), targetDir);
151 string absoluteSaveDirectory;
152 if (!SCR_AddonTool.GetAddonAbsolutePath(m_iAddon, relativeSaveDirectory, absoluteSaveDirectory,
false))
154 Print(
"Wrong path: " + SCR_AddonTool.ToFileSystem(SCR_AddonTool.GetAddonID(m_iAddon)) + relativeSaveDirectory,
LogLevel.WARNING);
158 if (!absoluteSaveDirectory.EndsWith(
"/"))
159 absoluteSaveDirectory +=
"/";
161 string absoluteFilePath = absoluteSaveDirectory + fileNameWithoutExtension +
"." + PREFAB_EXTENSION;
162 if (!m_bOverwriteFiles && FileIO.FileExists(absoluteFilePath))
164 Print(
"File already exists, skipping " + absoluteFilePath,
LogLevel.WARNING);
168 SCR_PrefabHelper.CreatePrefabFromXOB(foundXOB, absoluteFilePath, m_sParentPrefab, m_bCreateBasePrefab);
172 Debug.EndTimeMeasure(
"Importing " + importedXOBs +
"/" + foundXOBs.Count() +
" XOBs as Prefabs");
176 protected void CreateClonesOrChildren(array<string> rawSuffixes,
bool isCloning)
178 array<ResourceName> foundPrefabs = SCR_WorldEditorToolHelper.GetSelectedOrOpenedResources(PREFAB_EXTENSION);
179 if (foundPrefabs.IsEmpty())
181 Print(
"No Prefabs are opened or selected in the Resource Browser",
LogLevel.WARNING);
185 set<string> suffixes =
new set<string>();
186 foreach (
string suffix : rawSuffixes)
188 suffix = SanitiseAndFormatSuffix(suffix);
189 if (suffix.IsEmpty())
192 suffixes.Insert(suffix);
195 if (suffixes.IsEmpty())
198 suffixes.Insert(DEFAULT_CLONE_SUFFIX);
200 suffixes.Insert(DEFAULT_CHILD_SUFFIX);
205 Debug.BeginTimeMeasure();
207 foreach (ResourceName foundPrefab : foundPrefabs)
209 string fileNameWithoutExtension = FilePath.StripExtension(FilePath.StripPath(foundPrefab.GetPath()));
210 if (fileNameWithoutExtension.EndsWith(SCR_PrefabHelper.PREFAB_BASE_SUFFIX))
211 fileNameWithoutExtension = fileNameWithoutExtension.Substring(0, fileNameWithoutExtension.Length() - SCR_PrefabHelper.PREFAB_BASE_SUFFIX.Length());
213 string relativeSaveDirectory;
214 if (isCloning && !m_sCloneSavePath.IsEmpty())
215 relativeSaveDirectory = m_sCloneSavePath.GetPath();
216 else if (!isCloning && !m_sChildrenSavePath.IsEmpty())
217 relativeSaveDirectory = m_sChildrenSavePath.GetPath();
219 relativeSaveDirectory = FilePath.StripFileName(foundPrefab.GetPath());
221 string absoluteSaveDirectory;
222 if (!SCR_AddonTool.GetAddonAbsolutePath(m_iAddon, relativeSaveDirectory, absoluteSaveDirectory))
224 Print(
"Wrong path: " + SCR_AddonTool.GetAddonID(m_iAddon) + relativeSaveDirectory,
LogLevel.WARNING);
228 if (!absoluteSaveDirectory.EndsWith(
"/"))
229 absoluteSaveDirectory +=
"/";
231 foreach (
string suffix : suffixes)
233 string absoluteFilePath = absoluteSaveDirectory + fileNameWithoutExtension + suffix +
"." + PREFAB_EXTENSION;
234 if (!m_bOverwriteFiles && FileIO.FileExists(absoluteFilePath))
236 Print(
"File already exists, skipping " + absoluteFilePath,
LogLevel.WARNING);
242 resourceName = SCR_PrefabHelper.ClonePrefab(foundPrefab, absoluteFilePath);
244 resourceName = SCR_PrefabHelper.CreateChildPrefab(foundPrefab, absoluteFilePath);
247 Print(
"Could not create prefab " + FilePath.StripPath(absoluteFilePath),
LogLevel.ERROR);
254 Debug.EndTimeMeasure(
"Cloning " + created +
"/" + (foundPrefabs.Count() * suffixes.Count()) +
" (" + foundPrefabs.Count() +
" prefabs x " + suffixes.Count() +
" suffixes)");
256 Debug.EndTimeMeasure(
"Creating " + created +
"/" + (foundPrefabs.Count() * suffixes.Count()) +
" children (" + foundPrefabs.Count() +
" prefabs x " + suffixes.Count() +
" suffixes)");
264 protected string SanitiseAndFormatSuffix(
string suffix)
266 suffix = SCR_StringHelper.Filter(suffix, SCR_StringHelper.LETTERS + SCR_StringHelper.DIGITS +
"_-");
267 if (suffix.IsEmpty())
270 if (!suffix.StartsWith(
"_"))
271 suffix =
"_" + suffix;
277 protected array<string> GetSelectedSuffixes(
int selectedIndex)
281 switch (selectedIndex)
284 case 0:
return m_aSuffixes1;
285 case 1:
return m_aSuffixes2;
286 case 2:
return m_aSuffixes3;
ResourceName resourceName
UI Textures DeployMenu Briefing conflict_HintBanner_1_UI desc
class WorkbenchDialog_AbortRetryIgnore ButtonAttribute("OK", true)
proto void Print(void var, LogLevel level=LogLevel.NORMAL)
Prints content of variable to console/log.
LogLevel
Enum with severity of the logging message.
SCR_FieldOfViewSettings Attribute