2016-01-16 22:40:41 -05:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
2016-01-17 11:11:53 -05:00
|
|
|
|
using System.Reflection;
|
2016-01-16 22:40:41 -05:00
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using Mesen.GUI.Config;
|
2016-01-17 11:11:53 -05:00
|
|
|
|
using System.IO.Compression;
|
2017-09-12 20:52:12 -04:00
|
|
|
|
using System.Security.Cryptography;
|
2016-01-16 22:40:41 -05:00
|
|
|
|
|
|
|
|
|
namespace Mesen.GUI
|
|
|
|
|
{
|
|
|
|
|
class ResourceManager
|
|
|
|
|
{
|
2017-09-12 20:52:12 -04:00
|
|
|
|
public static string GetSha1Hash(byte[] fileData)
|
|
|
|
|
{
|
|
|
|
|
using(SHA1Managed sha1 = new SHA1Managed()) {
|
|
|
|
|
byte[] hash = sha1.ComputeHash(fileData);
|
|
|
|
|
|
|
|
|
|
var sb = new StringBuilder(hash.Length * 2);
|
|
|
|
|
foreach(byte b in hash) {
|
|
|
|
|
sb.Append(b.ToString("x2"));
|
|
|
|
|
}
|
|
|
|
|
return sb.ToString();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-08 23:23:31 -05:00
|
|
|
|
private static void ExtractFile(ZipArchiveEntry entry, string outputFilename)
|
|
|
|
|
{
|
2017-09-08 20:10:30 -04:00
|
|
|
|
if(File.Exists(outputFilename)) {
|
2017-09-12 20:52:12 -04:00
|
|
|
|
byte[] zipFileData = new byte[entry.Length];
|
|
|
|
|
using(Stream fileStream = entry.Open()) {
|
|
|
|
|
fileStream.Read(zipFileData, 0, (int)entry.Length);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
string diskFileSha1 = GetSha1Hash(File.ReadAllBytes(outputFilename));
|
|
|
|
|
string zipFileSha1 = GetSha1Hash(zipFileData);
|
|
|
|
|
|
|
|
|
|
if(diskFileSha1 != zipFileSha1) {
|
|
|
|
|
try {
|
|
|
|
|
File.Delete(outputFilename);
|
|
|
|
|
} catch { }
|
|
|
|
|
try {
|
|
|
|
|
File.WriteAllBytes(outputFilename, zipFileData);
|
|
|
|
|
} catch { }
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2017-09-08 20:10:30 -04:00
|
|
|
|
try {
|
2017-09-12 20:52:12 -04:00
|
|
|
|
//On Mono, using overwrite = true for ExtractToFile crashes/kills any currently running instance that uses the file.
|
|
|
|
|
//This is probably a Mono bug?
|
|
|
|
|
//Better to attempt a delete & then extract, like now (and like it used to be)
|
|
|
|
|
entry.ExtractToFile(outputFilename);
|
2017-09-08 20:10:30 -04:00
|
|
|
|
} catch { }
|
|
|
|
|
}
|
2016-02-08 23:23:31 -05:00
|
|
|
|
}
|
|
|
|
|
|
2016-02-19 13:05:04 -05:00
|
|
|
|
public static Stream GetZippedResource(string filename)
|
|
|
|
|
{
|
|
|
|
|
ZipArchive zip = new ZipArchive(Assembly.GetExecutingAssembly().GetManifestResourceStream("Mesen.GUI.Dependencies.Dependencies.zip"));
|
|
|
|
|
foreach(ZipArchiveEntry entry in zip.Entries) {
|
|
|
|
|
if(entry.Name == filename) {
|
|
|
|
|
return entry.Open();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return null;
|
2018-04-15 14:20:15 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static string ReadZippedResource(string filename)
|
|
|
|
|
{
|
|
|
|
|
ZipArchive zip = new ZipArchive(Assembly.GetExecutingAssembly().GetManifestResourceStream("Mesen.GUI.Dependencies.Dependencies.zip"));
|
|
|
|
|
foreach(ZipArchiveEntry entry in zip.Entries) {
|
2018-06-21 17:59:15 -04:00
|
|
|
|
string entryFileName = Program.IsMono ? entry.Name.Substring(entry.Name.LastIndexOf('\\') + 1) : entry.Name;
|
|
|
|
|
if(entryFileName == filename) {
|
2018-04-15 14:20:15 -04:00
|
|
|
|
using(Stream stream = entry.Open()) {
|
|
|
|
|
using(StreamReader reader = new StreamReader(stream)) {
|
|
|
|
|
return reader.ReadToEnd();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return null;
|
2016-02-19 13:05:04 -05:00
|
|
|
|
}
|
|
|
|
|
|
2016-05-22 14:43:07 -04:00
|
|
|
|
private static void CleanupOldFiles()
|
|
|
|
|
{
|
|
|
|
|
try {
|
|
|
|
|
if(Directory.Exists(Path.Combine(ConfigManager.HomeFolder, "WinMesen"))) {
|
|
|
|
|
Directory.Delete(Path.Combine(ConfigManager.HomeFolder, "WinMesen"), true);
|
|
|
|
|
}
|
2016-12-11 17:41:17 -05:00
|
|
|
|
if(File.Exists(Path.Combine(ConfigManager.HomeFolder, "WinMesen.dll"))) {
|
|
|
|
|
File.Delete(Path.Combine(ConfigManager.HomeFolder, "WinMesen.dll"));
|
|
|
|
|
}
|
2016-05-22 14:43:07 -04:00
|
|
|
|
if(File.Exists(Path.Combine(ConfigManager.HomeFolder, "NesNtsc.dll"))) {
|
|
|
|
|
File.Delete(Path.Combine(ConfigManager.HomeFolder, "NesNtsc.dll"));
|
|
|
|
|
}
|
|
|
|
|
if(File.Exists(Path.Combine(ConfigManager.HomeFolder, "BlipBuffer.dll"))) {
|
|
|
|
|
File.Delete(Path.Combine(ConfigManager.HomeFolder, "BlipBuffer.dll"));
|
|
|
|
|
}
|
|
|
|
|
} catch { }
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-31 22:26:24 -05:00
|
|
|
|
public static void ExtractGoogleDriveResources()
|
|
|
|
|
{
|
|
|
|
|
Directory.CreateDirectory(Path.Combine(ConfigManager.HomeFolder, "GoogleDrive"));
|
|
|
|
|
ZipArchive zip = new ZipArchive(Assembly.GetExecutingAssembly().GetManifestResourceStream("Mesen.GUI.Dependencies.Dependencies.zip"));
|
|
|
|
|
|
|
|
|
|
//Extract Google Drive-related DLLs
|
|
|
|
|
foreach(ZipArchiveEntry entry in zip.Entries) {
|
|
|
|
|
if(ResourceManager.GoogleDlls.Contains(entry.Name)) {
|
|
|
|
|
string outputFilename = Path.Combine(ConfigManager.HomeFolder, "GoogleDrive", entry.Name);
|
|
|
|
|
ExtractFile(entry, outputFilename);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-18 12:43:20 -05:00
|
|
|
|
public static bool ExtractResources()
|
2016-01-16 22:40:41 -05:00
|
|
|
|
{
|
2016-05-22 14:43:07 -04:00
|
|
|
|
CleanupOldFiles();
|
|
|
|
|
|
2016-01-16 22:40:41 -05:00
|
|
|
|
Directory.CreateDirectory(Path.Combine(ConfigManager.HomeFolder, "Resources"));
|
|
|
|
|
|
2016-01-17 11:11:53 -05:00
|
|
|
|
ZipArchive zip = new ZipArchive(Assembly.GetExecutingAssembly().GetManifestResourceStream("Mesen.GUI.Dependencies.Dependencies.zip"));
|
|
|
|
|
|
2016-01-16 22:40:41 -05:00
|
|
|
|
//Extract all needed files
|
2016-01-17 11:11:53 -05:00
|
|
|
|
string suffix = IntPtr.Size == 4 ? ".x86" : ".x64";
|
|
|
|
|
foreach(ZipArchiveEntry entry in zip.Entries) {
|
2016-12-22 18:44:54 -05:00
|
|
|
|
if(entry.Name.StartsWith("MesenCore") && !Program.IsMono && entry.Name.Contains(suffix)) {
|
|
|
|
|
string outputFilename = Path.Combine(ConfigManager.HomeFolder, entry.Name.Replace(suffix, ""));
|
|
|
|
|
ExtractFile(entry, outputFilename);
|
|
|
|
|
} else if(entry.Name.StartsWith("libMesenCore") && Program.IsMono && entry.Name.Contains(suffix)) {
|
|
|
|
|
string outputFilename = Path.Combine(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location), entry.Name.Replace(suffix, ""));
|
2016-05-02 20:22:17 -04:00
|
|
|
|
ExtractFile(entry, outputFilename);
|
2016-06-15 21:59:34 -04:00
|
|
|
|
} else if(entry.Name == "MesenUpdater.exe" || entry.Name == "MesenDB.txt") {
|
2016-12-18 12:43:20 -05:00
|
|
|
|
string outputFilename = Path.Combine(ConfigManager.HomeFolder, entry.Name);
|
2016-02-08 23:23:31 -05:00
|
|
|
|
ExtractFile(entry, outputFilename);
|
2017-05-06 15:27:48 -04:00
|
|
|
|
} else if(entry.Name == "Font.24.spritefont" || entry.Name == "Font.64.spritefont" || entry.Name == "LICENSE.txt" || entry.Name == "PixelFont.ttf") {
|
2016-12-18 12:43:20 -05:00
|
|
|
|
string outputFilename = Path.Combine(ConfigManager.HomeFolder, "Resources", entry.Name);
|
2016-02-11 18:34:55 -05:00
|
|
|
|
ExtractFile(entry, outputFilename);
|
2016-01-17 11:11:53 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
2016-12-18 12:43:20 -05:00
|
|
|
|
|
|
|
|
|
return true;
|
2016-01-16 22:40:41 -05:00
|
|
|
|
}
|
2019-01-31 22:26:24 -05:00
|
|
|
|
|
|
|
|
|
public static HashSet<string> GoogleDlls = new HashSet<string>() {
|
|
|
|
|
"BouncyCastle.Crypto.dll",
|
|
|
|
|
"Google.Apis.Auth.dll",
|
|
|
|
|
"Google.Apis.Auth.PlatformServices.dll",
|
|
|
|
|
"Google.Apis.Core.dll",
|
|
|
|
|
"Google.Apis.dll",
|
|
|
|
|
"Google.Apis.Drive.v3.dll",
|
|
|
|
|
"Google.Apis.PlatformServices.dll",
|
|
|
|
|
"Newtonsoft.Json.dll",
|
|
|
|
|
"Zlib.Portable.dll"
|
|
|
|
|
};
|
2016-01-16 22:40:41 -05:00
|
|
|
|
}
|
|
|
|
|
}
|