-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathDATInterfacer.cs
206 lines (174 loc) · 6.41 KB
/
DATInterfacer.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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DATExtract;
namespace DATManager
{
internal static class DATInterfacer
{
internal static string extractLocation = null;
private static DATDirectory datStructure;
internal static int[] GetResult()
{
int[] result = new int[2];
for (int i = 0; i < datFiles.Count; i++)
{
result[0] += datFiles[i].successfullyExtracted;
result[1] += datFiles[i].extractAmount;
}
return result;
}
public static void GetFromFiles(DATDirectory datStructure, CompFile[] files, int datReference)
{
foreach (CompFile file in files)
{
if (file.path == null) continue;
string[] root = file.path.Split(Path.DirectorySeparatorChar);
DATDirectory prevDirectory = datStructure;
for (int i = 1; i < root.Length; i++) // Start at 1, since the 0 element is empty since the string starts with \
{
string entry = root[i];
if (i == root.Length - 1)
{
prevDirectory.files[entry] = new CompFileReference(file, datReference);
}
else if (!prevDirectory.folders.ContainsKey(entry))
{
DATDirectory newEntry = prevDirectory.CreateNewDirectory(entry);
prevDirectory = newEntry;
}
else
{
prevDirectory = prevDirectory.folders[entry];
}
}
}
}
internal static DATDirectory GetStructure()
{
if (datStructure != null)
{
return datStructure;
}
datStructure = new DATDirectory("root");
for (int j = 0; j < datFiles.Count; j++)
{
DATFile datFile = datFiles[j];
CompFile[] files = datFile.files;
GetFromFiles(datStructure, files, j);
}
return datStructure;
}
internal static void Reset()
{
datFiles.Clear();
datStructure = null;
}
internal static bool extractUpdatedByUser = false;
internal static void Open(string filename)
{
Reset();
DATFile datFile = DATFile.Open(filename);
datFiles.Add(datFile);
if (extractUpdatedByUser == false)
{
extractLocation = Path.GetDirectoryName(filename);
}
}
internal static List<DATFile> datFiles = new List<DATFile>();
internal static void Add(string filename)
{
DATFile datFile = DATFile.Open(filename);
datFiles.Add(datFile);
if (extractUpdatedByUser == false)
{
extractLocation = Path.GetDirectoryName(filename);
}
}
internal static object toExtract = null;
private static List<CompFileReference> congregate = new List<CompFileReference>();
private static void Marshal(DATDirectory directory)
{
foreach (var entry in directory.files)
{
congregate.Add(entry.Value);
}
foreach (var entry in directory.folders)
{
Marshal(entry.Value);
}
}
public static void ExtractDirectory(DATDirectory directory)
{
Marshal(directory);
toExtract = congregate.ToArray();
LoadReporter();
//workerThread.RunWorkerAsync(congregate.ToArray());
congregate = new List<CompFileReference>(); // Dispose of previous list
}
public static void ExtractFile(CompFileReference file)
{
toExtract = file;
LoadReporter();
}
public static void ExtractAll()
{
toExtract = null;
LoadReporter();
}
public static BackgroundWorker workerThread = new BackgroundWorker();
private static bool workerSetup = false;
public static void SetupWorker()
{
if (workerSetup) return;
workerThread.DoWork += new DoWorkEventHandler(RunExtractor);
workerThread.WorkerReportsProgress = true;
workerThread.WorkerSupportsCancellation = true;
}
private static void LoadReporter()
{
WorkerReport modal = new WorkerReport();
modal.ShowDialog();
}
private static void RunExtractor(object sender, DoWorkEventArgs e)
{
if (e.Argument is CompFileReference[])
{
CompFileReference[] compFiles = (CompFileReference[])e.Argument;
List<CompFile>[] sortedFiles = new List<CompFile>[datFiles.Count];
for (int i = 0; i < datFiles.Count; i++)
{
sortedFiles[i] = new List<CompFile>();
}
foreach (CompFileReference compFile in compFiles)
{
sortedFiles[compFile.datID].Add(compFile.file);
}
for (int i = 0; i < datFiles.Count; i++)
{
if (sortedFiles[i].Count > 0)
{
Console.WriteLine("Extracting from {0}", Path.GetFileNameWithoutExtension(datFiles[i].fileLocation));
datFiles[i].ExtractCollection(sortedFiles[i].ToArray(), extractLocation, workerThread);
}
}
}
else if (e.Argument is CompFileReference)
{
CompFileReference compFile = (CompFileReference)e.Argument;
datFiles[compFile.datID].ExtractFile(compFile.file, extractLocation);
}
else
{
for (int i = 0; i < datFiles.Count; i++)
{
Console.WriteLine("Extracting {0}", Path.GetFileNameWithoutExtension(datFiles[i].fileLocation));
datFiles[i].ExtractAll(extractLocation, workerThread);
}
}
}
}
}