forked from TiredHobgoblin/Destiny-Collada-Generator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ApiSupport.cs
476 lines (418 loc) · 15.9 KB
/
ApiSupport.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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
using System;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Linq;
using System.Text.Json;
using System.Collections.Generic;
using Knapcode.TorSharp;
namespace DestinyColladaGenerator
{
//Methods for accessing the bungie.net web api
class apiSupport
{
private static string apiKey = null;
private static string apiRoot = @"https://www.bungie.net/Platform";
/*private static TorSharpSettings settings;
private static TorSharpProxy proxy;
private static HttpClientHandler handler;
private static void torSetup()
{
// configure
settings = new TorSharpSettings
{
ZippedToolsDirectory = Path.Combine(Path.GetTempPath(), "TorZipped"),
ExtractedToolsDirectory = Path.Combine(Path.GetTempPath(), "TorExtracted"),
PrivoxySettings = { Port = 8118 }, //{ Port = 1337 },
UseExistingTools = true,
TorSettings =
{
SocksPort = 9150, //1338,
ControlPort = 9151, //1339,
ControlPassword = "foobar",
},
};
// download tools
/*await new TorSharpToolFetcher(settings, new HttpClient()).FetchAsync();
proxy = new TorSharpProxy(settings);
handler = new HttpClientHandler
{
Proxy = new WebProxy(new Uri("http://localhost:" + settings.PrivoxySettings.Port))
};
/*await proxy.ConfigureAndStartAsync();
//var httpClient = new HttpClient(handler);
//Console.WriteLine(/*await httpClient.GetStringAsync("http://api.ipify.org").Result);
}*/
public static /*JObject*/dynamic makeCallJson(string url)
{
// for (int attempts=3; attempts>0; attempts--)
// {
// try
// {
using (var client = new HttpClient())
{
client.DefaultRequestHeaders.Add("X-API-Key", apiKey);
var response = client.GetAsync(url).Result;
var content = response.Content.ReadAsStringAsync().Result;
if (content.StartsWith('<') == true) return null;
dynamic item = JsonSerializer.Deserialize<ManifestData>(content); //JObject.Parse(content);
return item;
}
// }
// catch (TaskCanceledException e)
// {
// Console.WriteLine($"Failed to receive a response from the server. Attempts remaining: {attempts-1}");
// if (attempts == 1)
// {
// throw new HttpRequestException("Request timed out", e);
// }
// continue;
// }
// }
// return null;
}
public static dynamic makeCallGear(string url, string game)
{
using (var client = new HttpClient())
{
client.DefaultRequestHeaders.Add("X-API-Key", apiKey);
var response = client.GetAsync(url).Result;
var content = response.Content.ReadAsStringAsync().Result;
dynamic item = null;
if (game.Equals(""))
item = JsonSerializer.Deserialize<D1Shader>(content);
else
item = JsonSerializer.Deserialize<D2Shader>(content);
return item;
}
}
public static string makeCallString(string url)
{
// for (int attempts=3; attempts>0; attempts--)
// {
// try
// {
using (var client = new HttpClient())
{
client.DefaultRequestHeaders.Add("X-API-Key", apiKey);
var response = client.GetAsync(url).Result;
return response.Content.ReadAsStringAsync().Result;
}
// }
// catch (TaskCanceledException e)
// {
// Console.WriteLine($"Failed to receive a response from the server. Attempts remaining: {attempts-1}");
// if (attempts == 1)
// {
// throw new HttpRequestException("Request timed out", e);
// }
// continue;
// }
// }
// return null;
}
public static byte[] makeCall(string url)
{
// for (int attempts=3; attempts>0; attempts--)
// {
// try
// {
//if (Program.useTor) proxy.GetNewIdentityAsync();
using (var client = new HttpClient())
{
client.DefaultRequestHeaders.Add("X-API-Key", apiKey);
var response = client.GetAsync(url).Result;
var content = response.Content.ReadAsByteArrayAsync().Result;
return content;
}
// }
// catch (TaskCanceledException e)
// {
// Console.WriteLine($"Failed to receive a response from the server. Attempts remaining: {attempts-1}");
// if (attempts == 1)
// {
// throw new HttpRequestException("Request timed out", e);
// }
// continue;
// }
// }
// return null;
}
public static void updateLocalManifest()
{
Console.Write("Requesting latest api manifest...");
Object manifestJson = makeCallJson(apiRoot+"/Destiny2/Manifest/");
Console.WriteLine("Received.");
Console.Write("Updating local copy...");
using (StreamWriter manifestWriter = new StreamWriter(Path.Combine(new string[]{"Resources", "localManifest.json"})))
{
manifestWriter.Write(manifestJson.ToString());
}
Console.WriteLine("Done.");
}
public static void convertByHash(string game, string[] hashes = null, string fileOut = "")
{
//if (Program.useTor) torSetup();
bool runConverter = true;
while (runConverter)
{
string[] itemHashes = null;
if (hashes==null)
{
Console.Write("Input item hash(es) > ");
itemHashes = Console.ReadLine().Split(" ", System.StringSplitOptions.RemoveEmptyEntries);
}
else itemHashes = hashes;
bool skipMainConvert = false;
if (itemHashes.Length>1 && Program.multipleFolderOutput)
{
for (int h=0; h<itemHashes.Length; h++)
convertByHash(game, new string[]{itemHashes[h]}, fileOut);
skipMainConvert = true;
}
ShaderPresets.propertyChannels = new Dictionary<uint, Channels>();
ShaderPresets.propertyChannels.Clear();
ShaderPresets.presets = new Dictionary<string, string>();
ShaderPresets.channelData = new Dictionary<Channels, D2MatProps>();
ShaderPresets.channelData.Clear();
ShaderPresets.channelTextures = new Dictionary<Channels, D2TexturesContainer>();
ShaderPresets.channelTextures.Clear();
ShaderPresets.scripts = new Dictionary<string, string>();
if (itemHashes.Length > 0 && !skipMainConvert)
{
if (hashes==null)
{
Console.Write("Output directory > ");
fileOut = Console.ReadLine();
}
if (fileOut == "") fileOut = Path.Combine(AppDomain.CurrentDomain.BaseDirectory,"Output");
else fileOut = Path.GetFullPath(fileOut);
if (!Directory.Exists(fileOut))
{
Directory.CreateDirectory(fileOut);
}
List<APIItemData> items = new List<APIItemData>();
List<string> names = new List<string>();
List<int> counts = new List<int>();
foreach (string itemHash in itemHashes)
{
Console.Write("Calling item definition from manifest... ");
ManifestData itemDef = null;
// Some items have hidden entries that light.gg doesn't keep a copy of, but lowlidev does. Keeping the line for cases where this can be used.
if (game=="2") itemDef = makeCallJson($@"https://www.light.gg/db/items/{itemHash}/?raw=2");
//if (game=="2") itemDef = makeCallJson($@"https://lowlidev.com.au/destiny/api/gearasset/{itemHash}?destiny{game}");
// Light.gg DDOS protection keeps causing issues...
else
{
string message = ""; // Just to suppress the "e is unused" warning.
try{itemDef = makeCallJson($@"https://lowlidev.com.au/destiny/api/gearasset/{itemHash}?destiny{game}");}
catch (JsonException e) {message = e.Message;}
// Ignore the error, itemDef stays as null due to it and it works as it should.
}
if (itemDef == null) {Console.WriteLine("Item not found. Skipping."); continue;}
if (itemDef.gearAsset.ToString() == "false") {Console.WriteLine("Item is not marked as a gearasset. May be classified in this tool's manifest."); continue;}
Console.WriteLine("Done.");
APIItemData itemContainers = new APIItemData();
List<byte[]> geometryContainers = new List<byte[]>();
List<byte[]> textureContainers = new List<byte[]>();
string itemName = (game == "2") ? itemDef.definition.GetProperty("displayProperties").GetProperty("name").GetString() : itemDef.definition.GetProperty("itemName").GetString();
string itemType = (game == "2") ? itemDef.definition.GetProperty("itemTypeDisplayName").GetString() : itemDef.definition.GetProperty("itemTypeName").GetString();
uint itemBucket = (game == "2") ? itemDef.definition.GetProperty("inventory").GetProperty("bucketTypeHash").GetUInt32() : itemDef.definition.GetProperty("bucketTypeHash").GetUInt32();
itemContainers.type = itemType;
if (itemType == "Shader")
{
Console.WriteLine("Found shader. Skipping.");
continue;
}
itemContainers.bucket = itemBucket;
if (Program.multipleFolderOutput)
WriteCollada.multiOutItemName = itemName;
//if (itemDef.gearAsset.GetProperty("content").GetArrayLength() < 1)
//{
// Console.WriteLine($"{itemName} has no 3D content associated with it. Skipping.");
// continue;
//}
int nameIndex = names.IndexOf(itemName);
if (nameIndex == -1)
{
names.Add(itemName);
counts.Add(0);
}
else
{
counts[nameIndex]++;
itemName += "-"+counts[nameIndex];
}
if(itemDef.gearAsset.content.Length > 0)
{
string[] geometries = itemDef.gearAsset.content[0].geometry;
bool tG = geometries != null;
string[] textures = itemDef.gearAsset.content[0].textures;
bool tT = textures != null;
if (itemDef.gearAsset.content[0].region_index_sets != null)
{
for (int g=0; g<geometries.Length; g++)
{
byte[] geometryContainer = makeCall($@"https://www.bungie.net/common/destiny{game}_content/geometry/platform/mobile/geometry/{geometries[g]}");
geometryContainers.Add(geometryContainer);
}
for (int t=0; t<textures.Length; t++)
{
byte[] textureContainer = makeCall($@"https://www.bungie.net/common/destiny{game}_content/geometry/platform/mobile/textures/{textures[t]}");
textureContainers.Add(textureContainer);
}
itemContainers.geometry = geometryContainers.ToArray();
itemContainers.texture = textureContainers.ToArray();
itemContainers.name = itemName;
items.Add(itemContainers);
}
else if ((itemDef.gearAsset.content[0].female_index_set!=null) && (itemDef.gearAsset.content[0].male_index_set!=null))
{
IndexSet mSet = itemDef.gearAsset.content[0].male_index_set;
IndexSet fSet = itemDef.gearAsset.content[0].female_index_set;
for (int index=0; index<mSet.geometry.Length; index++)
{
int g = mSet.geometry[index];
if (fSet.geometry.Contains(g)) continue;
byte[] geometryContainer = makeCall($@"https://www.bungie.net/common/destiny{game}_content/geometry/platform/mobile/geometry/{geometries[g]}");
geometryContainers.Add(geometryContainer);
}
for (int t=0; t<textures.Length; t++)
{
byte[] textureContainer = makeCall($@"https://www.bungie.net/common/destiny{game}_content/geometry/platform/mobile/textures/{textures[t]}");
textureContainers.Add(textureContainer);
}
itemContainers.geometry = geometryContainers.ToArray();
itemContainers.texture = textureContainers.ToArray();
itemContainers.name = "Male_"+itemName;
items.Add(itemContainers);
APIItemData itemContainersFemale = new APIItemData();
itemContainersFemale.type = itemType;
List<byte[]> geometryContainersFemale = new List<byte[]>();
List<byte[]> textureContainersFemale = new List<byte[]>();
for (int index=0; index<fSet.geometry.Length; index++)
{
int g = fSet.geometry[index];
if (mSet.geometry.Contains(g)) continue;
byte[] geometryContainer = makeCall($@"https://www.bungie.net/common/destiny{game}_content/geometry/platform/mobile/geometry/{geometries[g]}");
geometryContainersFemale.Add(geometryContainer);
}
for (int t=0; t<textures.Length; t++)
{
byte[] textureContainer = makeCall($@"https://www.bungie.net/common/destiny{game}_content/geometry/platform/mobile/textures/{textures[t]}");
textureContainersFemale.Add(textureContainer);
}
itemContainersFemale.geometry = geometryContainersFemale.ToArray();
itemContainersFemale.texture = textureContainersFemale.ToArray();
itemContainersFemale.name = "Female_"+itemName;
items.Add(itemContainersFemale);
}
else if (tG == false && textures.Length != 0)
{
for (int t=0; t<textures.Length; t++)
{
byte[] textureContainer = makeCall($@"https://www.bungie.net/common/destiny{game}_content/geometry/platform/mobile/textures/{textures[t]}");
textureContainers.Add(textureContainer);
}
itemContainers.geometry = geometryContainers.ToArray();
itemContainers.texture = textureContainers.ToArray();
itemContainers.name = itemName;
items.Add(itemContainers);
}
else
{
Console.WriteLine(itemName + " has no geometry or textures, or is missing a gendered index set.");
}
}
else Console.WriteLine("Item has no content. Skipping geometry and textures.");
ShaderPresets.propertyChannels.Clear();
ShaderPresets.channelData.Clear();
ShaderPresets.channelTextures.Clear();
ShaderPresets.generatePresets(game, itemDef, itemName);
}
Converter.Convert(items.ToArray(), fileOut, game);
}
else if (skipMainConvert)
{}
else
Console.WriteLine("No hashes given.");
while (true)
{
if (hashes != null) { runConverter = false; break; }
Console.Write("Convert another file? (Y/N) ");
string runAgain = "";
runAgain = Console.ReadLine();
if (runAgain.ToUpper() == "Y")
{
break;
}
else if (runAgain.ToUpper() == "N")
{
runConverter = false;
break;
}
else
{
Console.WriteLine("Invalid input");
}
}
}
}
public class contentManifest
{
public contentManifestEntry[] entries { get; set; }
}
public class contentManifestEntry
{
public int id { get; set; }
public string json { get; set; }
}
public static void convertContentManifest(string game)
{
//if (Program.useTor) torSetup();
Console.Write("Manifest location > ");
string manifestLocation = Console.ReadLine();
string manifestContent = File.ReadAllText(manifestLocation);
dynamic manifestData = JsonSerializer.Deserialize<contentManifest>(manifestContent);
Console.Write("Starting hash > ");
int startHash = Convert.ToInt32(Console.ReadLine());
Console.Write("Output directory > ");
string fileOut = Console.ReadLine();
if (fileOut == "") fileOut = Path.Combine(AppDomain.CurrentDomain.BaseDirectory,"Output");
else fileOut = Path.GetFullPath(fileOut);
if (!Directory.Exists(fileOut))
{
Directory.CreateDirectory(fileOut);
}
string failHashes = "";
for (int h=startHash; h<manifestData.entries.Length; h++)
{
string itemHash = unchecked((uint) manifestData.entries[h].id).ToString();
try
{
convertByHash(game, new string[]{itemHash}, fileOut);
} catch {
failHashes += itemHash;
}
}
File.WriteAllText(Path.Combine(fileOut, "FailedHashes.txt"), failHashes);
}
public static void customCall()
{
Console.Write("Key > ");
string userKey = Console.ReadLine();
if (userKey != "") apiKey = userKey;
Console.Write("Call > ");
string callContent = Console.ReadLine();
Console.Write("Output name > ");
string fileOut = Console.ReadLine();
if (fileOut == "") fileOut = "Response.txt";
byte[] callResponse = makeCall(callContent);
using (BinaryWriter output = new BinaryWriter(File.Open(fileOut, FileMode.Create)))
{
output.Write(callResponse);
}
Console.WriteLine("Done.");
}
}
}