This repository was archived by the owner on Apr 1, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathconvertProject.cs
508 lines (421 loc) · 21.8 KB
/
convertProject.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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Xml;
using System.Data.SQLite;
using System.Xml.Linq;
using Microsoft.WindowsAPICodePack;
using Microsoft.WindowsAPICodePack.Dialogs;
namespace ModBuilder.Forms
{
public partial class convertProject : Form
{
public convertProject()
{
InitializeComponent();
}
private void browseOutputDirectory_Click(object sender, EventArgs e)
{
// Get us a new FolderBrowserDialog
CommonOpenFileDialog fb = new CommonOpenFileDialog();
fb.IsFolderPicker = true;
fb.Title = "Please select the directory where your project should be created.";
fb.EnsurePathExists = true;
CommonFileDialogResult rs = fb.ShowDialog();
if (rs == CommonFileDialogResult.Cancel)
return;
// Get the path.
string dir = fb.FileName;
// Did we get a valid response?
if (!String.IsNullOrEmpty(dir) && Directory.Exists(dir))
{
// Being the nice program I am, I shall check if a project already exists in this directory.
if (File.Exists(dir + "/data.sqlite") && File.Exists(dir + "/Package/package-info.xml"))
{
DialogResult qresult = MessageBox.Show("Mod Manager has detected that a project already exists in the selected directory. Are you sure you want to continue and possibly overwrite the existing project?", "Options", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
// No? Quit.
if (qresult == DialogResult.No)
return;
}
// Update the textbox.
outputDirectory.Text = dir;
}
}
private void browseInputPackageDirectory_Click(object sender, EventArgs e)
{
// Get us a new FolderBrowserDialog
CommonOpenFileDialog fb = new CommonOpenFileDialog();
fb.IsFolderPicker = true;
fb.Title = "Please select the directory where your package is located.";
fb.EnsurePathExists = true;
CommonFileDialogResult rs = fb.ShowDialog();
if (rs == CommonFileDialogResult.Cancel)
return;
// Get the path.
string dir = fb.FileName;
// Did we get a valid response?
if (!String.IsNullOrEmpty(dir) && Directory.Exists(dir))
{
// Being the nice program I am, I shall check if a project already exists in this directory.
if (!File.Exists(dir + "/package-info.xml"))
{
DialogResult qresult = MessageBox.Show("Mod Manager has detected that there is no package-info.xml in the package, so you will have to manually select any files in the package. Do you still want to continue?", "Converting", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
// No? Quit.
if (qresult == DialogResult.No)
return;
}
// Clean out the old fields.
packageInfoXMLPath.Text = "";
installXmlPath.Text = "";
readmeTXTPath.Text = "";
installPHPPath.Text = "";
uninstallPHPPath.Text = "";
installDatabasePHPPath.Text = "";
uninstallDatabasePHPPath.Text = "";
// Update the textbox.
packageInputPath.Text = dir;
#region Read the package-info.xml, if it exists.
if (File.Exists(dir + "/package-info.xml"))
{
packageInfoXMLPath.Text = dir + "\\package-info.xml";
XmlDocument doc = new XmlDocument();
doc.Load(dir + "/package-info.xml");
foreach (XmlNode l_packageNode in doc.LastChild.ChildNodes)
{
if (l_packageNode.Name == "install")
{
foreach (XmlNode l_operationNode in l_packageNode.ChildNodes)
{
switch (l_operationNode.Name)
{
case "modification":
installXmlPath.Text = dir + "/" + l_operationNode.InnerText;
break;
case "code":
installPHPPath.Text = dir + "/" + l_operationNode.InnerText;
break;
case "database":
installDatabasePHPPath.Text = dir + "/" + l_operationNode.InnerText;
break;
case "readme":
if (File.Exists(dir + "/" + l_operationNode.InnerText))
readmeTXTPath.Text = dir + "/" + l_operationNode.InnerText;
else
readmeTXTPath.Text = "Inline";
break;
}
}
}
if (l_packageNode.Name == "uninstall")
{
foreach (XmlNode l_operationNode in l_packageNode.ChildNodes)
{
switch (l_operationNode.Name)
{
case "code":
uninstallPHPPath.Text = dir + "/" + l_operationNode.InnerText;
break;
case "database":
uninstallDatabasePHPPath.Text = dir + "/" + l_operationNode.InnerText;
break;
}
}
}
}
}
#endregion
}
}
#region Browse for files
private string browseFile(string description, string filter)
{
// New file browser.
OpenFileDialog of = new OpenFileDialog();
// Set the description and alike.
of.CheckFileExists = true;
of.InitialDirectory = (!String.IsNullOrEmpty(packageInputPath.Text) ? packageInputPath.Text : "");
of.Title = (!String.IsNullOrEmpty(description) ? description : "Select a file...");
of.Filter = (!String.IsNullOrEmpty(filter) ? filter : "All files|*.*");
// Show the dialog.
of.ShowDialog();
// Did we get a valid input?
if (!string.IsNullOrEmpty(of.FileName) || !File.Exists(of.FileName))
return "false";
// We did. Now return it.
return of.FileName;
}
private void browsePackageInfoXML_Click(object sender, EventArgs e)
{
string result = browseFile("Select the package-info.xml file...", "XML files|*.xml");
if (result == "false")
return;
packageInfoXMLPath.Text = result;
}
private void browseReadmeTXT_Click(object sender, EventArgs e)
{
string result = browseFile("Select the file containing your readme...", "TXT files|*.txt");
if (result == "false")
return;
readmeTXTPath.Text = result;
}
private void browseInstallXML_Click(object sender, EventArgs e)
{
string result = browseFile("Select the XML file containing your installation instructions...", "XML files|*.xml");
if (result == "false")
return;
installXmlPath.Text = result;
}
private void browseInstallPHP_Click(object sender, EventArgs e)
{
string result = browseFile("Select the PHP file containing code to be run on installation...", "PHP files|*.php");
if (result == "false")
return;
installPHPPath.Text = result;
}
private void browseUninstallPHP_Click(object sender, EventArgs e)
{
string result = browseFile("Select the PHP file containing code to be run on deinstallation...", "PHP files|*.php");
if (result == "false")
return;
uninstallPHPPath.Text = result;
}
private void browseInstallDatabasePHP_Click(object sender, EventArgs e)
{
string result = browseFile("Select the PHP file containing database code to be run on installation...", "PHP files|*.php");
if (result == "false")
return;
installDatabasePHPPath.Text = result;
}
private void browseUninstallDatabasePHP_Click(object sender, EventArgs e)
{
string result = browseFile("Select the PHP file containing code to be run on deinstallation...", "PHP files|*.php");
if (result == "false")
return;
uninstallDatabasePHPPath.Text = result;
}
#endregion
private void okButton_Click(object sender, EventArgs e)
{
if (!Directory.Exists(packageInputPath.Text))
{
MessageBox.Show("Your input directory does not exist. Please select a different one.", "Convert Project", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
if (!Directory.Exists(outputDirectory.Text))
Directory.CreateDirectory(outputDirectory.Text);
// Make sure the user knows we're busy...
System.Threading.Thread.Sleep(100);
cvWorking.Visible = true;
modEditor me = new modEditor();
Dictionary<string, string> details = Classes.ModParser.parsePackageInfo(packageInfoXMLPath.Text);
me.generateSQL(outputDirectory.Text, true, details);
// Update a setting.
string updatesql = "UPDATE settings SET value = \"false\" WHERE key = \"autoGenerateModID\"";
SQLiteCommand ucomm = new SQLiteCommand(updatesql, me.conn);
ucomm.ExecuteNonQuery();
// Create a readme.txt if the text is inline.
bool readmeTextInline = (readmeTXTPath.Text == "Inline");
// Parse the XML.
#region Read the install.xml, if it exists.
if (File.Exists(installXmlPath.Text))
{
XmlTextReader xmldoc = new XmlTextReader(installXmlPath.Text);
xmldoc.DtdProcessing = DtdProcessing.Ignore;
string currfile = "";
string sql = "INSERT INTO instructions(id, before, after, type, file, optional) VALUES(null, @beforeText, @afterText, @type, @fileEdited, @optional)";
while (xmldoc.Read())
{
if (xmldoc.NodeType.Equals(XmlNodeType.Element))
{
switch (xmldoc.LocalName)
{
case "file":
currfile = xmldoc.GetAttribute("name");
break;
case "operation":
SQLiteCommand command = new SQLiteCommand(sql, me.conn);
command.Parameters.AddWithValue("@fileEdited", currfile);
command.Parameters.AddWithValue("@optional", (xmldoc.GetAttribute("error") == "skip"));
xmldoc.ReadToDescendant("search");
command.Parameters.AddWithValue("@type", xmldoc.GetAttribute("position"));
command.Parameters.AddWithValue("@beforeText", xmldoc.ReadElementContentAsString().Replace("\r", "\n").Replace("\n", "\r\n"));
xmldoc.ReadToNextSibling("add");
command.Parameters.AddWithValue("@afterText", xmldoc.ReadElementContentAsString().Replace("\r", "\n").Replace("\n", "\r\n"));
command.ExecuteNonQuery();
break;
}
}
}
}
#endregion
// Copy over any remaining files.
Dictionary<string, string> pfiles = new Dictionary<string, string>();
Directory.CreateDirectory(outputDirectory.Text + "/Package");
Directory.CreateDirectory(outputDirectory.Text + "/Source");
pfiles.Add("Package/package-info.xml", packageInfoXMLPath.Text);
if (!string.IsNullOrEmpty(readmeTXTPath.Text))
pfiles.Add("Package/readme.txt", readmeTXTPath.Text);
if (!string.IsNullOrEmpty(installPHPPath.Text))
pfiles.Add("Package/install.php", installPHPPath.Text);
if (!string.IsNullOrEmpty(uninstallPHPPath.Text))
pfiles.Add("Package/uninstall.php", uninstallPHPPath.Text);
if (!string.IsNullOrEmpty(installDatabasePHPPath.Text))
pfiles.Add("Package/installDatabase.php", installDatabasePHPPath.Text);
if (!string.IsNullOrEmpty(uninstallDatabasePHPPath.Text))
pfiles.Add("Package/uninstallDatabase.php", uninstallDatabasePHPPath.Text);
foreach (var pair in pfiles)
{
if (!File.Exists(pair.Value))
continue;
if (File.Exists(outputDirectory.Text + "/" + pair.Key))
File.Delete(outputDirectory.Text + "/" + pair.Key);
File.Copy(pair.Value, outputDirectory.Text + "/" + pair.Key);
}
#region Read the package.xml, if it exists, for any further information.
if (File.Exists(packageInfoXMLPath.Text))
{
XmlDocument l_document = new XmlDocument();
l_document.Load(packageInfoXMLPath.Text);
SQLiteCommand sql;
string sqlquery;
foreach (XmlNode l_packageNode in l_document.LastChild.ChildNodes)
{
Console.WriteLine("Test node name: " + l_packageNode.Name);
if (l_packageNode.Name == "install")
{
foreach (XmlNode l_operationNode in l_packageNode.ChildNodes)
{
Console.WriteLine("Test child node name: " + l_operationNode.Name);
switch (l_operationNode.Name)
{
case "readme":
if (readmeTextInline)
File.WriteAllText(outputDirectory.Text + "/Package/readme.txt", l_operationNode.InnerText.Replace("\r", "\n").Replace("\n", "\r\n"));
break;
case "require-file":
Console.WriteLine("File name: " + l_operationNode.Attributes["name"].Value);
Console.WriteLine("Destination: " + l_operationNode.Attributes["destination"].Value);
string[] pieces = l_operationNode.Attributes["name"].Value.Split('/');
string lastpiece = pieces[pieces.Length - 1];
if (!Directory.Exists(outputDirectory.Text + "/Source/" + l_operationNode.Attributes["name"].Value.Replace("/" + lastpiece, "")) && l_operationNode.Attributes["name"].Value != lastpiece)
Directory.CreateDirectory(outputDirectory.Text + "/Source/" + l_operationNode.Attributes["name"].Value.Replace("/" + lastpiece, ""));
File.Copy(packageInputPath.Text + "/" + l_operationNode.Attributes["name"].Value, outputDirectory.Text + "/Source/" + l_operationNode.Attributes["name"].Value, true);
Console.WriteLine("Copied file");
// Set up a query.
sqlquery = "INSERT INTO files(id, file_name, destination) VALUES(null, @fileName, @destination)";
sql = new SQLiteCommand(sqlquery, me.conn);
sql.Parameters.AddWithValue("@fileName", l_operationNode.Attributes["name"].Value);
sql.Parameters.AddWithValue("@destination", l_operationNode.Attributes["destination"].Value);
sql.ExecuteNonQuery();
break;
case "require-dir":
// Just copy over the dir.
DirectoryCopy(packageInputPath.Text + "/" + l_operationNode.Attributes["name"].Value, outputDirectory.Text + "/Source/" + l_operationNode.Attributes["name"].Value, true);
// Set up a query.
sqlquery = "INSERT INTO files(id, file_name, destination) VALUES(null, @fileName, @destination)";
sql = new SQLiteCommand(sqlquery, me.conn);
sql.Parameters.AddWithValue("@fileName", l_operationNode.Attributes["name"].Value);
sql.Parameters.AddWithValue("@destination", l_operationNode.Attributes["destination"].Value);
sql.ExecuteNonQuery();
break;
}
}
}
if (l_packageNode.Name == "uninstall")
{
foreach (XmlNode l_operationNode in l_packageNode.ChildNodes)
{
switch (l_operationNode.Name)
{
case "remove-file":
sqlquery = "INSERT INTO files_delete(id, file_name, type) VALUES(null, @fileName, @type)";
sql = new SQLiteCommand(sqlquery, me.conn);
sql.Parameters.AddWithValue("@fileName", l_operationNode.Attributes["name"].Value);
sql.Parameters.AddWithValue("@type", "file");
sql.ExecuteNonQuery();
break;
case "remove-dir":
sqlquery = "INSERT INTO files_delete(id, file_name, type) VALUES(null, @fileName, @type)";
sql = new SQLiteCommand(sqlquery, me.conn);
sql.Parameters.AddWithValue("@fileName", l_operationNode.Attributes["name"].Value);
sql.Parameters.AddWithValue("@type", "dir");
sql.ExecuteNonQuery();
break;
}
}
}
}
}
#endregion
me.Close();
DialogResult result = MessageBox.Show("The package has been converted, do you want to load it now?", "Converting Project", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (result == DialogResult.Yes)
ModBuilder.Classes.PackageWorker.bootstrapLoad(outputDirectory.Text);
Close();
}
private static void DirectoryCopy(string sourceDirName, string destDirName, bool copySubDirs)
{
DirectoryInfo dir = new DirectoryInfo(sourceDirName);
DirectoryInfo[] dirs = dir.GetDirectories();
if (!dir.Exists)
{
throw new DirectoryNotFoundException(
"Source directory does not exist or could not be found: "
+ sourceDirName);
}
if (!Directory.Exists(destDirName))
{
Directory.CreateDirectory(destDirName);
}
FileInfo[] files = dir.GetFiles();
foreach (FileInfo file in files)
{
string temppath = Path.Combine(destDirName, file.Name);
file.CopyTo(temppath, true);
}
if (copySubDirs)
{
foreach (DirectoryInfo subdir in dirs)
{
string temppath = Path.Combine(destDirName, subdir.Name);
DirectoryCopy(subdir.FullName, temppath, copySubDirs);
}
}
}
private void cancelButton_Click(object sender, EventArgs e)
{
// Just close.
Close();
}
private void cleanReadme_Click(object sender, EventArgs e)
{
readmeTXTPath.Text = "";
}
private void cleanInstall_Click(object sender, EventArgs e)
{
installXmlPath.Text = "";
}
private void cleanInstallCode_Click(object sender, EventArgs e)
{
installPHPPath.Text = "";
}
private void cleanDeinstallCode_Click(object sender, EventArgs e)
{
uninstallPHPPath.Text = "";
}
private void cleanDBInstall_Click(object sender, EventArgs e)
{
installDatabasePHPPath.Text = "";
}
private void cleanDBDeinstall_Click(object sender, EventArgs e)
{
uninstallDatabasePHPPath.Text = "";
}
}
}