Skip to content

Commit

Permalink
Resources are now embedded into the exe!
Browse files Browse the repository at this point in the history
  • Loading branch information
Donkie committed Dec 27, 2015
1 parent 5ffb4ce commit 07151d1
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 12 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ Debug/
Release/
!**/bin/x64/Release/
!**/bin/x86/Release/
References/
build/
bld/
[Oo]bj/
Expand Down
25 changes: 19 additions & 6 deletions WorkClocker/App.xaml.cs
Original file line number Diff line number Diff line change
@@ -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
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
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();
Expand Down
95 changes: 95 additions & 0 deletions WorkClocker/Helpers/EmbeddedAssembly.cs
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.
12 changes: 7 additions & 5 deletions WorkClocker/WorkClocker.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup>
<StartupObject />
<StartupObject>WorkClocker.App</StartupObject>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'">
<DebugSymbols>true</DebugSymbols>
Expand Down Expand Up @@ -85,6 +85,7 @@
<Reference Include="MouseKeyboardActivityMonitor, Version=4.1.0.0, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>References\MouseKeyboardActivityMonitor.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="System" />
<Reference Include="System.Data" />
Expand All @@ -103,10 +104,11 @@
<Reference Include="PresentationFramework" />
</ItemGroup>
<ItemGroup>
<ApplicationDefinition Include="App.xaml">
<Page Include="App.xaml">
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
</ApplicationDefinition>
</Page>
<Compile Include="Helpers\EmbeddedAssembly.cs" />
<Compile Include="Helpers\ListExtension.cs" />
<Compile Include="Helpers\WindowExe.cs" />
<Compile Include="Properties\Annotations.cs" />
Expand Down Expand Up @@ -167,15 +169,15 @@
<ItemGroup>
<None Include="Resources\clock.ico" />
<None Include="Resources\application.ico" />
<Resource Include="References\MouseKeyboardActivityMonitor.xml" />
<Resource Include="Resources\clock_pause.png" />
<Resource Include="Resources\clock_delete.png" />
<Resource Include="Resources\clock_play.png" />
<Resource Include="Resources\folder.png" />
<Resource Include="Resources\disk.png" />
<Content Include="References\MouseKeyboardActivityMonitor.dll" />
<EmbeddedResource Include="References\MouseKeyboardActivityMonitor.dll" />
<Resource Include="Resources\wrench.png" />
<Resource Include="Resources\clock.png" />
<Resource Include="References\MouseKeyboardActivityMonitor.xml" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Expand Down

0 comments on commit 07151d1

Please # to comment.