forked from Agorath/ChummerGenSR4
-
Notifications
You must be signed in to change notification settings - Fork 0
/
clsOmaeHelper.cs
409 lines (364 loc) · 15.5 KB
/
clsOmaeHelper.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.ServiceModel;
using System.Windows.Forms;
using System.Xml;
using Chummer.OmaeService;
using Chummer.TranslationService;
using System.IO.Packaging;
namespace Chummer
{
class OmaeHelper
{
/// <summary>
/// Set all of the BasicHttpBinding properties and configure the EndPoint. This is done to avoid the need for an app.config file to be shippped with the application.
/// </summary>
public omaeSoapClient GetOmaeService()
{
BasicHttpBinding objBinding = new BasicHttpBinding();
objBinding.Name = "omaeSoap";
objBinding.CloseTimeout = TimeSpan.FromMinutes(1);
objBinding.OpenTimeout = TimeSpan.FromMinutes(1);
objBinding.ReceiveTimeout = TimeSpan.FromMinutes(10);
objBinding.SendTimeout = TimeSpan.FromMinutes(1);
objBinding.AllowCookies = false;
objBinding.BypassProxyOnLocal = false;
objBinding.HostNameComparisonMode = HostNameComparisonMode.StrongWildcard;
objBinding.MaxBufferSize = 5242880; // 5 MB
objBinding.MaxReceivedMessageSize = 5242880; // 5 MB
objBinding.MaxBufferPoolSize = 524288;
objBinding.MessageEncoding = WSMessageEncoding.Text;
objBinding.TextEncoding = System.Text.Encoding.UTF8;
objBinding.TransferMode = TransferMode.Buffered;
objBinding.UseDefaultWebProxy = true;
objBinding.ReaderQuotas.MaxDepth = 32;
objBinding.ReaderQuotas.MaxStringContentLength = 8388608;
objBinding.ReaderQuotas.MaxArrayLength = 5242880;
objBinding.ReaderQuotas.MaxBytesPerRead = 4096;
objBinding.ReaderQuotas.MaxNameTableCharCount = 32565;
objBinding.Security.Mode = BasicHttpSecurityMode.None;
objBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.None;
objBinding.Security.Transport.ProxyCredentialType = HttpProxyCredentialType.None;
objBinding.Security.Transport.Realm = "";
objBinding.Security.Message.ClientCredentialType = BasicHttpMessageCredentialType.UserName;
objBinding.Security.Message.AlgorithmSuite = System.ServiceModel.Security.SecurityAlgorithmSuite.Default;
const string strEndPoint = "http://www.chummergen.com/dev/chummer/omae/omae.asmx";
EndpointAddress objEndPointAddress = new EndpointAddress(strEndPoint);
omaeSoapClient objService = new omaeSoapClient(objBinding, objEndPointAddress);
return objService;
}
/// <summary>
/// Set all of the BasicHttpBinding properties and configure the EndPoint. This is done to avoid the need for an app.config file to be shippped with the application.
/// </summary>
public translationSoapClient GetTranslationService()
{
BasicHttpBinding objBinding = new BasicHttpBinding();
objBinding.Name = "translationSoap";
objBinding.CloseTimeout = TimeSpan.FromMinutes(1);
objBinding.OpenTimeout = TimeSpan.FromMinutes(1);
objBinding.ReceiveTimeout = TimeSpan.FromMinutes(10);
objBinding.SendTimeout = TimeSpan.FromMinutes(1);
objBinding.AllowCookies = false;
objBinding.BypassProxyOnLocal = false;
objBinding.HostNameComparisonMode = HostNameComparisonMode.StrongWildcard;
objBinding.MaxBufferSize = 5242880; // 5 MB
objBinding.MaxReceivedMessageSize = 5242880; // 5 MB
objBinding.MaxBufferPoolSize = 524288;
objBinding.MessageEncoding = WSMessageEncoding.Text;
objBinding.TextEncoding = System.Text.Encoding.UTF8;
objBinding.TransferMode = TransferMode.Buffered;
objBinding.UseDefaultWebProxy = true;
objBinding.ReaderQuotas.MaxDepth = 32;
objBinding.ReaderQuotas.MaxStringContentLength = 8388608;
objBinding.ReaderQuotas.MaxArrayLength = 5242880;
objBinding.ReaderQuotas.MaxBytesPerRead = 4096;
objBinding.ReaderQuotas.MaxNameTableCharCount = 32565;
objBinding.Security.Mode = BasicHttpSecurityMode.None;
objBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.None;
objBinding.Security.Transport.ProxyCredentialType = HttpProxyCredentialType.None;
objBinding.Security.Transport.Realm = "";
objBinding.Security.Message.ClientCredentialType = BasicHttpMessageCredentialType.UserName;
objBinding.Security.Message.AlgorithmSuite = System.ServiceModel.Security.SecurityAlgorithmSuite.Default;
const string strEndPoint = "http://www.chummergen.com/dev/chummer/omae/translation.asmx";
EndpointAddress objEndPointAddress = new EndpointAddress(strEndPoint);
translationSoapClient objService = new translationSoapClient(objBinding, objEndPointAddress);
return objService;
}
/// <summary>
/// Write the contents of a MemoryStream to an XmlDocument.
/// </summary>
/// <param name="objStream">MemoryStream to read.</param>
public XmlDocument XmlDocumentFromStream(MemoryStream objStream)
{
string strXml = "";
objStream.Position = 0;
StreamReader objReader = new StreamReader(objStream);
strXml = objReader.ReadToEnd();
XmlDocument objXmlDocument = new XmlDocument();
objXmlDocument.LoadXml(strXml);
return objXmlDocument;
}
#region Base64
/// <summary>
/// Base64 encode a string.
/// </summary>
public string Base64Encode(string data)
{
try
{
byte[] bytEncode = new byte[data.Length];
bytEncode = System.Text.Encoding.UTF8.GetBytes(data);
string strEncodedData = Convert.ToBase64String(bytEncode);
return strEncodedData;
}
catch (Exception e)
{
throw new Exception("Error in Base64Encode" + e.Message);
}
}
/// <summary>
/// Decode a Base64 encoded string.
/// </summary>
public string Base64Decode(string data)
{
try
{
System.Text.UTF8Encoding encoder = new System.Text.UTF8Encoding();
System.Text.Decoder utf8Decode = encoder.GetDecoder();
byte[] bytToDecode = Convert.FromBase64String(data);
int charCount = utf8Decode.GetCharCount(bytToDecode, 0, bytToDecode.Length);
char[] chrDecoded = new char[charCount];
utf8Decode.GetChars(bytToDecode, 0, bytToDecode.Length, chrDecoded, 0);
string strResult = new String(chrDecoded);
return strResult;
}
catch (Exception e)
{
throw new Exception("Error in Base64Decode" + e.Message);
}
}
#endregion
#region Compression
/// <summary>
/// Compresses byte array to new byte array.
/// </summary>
public byte[] Compress(byte[] raw)
{
using (MemoryStream memory = new MemoryStream())
{
using (GZipStream gzip = new GZipStream(memory, CompressionMode.Compress, true))
{
gzip.Write(raw, 0, raw.Length);
}
return memory.ToArray();
}
}
/// <summary>
/// Decompress byte array to a new byte array.
/// </summary>
public byte[] Decompress(byte[] gzip)
{
// Create a GZIP stream with decompression mode.
// ... Then create a buffer and write into while reading from the GZIP stream.
using (GZipStream stream = new GZipStream(new MemoryStream(gzip), CompressionMode.Decompress))
{
const int size = 4096;
byte[] buffer = new byte[size];
using (MemoryStream memory = new MemoryStream())
{
int count = 0;
do
{
count = stream.Read(buffer, 0, size);
if (count > 0)
{
memory.Write(buffer, 0, count);
}
}
while (count > 0);
return memory.ToArray();
}
}
}
/// <summary>
/// Compress multiple files to a byte array.
/// </summary>
/// <param name="lstFiles">List of files to compress.</param>
public byte[] CompressMutiple(List<string> lstFiles)
{
MemoryStream objStream = new MemoryStream();
Package objPackage = ZipPackage.Open(objStream, FileMode.Create, FileAccess.ReadWrite);
foreach (string strFile in lstFiles)
{
Uri objUri = new Uri("/" + System.IO.Path.GetFileName(strFile).Replace(' ', '_'), UriKind.Relative);
PackagePart objPart = objPackage.CreatePart(objUri, System.Net.Mime.MediaTypeNames.Application.Zip, CompressionOption.Maximum);
byte[] bytBuffer = File.ReadAllBytes(strFile);
objPart.GetStream().Write(bytBuffer, 0, bytBuffer.Length);
}
objPackage.Close();
return objStream.ToArray();
}
/// <summary>
/// Compress multiple files to a file. This is only used for testing an compressing NPC packs. This is not to be used by end users.
/// </summary>
/// <param name="lstFiles">List of files to compress.</param>
/// <param name="strDestination">File to compress to.</param>
public void CompressMutipleToFile(List<string> lstFiles, string strDestination)
{
Package objPackage = ZipPackage.Open(strDestination, FileMode.Create, FileAccess.ReadWrite);
foreach (string strFile in lstFiles)
{
string[] strPath = System.IO.Path.GetDirectoryName(strFile).Replace(' ', '_').Split('\\');
string strPackFile = "/" + strPath[strPath.Length - 2] + "/" + strPath[strPath.Length - 1] + "/" + System.IO.Path.GetFileName(strFile).Replace(' ', '_');
if (strPackFile.StartsWith("/saves"))
strPackFile = strPackFile.Replace("/saves", string.Empty);
Uri objUri = new Uri(strPackFile, UriKind.Relative);
PackagePart objPart = objPackage.CreatePart(objUri, System.Net.Mime.MediaTypeNames.Application.Zip, CompressionOption.Maximum);
byte[] bytBuffer = File.ReadAllBytes(strFile);
objPart.GetStream().Write(bytBuffer, 0, bytBuffer.Length);
}
objPackage.Close();
}
/// <summary>
/// Decompress multiple files from a single zip file.
/// </summary>
/// <param name="bytBuffer">Byte array that contains the zip file.</param>
/// <param name="strPrefix">Prefix to attach to the decompressed files.</param>
public void DecompressDataFile(byte[] bytBuffer, string strPrefix)
{
string strFilePath = Path.Combine(Application.StartupPath, "data");
MemoryStream objStream = new MemoryStream();
objStream.Write(bytBuffer, 0, bytBuffer.Length);
Package objPackage = ZipPackage.Open(objStream, FileMode.Open, FileAccess.Read);
foreach (PackagePart objPart in objPackage.GetParts())
{
string strTarget = Path.Combine(strFilePath, objPart.Uri.ToString().Replace("/", string.Empty));
strTarget = strTarget.Replace("\\override", Path.DirectorySeparatorChar + "override" + strPrefix);
strTarget = strTarget.Replace("\\custom", Path.DirectorySeparatorChar + "custom" + strPrefix);
Stream objSource = objPart.GetStream(FileMode.Open, FileAccess.Read);
Stream objDestination = File.OpenWrite(strTarget);
byte[] bytFileBuffer = new byte[100000];
int intRead;
intRead = objSource.Read(bytFileBuffer, 0, bytFileBuffer.Length);
while (intRead > 0)
{
objDestination.Write(bytFileBuffer, 0, intRead);
intRead = objSource.Read(bytFileBuffer, 0, bytFileBuffer.Length);
}
objDestination.Close();
objSource.Close();
}
}
/// <summary>
/// Decompress multiple files from a single zip file.
/// </summary>
/// /// <param name="bytBuffer">Byte array that contains the zip file.</param>
public void DecompressCharacterSheet(byte[] bytBuffer)
{
string strFilePath = Path.Combine(Application.StartupPath, "sheets");
strFilePath = Path.Combine(strFilePath, "omae");
MemoryStream objStream = new MemoryStream();
objStream.Write(bytBuffer, 0, bytBuffer.Length);
Package objPackage = ZipPackage.Open(objStream, FileMode.Open, FileAccess.Read);
foreach (PackagePart objPart in objPackage.GetParts())
{
string strTarget = Path.Combine(strFilePath, objPart.Uri.ToString().Replace("/", string.Empty).Replace('_', ' '));
Stream objSource = objPart.GetStream(FileMode.Open, FileAccess.Read);
Stream objDestination = File.OpenWrite(strTarget);
byte[] bytFileBuffer = new byte[100000];
int intRead;
intRead = objSource.Read(bytFileBuffer, 0, bytFileBuffer.Length);
while (intRead > 0)
{
objDestination.Write(bytFileBuffer, 0, intRead);
intRead = objSource.Read(bytFileBuffer, 0, bytFileBuffer.Length);
}
objDestination.Close();
objSource.Close();
}
}
/// <summary>
/// Decompress multiple files from a single zip file.
/// </summary>
/// <param name="bytBuffer">Byte array that contains the zip file.</param>
public void DecompressNPCs(byte[] bytBuffer)
{
string strFilePath = Path.Combine(Application.StartupPath, "saves");
// If the directory does not exist, create it.
if (!Directory.Exists(strFilePath))
Directory.CreateDirectory(strFilePath);
MemoryStream objStream = new MemoryStream();
objStream.Write(bytBuffer, 0, bytBuffer.Length);
Package objPackage = ZipPackage.Open(objStream, FileMode.Open, FileAccess.Read);
foreach (PackagePart objPart in objPackage.GetParts())
{
string strTarget = Path.Combine(strFilePath, objPart.Uri.ToString().Replace('_', ' '));
Stream objSource = objPart.GetStream(FileMode.Open, FileAccess.Read);
string[] strDirectory = strTarget.Split('/');
if (!strDirectory[1].EndsWith(".chum"))
{
if (!Directory.Exists(Path.Combine(strFilePath, strDirectory[1])))
Directory.CreateDirectory(Path.Combine(strFilePath, strDirectory[1]));
}
if (!strDirectory[2].EndsWith(".chum"))
{
if (!Directory.Exists(Path.Combine(strFilePath, strDirectory[1] + Path.DirectorySeparatorChar + strDirectory[2])))
Directory.CreateDirectory(Path.Combine(strFilePath, strDirectory[1] + Path.DirectorySeparatorChar + strDirectory[2]));
}
Stream objDestination = File.OpenWrite(strFilePath + strTarget.Replace('/', Path.DirectorySeparatorChar));
byte[] bytFileBuffer = new byte[100000];
int intRead;
intRead = objSource.Read(bytFileBuffer, 0, bytFileBuffer.Length);
while (intRead > 0)
{
objDestination.Write(bytFileBuffer, 0, intRead);
intRead = objSource.Read(bytFileBuffer, 0, bytFileBuffer.Length);
}
objDestination.Close();
objSource.Close();
}
}
/// <summary>
/// Decompress multiple files from a single zip file.
/// </summary>
/// <param name="strExtract">Zip file to extract from.</param>
public void DecompressNPCs(string strExtract)
{
string strFilePath = Path.Combine(Application.StartupPath, "saves");
// If the directory does not exist, create it.
if (!Directory.Exists(strFilePath))
Directory.CreateDirectory(strFilePath);
Package objPackage = ZipPackage.Open(strExtract, FileMode.Open, FileAccess.Read);
foreach (PackagePart objPart in objPackage.GetParts())
{
string strTarget = Path.Combine(strFilePath, objPart.Uri.ToString().Replace('_', ' '));
Stream objSource = objPart.GetStream(FileMode.Open, FileAccess.Read);
string[] strDirectory = strTarget.Split('/');
if (!strDirectory[1].EndsWith(".chum"))
{
if (!Directory.Exists(Path.Combine(strFilePath, strDirectory[1])))
Directory.CreateDirectory(Path.Combine(strFilePath, strDirectory[1]));
}
if (!strDirectory[2].EndsWith(".chum"))
{
if (!Directory.Exists(Path.Combine(strFilePath, strDirectory[1] + Path.DirectorySeparatorChar + strDirectory[2])))
Directory.CreateDirectory(Path.Combine(strFilePath, strDirectory[1] + Path.DirectorySeparatorChar + strDirectory[2]));
}
Stream objDestination = File.OpenWrite(strFilePath + strTarget.Replace('/', Path.DirectorySeparatorChar));
byte[] bytFileBuffer = new byte[100000];
int intRead;
intRead = objSource.Read(bytFileBuffer, 0, bytFileBuffer.Length);
while (intRead > 0)
{
objDestination.Write(bytFileBuffer, 0, intRead);
intRead = objSource.Read(bytFileBuffer, 0, bytFileBuffer.Length);
}
objDestination.Close();
objSource.Close();
}
}
#endregion
}
}