-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Resources are now embedded into the exe!
- Loading branch information
Showing
5 changed files
with
121 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,7 +17,6 @@ Debug/ | |
Release/ | ||
!**/bin/x64/Release/ | ||
!**/bin/x86/Release/ | ||
References/ | ||
build/ | ||
bld/ | ||
[Oo]bj/ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Reflection; | ||
using System.Security.Cryptography; | ||
|
||
namespace WorkClocker.Helpers | ||
{ | ||
/// <summary> | ||
/// http://www.codeproject.com/Articles/528178/Load-DLL-From-Embedded-Resource | ||
/// </summary> | ||
public class EmbeddedAssembly | ||
{ | ||
// Version 1.3 | ||
|
||
private static Dictionary<string, Assembly> _dic; | ||
|
||
public static void Load(string embeddedResource, string fileName) | ||
{ | ||
if (_dic == null) | ||
_dic = new Dictionary<string, Assembly>(); | ||
|
||
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; | ||
} | ||
} | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters