Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public void Read()
- {
- // Chunks = new List<Chunk>();
- while (Reader.BaseStream.Position < Reader.BaseStream.Length)
- {
- var size = Reader.ReadInt32();
- var chunk = new Chunk();
- chunk.Read(Reader, size);
- Chunks.Add(chunk);
- }
- }
- public void DumpTextures(string folderPath)
- {
- if (!Directory.Exists(folderPath))
- Directory.CreateDirectory(folderPath);
- //Map textures to the font it's representing
- var fontDictionary = new Dictionary<Chunk, Chunk>();
- for (var i = 0; i < Chunks.Count; i++)
- {
- var chunk = Chunks[i];
- if (chunk.IsFontDesc)
- {
- fontDictionary[Chunks[i - 1]] = chunk;
- }
- }
- Console.WriteLine("Chunks: " + Chunks.Count);
- // Console.WriteLine();
- // Console.WriteLine(Filename + "'s procedural textures:");
- // foreach (var chunk in Chunks.Where(c => c.GetTextureType() == TextureType.Procedural))
- // {
- // Console.WriteLine("Texture at " + chunk.Offset);
- // }
- //
- // Console.WriteLine(Filename + "'s SpriteGen textures:");
- // foreach (var chunk in Chunks.Where(c => c.GetTextureType() == TextureType.SpriteGen))
- // {
- // Console.WriteLine("Texture at " + chunk.Offset);
- // }
- //
- // Console.WriteLine(Filename + "'s animated textures:");
- // foreach (var chunk in Chunks.Where(c => c.GetTextureType() == TextureType.Animated))
- // {
- // Console.WriteLine("Texture at " + chunk.Offset);
- // }
- // Console.WriteLine();
- var palettes = Chunks.Where(c => c.IsPalette).ToList();
- var textures = Chunks.Where(c =>
- {
- var type = c.GetTextureType();
- return (type == TextureType.Palette8Bit || type == TextureType.Palette4Bit) && c.Data != null;
- }).ToList();
- var tgaList = Chunks.Where(c => c.GetTextureType() == TextureType.TGA && c.Data != null).ToList();
- textures.AddRange(tgaList);
- var links = Chunks.Where(c => c.GetTextureType() == TextureType.PaletteLink).ToList();
- var paletteKeys = new List<string>();
- foreach (var link in links)
- {
- using (var linkReader = new BinaryReader(new MemoryStream(link.Data)))
- {
- var textureKey = linkReader.ReadInt32();
- var paletteKey = linkReader.ReadInt32();
- paletteKeys.Add(paletteKey.ToString("X8"));
- }
- }
- var linkedPalletes = new Dictionary<string, Chunk>();
- var distinctPaletteKeys = paletteKeys.Distinct().ToList();
- for (var i = 0; i < distinctPaletteKeys.Count; i++)
- {
- try
- {
- linkedPalletes[distinctPaletteKeys[i]] = palettes[i];
- }
- catch (Exception e)
- {
- Console.WriteLine("palettes.Count: " + palettes.Count);
- Console.WriteLine("palettes[0].Offset: " + palettes[0].Offset);
- Console.WriteLine("Filename: " + Filename);
- throw;
- }
- }
- var remainingTextures = Chunks.Where(c =>
- {
- var type = c.GetTextureType();
- return (type != TextureType.Palette8Bit && type != TextureType.Palette4Bit &&
- type != TextureType.TGA && type != TextureType.PaletteLink &&
- !c.IsPalette && !c.IsFontDesc) && c.Data != null;
- }).ToList();
- foreach (var remainingTexture in remainingTextures)
- {
- remainingTexture.Filename = Chunks.IndexOf(remainingTexture) + "_" + remainingTexture.GetTextureType() + ".bin";
- File.WriteAllBytes(folderPath + remainingTexture.Filename, remainingTexture.Data);
- }
- for (int i = 0; i < paletteKeys.Count + tgaList.Count; i++)
- {
- var texture = textures[i];
- var textureType = texture.GetTextureType();
- texture.Filename = Chunks.IndexOf(texture) + "_" + textureType + ".tga";
- if (fontDictionary.TryGetValue(texture, out var fontDesc))
- {
- fontDesc.Filename = Chunks.IndexOf(texture) + "_FONTDESC.bin";
- File.WriteAllBytes(folderPath + fontDesc.Filename, fontDesc.Data);
- }
- if (textureType == TextureType.TGA)
- {
- File.WriteAllBytes(folderPath + texture.Filename, texture.Data);
- continue;
- }
- if (i >= paletteKeys.Count)
- {
- Console.WriteLine("No palette: " + i);
- File.WriteAllBytes(folderPath + i + ".bin", texture.Data);
- continue;
- }
- var linkedPalette = linkedPalletes[paletteKeys[i]];
- var usesRGBA = linkedPalette.Data.Length == 0x400 || linkedPalette.Data.Length == 0x40;
- if (linkedPalette.Data.Length != 0x300 && linkedPalette.Data.Length != 0x400 && linkedPalette.Data.Length != 0x30 && linkedPalette.Data.Length != 0x40)
- {
- Console.WriteLine("Weird palette: " + texture.Unk3.Hex() + " | FontDesc: " + linkedPalette.IsFontDesc);
- File.WriteAllBytes(folderPath + i + ".bin", texture.Data);
- File.WriteAllBytes(folderPath + i + "_palette_" + linkedPalette.IsFontDesc + ".bin", linkedPalette.Data);
- continue;
- }
- //Parse the palettes
- var palList = new List<RGBA>();
- using (var reader = new BinaryReader(new MemoryStream(linkedPalette.Data)))
- {
- while (reader.BaseStream.Position < reader.BaseStream.Length)
- {
- palList.Add(new RGBA {B = reader.ReadByte(), G = reader.ReadByte(), R = reader.ReadByte(), A = usesRGBA ? reader.ReadByte() : (byte?) null});
- }
- }
- using (var writer = new BinaryWriter(File.OpenWrite(folderPath + texture.Filename)))
- {
- //TGAHeader
- writer.Write((byte) 0x00); //IDLength
- writer.Write((byte) 0x00); //ColorMapType
- writer.Write((byte) 0x02); //ImageType (Uncompressed, True-color Image)
- //ColorMapSpecification
- writer.Write((short) 0x00); //FirstIndexEntry
- writer.Write((short) 0x00); //ColorMapLength
- writer.Write((byte) 0x00); //ColorMapEntrySize
- //ImageSpecification
- writer.Write((short) 0); //XOrigin
- writer.Write((short) 0); //YOrigin
- writer.Write(texture.Width); //Width
- writer.Write(texture.Height); //Height
- writer.Write((byte) 32); //PixelDepth
- writer.Write((byte) 8); //ImageDescriptor
- //ImageData
- using (var reader = new BinaryReader(new MemoryStream(texture.Data)))
- {
- while (reader.BaseStream.Position < reader.BaseStream.Length)
- {
- var index = reader.ReadByte();
- RGBA rgba;
- if (textureType == TextureType.Palette4Bit)
- {
- var index1 = (index & 0b11110000) >> 4;
- var index2 = (index & 0b00001111);
- rgba = palList[index1];
- writer.Write(rgba.B);
- writer.Write(rgba.G);
- writer.Write(rgba.R);
- writer.Write(usesRGBA ? (byte) rgba.A : (byte) 0xFF);
- var rgba2 = palList[index2];
- writer.Write(rgba2.B);
- writer.Write(rgba2.G);
- writer.Write(rgba2.R);
- writer.Write(usesRGBA ? (byte) rgba2.A : (byte) 0xFF);
- continue;
- }
- rgba = palList[index];
- writer.Write(rgba.B);
- writer.Write(rgba.G);
- writer.Write(rgba.R);
- writer.Write(usesRGBA ? (byte) rgba.A : (byte) 0xFF);
- }
- }
- }
- }
- // var paletteLinks = new List<Chunk>(Chunks).Where(c => c.GetTextureType() == TextureType.PaletteLink).ToList();
- // paletteLinks.ForEach(chunk => chunk.TextureType = (short) TextureType.TGA);
- var importantChunks = Chunks.Where(c => c.GetTextureType() != TextureType.PaletteLink).ToList();
- // importantChunks.AddRange(paletteLinks);
- importantChunks.ForEach(chunk => chunk.Index = Chunks.IndexOf(chunk));
- var infoJSON = JsonConvert.SerializeObject(importantChunks, Formatting.Indented);
- File.WriteAllText(folderPath + "Info.json", infoJSON);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement