Advertisement
Craxic

TransistorTest

Jan 2nd, 2015
1,094
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 15.14 KB | None | 0 0
  1. // LICENCE: A lot of this was decompiled from https://github.com/mono/MonoGame so its licened under the Microsoft Public License (Ms-PL). Everything else: http://www.wtfpl.net/txt/copying/
  2.  
  3. using System;
  4. using System.Reflection;
  5. using Microsoft.Xna.Framework;
  6. using Microsoft.Xna.Framework.Content;
  7. using System.Collections.Generic;
  8. using System.IO;
  9. using System.Runtime.InteropServices;
  10. using Microsoft.Xna.Framework.Graphics;
  11. using System.Text.RegularExpressions;
  12. using System.Diagnostics;
  13.  
  14. namespace TransistorTest
  15. {
  16.     class DDSWriter {
  17.         const uint DDSD_CAPS        = 0x1;      // Required in every .dds file.
  18.         const uint DDSD_HEIGHT      = 0x2;      // Required in every .dds file.
  19.         const uint DDSD_WIDTH       = 0x4;      // Required in every .dds file.
  20.         const uint DDSD_PITCH       = 0x8;      // Required when pitch is provided for an uncompressed texture.
  21.         const uint DDSD_PIXELFORMAT = 0x1000;   // Required in every .dds file.
  22.         const uint DDSD_MIPMAPCOUNT = 0x20000;  // Required in a mipmapped texture.
  23.         const uint DDSD_LINEARSIZE  = 0x80000;  // Required when pitch is provided for a compressed texture.
  24.         const uint DDSD_DEPTH       = 0x800000; // Required in a depth texture.
  25.  
  26.         const uint DDPF_ALPHAPIXELS = 0x1;      // Texture contains alpha data; dwRGBAlphaBitMask contains valid data.
  27.         const uint DDPF_ALPHA       = 0x2;      // Used in some older DDS files for alpha channel only uncompressed data (dwRGBBitCount contains the alpha channel bitcount; dwABitMask contains valid data)
  28.         const uint DDPF_FOURCC      = 0x4;      // Texture contains compressed RGB data; dwFourCC contains valid data.
  29.         const uint DDPF_RGB         = 0x40;     // Texture contains uncompressed RGB data; dwRGBBitCount and the RGB masks (dwRBitMask, dwGBitMask, dwBBitMask) contain valid data.
  30.         const uint DDPF_YUV         = 0x200;    // Used in some older DDS files for YUV uncompressed data (dwRGBBitCount contains the YUV bit count; dwRBitMask contains the Y mask, dwGBitMask contains the U mask, dwBBitMask contains the V mask)
  31.         const uint DDPF_LUMINANCE   = 0x20000;  // Used in some older DDS files for single channel color uncompressed data (dwRGBBitCount contains the luminance channel bit count; dwRBitMask contains the channel mask). Can be combined with DDPF_ALPHAPIXELS for a two channel DDS file.
  32.        
  33.         const uint DDSCAPS_COMPLEX  = 0x8;      // Optional; must be used on any file that contains more than one surface (a mipmap, a cubic environment map, or mipmapped volume texture).
  34.         const uint DDSCAPS_MIPMAP   = 0x400000; // Optional; should be used for a mipmap.
  35.         const uint DDSCAPS_TEXTURE  = 0x1000;   // Required
  36.         public static void WriteDDS(string filename, uint width, uint height, byte[] textureData) {
  37.             FileStream fs = new FileStream(filename, FileMode.Create);
  38.             BinaryWriter bw = new BinaryWriter(fs);
  39.  
  40.             uint dwPitchOrLinearSize = 0x4000;
  41.  
  42.             bw.Write((uint)0x20534444); // dwMagic = 'DDS '
  43.             bw.Write((uint)124); // header.dwSize = 124
  44.             bw.Write(DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH | DDSD_PIXELFORMAT | DDSD_MIPMAPCOUNT | DDSD_LINEARSIZE); // header.dwFlags
  45.             bw.Write(height); // header.dwHeight
  46.             bw.Write(width); // header.dwWidth
  47.             bw.Write(dwPitchOrLinearSize); // header.dwPitchOrLinearSize
  48.             bw.Write((uint)0); // header.dwDepth
  49.             bw.Write((uint)0); // header.dwMipMapCount
  50.             // header.dwReserved1[11]
  51.             for (int i = 0; i < 11; i++) {
  52.                 bw.Write((uint)0);
  53.             }
  54.  
  55.             bw.Write((uint)32); // header.ddspf.dwSize
  56.             bw.Write(DDPF_FOURCC); // header.ddspf.dwFlags
  57.             bw.Write(System.Text.ASCIIEncoding.ASCII.GetBytes("DXT5")); // header.ddspf.dwFourCC
  58.             bw.Write((uint)0); // header.ddspf.dwRGBBitCount
  59.             bw.Write((uint)0x00000000); // header.ddspf.dwRBitMask
  60.             bw.Write((uint)0x00000000); // header.ddspf.dwGBitMask
  61.             bw.Write((uint)0x00000000); // header.ddspf.dwBBitMask
  62.             bw.Write((uint)0x00000000); // header.ddspf.dwABitMask
  63.            
  64.             bw.Write(DDSCAPS_TEXTURE); // header.ddspf.dwCaps
  65.             bw.Write((uint)0); // header.ddspf.dwCaps2
  66.             bw.Write((uint)0); // header.ddspf.dwCaps3
  67.             bw.Write((uint)0); // header.ddspf.dwCaps4
  68.             bw.Write((uint)0); // header.ddspf.dwReserved2
  69.            
  70.             //bw.Write((uint)0); // header.ddspf.bdata
  71.             //bw.Write((uint)0); // header.ddspf.bdata2
  72.  
  73.             //bw.Write((uint)0xFFFFFFFF);
  74.             bw.Write(textureData);
  75.         }
  76.     }
  77.  
  78.     abstract class  ContentTypeReader {
  79.         public abstract object Read(BinaryReader reader, string file);
  80.     }
  81.  
  82.     class RippedTexture2D : Texture2D {
  83.         public RippedTexture2D() : base() {
  84.  
  85.         }
  86.     }
  87.  
  88.     class Texture2DRipperReader : ContentTypeReader {
  89.         public override object Read(BinaryReader reader, string file)
  90.         {
  91.             SurfaceFormat pixelFormat = (SurfaceFormat)reader.ReadInt32();
  92.             int width = reader.ReadInt32();
  93.             int height = reader.ReadInt32();
  94.             reader.ReadInt32();
  95.             int num = reader.ReadInt32();
  96.             byte[] array = new byte[num];
  97.             int num2 = reader.Read(array, 0, num);
  98.  
  99.             string outfbland = MainClass.OUT_DIR + file.Replace("\\","/").Replace(":","");
  100.             string outf = outfbland + "." + width + "." + height + "." + pixelFormat.ToString();
  101.             System.IO.Directory.CreateDirectory(System.IO.Path.GetDirectoryName(outf));
  102.             System.IO.File.WriteAllBytes(outf, array);
  103.  
  104.             if (pixelFormat == SurfaceFormat.Dxt5) {
  105.                 DDSWriter.WriteDDS(outfbland + ".dds", (uint)width, (uint)height, array);
  106.                 Console.WriteLine("Ripped: " + outfbland + ".dds");
  107.             } else if (pixelFormat == SurfaceFormat.Color) {
  108.                 var bitmap = new System.Drawing.Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
  109.                 var locked = bitmap.LockBits(new System.Drawing.Rectangle(0, 0, width, height), System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
  110.                 if (array.Length != width * height * 4)
  111.                     throw new Exception();
  112.                 unsafe {
  113.                     byte* bytes = (byte*)locked.Scan0;
  114.                     for (int i=0; i<width * height * 4; i++) {
  115.                         bytes[i] = array[i];
  116.                     }
  117.                 }
  118.                 bitmap.UnlockBits(locked);
  119.                 bitmap.Save(outfbland + ".png");
  120.                 Console.WriteLine("Ripped: " + outfbland + ".png");
  121.             } else {
  122.                 Console.WriteLine("!!!FAILED!!!: " + pixelFormat + "");
  123.                 throw new Exception();
  124.             }
  125.  
  126.             return new RippedTexture2D();
  127.         }
  128.     }
  129.  
  130.     public sealed class ContentTypeReaderManager
  131.     {
  132.         //
  133.         // Fields
  134.         //
  135.         private ContentReader _reader;
  136.         private ContentTypeReader[] contentReaders;
  137.         //
  138.         // Constructors
  139.         //
  140.         public ContentTypeReaderManager(ContentReader reader)
  141.         {
  142.             this._reader = reader;
  143.         }
  144.         internal ContentTypeReader[] LoadAssetReaders()
  145.         {
  146.             int num = this._reader.Read7BitEncodedInt();
  147.             this.contentReaders = new ContentTypeReader[num];
  148.             for (int i = 0; i < num; i++)
  149.             {
  150.                 string text = this._reader.ReadString();
  151.                 if (text == "Microsoft.Xna.Framework.Content.Texture2DReader")
  152.                     this.contentReaders[i] = new Texture2DRipperReader();
  153.                 else
  154.                     throw new Exception();
  155.                 int num2 = this._reader.ReadInt32();
  156.             }
  157.             return this.contentReaders;
  158.         }
  159.     }
  160.  
  161.     public sealed class ContentReader : BinaryReader
  162.     {
  163.         //
  164.         // Fields
  165.         //
  166.         private ContentManager contentManager;
  167.         private ContentTypeReader[] typeReaders;
  168.         private List<KeyValuePair<int, Action<object>>> sharedResourceFixups;
  169.         private string assetName;
  170.         private GraphicsDevice graphicsDevice;
  171.         private ContentTypeReaderManager typeReaderManager;
  172.         //
  173.         // Properties
  174.         //
  175.         public string AssetName
  176.         {
  177.             get
  178.             {
  179.                 return this.assetName;
  180.             }
  181.         }
  182.         public ContentManager ContentManager
  183.         {
  184.             get
  185.             {
  186.                 return this.contentManager;
  187.             }
  188.         }
  189.         internal GraphicsDevice GraphicsDevice
  190.         {
  191.             get
  192.             {
  193.                 return this.graphicsDevice;
  194.             }
  195.         }
  196.         internal ContentTypeReader[] TypeReaders
  197.         {
  198.             get
  199.             {
  200.                 return this.typeReaders;
  201.             }
  202.         }
  203.         //
  204.         // Constructors
  205.         //
  206.         internal ContentReader(ContentManager manager, Stream stream, GraphicsDevice graphicsDevice, string assetName) : base(stream)
  207.         {
  208.             this.graphicsDevice = graphicsDevice;
  209.             this.contentManager = manager;
  210.             this.assetName = assetName;
  211.         }
  212.         //
  213.         // Methods
  214.         //
  215.         internal object ReadAsset<T>(string file) where T : class
  216.         {
  217.             object result = null;
  218.             this.typeReaderManager = new ContentTypeReaderManager(this);
  219.             this.typeReaders = this.typeReaderManager.LoadAssetReaders();
  220.             ContentTypeReader[] array = this.typeReaders;
  221.             //for (int i = 0; i < array.Length; i++)
  222.             //{
  223.                 //ContentTypeReader contentTypeReader = array[i];
  224.                 //contentTypeReader.Initialize(this.typeReaderManager);
  225.             //}
  226.             int num = base.Read7BitEncodedInt();
  227.             this.sharedResourceFixups = new List<KeyValuePair<int, Action<object>>>();
  228.             int num2 = base.Read7BitEncodedInt();
  229.             if (num2 > 0)
  230.             {
  231.                 result = this.typeReaders[num2 - 1].Read(this, file);
  232.             }
  233.             if (num > 0)
  234.             {
  235.                 this.ReadSharedResources(num);
  236.             }
  237.             return result;
  238.         }
  239.  
  240.         private void ReadSharedResources(int sharedResourceCount)
  241.         {
  242.             /*object[] array = new object[sharedResourceCount];
  243.             for (int i = 0; i < sharedResourceCount; i++)
  244.             {
  245.                 int num = base.Read7BitEncodedInt();
  246.                 if (num > 0)
  247.                 {
  248.                     ContentTypeReader typeReader = this.typeReaders[num - 1];
  249.                     array[i] = this.ReadObject<object>(typeReader);
  250.                 }
  251.                 else
  252.                 {
  253.                     array[i] = null;
  254.                 }
  255.             }
  256.             foreach (KeyValuePair<int, Action<object>> current in this.sharedResourceFixups)
  257.             {
  258.                 current.Value(array[current.Key]);
  259.             }*/
  260.             throw new Exception("STUB");
  261.         }
  262.     }
  263.  
  264.     class ExtractorGameAssetManager : GSGE.GameAssetManager {
  265.         public ExtractorGameAssetManager(Game game, IServiceProvider serviceProvider, string rootDirectory, string packageDirectory)
  266.             : base(game, serviceProvider, rootDirectory, packageDirectory)
  267.         {
  268.         }
  269.  
  270.         protected new T ReadAsset<T>(string assetName, Action<IDisposable> recordDisposableObject, object existingPrimaryObject = null) where T : class
  271.         {
  272.             if (string.IsNullOrEmpty(assetName))
  273.             {
  274.                 throw new ArgumentNullException("assetName");
  275.             }
  276.             object obj = null;
  277.             using (Stream stream = this.OpenStream(assetName))
  278.             {
  279.                 using (BinaryReader binaryReader = new BinaryReader(stream))
  280.                 {
  281.                     byte b = binaryReader.ReadByte();
  282.                     byte b2 = binaryReader.ReadByte();
  283.                     byte b3 = binaryReader.ReadByte();
  284.                     byte b4 = binaryReader.ReadByte();
  285.                     if (b != 88 || b2 != 78 || b3 != 66 || (b4 != 119 && b4 != 120 && b4 != 109 && b4 != 112))
  286.                     {
  287.                         throw new ContentLoadException("Asset does not appear to be a valid XNB file. Did you process your content for Windows?");
  288.                     }
  289.                     byte b5 = binaryReader.ReadByte();
  290.                     byte b6 = binaryReader.ReadByte();
  291.                     bool flag = (b6 & 128) != 0;
  292.                     if (b5 != 5 && b5 != 4)
  293.                     {
  294.                         throw new ContentLoadException("Invalid XNB version");
  295.                     }
  296.                     int num = binaryReader.ReadInt32();
  297.                     ContentReader contentReader;
  298.                     if (flag)
  299.                     {
  300.                         LzxDecoder lzxDecoder = new LzxDecoder(16);
  301.                         int num2 = num - 14;
  302.                         int num3 = binaryReader.ReadInt32();
  303.                         int num4 = num3 + 10;
  304.                         MemoryStream memoryStream = new MemoryStream(num3);
  305.                         int num5 = 0;
  306.                         int i = 0;
  307.                         while (i < num2)
  308.                         {
  309.                             stream.Seek((long)(i + 14), SeekOrigin.Begin);
  310.                             int num6 = stream.ReadByte();
  311.                             int num7 = stream.ReadByte();
  312.                             int num8 = num6 << 8 | num7;
  313.                             int num9 = 32768;
  314.                             if (num6 == 255)
  315.                             {
  316.                                 num6 = num7;
  317.                                 num7 = (int)((byte)stream.ReadByte());
  318.                                 num9 = (num6 << 8 | num7);
  319.                                 num6 = (int)((byte)stream.ReadByte());
  320.                                 num7 = (int)((byte)stream.ReadByte());
  321.                                 num8 = (num6 << 8 | num7);
  322.                                 i += 5;
  323.                             }
  324.                             else
  325.                             {
  326.                                 i += 2;
  327.                             }
  328.                             if (num8 == 0 || num9 == 0)
  329.                             {
  330.                                 break;
  331.                             }
  332.                             int num10 = lzxDecoder.Decompress(stream, num8, memoryStream, num9);
  333.                             i += num8;
  334.                             num5 += num9;
  335.                         }
  336.                         if (memoryStream.Position != (long)num3)
  337.                         {
  338.                             throw new ContentLoadException("Decompression of " + assetName + "failed.  Try decompressing with nativeDecompressXnb first.");
  339.                         }
  340.                         memoryStream.Seek(0L, SeekOrigin.Begin);
  341.                         contentReader = new ContentReader(this, memoryStream, null, assetName);
  342.                     }
  343.                     else
  344.                     {
  345.                         Console.WriteLine("Reading: " + assetName + ".dds");
  346.                         contentReader = new ContentReader(this, stream, null, assetName);
  347.                     }
  348.  
  349.                     using (contentReader)
  350.                     {
  351.                         obj = contentReader.ReadAsset<T>(assetName);
  352.                     }
  353.                 }
  354.             }
  355.             return (T)((object)obj);
  356.         }
  357.  
  358.         public override T Load<T>(string assetName)
  359.         {
  360.             object _loadLock
  361.                 = MainClass.GetInstanceField(typeof(GSGE.GameAssetManager), this, "_loadLock");
  362.             Dictionary<string, object> _loadedAssets
  363.                 = MainClass.GetInstanceField(typeof(GSGE.GameAssetManager), this, "_loadedAssets") as Dictionary<string, object>;
  364.  
  365.             object asset = null;
  366.             string name = ContentManager.CleanPath(assetName);
  367.             T result;
  368.             lock (_loadLock)
  369.             {
  370.                 if (_loadedAssets.TryGetValue(name, out asset))
  371.                 {
  372.                     result = (T)((object)asset);
  373.                     return result;
  374.                 }
  375.             }
  376.             asset = this.ReadAsset<T>(name, null, null);
  377.             lock (_loadLock)
  378.             {
  379.                 _loadedAssets[name] = asset;
  380.             }
  381.             result = (T)((object)asset);
  382.             return result;
  383.         }
  384.     }
  385.  
  386.     class MainClass
  387.     {
  388.         public const string OUT_DIR = "/home/matthew/Documents/TransistorUnpacked/";
  389.  
  390.         /// <summary>
  391.         /// Uses reflection to get the field value from an object.
  392.         /// </summary>
  393.         ///
  394.         /// <param name="type">The instance type.</param>
  395.         /// <param name="instance">The instance object.</param>
  396.         /// <param name="fieldName">The field's name which is to be fetched.</param>
  397.         ///
  398.         /// <returns>The field value from the object.</returns>
  399.         internal static object GetInstanceField(Type type, object instance, string fieldName)
  400.         {
  401.             BindingFlags bindFlags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic
  402.                 | BindingFlags.Static;
  403.             FieldInfo field = type.GetField(fieldName, bindFlags);
  404.             return field.GetValue(instance);
  405.         }
  406.  
  407.         public static void Main(string[] args)
  408.         {
  409.             Directory.Delete(OUT_DIR, true);
  410.             Directory.CreateDirectory(OUT_DIR);
  411.  
  412.             var game = new Game();
  413.             {
  414.                 GSGE.TextureHandleManager.Init(16384);
  415.                 ExtractorGameAssetManager gameAssetManager = new ExtractorGameAssetManager(null, game.Services, "Content", "Win\\Packages");
  416.                 GSGE.AssetManager.Initialize(gameAssetManager);
  417.             }
  418.             {
  419.                 var files = Directory.EnumerateFiles("Content/Win/Packages/", "*.pkg", SearchOption.TopDirectoryOnly);
  420.                 List<string> list = new List<string>();
  421.                 foreach (string f in files) {
  422.                     if (f.EndsWith(".pkg")) {
  423.                         list.Add(f.Substring(0, f.Length - 4));
  424.                     }
  425.                 }
  426.  
  427.                 var files2 = Directory.EnumerateFiles("Content/Win/Packages/720p/", "*.pkg", SearchOption.TopDirectoryOnly);
  428.                 List<string> list2 = new List<string>();
  429.                 foreach (string f in files2) {
  430.                     if (f.EndsWith(".pkg")) {
  431.                         list2.Add(f.Substring(0, f.Length - 4));
  432.                     }
  433.                 }
  434.  
  435.                 foreach (var f in list) {
  436.                     list2.Remove(f.Replace("Content/Win/Packages/", "Content/Win/Packages/720p/"));
  437.                 }
  438.                
  439.                 foreach (var f in list) {
  440.                     var f2 = f.Replace("Content/Win/Packages/", "");
  441.                     Console.WriteLine("Loading Package: " + f2);
  442.                     GSGE.AssetManager.LoadPackage(f2, GSGE.PackageGroup.Base);
  443.                 }
  444.             }
  445.  
  446.         }
  447.     }
  448. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement