Advertisement
Guest User

Untitled

a guest
Feb 20th, 2020
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.77 KB | None | 0 0
  1.         public void Read()
  2.         {
  3. //            Chunks = new List<Chunk>();
  4.  
  5.             while (Reader.BaseStream.Position < Reader.BaseStream.Length)
  6.             {
  7.                 var size = Reader.ReadInt32();
  8.                 var chunk = new Chunk();
  9.                 chunk.Read(Reader, size);
  10.  
  11.                 Chunks.Add(chunk);
  12.             }
  13.         }
  14.  
  15.         public void DumpTextures(string folderPath)
  16.         {
  17.             if (!Directory.Exists(folderPath))
  18.                 Directory.CreateDirectory(folderPath);
  19.  
  20.             //Map textures to the font it's representing
  21.             var fontDictionary = new Dictionary<Chunk, Chunk>();
  22.             for (var i = 0; i < Chunks.Count; i++)
  23.             {
  24.                 var chunk = Chunks[i];
  25.                 if (chunk.IsFontDesc)
  26.                 {
  27.                     fontDictionary[Chunks[i - 1]] = chunk;
  28.                 }
  29.             }
  30.  
  31.             Console.WriteLine("Chunks: " + Chunks.Count);
  32.            
  33. //            Console.WriteLine();
  34. //            Console.WriteLine(Filename + "'s procedural textures:");
  35. //            foreach (var chunk in Chunks.Where(c => c.GetTextureType() == TextureType.Procedural))
  36. //            {
  37. //                Console.WriteLine("Texture at " + chunk.Offset);
  38. //            }
  39. //
  40. //            Console.WriteLine(Filename + "'s SpriteGen textures:");
  41. //            foreach (var chunk in Chunks.Where(c => c.GetTextureType() == TextureType.SpriteGen))
  42. //            {
  43. //                Console.WriteLine("Texture at " + chunk.Offset);
  44. //            }
  45. //
  46. //            Console.WriteLine(Filename + "'s animated textures:");
  47. //            foreach (var chunk in Chunks.Where(c => c.GetTextureType() == TextureType.Animated))
  48. //            {
  49. //                Console.WriteLine("Texture at " + chunk.Offset);
  50. //            }
  51. //            Console.WriteLine();
  52.  
  53.             var palettes = Chunks.Where(c => c.IsPalette).ToList();
  54.             var textures = Chunks.Where(c =>
  55.             {
  56.                 var type = c.GetTextureType();
  57.                 return (type == TextureType.Palette8Bit || type == TextureType.Palette4Bit) && c.Data != null;
  58.             }).ToList();
  59.  
  60.             var tgaList = Chunks.Where(c => c.GetTextureType() == TextureType.TGA && c.Data != null).ToList();
  61.             textures.AddRange(tgaList);
  62.  
  63.             var links = Chunks.Where(c => c.GetTextureType() == TextureType.PaletteLink).ToList();
  64.             var paletteKeys = new List<string>();
  65.             foreach (var link in links)
  66.             {
  67.                 using (var linkReader = new BinaryReader(new MemoryStream(link.Data)))
  68.                 {
  69.                     var textureKey = linkReader.ReadInt32();
  70.                     var paletteKey = linkReader.ReadInt32();
  71.  
  72.                     paletteKeys.Add(paletteKey.ToString("X8"));
  73.                 }
  74.             }
  75.  
  76.             var linkedPalletes = new Dictionary<string, Chunk>();
  77.             var distinctPaletteKeys = paletteKeys.Distinct().ToList();
  78.             for (var i = 0; i < distinctPaletteKeys.Count; i++)
  79.             {
  80.                 try
  81.                 {
  82.                     linkedPalletes[distinctPaletteKeys[i]] = palettes[i];
  83.                 }
  84.                 catch (Exception e)
  85.                 {
  86.                     Console.WriteLine("palettes.Count: " + palettes.Count);
  87.                     Console.WriteLine("palettes[0].Offset: " + palettes[0].Offset);
  88.                     Console.WriteLine("Filename: " + Filename);
  89.                     throw;
  90.                 }
  91.             }
  92.  
  93.             var remainingTextures = Chunks.Where(c =>
  94.             {
  95.                 var type = c.GetTextureType();
  96.                 return (type != TextureType.Palette8Bit && type != TextureType.Palette4Bit &&
  97.                         type != TextureType.TGA && type != TextureType.PaletteLink &&
  98.                         !c.IsPalette && !c.IsFontDesc) && c.Data != null;
  99.             }).ToList();
  100.  
  101.             foreach (var remainingTexture in remainingTextures)
  102.             {
  103.                 remainingTexture.Filename = Chunks.IndexOf(remainingTexture) + "_" + remainingTexture.GetTextureType() + ".bin";
  104.                 File.WriteAllBytes(folderPath + remainingTexture.Filename, remainingTexture.Data);
  105.             }
  106.  
  107.             for (int i = 0; i < paletteKeys.Count + tgaList.Count; i++)
  108.             {
  109.                 var texture = textures[i];
  110.                 var textureType = texture.GetTextureType();
  111.  
  112.                 texture.Filename = Chunks.IndexOf(texture) + "_" + textureType + ".tga";
  113.  
  114.                 if (fontDictionary.TryGetValue(texture, out var fontDesc))
  115.                 {
  116.                     fontDesc.Filename = Chunks.IndexOf(texture) + "_FONTDESC.bin";
  117.                     File.WriteAllBytes(folderPath + fontDesc.Filename, fontDesc.Data);
  118.                 }
  119.  
  120.                 if (textureType == TextureType.TGA)
  121.                 {
  122.                     File.WriteAllBytes(folderPath + texture.Filename, texture.Data);
  123.                     continue;
  124.                 }
  125.  
  126.                 if (i >= paletteKeys.Count)
  127.                 {
  128.                     Console.WriteLine("No palette: " + i);
  129.                     File.WriteAllBytes(folderPath + i + ".bin", texture.Data);
  130.                     continue;
  131.                 }
  132.  
  133.                 var linkedPalette = linkedPalletes[paletteKeys[i]];
  134.                 var usesRGBA = linkedPalette.Data.Length == 0x400 || linkedPalette.Data.Length == 0x40;
  135.  
  136.                 if (linkedPalette.Data.Length != 0x300 && linkedPalette.Data.Length != 0x400 && linkedPalette.Data.Length != 0x30 && linkedPalette.Data.Length != 0x40)
  137.                 {
  138.                     Console.WriteLine("Weird palette: " + texture.Unk3.Hex() + " | FontDesc: " + linkedPalette.IsFontDesc);
  139.  
  140.                     File.WriteAllBytes(folderPath + i + ".bin", texture.Data);
  141.                     File.WriteAllBytes(folderPath + i + "_palette_" + linkedPalette.IsFontDesc + ".bin", linkedPalette.Data);
  142.                     continue;
  143.                 }
  144.  
  145.                 //Parse the palettes
  146.                 var palList = new List<RGBA>();
  147.                 using (var reader = new BinaryReader(new MemoryStream(linkedPalette.Data)))
  148.                 {
  149.                     while (reader.BaseStream.Position < reader.BaseStream.Length)
  150.                     {
  151.                         palList.Add(new RGBA {B = reader.ReadByte(), G = reader.ReadByte(), R = reader.ReadByte(), A = usesRGBA ? reader.ReadByte() : (byte?) null});
  152.                     }
  153.                 }
  154.  
  155.                 using (var writer = new BinaryWriter(File.OpenWrite(folderPath + texture.Filename)))
  156.                 {
  157.                     //TGAHeader
  158.                     writer.Write((byte) 0x00); //IDLength
  159.                     writer.Write((byte) 0x00); //ColorMapType
  160.                     writer.Write((byte) 0x02); //ImageType (Uncompressed, True-color Image)
  161.  
  162.                     //ColorMapSpecification
  163.                     writer.Write((short) 0x00); //FirstIndexEntry
  164.                     writer.Write((short) 0x00); //ColorMapLength
  165.                     writer.Write((byte) 0x00); //ColorMapEntrySize
  166.  
  167.                     //ImageSpecification
  168.                     writer.Write((short) 0); //XOrigin
  169.                     writer.Write((short) 0); //YOrigin
  170.                     writer.Write(texture.Width); //Width
  171.                     writer.Write(texture.Height); //Height
  172.                     writer.Write((byte) 32); //PixelDepth
  173.                     writer.Write((byte) 8); //ImageDescriptor
  174.  
  175.                     //ImageData
  176.                     using (var reader = new BinaryReader(new MemoryStream(texture.Data)))
  177.                     {
  178.                         while (reader.BaseStream.Position < reader.BaseStream.Length)
  179.                         {
  180.                             var index = reader.ReadByte();
  181.                             RGBA rgba;
  182.  
  183.                             if (textureType == TextureType.Palette4Bit)
  184.                             {
  185.                                 var index1 = (index & 0b11110000) >> 4;
  186.                                 var index2 = (index & 0b00001111);
  187.  
  188.                                 rgba = palList[index1];
  189.                                 writer.Write(rgba.B);
  190.                                 writer.Write(rgba.G);
  191.                                 writer.Write(rgba.R);
  192.                                 writer.Write(usesRGBA ? (byte) rgba.A : (byte) 0xFF);
  193.  
  194.                                 var rgba2 = palList[index2];
  195.                                 writer.Write(rgba2.B);
  196.                                 writer.Write(rgba2.G);
  197.                                 writer.Write(rgba2.R);
  198.                                 writer.Write(usesRGBA ? (byte) rgba2.A : (byte) 0xFF);
  199.                                 continue;
  200.                             }
  201.  
  202.                             rgba = palList[index];
  203.                             writer.Write(rgba.B);
  204.                             writer.Write(rgba.G);
  205.                             writer.Write(rgba.R);
  206.                             writer.Write(usesRGBA ? (byte) rgba.A : (byte) 0xFF);
  207.                         }
  208.                     }
  209.                 }
  210.             }
  211.  
  212. //            var paletteLinks = new List<Chunk>(Chunks).Where(c => c.GetTextureType() == TextureType.PaletteLink).ToList();
  213. //            paletteLinks.ForEach(chunk => chunk.TextureType = (short) TextureType.TGA);
  214.  
  215.             var importantChunks = Chunks.Where(c => c.GetTextureType() != TextureType.PaletteLink).ToList();
  216. //            importantChunks.AddRange(paletteLinks);
  217.             importantChunks.ForEach(chunk => chunk.Index = Chunks.IndexOf(chunk));
  218.  
  219.             var infoJSON = JsonConvert.SerializeObject(importantChunks, Formatting.Indented);
  220.             File.WriteAllText(folderPath + "Info.json", infoJSON);
  221.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement