Skip to content

Commit 8232663

Browse files
committed
Add C# script for listing unused scripting elements
1 parent 6b6e370 commit 8232663

File tree

2 files changed

+139
-0
lines changed

2 files changed

+139
-0
lines changed
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
// Script for listing unused scripting elements
2+
3+
// Using clauses.
4+
// Unless you know what's in the WAE code-base, you want to always include
5+
// these "standard usings".
6+
using System;
7+
using System.Globalization;
8+
using System.Text;
9+
using TSMapEditor.CCEngine;
10+
using TSMapEditor.Models;
11+
using TSMapEditor.Models.Enums;
12+
13+
namespace WAEScript
14+
{
15+
public class ListUnusedScriptingElements
16+
{
17+
/// <summary>
18+
/// Returns the description of this script.
19+
/// All scripts must contain this function.
20+
/// </summary>
21+
public string GetDescription() => "This script lists unused scripting elements." + Environment.NewLine + Environment.NewLine +
22+
"Unintentionally unused scripting elements might point to the map's scripting having a bug." + Environment.NewLine + Environment.NewLine +
23+
"Do you want to continue?";
24+
25+
/// <summary>
26+
/// Returns the message that is presented to the user if running this script succeeded.
27+
/// All scripts must contain this function.
28+
/// </summary>
29+
public string GetSuccessMessage()
30+
{
31+
if (sb.Length == 0)
32+
{
33+
return "The map has no unused scripting elements.";
34+
}
35+
36+
return "The map has the following unused scripting elements: " + Environment.NewLine + Environment.NewLine + sb.ToString();
37+
}
38+
39+
private StringBuilder sb;
40+
41+
private bool TriggerReferencesTeamType(Map map, TeamType teamType, Trigger trigger)
42+
{
43+
foreach (var action in trigger.Actions)
44+
{
45+
TriggerActionType triggerActionType = map.EditorConfig.TriggerActionTypes[action.ActionIndex];
46+
47+
for (int i = 0; i < triggerActionType.Parameters.Length; i++)
48+
{
49+
if (triggerActionType.Parameters[i].TriggerParamType == TriggerParamType.TeamType && action.Parameters[i] == teamType.ININame)
50+
{
51+
return true;
52+
}
53+
}
54+
}
55+
56+
return false;
57+
}
58+
59+
private bool TriggerReferencesLocalVariable(Map map, LocalVariable localVariable, Trigger trigger)
60+
{
61+
foreach (var action in trigger.Actions)
62+
{
63+
TriggerActionType triggerActionType = map.EditorConfig.TriggerActionTypes[action.ActionIndex];
64+
65+
for (int i = 0; i < triggerActionType.Parameters.Length; i++)
66+
{
67+
if (triggerActionType.Parameters[i].TriggerParamType == TriggerParamType.LocalVariable &&
68+
action.Parameters[i] == localVariable.Index.ToString(CultureInfo.InvariantCulture))
69+
{
70+
return true;
71+
}
72+
}
73+
}
74+
75+
return false;
76+
}
77+
78+
private bool ScriptReferencesLocalVariable(Map map, LocalVariable localVariable, Script script)
79+
{
80+
foreach (var scriptActionEntry in script.Actions)
81+
{
82+
ScriptAction scriptAction = map.EditorConfig.ScriptActions[scriptActionEntry.Action];
83+
84+
if (scriptAction.ParamType == TriggerParamType.LocalVariable)
85+
{
86+
if (scriptActionEntry.Argument == localVariable.Index)
87+
{
88+
return true;
89+
}
90+
}
91+
}
92+
93+
return false;
94+
}
95+
96+
/// <summary>
97+
/// The function that actually does the magic.
98+
/// </summary>
99+
/// <param name="map">Map argument that allows us to access map data.</param>
100+
public void Perform(Map map)
101+
{
102+
sb = new StringBuilder();
103+
104+
foreach (var taskforce in map.TaskForces)
105+
{
106+
if (!map.TeamTypes.Exists(tt => tt.TaskForce == taskforce))
107+
sb.Append($"- TaskForce \"{taskforce.Name}\"" + Environment.NewLine);
108+
}
109+
110+
foreach (var script in map.Scripts)
111+
{
112+
if (!map.TeamTypes.Exists(tt => tt.Script == script))
113+
sb.Append($"- Script \"{script.Name}\"" + Environment.NewLine);
114+
}
115+
116+
foreach (var teamtype in map.TeamTypes)
117+
{
118+
if (!map.AITriggerTypes.Exists(aitt => aitt.PrimaryTeam == teamtype || aitt.SecondaryTeam == teamtype) &&
119+
!map.Triggers.Exists(trigger => TriggerReferencesTeamType(map, teamtype, trigger)))
120+
{
121+
sb.Append($"- TeamType \"{teamtype.Name}\"" + Environment.NewLine);
122+
}
123+
}
124+
125+
foreach (var localVariable in map.LocalVariables)
126+
{
127+
if (!map.Triggers.Exists(trigger => TriggerReferencesLocalVariable(map, localVariable, trigger)) &&
128+
!map.Scripts.Exists(script => ScriptReferencesLocalVariable(map, localVariable, script)))
129+
{
130+
sb.Append($"- Local variable \"{localVariable.Name}\"" + Environment.NewLine);
131+
}
132+
}
133+
}
134+
}
135+
}

src/TSMapEditor/TSMapEditor.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
<Compile Remove="Config\Scripts\Activate Assign Mission Cursor Action.cs" />
3030
<Compile Remove="Config\Scripts\Apply Animated Water.cs" />
3131
<Compile Remove="Config\Scripts\Disable All Debug Triggers.cs" />
32+
<Compile Remove="Config\Scripts\List Unused Scripting Elements.cs" />
3233
<Compile Remove="Config\Scripts\Make Civilian Vehicles Sleep.cs" />
3334
<Compile Remove="Config\Scripts\Remove All Terrain Objects.cs" />
3435
<Compile Remove="Config\Scripts\Replace Autumn Grass With Tall Grass.cs" />
@@ -513,6 +514,9 @@
513514
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
514515
</None>
515516
<None Include="Config\Scripts\Disable All Debug Triggers.cs" />
517+
<None Include="Config\Scripts\List Unused Scripting Elements.cs">
518+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
519+
</None>
516520
<None Include="Config\Scripts\Make Civilian Vehicles Sleep.cs">
517521
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
518522
</None>

0 commit comments

Comments
 (0)