Advertisement
Guest User

Untitled

a guest
Apr 29th, 2017
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.30 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7.  
  8. namespace PluginTexturesWV
  9. {
  10.     public class TextureAsset
  11.     {
  12.         public enum TextureType
  13.         {
  14.             Texture2D = 0,
  15.             TextureCube = 1,
  16.             Texture3D = 2,
  17.             Texture2DArray = 3,
  18.             Texture1DArray = 4,
  19.             Texture1D = 5
  20.         }
  21.         public TextureType formatType;
  22.         public int formatID;
  23.         public ushort width;
  24.         public ushort height;
  25.         public ushort depth;
  26.         public ushort sliceCount;
  27.         public byte firstMip;
  28.         public int mipCount;
  29.         public List<int> mipDataSizes;
  30.         public byte[] chunkid;
  31.         public byte[] data;
  32.  
  33.         public TextureAsset(byte[] buff)
  34.         {
  35.             data = buff;
  36.             ReadData();
  37.         }
  38.  
  39.         private void ReadData()
  40.         {
  41.             MemoryStream m = new MemoryStream(data);
  42.             m.Seek(8, 0);
  43.             formatType = (TextureType)Helpers.ReadInt(m);
  44.             formatID = Helpers.ReadInt(m);
  45.             m.Seek(6, SeekOrigin.Current);
  46.             width = Helpers.ReadUShort(m);
  47.             height = Helpers.ReadUShort(m);
  48.             depth = Helpers.ReadUShort(m);
  49.             sliceCount = Helpers.ReadUShort(m);
  50.             mipCount = m.ReadByte();
  51.             firstMip = (byte)m.ReadByte();
  52.             chunkid = new byte[16];
  53.             m.Read(chunkid, 0, 16);
  54.             mipDataSizes = new List<int>();
  55.             for (int i = 0; i < Math.Min(mipCount, 14); i++)
  56.                 mipDataSizes.Add(Helpers.ReadInt(m));
  57.         }
  58.  
  59.         public static int[] KnownFormats = { 0x12, 0x36, 0x37, 0x3C };
  60.  
  61.         public bool isKnownFormat()
  62.         {
  63.             if (KnownFormats.Contains<int>(formatID))
  64.                 return true;
  65.             return false;
  66.         }
  67.  
  68.         public uint MakeFourCC(string input)
  69.         {
  70.             byte[] buff = new byte[4];
  71.             for (int i = 0; i < 4; i++)
  72.                 buff[i] = (byte)input[i];
  73.             return BitConverter.ToUInt32(buff, 0);
  74.         }
  75.  
  76.         public uint MakeFlags(bool hasPitch, bool hasMips, bool hasLinearSize, bool hasDepth)
  77.         {
  78.             uint result = 0x1007;
  79.             if (hasPitch)       result |= 0x8;
  80.             if (hasMips)        result |= 0x20000;
  81.             if (hasLinearSize)  result |= 0x80000;
  82.             if (hasDepth)       result |= 0x800000;
  83.             return result;
  84.         }
  85.  
  86.         public uint MakeFormatFlags(bool alphaPixel, bool alpha, bool fourCC, bool rgb, bool yuv, bool luminance)
  87.         {
  88.             uint result = 0;
  89.             if (alphaPixel) result |= 0x1;
  90.             if (alpha)      result |= 0x2;
  91.             if (fourCC)     result |= 0x4;
  92.             if (rgb)        result |= 0x40;
  93.             if (yuv)        result |= 0x200;
  94.             if (luminance)  result |= 0x20000;
  95.             return result;
  96.         }
  97.  
  98.         public uint MakeDX10Flags(bool isCube)
  99.         {
  100.             if (isCube)
  101.                 return 4;
  102.             else
  103.                 return 0;
  104.         }
  105.  
  106.         public uint MakeCaps(bool complex, bool hasMips)
  107.         {
  108.             uint result = 0x1000;
  109.             if (complex) result |= 0x8;
  110.             if (hasMips) result |= 0x400000;
  111.             return result;
  112.         }
  113.  
  114.         public uint MakeCaps2(bool isCube, bool cubePosX, bool cubeNegX, bool cubePosY, bool cubeNegY, bool cubePosZ, bool cubeNegZ, bool isVolume)
  115.         {
  116.             uint result = 0;
  117.             if (isCube)   result |= 0x200;
  118.             if (cubePosX) result |= 0x400;
  119.             if (cubeNegX) result |= 0x800;
  120.             if (cubePosY) result |= 0x1000;
  121.             if (cubeNegY) result |= 0x2000;
  122.             if (cubePosZ) result |= 0x4000;
  123.             if (cubeNegZ) result |= 0x8000;
  124.             if (isVolume) result |= 0x200000;
  125.             return result;
  126.         }
  127.  
  128.         public void WriteMainData(Stream s, int headerSize, bool hasPitch, bool hasMips, bool hasLinearSize, bool hasDepth)
  129.         {
  130.             Helpers.WriteInt(s, 0x20534444);
  131.             Helpers.WriteInt(s, headerSize);
  132.             Helpers.WriteUInt(s, MakeFlags(hasPitch, hasMips, hasLinearSize, hasDepth));
  133.             int factor = (int)Math.Pow(2, firstMip);
  134.             Helpers.WriteInt(s, height / factor);
  135.             Helpers.WriteInt(s, width / factor);
  136.             Helpers.WriteInt(s, mipDataSizes[0]);
  137.             Helpers.WriteInt(s, depth);
  138.             Helpers.WriteInt(s, mipCount - firstMip);
  139.             for (int i = 0; i < 11; i++)
  140.                 Helpers.WriteInt(s, 0);
  141.         }
  142.  
  143.         public void WritePixelFormat(Stream s, uint size, uint flags, uint fourCC, uint bitCount, uint rMask, uint gMask, uint bMask, uint aMask)
  144.         {
  145.             Helpers.WriteUInt(s, size);
  146.             Helpers.WriteUInt(s, flags);
  147.             Helpers.WriteUInt(s, fourCC);
  148.             Helpers.WriteUInt(s, bitCount);
  149.             Helpers.WriteUInt(s, rMask);
  150.             Helpers.WriteUInt(s, gMask);
  151.             Helpers.WriteUInt(s, bMask);
  152.             Helpers.WriteUInt(s, aMask);
  153.         }
  154.  
  155.         public void WriteDX10Header(Stream s, uint dxgiFormat, uint dimension, uint flags, uint arraySize, uint flags2)
  156.         {
  157.             Helpers.WriteUInt(s, dxgiFormat);
  158.             Helpers.WriteUInt(s, dimension);
  159.             Helpers.WriteUInt(s, flags);
  160.             Helpers.WriteUInt(s, arraySize);
  161.             Helpers.WriteUInt(s, flags2);
  162.         }
  163.  
  164.         public void WriteDDSHeader(Stream s)
  165.         {
  166.             switch (formatID)
  167.             {
  168.                 case 0x12:                    
  169.                     WriteMainData(s, 124, false, true, false, false);
  170.                     WritePixelFormat(s, 32, MakeFormatFlags(false, true, false, true, false, false), 0, 32, 0xFF, 0xFF00, 0xFF0000, 0xFF000000);
  171.                     Helpers.WriteUInt(s, MakeCaps(false, true));
  172.                     Helpers.WriteUInt(s, MakeCaps2(false, false, false, false, false, false, false, false));
  173.                     break;
  174.                 case 0x36:
  175.                 case 0x37:
  176.                     WriteMainData(s, 124, false, true, false, false);
  177.                     WritePixelFormat(s, 32, MakeFormatFlags(false, false, true, false, false, false), MakeFourCC("DXT1"), 0, 0, 0, 0, 0);
  178.                     Helpers.WriteUInt(s, MakeCaps(false, true));
  179.                     Helpers.WriteUInt(s, MakeCaps2(false, false, false, false, false, false, false, false));
  180.                     break;
  181.                 case 0x3C:
  182.                     WriteMainData(s, 124, false, true, false, false);
  183.                     WritePixelFormat(s, 32, MakeFormatFlags(false, false, true, false, false, false), MakeFourCC("DX10"), 0, 0, 0, 0, 0);
  184.                     Helpers.WriteUInt(s, MakeCaps(false, true));
  185.                     Helpers.WriteUInt(s, MakeCaps2(false, false, false, false, false, false, false, false));
  186.                     break;
  187.             }
  188.             Helpers.WriteInt(s, 0);
  189.             Helpers.WriteInt(s, 0);
  190.             Helpers.WriteInt(s, 0);
  191.             switch (formatID)
  192.             {
  193.                 case 0x3C:
  194.                     WriteDX10Header(s, 1, 3, MakeDX10Flags(false), 0, 0);
  195.                     break;
  196.             }
  197.         }        
  198.     }
  199. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement