Skip to content

Commit 8daaa16

Browse files
committed
Added several QOL features to the script viewer and some logic to prevent incorrect syntax highlighting in the edited names.
1 parent ad50ff2 commit 8daaa16

13 files changed

+352
-103
lines changed

AHT_Triggers/Data/ByteCodeDecompiler.cs

+7-9
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public class ByteCodeDecompiler
8585
/// <summary>
8686
/// How many spaces are added for each indentation level.
8787
/// </summary>
88-
private static readonly int INDENT_LEVEL = 4;
88+
public int IndentationAmount { get; set; }
8989

9090
/// <summary>
9191
/// Describes how a line of code will change the indentation.
@@ -198,10 +198,7 @@ private enum IndentState
198198
/// Instantiates the <see cref="ByteCodeDecompiler"/> with the <see cref="GameScript"/> that it will read data from.
199199
/// </summary>
200200
/// <param name="script">Script that the decompiler will read data from</param>
201-
public ByteCodeDecompiler(GameScript script)
202-
{
203-
this.Script = script;
204-
}
201+
public ByteCodeDecompiler(GameScript script) : this(script, null) { }
205202

206203
/// <summary>
207204
/// Instantiates the <see cref="ByteCodeDecompiler"/> with the <see cref="GameScript"/> that it will read data from,
@@ -213,6 +210,7 @@ public ByteCodeDecompiler(GameScript script, GameScriptSaveInfo info)
213210
{
214211
this.Script = script;
215212
this.SaveInfo = info;
213+
this.IndentationAmount = 4; //default
216214
}
217215

218216
/// <summary>
@@ -581,7 +579,7 @@ private string ValToString(int value)
581579

582580
/// <summary>
583581
/// Add whitespace before the given string <paramref name="s"/>
584-
/// according to the current <see cref="Indentation"/> and <see cref="INDENT_LEVEL"/>.
582+
/// according to the current <see cref="Indentation"/> and <see cref="IndentationAmount"/>.
585583
/// </summary>
586584
/// <param name="s">Command to indent</param>
587585
/// <returns>Indented command</returns>
@@ -593,7 +591,7 @@ private string AddIndentToCommand(string s)
593591
return s;
594592
} else
595593
{
596-
return new string(' ', Indentation * INDENT_LEVEL) + s;
594+
return new string(' ', Indentation * IndentationAmount) + s;
597595
}
598596
}
599597

