-
Notifications
You must be signed in to change notification settings - Fork 1
/
TXG.cs
183 lines (161 loc) · 6 KB
/
TXG.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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TXG2TPL
{
class TXG
{
public uint FileCount;
public uint[] Offsets;
public TXG(Stream stream, string[] files)
{
using (BigEndianWriter writer = new BigEndianWriter(stream))
{
writer.Write(files.Length);
Offsets = new uint[files.Length];
for (int i = 0; i < files.Length; i++)
{
writer.Write(0);
}
writer.AlignPosition(0x20, 0xFF);
for (int i = 0; i < files.Length; i++)
{
using (FileStream fStream = File.OpenRead(files[i]))
{
Offsets[i] = (uint)writer.BaseStream.Position;
TXGHeader txg = new TXGHeader(writer, new TPL(fStream));
}
}
writer.BaseStream.Position = 0x04;
for (int i = 0; i < files.Length; i++)
{
writer.Write(Offsets[i]);
}
}
}
public TXG(Stream stream, string outputDir)
{
using (BigEndianReader reader = new BigEndianReader(stream))
{
FileCount = reader.ReadUInt32();
Offsets = new uint[FileCount];
for (int i = 0; i < FileCount; i++)
{
Offsets[i] = reader.ReadUInt32();
}
for (int i = 0; i < FileCount; i++)
{
reader.BaseStream.Position = Offsets[i];
string fileName = Path.Combine(outputDir, i + ".tpl");
Console.WriteLine(string.Format("File Name: {0}", fileName));
TXGHeader header = new TXGHeader(reader);
MemoryStream mStream = new MemoryStream();
TPL tpl = new TPL(header, mStream);
File.WriteAllBytes(fileName, mStream.ToArray());
}
}
}
}
class TXGHeader
{
public uint ImageCount;
public uint ImageFormat;
public uint PaletteFormat;
public uint Width;
public uint Height;
public uint SingleImage;
public uint[] ImageOffsets;
public uint[] PaletteOffsets;
public byte[][] ImageData;
public byte[][] PaletteData;
public ImageDataFormat _Format;
public TXGHeader(BigEndianWriter writer, TPL tpl)
{
writer.Write(tpl.ImageCount);
writer.Write(tpl.ImageFormat);
writer.Write(tpl.PaletteFormat);
writer.Write((uint)tpl.Width);
writer.Write((uint)tpl.Height);
if (tpl.ImageCount != 1)
writer.Write(0);
else
writer.Write(1);
ImageOffsets = new uint[tpl.ImageCount];
PaletteOffsets = new uint[tpl.ImageCount];
long PastOffset = writer.BaseStream.Position;
for (int i = 0; i < tpl.ImageData.Length; i++)
{
writer.Write(0);
}
for (int i = 0; i < tpl.PaletteData.Length; i++)
{
writer.Write(0);
}
writer.AlignPosition(0x20, 0xFF);
for (int i = 0; i < tpl.ImageData.Length; i++)
{
ImageOffsets[i] = (uint)writer.BaseStream.Position;
writer.Write(tpl.ImageData[i]);
}
try
{
for (int i = 0; i < tpl.PaletteData.Length; i++)
{
PaletteOffsets[i] = (uint)writer.BaseStream.Position;
writer.Write(tpl.PaletteData[i]);
}
}
catch (ArgumentNullException ane) { }
long CurrentOffset = writer.BaseStream.Position;
writer.BaseStream.Position = PastOffset;
for (int i = 0; i < tpl.ImageData.Length; i++)
{
writer.Write(ImageOffsets[i]);
}
for (int i = 0; i < tpl.PaletteData.Length; i++)
{
writer.Write(PaletteOffsets[i]);
}
writer.BaseStream.Position = CurrentOffset;
}
public TXGHeader(BigEndianReader reader)
{
ImageCount = reader.ReadUInt32();
ImageFormat = reader.ReadUInt32();
PaletteFormat = reader.ReadUInt32();
Width = reader.ReadUInt32();
Height = reader.ReadUInt32();
SingleImage = reader.ReadUInt32();
_Format = ImageDataFormat.GetFormat((int)ImageFormat);
ImageData = new byte[ImageCount][];
PaletteData = new byte[ImageCount][];
Console.WriteLine(string.Format(Strings.TXGMessage, ImageCount, ImageFormat, PaletteFormat, Width, Height, Convert.ToBoolean(SingleImage)));
for (int i = 0; i < ImageCount; i++)
{
int ImageSize = _Format.CalculateDataSize((int)Width, (int)Height);
ImageData[i] = reader.ReadBytes(ImageSize, (int)reader.ReadUInt32());
}
if (_Format.HasPalette)
{
for (int i = 0; i < ImageCount; i++)
{
int palleteLength = (int)reader.ReadUInt32();
if (palleteLength != -1)
{
PaletteData[i] = reader.ReadBytes(0x200, palleteLength);
}
else
{
// Use previous image's palette
int previousPaletteLength = PaletteData[i - 1].Length;
PaletteData[i] = new byte[previousPaletteLength];
Array.Copy(PaletteData[i - 1], PaletteData[i], previousPaletteLength);
}
}
}
}
}
}