-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathConverter.cs
172 lines (153 loc) · 5.65 KB
/
Converter.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
using System;
using System.IO;
using System.Text.Json;
using System.Dynamic;
using System.Collections.Generic;
using Newtonsoft.Json.Linq;
namespace DestinyColladaGenerator
{
class Converter
{
/// <summary>
/// Loads a TGX binary file into a dynamic object
/// </summary>
/// <param name="data">The TGX binary file data</param>
/// <returns>A dynamic object containing the TGX binary file data</returns>
public static dynamic loadTGXBin(byte[] data)
{
Console.WriteLine("Loading model data...");
Console.Write("Reading TGXM header... ");
string magic = TGXMUtils.String(data, 0x0, 0x4); // TGXM
int version = (int)BitConverter.ToUInt32(data, 0x4);
int fileOffset = (int)BitConverter.ToUInt32(data, 0x8);
int fileCount = (int)BitConverter.ToUInt32(data, 0xC);
string fileIdentifier = TGXMUtils.String(data, 0x10, 0x100);
if (magic != "TGXM") {
Console.WriteLine("Invalid TGX File, skipping");
return null;
}
Console.WriteLine("Done.");
Dictionary<string,dynamic> files = new Dictionary<string,dynamic>();
dynamic renderMetadata = new object();
for (var f=0; f<fileCount; f++)
{
int headerOffset = fileOffset+(0x110*f);
string name = TGXMUtils.String(data, headerOffset, 0x100);
int offset = (int)BitConverter.ToUInt32(data, headerOffset+0x100);
int type = (int)BitConverter.ToUInt32(data, headerOffset+0x104);
int size = (int)BitConverter.ToUInt32(data, headerOffset+0x108);
Console.WriteLine("Loading file \""+name+".\" File size: "+size+" bytes.");
byte[] fileData = new byte[size];
if(!File.Exists(Path.Combine("Tilemaps",$"{name}.png")))
Array.ConstrainedCopy(data, offset, fileData, 0, size);
else
fileData = File.ReadAllBytes(Path.Combine("Tilemaps",$"{name}.png"));
dynamic file = new ExpandoObject();
file.name = name;
file.offset = offset;
file.type = type;
file.size = size;
if (name.IndexOf(".js") != -1)
{ // render_metadata.js
renderMetadata = JsonSerializer.Deserialize<RenderMetadata>(TGXMUtils.String(fileData,0,0));
files.Add("render_metadata_js", fileData);
file.data = renderMetadata;
}
else
{
file.data = fileData;
}
if (files.ContainsKey(name)) continue;
files.Add(name, file);
Console.WriteLine("File \""+name+"\" loaded.");
}
dynamic tgxBin = new ExpandoObject();
tgxBin.fileIdentifier = fileIdentifier;
tgxBin.files = files;
//tgxBin.lookup = fileLookup;
tgxBin.metadata = renderMetadata;
Console.WriteLine("Done loading model data.");
return tgxBin;
}
/// <summary>
/// Converts a TGX binary file into a Collada file
/// </summary>
/// <param name="data">The TGX binary file data</param>
/// <param name="fileOut">The folder to write the Collada file to</param>
/// <param name="game">Defines which game the model is from. Empty string for D1, "2" for D2</param>
public static void Convert(byte[] data, string fileOut, string game)
{
dynamic tgxBin = loadTGXBin(data);
List<dynamic> renderMeshes = Parsers.parseTGXAsset(tgxBin);
List<dynamic> renderRaws = new List<dynamic>
{
tgxBin
};
dynamic renderModel = new ExpandoObject();
renderModel.meshes = renderMeshes;
renderModel.textures = null;
renderModel.raws = renderRaws;
renderModel.name = "Model";
renderModel.type = "";
renderModel.bucket = 0;
List<dynamic> renderModels = new List<dynamic>
{
renderModel
};
WriteCollada.WriteFile(renderModels, fileOut, game);
}
/// <summary>
/// Converts an array of API geometry data to Collada files
/// </summary>
/// <param name="binItems">The API geometry</param>
/// <param name="fileOut">The folder to write the Collada files to</param>
/// <param name="game">Defines which game the model is from. Empty string for D1, "2" for D2</param>
public static void Convert(APIItemData[] binItems, string fileOut, string game)
{
List<dynamic> renderModels = new List<dynamic>();
foreach (APIItemData itemContainers in binItems)
{
byte[][] geometry = itemContainers.geometry;
byte[][] textures = itemContainers.texture;
string name = itemContainers.name;
string type = itemContainers.type;
uint bucket = itemContainers.bucket;
dynamic renderModel = new ExpandoObject();
List<dynamic> renderMeshes = new List<dynamic>();
Dictionary<string,dynamic> renderTextures = new Dictionary<string,dynamic>();
List<dynamic> plates = new List<dynamic>();
List<dynamic> renderRaws = new List<dynamic>();
foreach (byte[] data in geometry)
{
dynamic tgxBin = loadTGXBin(data);
if (tgxBin == null) continue;
renderRaws.Add(tgxBin);
List<dynamic> meshes = Parsers.parseTGXAsset(tgxBin);
foreach (ExpandoObject mesh in meshes)
{
renderMeshes.Add(mesh);
}
plates.Add(tgxBin.metadata.texture_plates);
}
renderTextures.Add("texturePlates", plates);
foreach (byte[] data in textures)
{
dynamic tgxBin = loadTGXBin(data);
if (tgxBin == null) continue;
foreach (dynamic texture in tgxBin.files)
{
renderTextures.Add(texture.Key, texture.Value.data);
}
}
renderModel.meshes = renderMeshes;
renderModel.textures = renderTextures;
renderModel.name = name;
renderModel.type = type;
renderModel.bucket = bucket;
renderModel.raws = renderRaws;
renderModels.Add(renderModel);
}
WriteCollada.WriteFile(renderModels, fileOut, game);
}
}
}