@@ -921,7 +919,7 @@ public string DecompileScript(out DecodeResult res)
921919
{
922920
if (v.proc == CurrentProc)
923921
{
924-
codeStr.Insert(i + offs, new string(' ', INDENT_LEVEL) + "INT " + GetVarName(v.index));
922+
codeStr.Insert(i + offs, new string(' ', IndentationAmount) + "INT " + GetVarName(v.index));
925923
offs++;
926924

927925
addNewLine = true;
@@ -952,7 +950,7 @@ public string DecompileScript(out DecodeResult res)
952950

953951
if (lblName != null)
954952
{
955-
codeStr.Insert(i + offs, new string(' ', INDENT_LEVEL) + "LABEL " + lblName);
953+
codeStr.Insert(i + offs, new string(' ', IndentationAmount) + "LABEL " + lblName);
956954
offs++;
957955
}
958956

AHT_Triggers/Data/ByteSwapper.cs

+1-3
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,8 @@
66

77
namespace AHT_Triggers.Data
88
{
9-
internal class ByteSwapper
9+
internal static class ByteSwapper
1010
{
11-
private ByteSwapper() { }
12-
1311
public static ushort SwapBytes(ushort x)
1412
{
1513
return (ushort)((ushort)((x & 0xff) << 8) | ((x >> 8) & 0xff));

AHT_Triggers/Data/HardCodedMapInfo.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace AHT_Triggers.Data
99
/// <summary>
1010
/// Map information that is defined in the game's executable instead of the map file.
1111
/// </summary>
12-
public class MapInfo
12+
public static class MapInfo
1313
{
1414
public struct HardCodedMapInfo
1515
{

AHT_Triggers/Data/ScriptSaveInfoHandler.cs

+3-5
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,14 @@ namespace AHT_Triggers.Data
88
/// Contains a public static variable to store the saved info for the current session.
99
/// Provides methods to load/save variable names from/to a file.
1010
/// </summary>
11-
public class ScriptSaveInfoHandler
11+
public static class ScriptSaveInfoHandler
1212
{
1313
/// <summary>
1414
/// Active saved info object for the gamescript being viewed.
15+
/// Made static so as to be accessed from anywhere.
1516
/// </summary>
1617
public static GameScriptSaveInfo ActiveInfo = new GameScriptSaveInfo();
1718

18-
//private ctor (this class does not need instancing)
19-
private ScriptSaveInfoHandler() { }
20-
2119
/// <summary>
2220
/// Get the path to where the info for the script with the given info will be saved/loaded from.
2321
/// This is the app directory + a folder called "gs_saved" + the file name.
@@ -252,7 +250,7 @@ public static void SaveInfoToFile(string fileName, int mapIndex, Trigger trigger
252250

253251
//Open file
254252
using (StreamWriter writer = new StreamWriter(
255-
new FileStream(txtpath, FileMode.Create, FileAccess.Write)
253+
new FileStream(txtpath, FileMode.Create)
256254
)
257255
)
258256
{

AHT_Triggers/EditVars.Designer.cs

+9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

AHT_Triggers/EditVars.cs

+128-12
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ public partial class EditVars : Form
1919
private readonly int selectedMap;
2020
private readonly Trigger trigger;
2121

22+
//Lists to keep track of the last value in any given cell of the gridviews, so it can be reverted
23+
private readonly List<string> savedVarNames = new List<string>();
24+
private readonly List<string> savedProcNames = new List<string>();
25+
private readonly List<string> savedLabelNames = new List<string>();
26+
2227
public EditVars(ScriptViewer parent, string viewingFile, int selectedMap, Trigger trigger)
2328
{
2429
InitializeComponent();
@@ -47,15 +52,27 @@ private void PopulateLists()
4752

4853
for (int i = 0; i < Info.NumVars(); i++)
4954
{
50-
DGV_Vars.Rows.Add(Info.GetVar(i));
55+
int cellNr = DGV_Vars.Rows.Add(Info.GetVar(i));
56+
var cell = DGV_Vars.Rows[cellNr].Cells[0];
57+
58+
if (i < 24)
59+
{
60+
cell.Style.BackColor = Color.LightGray;
61+
}
62+
63+
savedVarNames.Add(Info.GetVar(i));
5164
}
5265
for (int i = 0; i < Info.NumProcs(); i++)
5366
{
5467
DGV_Procs.Rows.Add(Info.GetProc(i));
68+
69+
savedProcNames.Add(Info.GetProc(i));
5570
}
5671
foreach (KeyValuePair<int, string> entry in Info.Labels)
5772
{
5873
DGV_Labels.Rows.Add(entry.Value);
74+
75+
savedLabelNames.Add(entry.Value);
5976
}
6077
}
6178

@@ -99,17 +116,6 @@ private void SaveScriptInfo()
99116
ScriptViewerWnd.InsertDecompiledCode();
100117
}
101118

102-
private void Btn_Apply_Click(object sender, EventArgs e)
103-
{
104-
SaveScriptInfo();
105-
}
106-
107-
private void DGV_Vars_CellValueChanged(object sender, DataGridViewCellEventArgs e)
108-
{
109-
GameScriptSaveInfo Info = ScriptSaveInfoHandler.ActiveInfo;
110-
Info.SetVar(e.RowIndex, DGV_Vars.Rows[e.RowIndex].Cells[0].Value.ToString());
111-
}
112-
113119
private void Btn_Reset_Click(object sender, EventArgs e)
114120
{
115121
DialogResult res = MessageBox.Show(
@@ -135,5 +141,115 @@ private void Btn_Reset_Click(object sender, EventArgs e)
135141
PopulateLists();
136142
}
137143
}
144+
145+
private void Btn_Apply_Click(object sender, EventArgs e)
146+
{
147+
SaveScriptInfo();
148+
}
149+
150+
private void DGV_Vars_CellValueChanged(object sender, DataGridViewCellEventArgs e)
151+
{
152+
if (e.RowIndex < 0)
153+
{
154+
return;
155+
}
156+
157+
DataGridViewCell cell = DGV_Vars.Rows[e.RowIndex].Cells[0];
158+
159+
//Check if using reserved syntax
160+
foreach (string s in Syntax.SYNTAX_KEYWORDS)
161+
{
162+
if (((string)cell.Value).IndexOf(s) >= 0)
163+
{
164+
MessageBox.Show("A variable cannot contain reserved keyword " + s + ".", "Invalid name",
165+
MessageBoxButtons.OK, MessageBoxIcon.Warning);
166+
167+
cell.Value = savedVarNames[e.RowIndex];
168+
return;
169+
}
170+
}
171+
//Check if using hashcode naming
172+
if (((string)cell.Value).IndexOf("HT_") >= 0)
173+
{
174+
MessageBox.Show("A variable cannot contain \"HT_\", as this marks the start of a hashcode.", "Invalid name",
175+
MessageBoxButtons.OK, MessageBoxIcon.Warning);
176+
177+
cell.Value = savedVarNames[e.RowIndex];
178+
return;
179+
}
180+
181+
//Else overwrite our saved value
182+
savedVarNames[e.RowIndex] = (string)cell.Value;
183+
}
184+
185+
private void DGV_Procs_CellValueChanged(object sender, DataGridViewCellEventArgs e)
186+
{
187+
if (e.RowIndex < 0)
188+
{
189+
return;
190+
}
191+
192+
DataGridViewCell cell = DGV_Procs.Rows[e.RowIndex].Cells[0];
193+
194+
//Check if using reserved syntax
195+
foreach (string s in Syntax.SYNTAX_KEYWORDS)
196+
{
197+
if (((string)cell.Value).IndexOf(s) >= 0)
198+
{
199+
MessageBox.Show("A procedure cannot contain reserved keyword " + s + ".", "Invalid name",
200+
MessageBoxButtons.OK, MessageBoxIcon.Warning);
201+
202+
cell.Value = savedProcNames[e.RowIndex];
203+
return;
204+
}
205+
}
206+
//Check if using hashcode naming
207+
if (((string)cell.Value).IndexOf("HT_") >= 0)
208+
{
209+
MessageBox.Show("A procedure cannot contain \"HT_\", as this marks the start of a hashcode.", "Invalid name",
210+
MessageBoxButtons.OK, MessageBoxIcon.Warning);
211+
212+
cell.Value = savedProcNames[e.RowIndex];
213+
return;
214+
}
215+
216+
//Else overwrite our saved value
217+
savedProcNames[e.RowIndex] = (string)cell.Value;
218+
}
219+
220+
private void DGV_Labels_CellValueChanged(object sender, DataGridViewCellEventArgs e)
221+
{
222+
if (e.RowIndex < 0)
223+
{
224+
return;
225+
}
226+
227+
DataGridViewCell cell = DGV_Labels.Rows[e.RowIndex].Cells[0];
228+
229+
//Check if using reserved syntax
230+
foreach (string s in Syntax.SYNTAX_KEYWORDS)
231+
{
232+
if (((string)cell.Value).IndexOf(s) >= 0)
233+
{
234+
MessageBox.Show("A label cannot contain reserved keyword " + s + ".", "Invalid name",
235+
MessageBoxButtons.OK, MessageBoxIcon.Warning);
236+
237+
cell.Value = savedLabelNames[e.RowIndex];
238+
return;
239+
}
240+
}
241+
//Check if using hashcode naming
242+
if (((string)cell.Value).IndexOf("HT_") >= 0)
243+
{
244+
MessageBox.Show("A label cannot contain \"HT_\", as this marks the start of a hashcode.", "Invalid name",
245+
MessageBoxButtons.OK, MessageBoxIcon.Warning);
246+
247+
cell.Value = savedLabelNames[e.RowIndex];
248+
return;
249+
}
250+
251+
//Else overwrite our saved value
252+
savedLabelNames[e.RowIndex] = (string)cell.Value;
253+
}
138254
}
139255
}

AHT_Triggers/EditVars.resx

+8-2
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,12 @@
120120
<metadata name="Vars.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
121121
<value>True</value>
122122
</metadata>
123+
<metadata name="ToolTips.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
124+
<value>17, 17</value>
125+
</metadata>
126+
<metadata name="ToolTips.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
127+
<value>17, 17</value>
128+
</metadata>
123129
<metadata name="dataGridViewTextBoxColumn1.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
124130
<value>True</value>
125131
</metadata>
@@ -129,7 +135,7 @@
129135
<metadata name="dataGridViewTextBoxColumn2.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
130136
<value>True</value>
131137
</metadata>
132-
<metadata name="dataGridViewTextBoxColumn2.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
133-
<value>True</value>
138+
<metadata name="ToolTips.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
139+
<value>17, 17</value>
134140
</metadata>
135141
</root>

0 commit comments

Comments
 (0)