-
Notifications
You must be signed in to change notification settings - Fork 125
Expand file tree
/
Copy pathTextureFile.cs
More file actions
456 lines (368 loc) · 20.5 KB
/
TextureFile.cs
File metadata and controls
456 lines (368 loc) · 20.5 KB
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
using AssetRipper.TextureDecoder.Astc;
using AssetRipper.TextureDecoder.Atc;
using AssetRipper.TextureDecoder.Bc;
using AssetRipper.TextureDecoder.Dxt;
using AssetRipper.TextureDecoder.Etc;
using AssetRipper.TextureDecoder.Pvrtc;
using AssetRipper.TextureDecoder.Rgb;
using AssetRipper.TextureDecoder.Rgb.Formats;
using AssetRipper.TextureDecoder.Yuy2;
using AssetsTools.NET.Extra;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace AssetsTools.NET.Texture
{
public class TextureFile
{
public string m_Name;
public int m_ForcedFallbackFormat;
public bool m_DownscaleFallback;
public int m_Width;
public int m_Height;
public int m_CompleteImageSize;
public int m_TextureFormat;
public int m_MipCount;
public bool m_MipMap;
public bool m_IsReadable;
public bool m_ReadAllowed;
public bool m_StreamingMipmaps;
public int m_StreamingMipmapsPriority;
public int m_ImageCount;
public int m_TextureDimension;
public GLTextureSettings m_TextureSettings;
public int m_LightmapFormat;
public int m_ColorSpace;
public byte[] pictureData;
public StreamingInfo m_StreamData;
public struct GLTextureSettings
{
public int m_FilterMode;
public int m_Aniso;
public float m_MipBias;
public int m_WrapMode;
public int m_WrapU;
public int m_WrapV;
public int m_WrapW;
}
public struct StreamingInfo
{
public ulong offset;
public uint size;
public string path;
}
public static TextureFile ReadTextureFile(AssetTypeValueField baseField)
{
TextureFile texture = new TextureFile();
AssetTypeValueField tempField;
texture.m_Name = baseField["m_Name"].AsString;
if (!(tempField = baseField["m_ForcedFallbackFormat"]).IsDummy)
texture.m_ForcedFallbackFormat = tempField.AsInt;
if (!(tempField = baseField["m_DownscaleFallback"]).IsDummy)
texture.m_DownscaleFallback = tempField.AsBool;
texture.m_Width = baseField["m_Width"].AsInt;
texture.m_Height = baseField["m_Height"].AsInt;
if (!(tempField = baseField["m_CompleteImageSize"]).IsDummy)
texture.m_CompleteImageSize = tempField.AsInt;
texture.m_TextureFormat = baseField["m_TextureFormat"].AsInt;
if (!(tempField = baseField["m_MipCount"]).IsDummy)
texture.m_MipCount = tempField.AsInt;
if (!(tempField = baseField["m_MipMap"]).IsDummy)
texture.m_MipMap = tempField.AsBool;
texture.m_IsReadable = baseField["m_IsReadable"].AsBool;
if (!(tempField = baseField["m_ReadAllowed"]).IsDummy)
texture.m_ReadAllowed = tempField.AsBool;
if (!(tempField = baseField["m_StreamingMipmaps"]).IsDummy)
texture.m_StreamingMipmaps = tempField.AsBool;
if (!(tempField = baseField["m_StreamingMipmapsPriority"]).IsDummy)
texture.m_StreamingMipmapsPriority = tempField.AsInt;
texture.m_ImageCount = baseField["m_ImageCount"].AsInt;
texture.m_TextureDimension = baseField["m_TextureDimension"].AsInt;
AssetTypeValueField textureSettings = baseField["m_TextureSettings"];
texture.m_TextureSettings.m_FilterMode = textureSettings["m_FilterMode"].AsInt;
texture.m_TextureSettings.m_Aniso = textureSettings["m_Aniso"].AsInt;
texture.m_TextureSettings.m_MipBias = textureSettings["m_MipBias"].AsFloat;
if (!(tempField = textureSettings["m_WrapMode"]).IsDummy)
texture.m_TextureSettings.m_WrapMode = tempField.AsInt;
if (!(tempField = textureSettings["m_WrapU"]).IsDummy)
texture.m_TextureSettings.m_WrapU = tempField.AsInt;
if (!(tempField = textureSettings["m_WrapV"]).IsDummy)
texture.m_TextureSettings.m_WrapV = tempField.AsInt;
if (!(tempField = textureSettings["m_WrapW"]).IsDummy)
texture.m_TextureSettings.m_WrapW = tempField.AsInt;
if (!(tempField = baseField["m_LightmapFormat"]).IsDummy)
texture.m_LightmapFormat = tempField.AsInt;
if (!(tempField = baseField["m_ColorSpace"]).IsDummy)
texture.m_ColorSpace = tempField.AsInt;
AssetTypeValueField imageData = baseField["image data"];
if (imageData.TemplateField.ValueType == AssetValueType.ByteArray)
{
texture.pictureData = imageData.AsByteArray;
}
else
{
int imageDataSize = imageData.Children.Count;
texture.pictureData = new byte[imageDataSize];
for (int i = 0; i < imageDataSize; i++)
{
texture.pictureData[i] = (byte)imageData[i].AsInt;
}
}
AssetTypeValueField streamData;
if (!(streamData = baseField["m_StreamData"]).IsDummy)
{
texture.m_StreamData.offset = streamData["offset"].AsULong;
texture.m_StreamData.size = streamData["size"].AsUInt;
texture.m_StreamData.path = streamData["path"].AsString;
}
return texture;
}
public void WriteTo(AssetTypeValueField baseField)
{
AssetTypeValueField tempField;
baseField["m_Name"].AsString = m_Name;
if (!(tempField = baseField["m_ForcedFallbackFormat"]).IsDummy)
tempField.AsInt = m_ForcedFallbackFormat;
if (!(tempField = baseField["m_DownscaleFallback"]).IsDummy)
tempField.AsBool = m_DownscaleFallback;
baseField["m_Width"].AsInt = m_Width;
baseField["m_Height"].AsInt = m_Height;
if (!(tempField = baseField["m_CompleteImageSize"]).IsDummy)
tempField.AsInt = m_CompleteImageSize;
baseField["m_TextureFormat"].AsInt = m_TextureFormat;
if (!(tempField = baseField["m_MipCount"]).IsDummy)
tempField.AsInt = m_MipCount;
if (!(tempField = baseField["m_MipMap"]).IsDummy)
tempField.AsBool = m_MipMap;
baseField["m_IsReadable"].AsBool = m_IsReadable;
if (!(tempField = baseField["m_ReadAllowed"]).IsDummy)
tempField.AsBool = m_ReadAllowed;
if (!(tempField = baseField["m_StreamingMipmaps"]).IsDummy)
tempField.AsBool = m_StreamingMipmaps;
if (!(tempField = baseField["m_StreamingMipmapsPriority"]).IsDummy)
tempField.AsInt = m_StreamingMipmapsPriority;
baseField["m_ImageCount"].AsInt = m_ImageCount;
baseField["m_TextureDimension"].AsInt = m_TextureDimension;
AssetTypeValueField textureSettings = baseField["m_TextureSettings"];
textureSettings["m_FilterMode"].AsInt = m_TextureSettings.m_FilterMode;
textureSettings["m_Aniso"].AsInt = m_TextureSettings.m_Aniso;
textureSettings["m_MipBias"].AsFloat = m_TextureSettings.m_MipBias;
if (!(tempField = textureSettings["m_WrapMode"]).IsDummy)
tempField.AsInt = m_TextureSettings.m_WrapMode;
if (!(tempField = textureSettings["m_WrapU"]).IsDummy)
tempField.AsInt = m_TextureSettings.m_WrapU;
if (!(tempField = textureSettings["m_WrapV"]).IsDummy)
tempField.AsInt = m_TextureSettings.m_WrapV;
if (!(tempField = textureSettings["m_WrapW"]).IsDummy)
tempField.AsInt = m_TextureSettings.m_WrapW;
if (!(tempField = baseField["m_LightmapFormat"]).IsDummy)
tempField.AsInt = m_LightmapFormat;
if (!(tempField = baseField["m_ColorSpace"]).IsDummy)
tempField.AsInt = m_ColorSpace;
AssetTypeValueField imageData = baseField["image data"];
if (imageData.TemplateField.ValueType == AssetValueType.ByteArray)
{
imageData.AsByteArray = pictureData;
}
else
{
imageData.AsArray = new AssetTypeArrayInfo(pictureData.Length);
List<AssetTypeValueField> children = new List<AssetTypeValueField>(pictureData.Length);
for (int i = 0; i < pictureData.Length; i++)
{
AssetTypeValueField child = ValueBuilder.DefaultValueFieldFromArrayTemplate(imageData);
child.AsByte = pictureData[i];
children[i] = child;
}
imageData.Children = children;
}
AssetTypeValueField streamData;
if (!(streamData = baseField["m_StreamData"]).IsDummy)
{
streamData["offset"].AsULong = m_StreamData.offset;
streamData["size"].AsUInt = m_StreamData.size;
streamData["path"].AsString = m_StreamData.path;
}
}
public bool SetPictureDataFromBundle(BundleFileInstance inst)
{
StreamingInfo streamInfo = m_StreamData;
string searchPath = streamInfo.path;
if (streamInfo.path.StartsWith("archive:/"))
searchPath = searchPath.Substring(9);
searchPath = Path.GetFileName(searchPath);
AssetBundleFile bundle = inst.file;
AssetsFileReader reader = bundle.DataReader;
AssetBundleDirectoryInfo info = BundleHelper.GetDirInfo(inst.file, searchPath);
if (info == null)
{
return false;
}
reader.Position = info.Offset + (long)streamInfo.offset;
pictureData = reader.ReadBytes((int)streamInfo.size);
m_StreamData.offset = 0;
m_StreamData.size = 0;
m_StreamData.path = "";
return true;
}
public byte[] GetTextureData(AssetsFileInstance inst)
{
if (inst.parentBundle != null && m_StreamData.path != string.Empty)
{
SetPictureDataFromBundle(inst.parentBundle);
}
return GetTextureData(Path.GetDirectoryName(inst.path));
}
public byte[] GetTextureData(AssetsFile file)
{
string path = null;
if (file.Reader.BaseStream is FileStream fs)
{
path = Path.GetDirectoryName(fs.Name);
}
return GetTextureData(path);
}
public byte[] GetTextureData(string rootPath, bool useBgra = true)
{
if ((pictureData == null || pictureData.Length == 0) && !string.IsNullOrEmpty(m_StreamData.path) && m_StreamData.size != 0)
{
string fixedStreamPath = m_StreamData.path;
if (!Path.IsPathRooted(fixedStreamPath) && rootPath != null)
{
fixedStreamPath = Path.Combine(rootPath, fixedStreamPath);
}
if (File.Exists(fixedStreamPath))
{
using Stream stream = File.OpenRead(fixedStreamPath);
stream.Position = (long)m_StreamData.offset;
pictureData = new byte[m_StreamData.size];
stream.Read(pictureData, 0, (int)m_StreamData.size);
}
else
{
return null;
}
}
return DecodeManaged(pictureData, (TextureFormat)m_TextureFormat, m_Width, m_Height, useBgra);
}
public byte[] GetTextureData(string rootPath, AssetBundleFile bundle)
{
if ((pictureData == null || pictureData.Length == 0) && m_StreamData.path != null && m_StreamData.path.StartsWith("archive:/") && bundle != null)
{
string resourceFileName = m_StreamData.path.Split('/').Last();
int resourceFileIndex = bundle.GetFileIndex(resourceFileName);
if (resourceFileIndex >= 0)
{
bundle.GetFileRange(resourceFileIndex, out long resourceFileOffset, out _);
pictureData = new byte[m_StreamData.size];
bundle.Reader.Position = resourceFileOffset + (long)m_StreamData.offset;
bundle.Reader.Read(pictureData, 0, pictureData.Length);
}
}
return GetTextureData(rootPath);
}
public void SetTextureData(byte[] bgra, int width, int height)
{
byte[] encoded = Encode(bgra, (TextureFormat)m_TextureFormat, width, height);
if (encoded == null)
throw new NotSupportedException("The current texture format is not supported for encoding.");
SetTextureDataRaw(encoded, width, height);
}
public void SetTextureDataRaw(byte[] encodedData, int width, int height)
{
m_Width = width;
m_Height = height;
m_StreamData.path = "";
m_StreamData.offset = 0;
m_StreamData.size = 0;
pictureData = encodedData;
m_CompleteImageSize = encodedData.Length;
}
public static byte[] DecodeManaged(byte[] data, TextureFormat format, int width, int height, bool useBgra = true)
{
byte[] output = Array.Empty<byte>();
int size = format switch
{
TextureFormat.Alpha8 => RgbConverter.Convert<ColorA<byte>, byte, ColorBGRA32, byte>(data, width, height, out output),
TextureFormat.ARGB4444 => RgbConverter.Convert<ColorARGB16, byte, ColorBGRA32, byte>(data, width, height, out output),
TextureFormat.RGB24 => RgbConverter.Convert<ColorRGB<byte>, byte, ColorBGRA32, byte>(data, width, height, out output),
TextureFormat.RGBA32 => RgbConverter.Convert<ColorRGBA<byte>, byte, ColorBGRA32, byte>(data, width, height, out output),
TextureFormat.ARGB32 => RgbConverter.Convert<ColorARGB32, byte, ColorBGRA32, byte>(data, width, height, out output),
TextureFormat.R16 => RgbConverter.Convert<ColorR<ushort>, ushort, ColorBGRA32, byte>(data, width, height, out output),
TextureFormat.RGBA4444 => RgbConverter.Convert<ColorRGBA16, byte, ColorBGRA32, byte>(data, width, height, out output),
TextureFormat.BGRA32 => data.Length,
TextureFormat.RG16 => RgbConverter.Convert<ColorR<byte>, byte, ColorBGRA32, byte>(data, width, height, out output),
TextureFormat.R8 => RgbConverter.Convert<ColorR<byte>, byte, ColorBGRA32, byte>(data, width, height, out output),
TextureFormat.RHalf => RgbConverter.Convert<ColorR<Half>, Half, ColorBGRA32, byte>(data, width, height, out output),
TextureFormat.RGHalf => RgbConverter.Convert<ColorRG<Half>, Half, ColorBGRA32, byte>(data, width, height, out output),
TextureFormat.RGBAHalf => RgbConverter.Convert<ColorRGBA<Half>, Half, ColorBGRA32, byte>(data, width, height, out output),
TextureFormat.RFloat => RgbConverter.Convert<ColorR<float>, float, ColorBGRA32, byte>(data, width, height, out output),
TextureFormat.RGFloat => RgbConverter.Convert<ColorRG<float>, float, ColorBGRA32, byte>(data, width, height, out output),
TextureFormat.RGBAFloat => RgbConverter.Convert<ColorRGBA<float>, float, ColorBGRA32, byte>(data, width, height, out output),
TextureFormat.RGB9e5Float => RgbConverter.Convert<ColorRGB9e5, double, ColorBGRA32, byte>(data, width, height, out output),
TextureFormat.RG32 => RgbConverter.Convert<ColorRG<ushort>, ushort, ColorBGRA32, byte>(data, width, height, out output),
TextureFormat.RGB48 => RgbConverter.Convert<ColorRGB<ushort>, ushort, ColorBGRA32, byte>(data, width, height, out output),
TextureFormat.RGBA64 => RgbConverter.Convert<ColorRGBA<ushort>, ushort, ColorBGRA32, byte>(data, width, height, out output),
TextureFormat.DXT1 => DxtDecoder.DecompressDXT1(data, width, height, out output),
TextureFormat.DXT3 => DxtDecoder.DecompressDXT3(data, width, height, out output),
TextureFormat.DXT5 => DxtDecoder.DecompressDXT5(data, width, height, out output),
TextureFormat.BC4 => Bc4.Decompress(data, width, height, out output),
TextureFormat.BC5 => Bc5.Decompress(data, width, height, out output),
TextureFormat.BC6H => Bc6h.Decompress(data, width, height, false, out output),
TextureFormat.BC7 => Bc7.Decompress(data, width, height, out output),
TextureFormat.ETC_RGB4 => EtcDecoder.DecompressETC(data, width, height, out output),
TextureFormat.ETC2_RGB4 => EtcDecoder.DecompressETC2(data, width, height, out output),
TextureFormat.ETC2_RGBA1 => EtcDecoder.DecompressETC2A1(data, width, height, out output),
TextureFormat.ETC2_RGBA8 => EtcDecoder.DecompressETC2A8(data, width, height, out output),
TextureFormat.EAC_R => EtcDecoder.DecompressEACRUnsigned(data, width, height, out output),
TextureFormat.EAC_R_SIGNED => EtcDecoder.DecompressEACRSigned(data, width, height, out output),
TextureFormat.EAC_RG => EtcDecoder.DecompressEACRGUnsigned(data, width, height, out output),
TextureFormat.EAC_RG_SIGNED => EtcDecoder.DecompressEACRGSigned(data, width, height, out output),
TextureFormat.ASTC_RGB_4x4 or
TextureFormat.ASTC_RGBA_4x4 => AstcDecoder.DecodeASTC(data, width, height, 4, 4, out output),
TextureFormat.ASTC_RGB_5x5 or
TextureFormat.ASTC_RGBA_5x5 => AstcDecoder.DecodeASTC(data, width, height, 5, 5, out output),
TextureFormat.ASTC_RGB_6x6 or
TextureFormat.ASTC_RGBA_6x6 => AstcDecoder.DecodeASTC(data, width, height, 6, 6, out output),
TextureFormat.ASTC_RGB_8x8 or
TextureFormat.ASTC_RGBA_8x8 => AstcDecoder.DecodeASTC(data, width, height, 8, 8, out output),
TextureFormat.ASTC_RGB_10x10 or
TextureFormat.ASTC_RGBA_10x10 => AstcDecoder.DecodeASTC(data, width, height, 10, 10, out output),
TextureFormat.ASTC_RGB_12x12 or
TextureFormat.ASTC_RGBA_12x12 => AstcDecoder.DecodeASTC(data, width, height, 12, 12, out output),
TextureFormat.ATC_RGB4 => AtcDecoder.DecompressAtcRgb4(data, width, height, out output),
TextureFormat.ATC_RGBA8 => AtcDecoder.DecompressAtcRgba8(data, width, height, out output),
TextureFormat.PVRTC_RGB2 or
TextureFormat.PVRTC_RGBA2 => PvrtcDecoder.DecompressPVRTC(data, width, height, true, out output),
TextureFormat.PVRTC_RGB4 or
TextureFormat.PVRTC_RGBA4 => PvrtcDecoder.DecompressPVRTC(data, width, height, false, out output),
TextureFormat.YUY2 => Yuy2Decoder.DecompressYUY2(data, width, height, out output),
_ => 0
};
if (size == 0)
return null;
return output;
}
public static byte[] Encode(byte[] data, TextureFormat format, int width, int height)
{
return format switch
{
TextureFormat.R8 => RGBAEncoders.EncodeR8(data, width, height),
TextureFormat.R16 => RGBAEncoders.EncodeR16(data, width, height),
TextureFormat.RG16 => RGBAEncoders.EncodeRG16(data, width, height),
TextureFormat.RGB24 => RGBAEncoders.EncodeRGB24(data, width, height),
TextureFormat.RGB565 => RGBAEncoders.EncodeRGB565(data, width, height),
TextureFormat.RGBA32 => RGBAEncoders.EncodeRGBA32(data, width, height),
TextureFormat.ARGB32 => RGBAEncoders.EncodeARGB32(data, width, height),
TextureFormat.RGBA4444 => RGBAEncoders.EncodeRGBA4444(data, width, height),
TextureFormat.ARGB4444 => RGBAEncoders.EncodeARGB4444(data, width, height),
TextureFormat.Alpha8 => RGBAEncoders.EncodeAlpha8(data, width, height),
TextureFormat.RHalf => RGBAEncoders.EncodeRHalf(data, width, height),
TextureFormat.RGHalf => RGBAEncoders.EncodeRGHalf(data, width, height),
TextureFormat.RGBAHalf => RGBAEncoders.EncodeRGBAHalf(data, width, height),
_ => null
};
}
}
}