Arma Reforger Explorer 1.7.0.54
Arma Reforger Code Explorer by Zeroy - Thanks to MisterOutofTime
Loading...
Searching...
No Matches
SCR_RuinPrefabAssignerPlugin.c
Go to the documentation of this file.
1#ifdef WORKBENCH
3 name: " Ruin Prefab Assigner",
4 category: "Building Destruction",
5 description: "Assign Ruin Prefab to building Prefabs with a Ruin in the same directory",
6 wbModules: { "WorldEditor" },
7 awesomeFontCode: 0xF6BE)]
8class SCR_RuinPrefabAssignerPlugin : WorkbenchPlugin
9{
10 protected static const string PREFAB_EXTENSION = "et";
11 protected static const string PREFAB_EXTENSION_DOTTED = "." + PREFAB_EXTENSION;
12
13 //------------------------------------------------------------------------------------------------
14 protected override void Run()
15 {
16 WorldEditorAPI worldEditorAPI = SCR_WorldEditorToolHelper.GetWorldEditorAPI();
17// if (!worldEditorAPI)
18// {
19// Print("World Editor API is not available", LogLevel.WARNING);
20// return;
21// }
22
23 Resource resource;
24 BaseResourceObject baseResourceObject;
25 IEntitySource entitySource;
26 IEntityComponentSource componentSource;
27 BaseContainerList effects;
28 BaseContainer effect;
29 typename type;
30 array<ref ContainerIdPathEntry> weApiPath = {};
31 array<string> ruinPrefabEndings = { "_ruin", "_Ruin", "_ruins", "_Ruins" };
32 array<string> basePrefabEndings = { "_base", "_Base" };
33 array<ResourceName> ruinPrefabs = SCR_WorkbenchHelper.SearchWorkbenchResources({ PREFAB_EXTENSION }, { "_Ruin" }); // covers both _ruin and _ruins
34 foreach (ResourceName ruinPrefab : ruinPrefabs)
35 {
36 string ruinRelativeFilePath = ruinPrefab.GetPath();
37 if (!ruinRelativeFilePath)
38 continue;
39
40 string ruinFileName = FilePath.StripExtension(FilePath.StripPath(ruinRelativeFilePath));
41 if (ruinFileName.EndsWith("_base"))
42 continue;
43
44 if (!SCR_StringHelper.EndsWithAny(ruinFileName, ruinPrefabEndings)) // ...it should
45 {
46 Print("skipping an invalid ruin filename (must END with _[Rr]uin(s), no? what's the standard?) - " + ruinRelativeFilePath, LogLevel.WARNING);
47 continue;
48 }
49
50 string relativeDirectory = FilePath.StripFileName(ruinRelativeFilePath);
51 if (!relativeDirectory.EndsWith(SCR_StringHelper.SLASH))
52 relativeDirectory += SCR_StringHelper.SLASH;
53
54 string buildingFileNameWithoutExtension = ruinFileName.Substring(0, ruinFileName.LastIndexOf(SCR_StringHelper.UNDERSCORE));
55 string baseBuildingRelativeFilePath;
56 foreach (string basePrefabEnding : basePrefabEndings)
57 {
58 if (FileIO.FileExists(relativeDirectory + buildingFileNameWithoutExtension + basePrefabEnding + PREFAB_EXTENSION_DOTTED))
59 {
60 baseBuildingRelativeFilePath = relativeDirectory + buildingFileNameWithoutExtension + basePrefabEnding + PREFAB_EXTENSION_DOTTED;
61 break;
62 }
63 }
64
65 if (!baseBuildingRelativeFilePath)
66 {
67 Print("Cannot find the base building Prefab for " + ruinPrefab, LogLevel.WARNING);
68 continue;
69 }
70
71 ResourceName baseBuildingPrefab = Workbench.GetResourceName(baseBuildingRelativeFilePath);
72 if (!baseBuildingPrefab)
73 {
74 Print("Skipping base building Prefab, not registered - " + baseBuildingRelativeFilePath, LogLevel.WARNING);
75 continue;
76 }
77
78 resource = Resource.Load(baseBuildingPrefab);
79 if (!resource.IsValid())
80 continue;
81
82 baseResourceObject = resource.GetResource();
83 if (!baseResourceObject)
84 continue;
85
86 entitySource = baseResourceObject.ToBaseContainer();
87 if (!entitySource)
88 continue;
89
90 int componentIndex = SCR_BaseContainerTools.FindComponentIndex(entitySource, SCR_DestructibleBuildingComponent);
91 if (componentIndex < 0)
92 {
93 Print("Base building Prefab does not have an SCR_DestructibleBuildingComponent component - " + baseBuildingPrefab, LogLevel.WARNING);
94 continue;
95 }
96
97 componentSource = entitySource.GetComponent(componentIndex);
98 if (!componentSource)
99 {
100 Print("Base building Prefab does not have an SCR_DestructibleBuildingComponent component (but has an index?!) - " + baseBuildingPrefab, LogLevel.WARNING);
101 continue;
102 }
103
104 effects = componentSource.GetObjectArray("m_aEffects");
105 if (!effects)
106 {
107 Print("Base building Prefab does not have SCR_DestructibleBuildingComponent's m_aEffects initialised - " + baseBuildingPrefab, LogLevel.WARNING);
108 continue;
109 }
110
111 bool found;
112 int effectIndex = -1;
113 ResourceName baseBuildingRuinPrefab;
114 int effectsCount = effects.Count();
115 for (int i; i < effectsCount; ++i)
116 {
117 effect = effects.Get(i);
118 type = effect.GetClassName().ToType();
119 if (!type || !type.IsInherited(SCR_TimedPrefab))
120 continue;
121
122 if (effect.Get("m_sRuinsPrefab", baseBuildingRuinPrefab))
123 {
124 if (baseBuildingRuinPrefab)
125 {
126 if (baseBuildingRuinPrefab == ruinPrefab)
127 {
128 Print("Ruin already set - " + baseBuildingPrefab + " ruin is " + ruinFileName, LogLevel.NORMAL);
129 found = true;
130 break;
131 }
132 else
133 {
134 Print("Ruin already set to something different - " + baseBuildingPrefab + "\nwant to set: " + ruinPrefab + "\nis currently: " + baseBuildingRuinPrefab, LogLevel.NORMAL);
135 found = true;
136 break;
137 }
138 }
139 else // found empty
140 {
141 effectIndex = i;
142 break;
143 }
144 }
145 }
146
147 if (found)
148 continue;
149
150 weApiPath.Clear();
151 weApiPath.Insert(new ContainerIdPathEntry("Components", componentIndex));
152 if (effectIndex < 0) // create it
153 {
154 if (!worldEditorAPI.CreateObjectArrayVariableMember(entitySource, weApiPath, "m_aEffects", "SCR_TimedPrefab", effectsCount))
155 {
156 Print("Cannot create a new m_aEffects member - " + baseBuildingPrefab, LogLevel.WARNING);
157 continue;
158 }
159
160 Print("Successful creation of a new m_aEffects member", LogLevel.NORMAL);
161 effectIndex = effectsCount;
162 }
163
164 weApiPath.Insert(new ContainerIdPathEntry("m_aEffects", effectIndex));
165 if (!worldEditorAPI.SetVariableValue(entitySource, weApiPath, "m_sRuinsPrefab", ruinPrefab))
166 {
167 Print("Cannot set m_sRuinsPrefab to the wanted value - " + baseBuildingPrefab, LogLevel.WARNING);
168 continue;
169 }
170
171 // good?
172 }
173 }
174
175 /*
176 //------------------------------------------------------------------------------------------------
177 protected override void Configure()
178 {
179 // if (!Workbench.ScriptDialog("SCR_RuinPrefabAssignerPlugin", "<Description>", this))
180 // return;
181 }
182
183 //------------------------------------------------------------------------------------------------
184 [ButtonAttribute("Close", true)]
185 protected int ButtonClose()
186 {
187 return 1;
188 }
189 */
190
191 //------------------------------------------------------------------------------------------------
192 // constructor
193 protected void SCR_RuinPrefabAssignerPlugin();
194}
195#endif
void ContainerIdPathEntry(string propertyName, int index=-1)
Definition worldEditor.c:30
GenerateFlowMaps WorkbenchPlugin WorkbenchPluginAttribute("Regenerate river flow-maps", "Generate and save/overwrite river flow-maps", "", "", {"WorldEditor"}, "", 0xf773)
Definition FlowmapTool.c:59
EDamageType type
override void Run()
Object holding reference to resource. In destructor release the resource.
Definition Resource.c:25
static bool EndsWithAny(string input, notnull array< string > lineEnds)
static array< ResourceName > SearchWorkbenchResources(array< string > fileExtensions=null, array< string > searchStrArray=null, string rootPath="", bool recursive=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