private static bool ConvertTexture(Superbundle.BundleInfo bundleInfo,
                                           Superbundle.ResourceInfo resourceInfo,
                                           ICatalogEntryInfo entry,
                                           string outputPath,
                                           CatalogLookup catalogLookup,
                                           List <TableOfContentsFile> commonBundles)
        {
            TextureHeader textureHeader;

            using (var temp = new MemoryStream())
            {
                Extraction.Extract(resourceInfo, entry, temp);
                temp.Position = 0;
                textureHeader = TextureHeader.Read(temp);
                if (temp.Position != temp.Length)
                {
                    throw new FormatException();
                }
            }

            if (textureHeader.Type != TextureType._2d)
            {
                return(false);
            }

            if (textureHeader.Unknown00 != 0 ||
                textureHeader.Unknown04 != 0 ||
                textureHeader.Unknown10 != 0 ||
                textureHeader.Unknown14 != 0 ||
                textureHeader.Unknown1C != 1)
            {
                throw new FormatException();
            }

            SHA1 chunkSHA1;

            if (GetChunkSHA1(bundleInfo, commonBundles, textureHeader.DataChunkId, out chunkSHA1) == false)
            {
                throw new InvalidOperationException();
            }

            var dataEntry = catalogLookup.GetEntry(chunkSHA1, textureHeader.TotalSize);

            byte[] dataBytes;
            using (var temp = new MemoryStream())
            {
                Extraction.Extract(dataEntry, textureHeader.TotalSize, temp);
                temp.Position = 0;
                dataBytes     = temp.GetBuffer();
            }

            DDSUtils.WriteFile(textureHeader, dataBytes, outputPath + ".dds");
            return(true);
        }
        public static void Main(string[] args)
        {
            bool convertTextures = false;
            bool showHelp        = false;

            var options = new OptionSet()
            {
                { "convert-textures", "convert textures", v => convertTextures = v != null },
                { "h|help", "show this message and exit", v => showHelp = v != null },
            };

            List <string> extras;

            try
            {
                extras = options.Parse(args);
            }
            catch (OptionException e)
            {
                Console.Write("{0}: ", GetExecutableName());
                Console.WriteLine(e.Message);
                Console.WriteLine("Try `{0} --help' for more information.", GetExecutableName());
                return;
            }

            if (extras.Count < 1 || extras.Count > 2 || showHelp == true)
            {
                Console.WriteLine("Usage: {0} [OPTIONS]+ input_[sb|toc] [output_dir]", GetExecutableName());
                Console.WriteLine();
                Console.WriteLine("Options:");
                options.WriteOptionDescriptions(Console.Out);
                return;
            }

            string bundlePath, superbundlePath, layoutPath, outputBasePath;

            if (Path.GetExtension(extras[0]) == ".sb")
            {
                superbundlePath = Path.GetFullPath(extras[0]);
                bundlePath      = Path.ChangeExtension(superbundlePath, ".toc");
                layoutPath      = Helpers.FindLayoutPath(superbundlePath);
                outputBasePath  = extras.Count > 1 ? extras[1] : Path.ChangeExtension(superbundlePath, null) + "_unpack";
            }
            else
            {
                bundlePath      = Path.GetFullPath(extras[0]);
                superbundlePath = Path.ChangeExtension(bundlePath, ".sb");
                layoutPath      = Helpers.FindLayoutPath(bundlePath);
                outputBasePath  = extras.Count > 1 ? extras[1] : Path.ChangeExtension(bundlePath, null) + "_unpack";
            }

            if (string.IsNullOrEmpty(layoutPath) == true)
            {
                Console.WriteLine("Could not find layout file.");
                return;
            }

            var dataPath = Path.GetDirectoryName(layoutPath) ?? "";

            var bundle      = TableOfContentsFile.Read(bundlePath);
            var superbundle = SuperbundleFile.Read(superbundlePath);

            var extensionsById = ResourceTypes.GetExtensions();

            if (bundle.IsCas == false)
            {
                throw new NotImplementedException();
            }
            else
            {
                var commonBundlePaths = Directory.GetFiles(dataPath, "chunks*.toc", SearchOption.AllDirectories);
                var commonBundles     = new List <TableOfContentsFile>();
                foreach (var commonBundlePath in commonBundlePaths)
                {
                    var commonBundle = TableOfContentsFile.Read(commonBundlePath);
                    commonBundles.Add(commonBundle);
                }

                var superbundleName = Path.ChangeExtension(superbundlePath.Substring(dataPath.Length + 1), null);
                superbundleName = Helpers.FilterName(superbundleName).ToLowerInvariant();

                var layout        = LayoutFile.Read(layoutPath);
                var installChunks = GetSuperbundleInstallChunks(layout, superbundleName);
                var catalogLookup = new CatalogLookup(dataPath);

                foreach (var installChunk in installChunks)
                {
                    if (catalogLookup.Add(installChunk.InstallBundle) == false)
                    {
                        Console.WriteLine("Failed to load catalog for '{0}'.", installChunk.Name);
                    }
                }

                foreach (var bundleInfo in superbundle.Bundles)
                {
                    if (bundleInfo.Resources == null)
                    {
                        continue;
                    }

                    foreach (var resourceInfo in bundleInfo.Resources)
                    {
                        var entry = catalogLookup.GetEntry(resourceInfo);
                        if (entry == null)
                        {
                            Console.WriteLine("Could not find catalog entry for '{0}'.", resourceInfo.Name);
                            continue;
                        }

                        if (entry.CompressedSize != resourceInfo.Size)
                        {
                            throw new FormatException();
                        }

                        var outputName       = Helpers.FilterPath(resourceInfo.Name);
                        var outputPath       = Path.Combine(outputBasePath, outputName + ".dummy");
                        var outputParentPath = Path.GetDirectoryName(outputPath);
                        if (string.IsNullOrEmpty(outputParentPath) == false)
                        {
                            Directory.CreateDirectory(outputParentPath);
                        }

                        Console.WriteLine("{0}", resourceInfo.Name);

                        bool wasConverted = false;
                        if (convertTextures == true && resourceInfo.ResourceType == ResourceTypes.Texture)
                        {
                            wasConverted = ConvertTexture(bundleInfo,
                                                          resourceInfo,
                                                          entry,
                                                          outputPath,
                                                          catalogLookup,
                                                          commonBundles);
                        }

                        if (wasConverted == false)
                        {
                            string extension;
                            if (extensionsById.TryGetValue(resourceInfo.ResourceType, out extension) == true)
                            {
                                extension = "." + extension;
                            }
                            else
                            {
                                extension = ".#" + resourceInfo.ResourceType.ToString("X8");
                            }

                            outputPath = Path.Combine(outputBasePath, outputName + extension);
                            using (var output = File.Create(outputPath))
                            {
                                Extraction.Extract(resourceInfo, entry, output);
                            }
                        }
                    }
                }
            }
        }