-
Notifications
You must be signed in to change notification settings - Fork 0
/
BananaManager.cs
124 lines (101 loc) · 3.99 KB
/
BananaManager.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
using System;
using System.Collections;
using System.Globalization;
using System.IO;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
using System.Text;
using Banana.Core.Properties;
namespace Banana.Core
{
public class BananaManager
{
#region Constructor
/// <summary>
/// Start an iteration with Banana.Core
/// </summary>
public BananaManager()
{
// Import resources
ImportResources();
// Load banana libs
LoadBananaLibs();
}
#endregion
#region Privates
/// <summary>
/// Current release name. * Useful for new releases *
/// </summary>
private const string BananaCoreRelease = "PeelBanana2";
/// <summary>
/// Banana core resources path.
/// </summary>
protected string BananaCoreFolder =
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\Banana.Core";
#endregion
#region Methods
#region External resources engine
/// <summary>
/// Import libraries from resources.
/// </summary>
private void ImportResources()
{
if (!Directory.Exists(BananaCoreFolder + "\\libs\\" + BananaCoreRelease))
Directory.CreateDirectory(BananaCoreFolder + "\\libs\\" + BananaCoreRelease);
var resourceSet = Resources.ResourceManager.GetResourceSet(CultureInfo.CurrentUICulture, true, true);
foreach (DictionaryEntry entry in resourceSet)
if (entry.Value is byte[])
{
var resource = (byte[]) entry.Value;
if (!File.Exists(BananaCoreFolder + "\\libs\\" + BananaCoreRelease + "\\" + entry.Key + ".dll"))
File.WriteAllBytes(
BananaCoreFolder + "\\libs\\" + BananaCoreRelease + "\\" + entry.Key + ".dll",
resource);
else
{
if (MD5Check(resource) !=
MD5Check(
File.ReadAllBytes(BananaCoreFolder + "\\libs\\" + BananaCoreRelease + "\\" + entry.Key +
".dll")))
File.WriteAllBytes(
BananaCoreFolder + "\\libs\\" + BananaCoreRelease + "\\" + entry.Key + ".dll",
resource);
}
}
else if (entry.Value is string)
{
if (!File.Exists(BananaCoreFolder + "\\libs\\" + BananaCoreRelease + "\\" + entry.Key + ".txt"))
File.AppendAllText(
BananaCoreFolder + "\\libs\\" + BananaCoreRelease + "\\" + entry.Key + ".txt",
(string) entry.Value);
}
}
#endregion
#region Library engine
[DllImport("kernel32.dll")]
private static extern IntPtr LoadLibrary(string lpFileName);
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern bool SetDllDirectory(string lpPathName);
/// <summary>
/// Load external libraries from banana.core folder
/// </summary>
private void LoadBananaLibs()
{
SetDllDirectory(BananaCoreFolder + "\\libs\\" + BananaCoreRelease);
}
private string MD5Check(byte[] src, bool upperCase = false)
{
var bytes = new byte[0];
using (var md5 = MD5.Create())
{
bytes = md5.ComputeHash(src);
}
var result = new StringBuilder(bytes.Length*2);
for (var i = 0; i < bytes.Length; i++)
result.Append(bytes[i].ToString(upperCase ? "X2" : "x2"));
return result.ToString();
}
#endregion
#endregion
}
}