소스 검색

Fixed some bugs

deadmoon 5 년 전
부모
커밋
d2bdccc6b7
3개의 변경된 파일90개의 추가작업 그리고 22개의 파일을 삭제
  1. 83 0
      Optimizer/EmbeddedAssembly.cs
  2. 1 0
      Optimizer/Optimizer.csproj
  3. 6 22
      Optimizer/Program.cs

+ 83 - 0
Optimizer/EmbeddedAssembly.cs

@@ -0,0 +1,83 @@
+using System;
+using System.Collections.Generic;
+using System.Text;
+using System.IO;
+using System.Reflection;
+using System.Security.Cryptography;
+
+namespace Optimizer
+{
+    internal class EmbeddedAssembly
+    {
+        static Dictionary<string, Assembly> _dictionary;
+
+        internal static void Load(string embeddedResource, string fileName)
+        {
+            if (_dictionary == null) _dictionary = new Dictionary<string, Assembly>();
+
+            byte[] bytes = null;
+            Assembly assembly = null;
+            Assembly currentAssembly = Assembly.GetExecutingAssembly();
+
+            using (Stream stream = currentAssembly.GetManifestResourceStream(embeddedResource))
+            {
+                if (stream == null) throw new Exception(embeddedResource + " is not found in Embedded Resources.");
+
+                bytes = new byte[(int)stream.Length];
+                stream.Read(bytes, 0, (int)stream.Length);
+
+                try
+                {
+                    assembly = Assembly.Load(bytes);
+
+                    _dictionary.Add(assembly.FullName, assembly);
+                    return;
+                }
+                catch { }
+            }
+
+            bool fileOk = false;
+            string tempFile = string.Empty;
+
+            using (SHA1CryptoServiceProvider sha1 = new SHA1CryptoServiceProvider())
+            {
+                string fileHash = BitConverter.ToString(sha1.ComputeHash(bytes)).Replace("-", string.Empty);
+
+                tempFile = Path.GetTempPath() + fileName;
+
+                if (File.Exists(tempFile))
+                {
+                    byte[] byteArray = File.ReadAllBytes(tempFile);
+                    string fileHash2 = BitConverter.ToString(sha1.ComputeHash(byteArray)).Replace("-", string.Empty);
+
+                    if (fileHash == fileHash2)
+                    {
+                        fileOk = true;
+                    }
+                }
+                else
+                {
+                    fileOk = false;
+                }
+            }
+
+            if (!fileOk)
+            {
+                File.WriteAllBytes(tempFile, bytes);
+            }
+
+            assembly = Assembly.LoadFile(tempFile);
+
+            _dictionary.Add(assembly.FullName, assembly);
+        }
+
+        internal static Assembly Get(string assemblyFullName)
+        {
+            if (_dictionary == null || _dictionary.Count == 0) return null;
+
+            if (_dictionary.ContainsKey(assemblyFullName)) return _dictionary[assemblyFullName];
+
+            return null;
+        }
+    }
+}

+ 1 - 0
Optimizer/Optimizer.csproj

@@ -89,6 +89,7 @@
     <Compile Include="EdgeForm.Designer.cs">
       <DependentUpon>EdgeForm.cs</DependentUpon>
     </Compile>
+    <Compile Include="EmbeddedAssembly.cs" />
     <Compile Include="Enums.cs" />
     <Compile Include="HostsEditorForm.cs">
       <SubType>Form</SubType>

+ 6 - 22
Optimizer/Program.cs

@@ -12,8 +12,8 @@ namespace Optimizer
 
         // Enter current version here
 
-        internal readonly static float Major = 0;
-        internal readonly static float Minor = 0;
+        internal readonly static float Major = 4;
+        internal readonly static float Minor = 8;
 
         internal static string GetCurrentVersionTostring()
         {
@@ -27,6 +27,8 @@ namespace Optimizer
 
         /* END OF VERSION PROPERTIES */
 
+        const string _jsonAssembly = @"Optimizer.Newtonsoft.Json.dll";
+
         internal static MainForm MainForm;
 
         readonly static string _adminMissingMessage = "Optimizer needs to be run as administrator!\nApp will now close...";
@@ -35,6 +37,7 @@ namespace Optimizer
         [STAThread]
         static void Main(string[] switches)
         {
+            EmbeddedAssembly.Load(_jsonAssembly, _jsonAssembly.Replace("Optimizer.", string.Empty));
             AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;
 
             Application.EnableVisualStyles();
@@ -142,26 +145,7 @@ namespace Optimizer
 
         private static Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
         {
-            return LoadEmbedDLLs();
-        }
-
-        private static Assembly LoadEmbedDLLs()
-        {
-            byte[] ba = null;
-            Assembly r = null;
-
-            string resource = "Optimizer.Newtonsoft.Json.dll";
-            Assembly asm = Assembly.GetExecutingAssembly();
-
-            using (Stream s = asm.GetManifestResourceStream(resource))
-            {
-                ba = new byte[(int)s.Length];
-                s.Read(ba, 0, (int)s.Length);
-
-                r = Assembly.Load(ba);
-            }
-
-            return r;
+            return EmbeddedAssembly.Get(args.Name);
         }
     }
 }