Arma Reforger Explorer 1.7.0.54
Arma Reforger Code Explorer by Zeroy - Thanks to MisterOutofTime
Loading...
Searching...
No Matches
SCR_PrefabManagementTool.c
Go to the documentation of this file.
1#ifdef WORKBENCH
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
11{
12 /*
13 Category: General
14 */
15
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;
18
19 [Attribute(defvalue: "1", desc: "If a destination file already exists, it is overwritten", category: "General")]
20 protected bool m_bOverwriteFiles;
21
22 /*
23 Category: Suffixes
24 */
25
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;
28
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;
31
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;
34
35 // ^ more can be added if needed
36
37 /*
38 Category: Clone
39 */
40
41 [Attribute(defvalue: "0", desc: "Which Suffixes array will be used for Clones - 0 = no suffixes", /* uiwidget: UIWidgets.Slider, */params: "0 3 1", category: "Clone")]
42 protected int m_iCloneSuffixes;
43
44 [Attribute(defvalue: "{C502C0D612FB13CE}Prefabs", desc: "Path to save the clones", params: "unregFolders", category: "Clone")]
45 protected ResourceName m_sCloneSavePath;
46
47 /*
48 Category: Children
49 */
50
51 [Attribute(defvalue: "0", desc: "Which Suffixes array will be used for creating Children - 0 = no suffixes", /* uiwidget: UIWidgets.Slider, */params: "0 3 1", category: "Children")]
52 protected int m_iChildrenSuffixes;
53
54 [Attribute(defvalue: "{C502C0D612FB13CE}Prefabs", desc: "Path to save the prefabs' children", params: "unregFolders", category: "Children")]
55 protected ResourceName m_sChildrenSavePath;
56
57 /*
58 Category: XOB Import
59 */
60
61 [Attribute(defvalue: "{C502C0D612FB13CE}Prefabs", desc: "Path to save the XOB prefabs", params: "unregFolders", category: "XOB Import")]
62 protected ResourceName m_sXOBSavePath;
63
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;
68
69 [Attribute(desc: "Parent prefab to created prefabs", uiwidget: UIWidgets.ResourcePickerThumbnail, params: PREFAB_EXTENSION, category: "XOB Import")]
70 protected ResourceName m_sParentPrefab;
71
72 [Attribute(desc: "Create a \"_base\" prefab for each imported xob", params: PREFAB_EXTENSION, category: "XOB Import")]
73 protected bool m_bCreateBasePrefab;
74
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";
78
79 //------------------------------------------------------------------------------------------------
80 [ButtonAttribute("Clone Prefabs")]
81 protected void BtnClonePrefabs()
82 {
83 array<string> suffixes = GetSelectedSuffixes(m_iCloneSuffixes);
84 if (!suffixes)
85 {
86 Print("No such suffixes defined - check the index", LogLevel.ERROR);
87 return;
88 }
89
90 CreateClonesOrChildren(suffixes, true);
91 }
92
93 //------------------------------------------------------------------------------------------------
94 [ButtonAttribute("Create Children")]
95 protected void BtnCreateChildren()
96 {
97 array<string> suffixes = GetSelectedSuffixes(m_iChildrenSuffixes);
98 if (!suffixes)
99 {
100 Print("No such suffixes defined - check the index", LogLevel.ERROR);
101 return;
102 }
103
104 CreateClonesOrChildren(suffixes, false);
105 }
106
107 //------------------------------------------------------------------------------------------------
108 [ButtonAttribute("Import XOBs")]
109 protected void BtnImportXOBs()
110 {
111 array<ResourceName> foundXOBs;
112
113 string sourceDir;
114 string targetDir;
115
116 if (m_bKeepSingleDirectoryStructure)
117 {
118 array<ResourceName> dirs = SCR_WorldEditorToolHelper.GetSelectedDirectories();
119 if (dirs && dirs.Count() == 1) // get currently selected directory's XOB content
120 {
121 sourceDir = dirs[0].GetPath();
122 targetDir = m_sXOBSavePath.GetPath();
123 }
124 }
125
126 // get -selected- XOBs
127 foundXOBs = SCR_WorldEditorToolHelper.GetSelectedOrOpenedResources("xob");
128
129 if (foundXOBs.IsEmpty())
130 {
131 Print("No models are opened or selected in the Resource Browser", LogLevel.WARNING);
132 return;
133 }
134
135 int importedXOBs;
136
137 Debug.BeginTimeMeasure();
138
139 foreach (ResourceName foundXOB : foundXOBs)
140 {
141 string fileNameWithoutExtension = FilePath.StripExtension(FilePath.StripPath(foundXOB.GetPath()));
142
143 string relativeSaveDirectory;
144 if (m_sXOBSavePath.IsEmpty()) // save in xob's directory
145 relativeSaveDirectory = FilePath.StripFileName(foundXOB.GetPath());
146 else if (sourceDir.IsEmpty()) // save in provided directory
147 relativeSaveDirectory = m_sXOBSavePath.GetPath();
148 else // save in provided directory using tree structure
149 relativeSaveDirectory = SCR_PrefabHelper.GetRelativeParentDirectory(FilePath.StripFileName(foundXOB.GetPath()), targetDir);
150
151 string absoluteSaveDirectory;
152 if (!SCR_AddonTool.GetAddonAbsolutePath(m_iAddon, relativeSaveDirectory, absoluteSaveDirectory, false))
153 {
154 Print("Wrong path: " + SCR_AddonTool.ToFileSystem(SCR_AddonTool.GetAddonID(m_iAddon)) + relativeSaveDirectory, LogLevel.WARNING);
155 continue;
156 }
157
158 if (!absoluteSaveDirectory.EndsWith("/"))
159 absoluteSaveDirectory += "/";
160
161 string absoluteFilePath = absoluteSaveDirectory + fileNameWithoutExtension + "." + PREFAB_EXTENSION;
162 if (!m_bOverwriteFiles && FileIO.FileExists(absoluteFilePath))
163 {
164 Print("File already exists, skipping " + absoluteFilePath, LogLevel.WARNING);
165 continue;
166 }
167
168 SCR_PrefabHelper.CreatePrefabFromXOB(foundXOB, absoluteFilePath, m_sParentPrefab, m_bCreateBasePrefab);
169 importedXOBs++;
170 }
171
172 Debug.EndTimeMeasure("Importing " + importedXOBs + "/" + foundXOBs.Count() + " XOBs as Prefabs");
173 }
174
175 //------------------------------------------------------------------------------------------------
176 protected void CreateClonesOrChildren(array<string> rawSuffixes, bool isCloning)
177 {
178 array<ResourceName> foundPrefabs = SCR_WorldEditorToolHelper.GetSelectedOrOpenedResources(PREFAB_EXTENSION);
179 if (foundPrefabs.IsEmpty())
180 {
181 Print("No Prefabs are opened or selected in the Resource Browser", LogLevel.WARNING);
182 return;
183 }
184
185 set<string> suffixes = new set<string>();
186 foreach (string suffix : rawSuffixes)
187 {
188 suffix = SanitiseAndFormatSuffix(suffix);
189 if (suffix.IsEmpty())
190 continue;
191
192 suffixes.Insert(suffix);
193 }
194
195 if (suffixes.IsEmpty())
196 {
197 if (isCloning)
198 suffixes.Insert(DEFAULT_CLONE_SUFFIX);
199 else
200 suffixes.Insert(DEFAULT_CHILD_SUFFIX);
201 }
202
203 int created;
204
205 Debug.BeginTimeMeasure();
206
207 foreach (ResourceName foundPrefab : foundPrefabs)
208 {
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());
212
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();
218 else
219 relativeSaveDirectory = FilePath.StripFileName(foundPrefab.GetPath()); // prefab's directory
220
221 string absoluteSaveDirectory;
222 if (!SCR_AddonTool.GetAddonAbsolutePath(m_iAddon, relativeSaveDirectory, absoluteSaveDirectory))
223 {
224 Print("Wrong path: " + SCR_AddonTool.GetAddonID(m_iAddon) + relativeSaveDirectory, LogLevel.WARNING);
225 continue;
226 }
227
228 if (!absoluteSaveDirectory.EndsWith("/"))
229 absoluteSaveDirectory += "/";
230
231 foreach (string suffix : suffixes)
232 {
233 string absoluteFilePath = absoluteSaveDirectory + fileNameWithoutExtension + suffix + "." + PREFAB_EXTENSION;
234 if (!m_bOverwriteFiles && FileIO.FileExists(absoluteFilePath))
235 {
236 Print("File already exists, skipping " + absoluteFilePath, LogLevel.WARNING);
237 continue;
238 }
239
240 ResourceName resourceName;
241 if (isCloning)
242 resourceName = SCR_PrefabHelper.ClonePrefab(foundPrefab, absoluteFilePath);
243 else
244 resourceName = SCR_PrefabHelper.CreateChildPrefab(foundPrefab, absoluteFilePath);
245
246 if (resourceName.IsEmpty())
247 Print("Could not create prefab " + FilePath.StripPath(absoluteFilePath), LogLevel.ERROR);
248
249 created++;
250 }
251 }
252
253 if (isCloning)
254 Debug.EndTimeMeasure("Cloning " + created + "/" + (foundPrefabs.Count() * suffixes.Count()) + " (" + foundPrefabs.Count() + " prefabs x " + suffixes.Count() + " suffixes)");
255 else
256 Debug.EndTimeMeasure("Creating " + created + "/" + (foundPrefabs.Count() * suffixes.Count()) + " children (" + foundPrefabs.Count() + " prefabs x " + suffixes.Count() + " suffixes)");
257 }
258
259 //------------------------------------------------------------------------------------------------
264 protected string SanitiseAndFormatSuffix(string suffix)
265 {
266 suffix = SCR_StringHelper.Filter(suffix, SCR_StringHelper.LETTERS + SCR_StringHelper.DIGITS + "_-");
267 if (suffix.IsEmpty())
268 return suffix;
269
270 if (!suffix.StartsWith("_"))
271 suffix = "_" + suffix;
272
273 return suffix;
274 }
275
276 //------------------------------------------------------------------------------------------------
277 protected array<string> GetSelectedSuffixes(int selectedIndex)
278 {
279 selectedIndex--; // let's get civilised shalt we
280
281 switch (selectedIndex)
282 {
283 case -1: return {}; // index 0 = no suffixes
284 case 0: return m_aSuffixes1;
285 case 1: return m_aSuffixes2;
286 case 2: return m_aSuffixes3;
287
288 // ^ more can be added
289 }
290
291 return null;
292 }
293}
294#endif // WORKBENCH
ResourceName resourceName
Definition SCR_AIGroup.c:66
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.
Definition LogLevel.c:14
SCR_FieldOfViewSettings Attribute
void Debug()
Definition Types.c:327