This repository was archived by the owner on Aug 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathForm1.cs
More file actions
583 lines (506 loc) · 21.1 KB
/
Copy pathForm1.cs
File metadata and controls
583 lines (506 loc) · 21.1 KB
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
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
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.Diagnostics;
namespace Hearts_of_Oak_Packager
{
public partial class Form1 : Form
{
int cLevel;
public List<string> IncludeFolders = new List<string>();
public List<string> IgnoreExtensions = new List<string>();
public List<string> IgnoreFolders = new List<string>();
public List<string> IgnoreFiles = new List<string>();
public List<string> IgnoreCompress = new List<string>();
public List<_setting> extRedirectPath = new List<_setting>();
public List<_setting> FolderRedirect = new List<_setting>();
public static string _settingFile = "settings.txt";
public List<_setting> settings = new List<_setting>();
private string settingFile = "settings.txt";
public Form1()
{
InitializeComponent();
cbxCompressionLevel.SelectedItem = "9";
FillListFromFile("IncludeFolders.txt", IncludeFolders);
FillListFromFile("IgnoreExtensions.txt", IgnoreExtensions);
FillListFromFile("IgnoreFolders.txt", IgnoreFolders);
FillListFromFile("IgnoreFiles.txt", IgnoreFiles);
FillListFromFile("IgnoreCompress.txt", IgnoreCompress);
GetSettings("SpecialExtensions.txt", extRedirectPath);
GetSettings("FolderRedirect.txt", FolderRedirect);
}
void FillListFromFile(string txtFile, List<string> _list)
{
if (File.Exists(txtFile))
{
AddRange(File.ReadAllLines(txtFile), _list);
}
}
public void AddRange(IEnumerable<string> collection, List<string> _list)
{
foreach (var i in collection) _list.Add(i);
}
private void btnCERootBrowse_Click(object sender, EventArgs e)
{
fbdCERoot.ShowNewFolderButton = false;
fbdCERoot.ShowDialog();
txbCERoot.Text = fbdCERoot.SelectedPath.ToString();
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Close();
}
private void btnGamePath_Click(object sender, EventArgs e)
{
fbdCERoot.ShowNewFolderButton = false;
fbdCERoot.ShowDialog();
txbGamePath.Text = fbdCERoot.SelectedPath.ToString();
}
private void btnOutput_Click(object sender, EventArgs e)
{
fbdCERoot.ShowNewFolderButton = true;
fbdCERoot.ShowDialog();
txbOutput.Text = fbdCERoot.SelectedPath.ToString();
}
private void btnBuild_Click(object sender, EventArgs e)
{
if (!bwpMove.IsBusy)
{
bwpMove.RunWorkerAsync();
}
}
#region "Functions"
private void FileSearchAndMove(string sourceDirName, string destDirName, string BaseDir)
{
foreach (string d in Directory.GetDirectories(sourceDirName))
{
foreach (string f in Directory.GetFiles(d))
{
//see if the extension matches a special extension
FileInfo fi = new FileInfo(f);
foreach (_setting s in extRedirectPath) {
if (fi.Extension == s._value) {
//correct it to be the folder it should be in, replace the base path subdir
string destDir = d.Replace(BaseDir,"");
destDir = destDirName + "\\" + s._name + destDir;
if (!Directory.Exists(destDir)) { Directory.CreateDirectory(destDir); };
destDir = destDir + "\\" + fi.Name;
fi.CopyTo(destDir, true);
}
}
}
FileSearchAndMove(d, destDirName, BaseDir);
}
}
private void DirectoryCopy(string sourceDirName, string destDirName, bool copySubDirs)
{
if (bwpMove.CancellationPending)
{
return;
}
// Get the subdirectories for the specified directory.
DirectoryInfo dir = new DirectoryInfo(sourceDirName);
DirectoryInfo[] dirs = dir.GetDirectories();
if (!dir.Exists)
{
bwpReport("Source directory does not exist or could not be found: "
+ sourceDirName);
//throw new DirectoryNotFoundException(
// "Source directory does not exist or could not be found: "
// + sourceDirName);
}
// If the destination directory doesn't exist, create it.
if (!Directory.Exists(destDirName))
{
Directory.CreateDirectory(destDirName);
}
// Get the files in the directory and copy them to the new location.
FileInfo[] files = dir.GetFiles();
foreach (FileInfo file in files)
{
if (!IgnoreExtensions.Any(file.Extension.Contains) && !IgnoreFiles.Any(file.Name.Contains))
{
string temppath = Path.Combine(destDirName, file.Name);
file.CopyTo(temppath, true);
bwpReport("Copying " + file.Name);
}
}
// If copying subdirectories, copy them and their contents to new location.
if (copySubDirs)
{
foreach (DirectoryInfo subdir in dirs)
{
if (!IgnoreFolders.Any(subdir.Name.ToLower().Equals)) {
string temppath = Path.Combine(destDirName, subdir.Name);
DirectoryCopy(subdir.FullName, temppath, copySubDirs);
}
}
}
}
private bool CompressFolder(string fPath)
{
try
{
string excludes = "";
foreach (string sI in IgnoreExtensions)
{
excludes += " -x!*" + sI;
}
foreach (string sI in IgnoreFiles)
{
excludes += " -x!" + sI;
}
string pClean = fPath + "\\*.* ";
Process p = new Process();
p.StartInfo.Verb = "runas";
p.StartInfo.CreateNoWindow = true;
p.StartInfo.UseShellExecute = false;
p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
p.StartInfo.FileName = "7za.exe";
p.StartInfo.Arguments = "a -tzip -r -v2g -mx" + cLevel + " " + fPath + ".pak " + pClean + excludes;
p.Start();
p.WaitForExit();
return true;
}
catch (Exception)
{
return false;
}
}
// Gets settings from a file. Setting is split by line then formatted as "name=value"
public static void GetSettings(string _fileName, List<_setting> _settings)
{
if (File.Exists(_fileName))
{
string[] sAllLines = File.ReadAllLines(_fileName);
foreach (string sLine in sAllLines)
{
//strip out comments from the lines
string[] fSplit = sLine.Split(new string[] { "--" }, System.StringSplitOptions.None);
if (fSplit[0].Trim().Length > 0)
{
//get the settings
string[] sSplit = sLine.Split(new Char[] { '=' });
if (sSplit.Length > 1)
{
_setting sInfo = new _setting();
sInfo._name = sSplit[0];
sInfo._value = sSplit[1];
_settings.Add(sInfo);
}
}
}
}
}
// overload load of GetSettings that builds it using a binding list instead of string list
public static void GetSettings(string _fileName, BindingList<_setting> _settings)
{
List<_setting> _sSettings = new List<_setting>();
GetSettings(_fileName, _sSettings);
foreach (_setting s in _sSettings)
{
_settings.Add(s);
}
}
//write out a file name with settings from list
private void SaveSettings(string _fileName, List<_setting> _settings)
{
if (_fileName == "") { return; };
List<string> _stringSettings = new List<string>();
if (File.Exists(_fileName)) { File.Delete(_fileName); };
foreach (_setting s in _settings)
{
_stringSettings.Add(s._name + "=" + s._value);
}
File.WriteAllLines(_fileName, _stringSettings);
MessageBox.Show("Settings saved.", "Settings", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
//get specific setting from list of settings
private string GetSetting(string name, List<_setting> _settings, string returntype = "string")
{
string returnValue = "";
if (_settings.Count > 0)
{
foreach (_setting s in _settings)
{
if (s._name.ToLower() == name.ToLower())
{
returnValue = s._value;
}
}
}
switch (returntype) {
case "bool":
if (returnValue == "" || returnValue.ToLower() == "false") { return "false"; };
if (returnValue.ToLower() == "true") { return "true"; };
break;
case "string":
break;
};
return returnValue;
}
#endregion
#region "Background Worker"
private void bwpMove_DoWork(object sender, DoWorkEventArgs e)
{
foreach (string f in Directory.GetFiles(txbCERoot.Text))
{
FileInfo fName = new FileInfo(@f);
if (!IgnoreExtensions.Any(fName.Extension.Contains) && !IgnoreFiles.Any(fName.Name.Contains))
File.Copy(f, txbOutput.Text + "\\" + fName.Name, true);
}
if (cbxCompressGame.Checked)
{
string gPath = new DirectoryInfo(txbGamePath.Text).Name;
FileSearchAndMove(txbGamePath.Text, Path.Combine(txbOutput.Text, gPath).ToString(), txbGamePath.Text);
}
//move all of the main paths except for the game folder
foreach (String d in Directory.GetDirectories(txbCERoot.Text))
{
string sPath = new DirectoryInfo(@d).Name;
string oPath = txbOutput.Text + "\\" + sPath;
if (IncludeFolders.Any(sPath.Contains))
{
//recursively move files from this path to the output folder
DirectoryCopy(d, oPath, true);
bwpReport(sPath + " copied", true);
}
};
//move all files in the game path
string iPath = new DirectoryInfo(txbGamePath.Text).Name;
string ogPath = txbOutput.Text + "\\" + iPath;
if (!Directory.Exists(ogPath)) { Directory.CreateDirectory(ogPath); };
foreach (string d in Directory.GetDirectories(txbGamePath.Text))
{
string dPath = new DirectoryInfo(@d).Name;
if (bwpMove.CancellationPending) { return; };
foreach (string f in Directory.GetFiles(txbGamePath.Text))
{
FileInfo fName = new FileInfo(@f);
if (!IgnoreExtensions.Any(fName.Extension.Contains) && !IgnoreFiles.Any(fName.Name.Contains))
File.Copy(f, ogPath + "\\" + fName.Name, true);
}
if (cbxCompressGame.Checked)
{
if (!IgnoreCompress.Any(dPath.Contains))
{
// find what folder this path would normally end up in
//if (!Directory.Exists(ogPath + "\\" + dPath)) { Directory.CreateDirectory(ogPath + "\\" + dPath); };
string dPath2 = dPath;
foreach (_setting s in FolderRedirect)
{
if (s._value == dPath)
{
dPath2 = s._name;
}
}
if (!Directory.Exists(ogPath + "\\" + dPath2)) { Directory.CreateDirectory(ogPath + "\\" + dPath2); };
DirectoryCopy(d, ogPath + "\\" + dPath2 + "\\" + dPath, true);
bwpReport("Compressing game pak " + dPath);
bwpReport(dPath + " packing", false);
if (CompressFolder(ogPath + "\\" + dPath2))
{
bwpReport(dPath + " packed", true);
}
else
{
bwpReport(dPath + " error on packing", true);
}
try
{
Directory.Delete(ogPath + "\\" + dPath2, true);
}
catch (Exception)
{
throw;
}
}
else
{
DirectoryCopy(d, ogPath + "\\" + dPath, true);
bwpReport(dPath + " copied", true);
}
}
else
{
DirectoryCopy(d, ogPath + "\\" + dPath, true);
bwpReport(dPath + " copied", true);
}
}
//now got through and pack the special folders that didn't get packed the first time around
if (cbxCompressGame.Checked)
{
foreach (string d in Directory.GetDirectories(ogPath))
{
if (!IgnoreCompress.Any(d.Contains) && Directory.Exists(d))
{
bwpReport(new DirectoryInfo(@d).Name + " packing", false);
CompressFolder(d);
Directory.Delete(d, true);
bwpReport(new DirectoryInfo(@d).Name + " packed", true);
}
}
//cleanup the names
foreach (string s in Directory.GetFiles(ogPath))
{
string[] fName = s.Split(new Char[] { '.' });
if (fName[fName.Length - 2].ToLower() == "pak")
{
int iValue = 0;
if (int.TryParse(fName[fName.Length - 1], out iValue)) {
fName[fName.Length - 1] = "";
if (iValue > 1) { fName[fName.Length - 1] = iValue.ToString(); }
}
string newName = "";
for (int i = 0; i < fName.Length - 2; i++)
{
newName += fName[i];
}
newName += fName[fName.Length - 1] + ".pak";
if (File.Exists(newName)) { File.Delete(newName); };
File.Move(s,newName);
}
}
}
}
private void bwpMove_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
if (e.ProgressPercentage == 1)
{
listBox1.Items.Insert(0,e.UserState.ToString());
}
else
{
lblFileStatus.Text = e.UserState.ToString();
}
}
private void bwpReport(string msg)
{
bwpMove.ReportProgress(0, msg);
}
private void bwpReport(string msg, bool logme)
{
int iP = 0;
if (logme) { iP = 1; };
bwpMove.ReportProgress(iP, msg);
}
#endregion
private void tmrMtc_Tick(object sender, EventArgs e)
{
if (bwpMove.IsBusy)
{
btnBuild.Enabled = false;
lblFileStatus.Visible = true;
progressBar1.Visible = true;
btnCancel.Enabled = true;
if (progressBar1.Value > 99) { progressBar1.Value = 1; }
progressBar1.Value += 1;
}
else
{
btnBuild.Enabled = true;
btnCancel.Enabled = false;
lblFileStatus.Visible = false;
progressBar1.Visible = false;
}
}
private void btnCancel_Click(object sender, EventArgs e)
{
if (bwpMove.IsBusy)
{
bwpMove.CancelAsync();
}
}
private void cbxCompressionLevel_SelectedIndexChanged(object sender, EventArgs e)
{
cLevel = Convert.ToInt16(cbxCompressionLevel.SelectedItem.ToString());
}
private void Form1_Load(object sender, EventArgs e)
{
//load form settings
GetSettings(settingFile, settings);
foreach (_setting s in extRedirectPath)
{
IgnoreExtensions.Add(s._value);
}
}
private void OpenSetting(string SettingName)
{
EditSetting f = new EditSetting();
f.setting = SettingName;
f.ShowDialog();
}
#region "Toolbar Buttons"
private void includeFoldersToolStripMenuItem_Click(object sender, EventArgs e)
{
OpenSetting("IncludeFolders");
}
private void ignoreExtensionsToolStripMenuItem_Click(object sender, EventArgs e)
{
OpenSetting("IgnoreExtensions");
}
private void ignoreFoldersToolStripMenuItem_Click(object sender, EventArgs e)
{
OpenSetting("IgnoreFolders");
}
private void ignoreFilesToolStripMenuItem_Click(object sender, EventArgs e)
{
OpenSetting("IgnoreFiles");
}
private void ignoreGameFoldersToolStripMenuItem_Click(object sender, EventArgs e)
{
OpenSetting("IgnoreCompress");
}
private void settingscfgToolStripMenuItem_Click(object sender, EventArgs e)
{
EditSettingFile f = new EditSettingFile();
f.strFileName = "System";
f._path = txbCERoot.Text;
f.ShowDialog();
}
private void saveToolStripMenuItem_Click(object sender, EventArgs e)
{
saveFileDialog1.InitialDirectory = AppDomain.CurrentDomain.BaseDirectory;
if (saveFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
List<_setting> tSettings = new List<_setting>();
tSettings.Add(new _setting() { _name = "rootpath", _value = txbCERoot.Text });
tSettings.Add(new _setting() { _name = "gamepath", _value = txbGamePath.Text });
tSettings.Add(new _setting() { _name = "outputpath", _value = txbOutput.Text });
tSettings.Add(new _setting() { _name = "compressgame", _value = cbxCompressGame.Checked.ToString() });
tSettings.Add(new _setting() { _name = "compressionlevel", _value = cbxCompressionLevel.SelectedItem.ToString() });
SaveSettings(saveFileDialog1.FileName, tSettings);
}
}
private void loadToolStripMenuItem_Click(object sender, EventArgs e)
{
if (ofdSaveFile.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
if (ofdSaveFile.CheckFileExists) {
List<_setting> tSettings = new List<_setting>();
GetSettings(ofdSaveFile.FileName, tSettings);
if(tSettings.Count > 0){
settings = tSettings;
txbCERoot.Text = GetSetting("rootpath", settings);
txbGamePath.Text = GetSetting("gamepath", settings);
cbxCompressGame.Checked = Convert.ToBoolean(GetSetting("compressgame", settings, "bool"));
cbxCompressionLevel.SelectedItem = GetSetting("compressionlevel", settings);
txbOutput.Text = GetSetting("outputpath", settings);
}
}
}
}
#endregion
}
public class _setting
{
public string _name;
public string _value;
}
}