diff --git a/.gitignore b/.gitignore index 2d79d44..d0f3ae9 100644 --- a/.gitignore +++ b/.gitignore @@ -17,7 +17,6 @@ Debug/ Release/ !**/bin/x64/Release/ !**/bin/x86/Release/ -References/ build/ bld/ [Oo]bj/ diff --git a/WorkClocker/App.xaml.cs b/WorkClocker/App.xaml.cs index 4e3fa2a..0fef547 100644 --- a/WorkClocker/App.xaml.cs +++ b/WorkClocker/App.xaml.cs @@ -1,18 +1,31 @@ using System; -using System.Collections.Generic; -using System.Configuration; -using System.Data; -using System.Linq; -using System.Threading.Tasks; using System.Windows; +using WorkClocker.Helpers; namespace WorkClocker { /// /// Interaction logic for App.xaml /// - public partial class App : Application + public partial class App { + private static System.Reflection.Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args) + { + return EmbeddedAssembly.Get(args.Name); + } + + [STAThread] + public static void Main() + { + const string res = "WorkClocker.References.MouseKeyboardActivityMonitor.dll"; + EmbeddedAssembly.Load(res, "MouseKeyboardActivityMonitor.dll"); + AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve; + + var application = new App(); + application.InitializeComponent(); + application.Run(); + } + private void Application_Exit(object sender, ExitEventArgs e) { WorkClocker.Properties.Settings.Default.Save(); diff --git a/WorkClocker/Helpers/EmbeddedAssembly.cs b/WorkClocker/Helpers/EmbeddedAssembly.cs new file mode 100644 index 0000000..a6d7766 --- /dev/null +++ b/WorkClocker/Helpers/EmbeddedAssembly.cs @@ -0,0 +1,95 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Reflection; +using System.Security.Cryptography; + +namespace WorkClocker.Helpers +{ + /// + /// http://www.codeproject.com/Articles/528178/Load-DLL-From-Embedded-Resource + /// + public class EmbeddedAssembly + { + // Version 1.3 + + private static Dictionary _dic; + + public static void Load(string embeddedResource, string fileName) + { + if (_dic == null) + _dic = new Dictionary(); + + byte[] ba; + Assembly asm; + var curAsm = Assembly.GetExecutingAssembly(); + + using (var stm = curAsm.GetManifestResourceStream(embeddedResource)) + { + // Either the file is not existed or it is not mark as embedded resource + if (stm == null) + throw new Exception(embeddedResource + " is not found in Embedded Resources."); + + // Get byte[] from the file from embedded resource + ba = new byte[(int)stm.Length]; + stm.Read(ba, 0, (int)stm.Length); + try + { + asm = Assembly.Load(ba); + + // Add the assembly/dll into dictionary + _dic.Add(asm.FullName, asm); + return; + } + catch + { + // Purposely do nothing + // Unmanaged dll or assembly cannot be loaded directly from byte[] + // Let the process fall through for next part + } + } + + bool fileOk; + string tempFile; + + using (var sha1 = new SHA1CryptoServiceProvider()) + { + var fileHash = BitConverter.ToString(sha1.ComputeHash(ba)).Replace("-", string.Empty); + + tempFile = Path.GetTempPath() + fileName; + + if (File.Exists(tempFile)) + { + var bb = File.ReadAllBytes(tempFile); + var fileHash2 = BitConverter.ToString(sha1.ComputeHash(bb)).Replace("-", string.Empty); + + fileOk = fileHash == fileHash2; + } + else + { + fileOk = false; + } + } + + if (!fileOk) + { + File.WriteAllBytes(tempFile, ba); + } + + asm = Assembly.LoadFile(tempFile); + + _dic.Add(asm.FullName, asm); + } + + public static Assembly Get(string assemblyFullName) + { + if (_dic == null || _dic.Count == 0) + return null; + + if (_dic.ContainsKey(assemblyFullName)) + return _dic[assemblyFullName]; + + return null; + } + } +} \ No newline at end of file diff --git a/WorkClocker/References/MouseKeyboardActivityMonitor.dll b/WorkClocker/References/MouseKeyboardActivityMonitor.dll new file mode 100644 index 0000000..88441f4 Binary files /dev/null and b/WorkClocker/References/MouseKeyboardActivityMonitor.dll differ diff --git a/WorkClocker/WorkClocker.csproj b/WorkClocker/WorkClocker.csproj index 358114b..ebaa99b 100644 --- a/WorkClocker/WorkClocker.csproj +++ b/WorkClocker/WorkClocker.csproj @@ -38,7 +38,7 @@ 4 - + WorkClocker.App true @@ -85,6 +85,7 @@ False References\MouseKeyboardActivityMonitor.dll + False @@ -103,10 +104,11 @@ - + MSBuild:Compile Designer - + + @@ -167,15 +169,15 @@ + - + -