Ejemplo n.º 1
0
        public static void Main(string[] args)
        {
            bool convertTextures = false;
            bool noPatch         = false;
            bool noCatch         = false;
            int  logLevelOrdinal = 3;
            bool showHelp        = false;

            var options = new OptionSet()
            {
                { "t|convert-textures", "convert textures", v => convertTextures = v != null },
                { "no-patch", "don't use patch data", v => noPatch = v != null },
                { "no-catch", "don't catch exceptions when loading data", v => noCatch = v != null },
                { "v|verbose", "increase log level (-v/-vv/-vvv)", v => IncreaseLogLevel(v, ref logLevelOrdinal) },
                { "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;
            }

            LogHelper.SetConfiguration(NLog.LogLevel.FromOrdinal(logLevelOrdinal));

            var inputPath      = extras[0];
            var outputBasePath = extras.Count > 1 ? extras[1] : Path.ChangeExtension(inputPath, null) + "_res_unpack";

            string superbundleName;
            var    dataBasePath = Discovery.FindBasePath(inputPath, out superbundleName);

            if (string.IsNullOrEmpty(dataBasePath) == true)
            {
                Logger.Error("Failed to discover base game path.");
                return;
            }

            var dataManager = DataManager.Initialize(dataBasePath, noPatch);

            if (dataManager == null)
            {
                Logger.Fatal("Could not initialize superbundle manager.");
                return;
            }

            var extensionsById = ResourceTypes.GetExtensions();

            var superbundle = dataManager.MountSuperbundle(superbundleName);

            foreach (var resourceInfo in superbundle.Bundles
                     .Where(bi => bi.Resources != null)
                     .SelectMany(bi => bi.Resources)
                     .OrderBy(bi => bi.Name))
            {
                using (var data = new MemoryStream())
                {
                    Logger.Info(resourceInfo.Name);

                    if (noCatch == true)
                    {
                        dataManager.LoadData(resourceInfo, data);
                        data.Position = 0;
                    }
                    else
                    {
                        try
                        {
                            dataManager.LoadData(resourceInfo, data);
                            data.Position = 0;
                        }
                        catch (ChunkCryptoKeyMissingException e)
                        {
                            Logger.Warn("Cannot decrypt '{0}' without crypto key '{1}'.",
                                        resourceInfo.Name,
                                        e.KeyId);
                            continue;
                        }
                        catch (Exception e)
                        {
                            Logger.Warn(e, "Exception while loading '{0}':", resourceInfo.Name);
                            continue;
                        }
                    }

                    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);
                    }

                    bool wasConverted = false;
                    if (convertTextures == true && resourceInfo.ResourceType == ResourceTypes.Texture)
                    {
                        outputPath   = Path.Combine(outputBasePath, outputName + ".dds");
                        wasConverted = ConvertTexture(data, outputPath, dataManager);
                    }

                    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))
                        {
                            data.Position = 0;
                            output.WriteFromStream(data, data.Length);
                        }
                    }
                }
            }

            dataManager.Dispose();
        }