-
Notifications
You must be signed in to change notification settings - Fork 3
/
StructureForm.cs
201 lines (190 loc) · 8.64 KB
/
StructureForm.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
using ScriptNET;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
namespace MapleShark
{
public partial class StructureForm : DockContent
{
private MaplePacket mParsing = null;
private Stack<StructureNode> mSubNodes = new Stack<StructureNode>();
public StructureForm()
{
InitializeComponent();
}
public MainForm MainForm { get { return ParentForm as MainForm; } }
public TreeView Tree { get { return mTree; } }
public void ParseMaplePacket(MaplePacket pPacket)
{
mTree.Nodes.Clear();
mSubNodes.Clear();
pPacket.Rewind();
string scriptPath = Application.StartupPath + Path.DirectorySeparatorChar + "Scripts" + Path.DirectorySeparatorChar + pPacket.Locale.ToString() + Path.DirectorySeparatorChar + pPacket.Build.ToString() + Path.DirectorySeparatorChar + (pPacket.Outbound ? "Outbound" : "Inbound") + Path.DirectorySeparatorChar + "0x" + pPacket.Opcode.ToString("X4") + ".txt";
string commonPath = Application.StartupPath + Path.DirectorySeparatorChar + "Scripts" + Path.DirectorySeparatorChar + pPacket.Locale.ToString() + Path.DirectorySeparatorChar + pPacket.Build.ToString() + Path.DirectorySeparatorChar + "Common.txt";
if (File.Exists(scriptPath))
{
mParsing = pPacket;
try
{
StringBuilder scriptCode = new StringBuilder();
scriptCode.Append(File.ReadAllText(scriptPath));
if (File.Exists(commonPath)) scriptCode.Append(File.ReadAllText(commonPath));
Script script = Script.Compile(scriptCode.ToString());
script.Context.SetItem("ScriptAPI", new ScriptAPI(this));
script.Execute();
}
catch (Exception exc)
{
OutputForm output = new OutputForm("Script Error");
output.Append(exc.ToString());
output.Show(DockPanel, new Rectangle(MainForm.Location, new Size(400, 400)));
}
mParsing = null;
}
if (pPacket.Remaining > 0) mTree.Nodes.Add(new StructureNode("Undefined", pPacket.InnerBuffer, pPacket.Cursor, pPacket.Remaining));
}
private TreeNodeCollection CurrentNodes { get { return mSubNodes.Count > 0 ? mSubNodes.Peek().Nodes : mTree.Nodes; } }
internal string APIGetFiletime()
{
string ret = DateTime.Now.ToFileTime().ToString().Substring(12);
return ret;
}
internal byte APIAddByte(string pName)
{
byte value;
if (!mParsing.ReadByte(out value)) throw new Exception("Insufficient packet data");
CurrentNodes.Add(new StructureNode(pName, mParsing.InnerBuffer, mParsing.Cursor - 1, 1));
return value;
}
internal sbyte APIAddSByte(string pName)
{
sbyte value;
if (!mParsing.ReadSByte(out value)) throw new Exception("Insufficient packet data");
CurrentNodes.Add(new StructureNode(pName, mParsing.InnerBuffer, mParsing.Cursor - 1, 1));
return value;
}
internal ushort APIAddUShort(string pName)
{
ushort value;
if (!mParsing.ReadUShort(out value)) throw new Exception("Insufficient packet data");
CurrentNodes.Add(new StructureNode(pName, mParsing.InnerBuffer, mParsing.Cursor - 2, 2));
return value;
}
internal short APIAddShort(string pName)
{
short value;
if (!mParsing.ReadShort(out value)) throw new Exception("Insufficient packet data");
CurrentNodes.Add(new StructureNode(pName, mParsing.InnerBuffer, mParsing.Cursor - 2, 2));
return value;
}
internal uint APIAddUInt(string pName)
{
uint value;
if (!mParsing.ReadUInt(out value)) throw new Exception("Insufficient packet data");
CurrentNodes.Add(new StructureNode(pName, mParsing.InnerBuffer, mParsing.Cursor - 4, 4));
return value;
}
internal int APIAddInt(string pName)
{
int value;
if (!mParsing.ReadInt(out value)) throw new Exception("Insufficient packet data");
CurrentNodes.Add(new StructureNode(pName, mParsing.InnerBuffer, mParsing.Cursor - 4, 4));
return value;
}
internal float APIAddFloat(string pName)
{
float value;
if (!mParsing.ReadFloat(out value)) throw new Exception("Insufficient packet data");
CurrentNodes.Add(new StructureNode(pName, mParsing.InnerBuffer, mParsing.Cursor - 4, 4));
return value;
}
internal bool APIAddBool(string pName)
{
byte value;
if (!mParsing.ReadByte(out value)) throw new Exception("Insufficient packet data");
CurrentNodes.Add(new StructureNode(pName, mParsing.InnerBuffer, mParsing.Cursor - 1, 1));
return Convert.ToBoolean(value);
}
internal long APIAddLong(string pName)
{
long value;
if (!mParsing.ReadLong(out value)) throw new Exception("Insufficient packet data");
CurrentNodes.Add(new StructureNode(pName, mParsing.InnerBuffer, mParsing.Cursor - 8, 8));
return value;
}
internal long APIAddFlippedLong(string pName)
{
long value;
if (!mParsing.ReadFlippedLong(out value)) throw new Exception("Insufficient packet data");
CurrentNodes.Add(new StructureNode(pName, mParsing.InnerBuffer, mParsing.Cursor - 8, 8));
return value;
}
internal double APIAddDouble(string pName)
{
double value;
if (!mParsing.ReadDouble(out value)) throw new Exception("Insufficient packet data");
CurrentNodes.Add(new StructureNode(pName, mParsing.InnerBuffer, mParsing.Cursor - 8, 8));
return value;
}
internal string APIAddString(string pName)
{
APIStartNode(pName);
short size = APIAddShort("Size");
string value = APIAddPaddedString("String", size);
APIEndNode(false);
return value;
}
internal string APIAddPaddedString(string pName, int pLength)
{
string value;
if (!mParsing.ReadPaddedString(out value, pLength)) throw new Exception("Insufficient packet data");
CurrentNodes.Add(new StructureNode(pName, mParsing.InnerBuffer, mParsing.Cursor - pLength, pLength));
return value;
}
internal void APIAddField(string pName, int pLength)
{
byte[] buffer = new byte[pLength];
if (!mParsing.ReadBytes(buffer)) throw new Exception("Insufficient packet data");
CurrentNodes.Add(new StructureNode(pName, mParsing.InnerBuffer, mParsing.Cursor - pLength, pLength));
}
internal void APIAddComment(string pComment)
{
CurrentNodes.Add(new StructureNode(pComment, mParsing.InnerBuffer, mParsing.Cursor, 0));
}
internal void APIStartNode(string pName)
{
StructureNode node = new StructureNode(pName, mParsing.InnerBuffer, mParsing.Cursor, 0);
if (mSubNodes.Count > 0) mSubNodes.Peek().Nodes.Add(node);
else mTree.Nodes.Add(node);
mSubNodes.Push(node);
}
internal void APIEndNode(bool pExpand)
{
if (mSubNodes.Count > 0)
{
StructureNode node = mSubNodes.Pop();
node.Length = mParsing.Cursor - node.Cursor;
if (pExpand) node.Expand();
}
}
internal int APIRemaining() { return mParsing.Remaining; }
private void mTree_AfterSelect(object pSender, TreeViewEventArgs pArgs)
{
StructureNode node = pArgs.Node as StructureNode;
if (node == null) { MainForm.DataForm.HexBox.SelectionLength = 0; MainForm.PropertyForm.Properties.SelectedObject = null; return; }
MainForm.DataForm.HexBox.SelectionStart = node.Cursor;
MainForm.DataForm.HexBox.SelectionLength = node.Length;
MainForm.PropertyForm.Properties.SelectedObject = new StructureSegment(node.Buffer, node.Cursor, node.Length);
}
private void mTree_KeyDown(object pSender, KeyEventArgs pArgs)
{
MainForm.CopyPacketHex(pArgs);
}
}
}