forked from CBLoader/CBLoader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Utils.cs
208 lines (193 loc) · 8.21 KB
/
Utils.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Runtime.InteropServices;
using System.Xml.Linq;
using Microsoft.Win32;
using System.Security.Cryptography;
namespace CharacterBuilderLoader
{
[Flags]
public enum AllocationType
{
Commit = 0x1000,
Reserve = 0x2000,
Decommit = 0x4000,
Release = 0x8000,
Reset = 0x80000,
Physical = 0x400000,
TopDown = 0x100000,
WriteWatch = 0x200000,
LargePages = 0x20000000
}
[Flags]
public enum MemoryProtection
{
Execute = 0x10,
ExecuteRead = 0x20,
ExecuteReadWrite = 0x40,
ExecuteWriteCopy = 0x80,
NoAccess = 0x01,
ReadOnly = 0x02,
ReadWrite = 0x04,
WriteCopy = 0x08,
GuardModifierflag = 0x100,
NoCacheModifierflag = 0x200,
WriteCombineModifierflag = 0x400
}
public class Utils
{
public static void ExtractKeyFile(string filename)
{
RegistryKey rk = Registry.LocalMachine.OpenSubKey("SOFTWARE").OpenSubKey("Wizards of the Coast")
.OpenSubKey(FileManager.APPLICATION_ID);
string currentVersion = rk.GetValue(null).ToString();
string encryptedKey = rk.OpenSubKey(currentVersion).GetValue(null).ToString();
byte[] stuff = new byte[] { 0x19, 0x25, 0x49, 0x62, 12, 0x41, 0x55, 0x1c, 0x15, 0x2f };
byte[] base64Str = Convert.FromBase64String(encryptedKey);
string realKey = Convert.ToBase64String(ProtectedData.Unprotect(base64Str, stuff, DataProtectionScope.LocalMachine));
XDocument xd = new XDocument();
XElement applications = new XElement("Applications");
XElement application = new XElement("Application");
application.Add(new XAttribute("ID", FileManager.APPLICATION_ID));
application.Add(new XAttribute("CurrentUpdate", currentVersion));
application.Add(new XAttribute("InProgress", "true"));
application.Add(new XAttribute("InstallStage", "Complete"));
application.Add(new XAttribute("InstallDate", DateTime.Now.ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ss.fffffffK")));
XElement update = new XElement("Update" + currentVersion);
update.Add(realKey);
application.Add(update);
applications.Add(application);
xd.Add(applications);
xd.Save(filename);
}
public static void CheckIfUserAssoc()
{
var key = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("Software").OpenSubKey("Classes").OpenSubKey(".dnd4e");
if (key == null || key.OpenSubKey("shell") == null)
UpdateRegistry(true);
}
/// <summary>
/// Sets .dnd4e File Association to CBLoader.
/// This means that the user can double-click a character file and launch CBLoader.
/// </summary>
public static void UpdateRegistry(bool silent = false)
{ // I'm not going to bother explaining File Associations. Either look it up yourself, or trust me that it works.
try // Changing HKCL needs admin permissions
{
Microsoft.Win32.RegistryKey cuClasses = Microsoft.Win32.Registry.CurrentUser.CreateSubKey("Software").CreateSubKey("Classes");
var k = cuClasses.CreateSubKey(".dnd4e");
k.SetValue("", ".dnd4e");
k = k.CreateSubKey("shell");
k = k.CreateSubKey("open");
k = k.CreateSubKey("command");
k.SetValue("", "\"" + (Environment.CurrentDirectory.ToString() + "\\CBLoader.exe\" \"%1\""));
// All Users
k = Microsoft.Win32.Registry.ClassesRoot.CreateSubKey(".dnd4e");
k.SetValue("", ".dnd4e");
k = k.CreateSubKey("shell");
k = k.CreateSubKey("open");
k = k.CreateSubKey("command");
k.SetValue("", "\"" + (Environment.CurrentDirectory.ToString() + "\\CBLoader.exe\" \"%1\""));
// And the cbconfig files
k = Microsoft.Win32.Registry.ClassesRoot.CreateSubKey(".cbconfig");
k.SetValue("", ".cbconfig");
k = k.CreateSubKey("shell");
k = k.CreateSubKey("open");
k = k.CreateSubKey("command");
k.SetValue("", "\"" + (Environment.CurrentDirectory.ToString() + "\\CBLoader.exe\" -c \"%1\""));
}
catch (UnauthorizedAccessException ua)
{
if (!silent)
Log.Error("There was a problem setting file associations", ua);
}
}
[DllImport("kernel32.dll", SetLastError = true, ExactSpelling = true)]
public static extern IntPtr VirtualAllocEx(IntPtr hProcess, IntPtr lpAddress,
uint dwSize, AllocationType flAllocationType, MemoryProtection flProtect);
private static byte[] buffer = new byte[1024];
/// <summary>
/// Reads a null terminated string
/// </summary>
/// <param name="fs"></param>
/// <returns></returns>
public static string ReadString(FileStream fs)
{
char c;
String retVal = "";
while ((c = (char)fs.ReadByte()) != '\0')
{
retVal += c;
}
return retVal;
}
public static string ReadString(byte[] data, int offset)
{
char c;
String retVal = "";
while (offset < data.Length && (c = (char)data[offset]) != '\0')
{
retVal += c;
offset++;
}
return retVal;
}
public static string ReadString(FileStream fs, int length)
{
fs.Read(buffer, 0, length);
return System.Text.ASCIIEncoding.ASCII.GetString(buffer, 0, 8);
}
public static short ReadShort(FileStream fs)
{
fs.Read(buffer, 0, 2);
return BitConverter.ToInt16(buffer, 0);
}
public static int ReadInt(FileStream fs)
{
fs.Read(buffer, 0, 4);
return BitConverter.ToInt32(buffer, 0);
}
public static long ReadLong(FileStream fs)
{
fs.Read(buffer, 0, 8);
return BitConverter.ToInt64(buffer, 0);
}
internal static void CheckForNewVersion(string Version)
{
try
{
var wc = new System.Net.WebClient();
var ver = wc.DownloadString(@"https://github.com/CBLoader/CBLoader/raw/master/Releases/Current.txt").Trim();
if (ver != Version)
{
var zip = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), string.Format(@"ddi\Updates\CBLoader v{0}.zip", ver));
Directory.CreateDirectory(Path.GetDirectoryName(zip));
if (File.Exists(zip))
{
}
else
{
Log.Info("New version available. Download?");
if (Console.ReadKey().Key == ConsoleKey.Y)
{
Console.WriteLine();
wc.DownloadFile(string.Format(@"https://github.com/CBLoader/CBLoader/raw/master/Releases/CBLoader {0}.zip", ver), zip);
Log.Info("Downloaded new version to My Documents\\ddi folder.");
System.Diagnostics.Process.Start(Path.GetDirectoryName(zip));
}
}
}
}
catch (System.Net.WebException c)
{
if (c.ToString().Contains("could not be resolved")) // No Internet
Log.Debug("No internet access");
else
Log.Error("Failed checking for Updates", c);
}
}
}
}