-
Notifications
You must be signed in to change notification settings - Fork 0
/
HashMapGenTool.cs
332 lines (291 loc) · 12.1 KB
/
HashMapGenTool.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
using Aspose.Zip.Gzip;
using Aspose.Zip.SevenZip;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO.Compression;
using System.Linq;
using System.Reflection;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static SteamAchievmentViewer.Form1;
using static System.Net.Mime.MediaTypeNames;
namespace SteamAchievmentViewer
{
public partial class HashMapGenTool : Form
{
public delegate void AddTextDelegate(String text = "");
String emuDeckRomPath = "";
String emuDeckCachePath = "";
Dictionary<String, Dictionary<String, String>> hashMap = new Dictionary<String, Dictionary<String, String>>();
// Add Finished Event
private Action<String> finishedProcessingAction = null;
public Action hashMapReady = null;
String hashErrorLogPath = "";
public HashMapGenTool(String emuDeckRomPath, string emuDeckCachePath)
{
InitializeComponent();
this.emuDeckRomPath = emuDeckRomPath;
finishedProcessingAction += processingDone;
this.emuDeckCachePath = emuDeckCachePath;
hashErrorLogPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "\\hashErrors.txt";
displayTxtBox.DetectUrls = false;
this.BackColor = Color.FromArgb(80, 84, 82);
this.ForeColor = Color.White;
displayTxtBox.BackColor = Color.FromArgb(120, 120, 120);
displayTxtBox.ForeColor = Color.White;
// Set Minimum to 1 to represent the first file being copied.
progressBar.Minimum = 1;
// Set the initial value of the ProgressBar.
progressBar.Value = 1;
// Set the Step property to a value of 1 to represent each file being copied.
progressBar.Step = 1;
}
private void processingDone(String message)
{
// Detaches the event handler
if (hashMapReady != null)
{
hashMapReady();
}
this.Invoke((MethodInvoker)delegate
{
this.Close();
});
}
private void HashMapGenTool_Shown(object sender, EventArgs e)
{
if (Directory.Exists(emuDeckRomPath) && Directory.Exists(emuDeckRomPath + $"\\snes"))
{
hashMap = loadHashMap(emuDeckCachePath);
if (File.Exists(this.emuDeckCachePath))
{
File.Delete(this.emuDeckCachePath);
}
// Start Generation
Thread thread = new Thread(getEDHashMap);
thread.IsBackground = true;
thread.Start(this.emuDeckRomPath);
}
else
{
finishedProcessingAction($"Emudeck Path {emuDeckRomPath} does not exist.");
}
}
public void addTextToDisplayBox(String text)
{
displayTxtBox.AppendText(text);
displayTxtBox.ScrollToCaret();
}
public void InvokeAddTextToDisplayBox(String text)
{
if (this.InvokeRequired)
{
object[] parameters = new object[] { text };
this.Invoke(new AddTextDelegate(addTextToDisplayBox), parameters);
}
else
{
addTextToDisplayBox(text);
}
}
private List<String> troubleMakers = new List<string>();
public void updatehashMapForFiles(Object obj)
{
List<String> files = (List<String>)obj;
Parallel.ForEach(files, file => //foreach (String file in files)
{
InvokeAddTextToDisplayBox($"Processsing File {file}\n");
this.Invoke((MethodInvoker)delegate
{
progressBar.PerformStep();
});
try
{
byte[] data = null;
long lastModifiedTime = File.GetLastWriteTimeUtc(file).ToFileTimeUtc();
String systemFolderName = file.Replace(emuDeckRomPath, "").Trim().TrimStart('/').TrimStart('\\').Split(new char[] { '\\' })[0].ToLower();
List<String> arcadeFolders = new List<String>() { "arcade", "fbneo", "fba", "mame2003", "mame2010", "gamecom", "vsmile" };
List<String> zipExt = new List<String>() { ".zip", ".gz", ".gzip", ".7z", ".7zip" };
String hash = "";
if (zipExt.Contains(Path.GetExtension(file).ToLower()) && !arcadeFolders.Contains(systemFolderName))
{
hash = DecompressAndHash(file);
}
else
{
hash = calcMD5Hash(file);
}
Dictionary<String, String> tmp = new Dictionary<String, String>() { { "hash", hash }, { "modified", $"{lastModifiedTime}" } };
if (hashMap.ContainsKey(file))
{
hashMap[file] = tmp;
}
else
{
hashMap.Add(file, tmp);
}
}
catch
{
// SKIP FIles if theyre too big 2GB+
troubleMakers.Add(file);
}
});
}
public void getEDHashMap(object obj)
{
String path = (String)obj;
Stopwatch timer = new Stopwatch();
List<String> files = new List<String>();
timer.Start();
int chunkSize = 500;
int skipCount = 0;
troubleMakers = new List<String>();
if (Directory.Exists(path))
{
files = Directory.EnumerateFiles(path, "*", SearchOption.AllDirectories).Select(s => s).ToList();
this.Invoke((MethodInvoker)delegate
{
// Set Maximum to the total number of files to copy.
progressBar.Maximum = files.Count;
});
foreach (KeyValuePair<String, Dictionary<String, String>> kvp in hashMap)
{
if (files.Contains(kvp.Key) && File.GetLastWriteTimeUtc(kvp.Key).ToFileTimeUtc() == long.Parse(kvp.Value["modified"]))
{
files.Remove(kvp.Key);
skipCount++;
InvokeAddTextToDisplayBox($"Hash Exists: {kvp.Key}\n");
this.Invoke((MethodInvoker)delegate
{
progressBar.PerformStep();
});
}
}
// Thread every 500 files
//Parallel.ForEach(files, file => //foreach (String file in files)
//{
// InvokeAddTextToDisplayBox($"Processsing File {file}\n");
// this.Invoke((MethodInvoker)delegate
// {
// // Set Maximum to the total number of files to copy.
// progressBar.PerformStep();
// });
// try
// {
// byte[] data = null;
// if (Path.GetExtension(file).ToLower() == ".zip")
// {
// data = DecompressRom(File.ReadAllBytes(file));
// }
// else
// {
// data = File.ReadAllBytes(file);
// if (data == null)
// {
// }
// }
// hashMap.Add(file, calcMD5Hash(data));
// }
// catch
// {
// // SKIP FIles if theyre too big 2GB+
// troubleMakers.Add(file);
// }
//});
List<Thread> threads = new List<Thread>();
for (int i = 0; i < files.Count; i+=chunkSize){
Thread tmp = new Thread(updatehashMapForFiles);
tmp.IsBackground = true;
threads.Add(tmp);
tmp.Start(files.GetRange(i, ((i + chunkSize) < files.Count) ? chunkSize : files.Count - i));
}
foreach (Thread tmp in threads)
{
tmp.Join();
}
}
timer.Stop();
if (hashMap.Keys.Count > 0)
{
String emuDeckJson = "{\n";
emuDeckJson += String.Join(", \n\t", hashMap.Select(kvp => $"\"{kvp.Key.Replace("\\", "\\\\")}\": {{\n\t\t{String.Join(", \n\t\t", kvp.Value.Select(kvp2 => $"\"{kvp2.Key}\" : \"{kvp2.Value}\""))}\n\t}}"));
emuDeckJson += "\n}";
File.WriteAllText(emuDeckCachePath, emuDeckJson);
}
if (File.Exists(hashErrorLogPath))
{
File.Delete(hashErrorLogPath);
}
if (troubleMakers.Count > 0)
{
File.WriteAllText(hashErrorLogPath, "\n" + String.Join("\n", troubleMakers));
}
MessageBox.Show($"Hashed {hashMap.Keys.Count}/{files.Count + skipCount} files in {timer.Elapsed.TotalSeconds} s.{((troubleMakers.Count > 0) ? $"\nFailed to generate a hash for {troubleMakers.Count} files.\nSee hashErrors.txt for more info." : "")}");
finishedProcessingAction("Hash Map Generation Complete.");
}
static String DecompressAndHash(String path)
{
String rv = "";
Stream romData = null;
String ext = Path.GetExtension(path).ToLower();
if (ext == ".zip")
{
ZipArchive arch = new ZipArchive(File.Open(path, FileMode.Open));
if (arch.Entries.Count > 0)
{
ZipArchiveEntry ent = arch.Entries[0];
romData = arch.Entries[0].Open();
}
}
else if (ext == ".7z" || ext == ".7zip")
{
SevenZipArchive archive = new SevenZipArchive(path);
romData = archive.Entries[0].Open();
}
else if (ext == ".gz" || ext == ".gzip")
{
GzipArchive archive = new GzipArchive(path);
romData = archive.Open();
}
else
{
throw new Exception($"Zip format {ext} not supported");
}
if (romData != null)
{
rv = calcMD5Hash(romData);
romData.Close();
}
return rv;
}
public static String calcMD5Hash(String path)
{
String rv = "";
using (FileStream fs = File.OpenRead(path))
using (System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create())
{
byte[] hashBytes = md5.ComputeHash(fs);
rv = Convert.ToHexString(hashBytes).ToLower(); // .NET 5 +
fs.Close();
}
return rv;
}
public static String calcMD5Hash(Stream data)
{
String rv = "";
using (System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create())
{
byte[] hashBytes = md5.ComputeHash(data);
rv = Convert.ToHexString(hashBytes).ToLower(); // .NET 5 +
}
return rv;
}
}
}