-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathMainForm.cs
238 lines (192 loc) · 7.63 KB
/
MainForm.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
using System;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Runtime.CompilerServices;
using System.Xml;
using Packed_Section_Reader;
using Primitive_File_Reader;
namespace wottools
{
public partial class MainForm : Form
{
public string PackedFileName = "";
public static readonly string sver = "0.5";
public static readonly string stitle = "WoT Mod Tools ";
public Packed_Section PS = new Packed_Section();
public Primitive_File PF = new Primitive_File();
public static readonly Int32 Binary_Header = 0x42a14e65;
public XmlDocument xDoc;
public MainForm(string args)
{
InitializeComponent();
if (args.Length != 0)
{
openFile(args);
}
}
private string FormatXml(string sUnformattedXml)
{
//load unformatted xml into a dom
XmlDocument xd = new XmlDocument();
xd.LoadXml(sUnformattedXml);
//will hold formatted xml
StringBuilder sb = new StringBuilder();
//pumps the formatted xml into the StringBuilder above
StringWriter sw = new StringWriter(sb);
//does the formatting
XmlTextWriter xtw = null;
try
{
//point the xtw at the StringWriter
xtw = new XmlTextWriter(sw);
//we want the output formatted
xtw.Formatting = Formatting.Indented;
//get the dom to dump its contents into the xtw
xd.WriteTo(xtw);
}
finally
{
//clean up even if error
if (xtw != null)
xtw.Close();
}
//return the formatted xml
return sb.ToString();
}
public void DecodePackedFile(BinaryReader reader)
{
reader.ReadSByte();
List<string> dictionary = PS.readDictionary(reader);
XmlNode xmlroot = xDoc.CreateNode(XmlNodeType.Element, PackedFileName, "");
PS.readElement(reader, xmlroot, xDoc, dictionary);
xDoc.AppendChild(xmlroot);
txtOut.AppendText(FormatXml(xDoc.OuterXml));
}
public void ReadPrimitiveFile(string file)
{
FileStream F = new FileStream(file, FileMode.Open, FileAccess.Read);
BinaryReader reader = new BinaryReader(F);
XmlComment ptiComment = xDoc.CreateComment("DO NOT SAVE THIS FILE! THIS CODE IS JUST FOR INFORMATION PUPORSES!");
XmlNode xmlprimitives = xDoc.CreateNode(XmlNodeType.Element, "primitives", "");
PF.ReadPrimitives(reader, xmlprimitives, xDoc);
xDoc.AppendChild(ptiComment);
xDoc.AppendChild(xmlprimitives);
txtOut.AppendText(FormatXml(xDoc.OuterXml));
}
public void openFile(string file)
{
saveAsToolStripMenuItem.Enabled = false;
btnSave.Enabled = false;
xDoc = new XmlDocument();
txtOut.Clear();
PackedFileName = Path.GetFileName(file);
PackedFileName = PackedFileName.ToLower();
Text = stitle + sver + " - " + PackedFileName;
FileStream F = new FileStream(file, FileMode.Open, FileAccess.Read);
BinaryReader reader = new BinaryReader(F);
Int32 head = reader.ReadInt32();
if (head == Packed_Section.Packed_Header)
{
DecodePackedFile(reader);
saveAsToolStripMenuItem.Enabled = true;
btnSave.Enabled = true;
}
else if (head == Binary_Header)
{
ReadPrimitiveFile(file);
//saveAsToolStripMenuItem.Enabled = true;
//btnSave.Enabled = true;
}
else
{
if (PackedFileName.Contains(".xml") || PackedFileName.Contains(".def") || PackedFileName.Contains(".visual") || PackedFileName.Contains(".chunk") || PackedFileName.Contains(".settings") || PackedFileName.Contains(".model"))
{
saveAsToolStripMenuItem.Enabled = true;
btnSave.Enabled = true;
txtOut.LoadFile(file, RichTextBoxStreamType.PlainText);
}
else
{
saveAsToolStripMenuItem.Enabled = false;
btnSave.Enabled = false;
throw new IOException("Invalid header");
}
}
reader.Close();
F.Close();
}
private void openToolStripMenuItem_Click(object sender, EventArgs e)
{
using (var ofd = new OpenFileDialog { Filter = "WoT Packed XML|*.xml;*.def;*.visual;*.chunk;*.settings;*.primitives;*.model;*.animation;*.anca|All files|*.*" })
if (DialogResult.OK == ofd.ShowDialog())
{
openFile(ofd.FileName);
}
}
private void saveAsToolStripMenuItem_Click(object sender, EventArgs e)
{
using (var sfd = new SaveFileDialog { })
{
sfd.Filter = "Unpacked XML|*.xml";
string SaveFileName = PackedFileName;
if (!PackedFileName.Contains(".xml"))
SaveFileName = SaveFileName + ".xml";
sfd.FileName = SaveFileName;
if (DialogResult.OK == sfd.ShowDialog())
{
xDoc.Save(sfd.FileName);
//txtOut.SaveFile(sfd.FileName, RichTextBoxStreamType.UnicodePlainText);
}
}
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
Close();
}
private void MainForm_Load(object sender, EventArgs e)
{
this.Text = stitle + sver;
this.DragEnter += new DragEventHandler(MainForm_DragEnter);
this.DragDrop += new DragEventHandler(MainForm_DragDrop);
}
void MainForm_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
e.Effect = DragDropEffects.Copy;
else
e.Effect = DragDropEffects.None;
}
void MainForm_DragDrop(object sender, DragEventArgs e)
{
string[] FileList = (string[])e.Data.GetData(DataFormats.FileDrop, false);
foreach (string File in FileList)
openFile(File);
}
private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
{
using (var abd = new AboutBox { })
{
if (DialogResult.Cancel == abd.ShowDialog())
{
}
}
}
private void newToolStripMenuItem_Click(object sender, EventArgs e)
{
saveAsToolStripMenuItem.Enabled = false;
btnSave.Enabled = false;
txtOut.Clear();
}
private void optionsToolStripMenuItem_Click(object sender, EventArgs e)
{
//
}
}
}