示例#1
0
        private static void CreatePackageResource(string file = "")
        {
            if (string.IsNullOrEmpty(file))
            {
                file = CommandLineActions.DestinationPath;
                CommandLineLogger.LogDebug($"Using destination path: {file}");
            }

            var options = new PackageCreationOptions();

            options.Version = CommandLineActions.PackageVersion;

            Dictionary <string, object> compressionOptions = CommandLineArguments.GetCompressionOptions(Path.GetExtension(file)?.ToLower() == ".lsv" ? "zlib" : Args.CompressionMethod, options.Version);

            options.Compression     = (CompressionMethod)compressionOptions["Compression"];
            options.FastCompression = (bool)compressionOptions["FastCompression"];

            var fast = options.FastCompression ? "Fast" : "Normal";

            CommandLineLogger.LogDebug($"Using compression method: {options.Compression.ToString()} ({fast})");

            var packager = new Packager();

            packager.CreatePackage(file, CommandLineActions.SourcePath, options);

            CommandLineLogger.LogInfo("Package created successfully.");
        }
示例#2
0
        private static void ListPackageFiles(string packagePath, Func <AbstractFileInfo, bool> filter = null)
        {
            try
            {
                using (var reader = new PackageReader(packagePath))
                {
                    Package package = reader.Read();

                    List <AbstractFileInfo> files = package.Files;

                    if (filter != null)
                    {
                        files = files.FindAll(obj => filter(obj));
                    }

                    foreach (AbstractFileInfo fileInfo in files.OrderBy(obj => obj.Name))
                    {
                        Console.WriteLine($"{fileInfo.Name}\t{fileInfo.Size()}\t{fileInfo.CRC()}");
                    }
                }
            }
            catch (NotAPackageException)
            {
                CommandLineLogger.LogError("Failed to list package contents because the package is not an Original Sin package or savegame archive");
            }
            catch (Exception e)
            {
                CommandLineLogger.LogFatal($"Failed to list package: {e.Message}", 2);
                CommandLineLogger.LogTrace($"{e.StackTrace}");
            }
        }
        private static void ConvertResource(string file)
        {
            var exporter = new Exporter
            {
                Options = UpdateExporterSettings()
            };

            if (!string.IsNullOrEmpty(file))
            {
                exporter.Options.InputPath = file;
            }

#if !DEBUG
            try
            {
#endif
            exporter.Export();

            CommandLineLogger.LogInfo("Export completed successfully.");
#if !DEBUG
        }
        catch (Exception e)
        {
            CommandLineLogger.LogFatal($"Export failed: {e.Message + Environment.NewLine + e.StackTrace}", 2);
        }
#endif
        }
示例#4
0
        public static string TryToValidatePath(string path)
        {
            CommandLineLogger.LogDebug($"Using path: {path}");

            if (string.IsNullOrWhiteSpace(path))
            {
                CommandLineLogger.LogFatal($"Cannot parse path from input: {path}", 1);
            }

            Uri uri = null;

            try
            {
                Uri.TryCreate(path, UriKind.RelativeOrAbsolute, out uri);
            }
            catch (InvalidOperationException)
            {
                CommandLineLogger.LogFatal($"Cannot proceed without absolute path [E1]: {path}", 1);
            }

            if (uri != null && (!Path.IsPathRooted(path) || !uri.IsFile))
            {
                CommandLineLogger.LogFatal($"Cannot proceed without absolute path [E2]: {path}", 1);
            }

            // ReSharper disable once AssignNullToNotNullAttribute
            path = Path.GetFullPath(path);

            return(path);
        }
示例#5
0
        private static void ExtractPackageResource(string file = "", string folder = "")
        {
            if (string.IsNullOrEmpty(file))
            {
                file = CommandLineActions.SourcePath;
                CommandLineLogger.LogDebug($"Using source path: {file}");
            }

            try
            {
                Packager packager = new Packager();

                string extractionPath = GetExtractionPath(folder, CommandLineActions.DestinationPath);

                CommandLineLogger.LogDebug($"Using extraction path: {extractionPath}");

                packager.UncompressPackage(file, extractionPath);

                CommandLineLogger.LogInfo($"Extracted package to: {extractionPath}");
            }
            catch (NotAPackageException)
            {
                CommandLineLogger.LogFatal("Failed to extract package because the package is not an Original Sin package or savegame archive", 1);
            }
            catch (Exception e)
            {
                CommandLineLogger.LogFatal($"Failed to extract package: {e.Message}", 2);
                CommandLineLogger.LogTrace($"{e.StackTrace}");
            }
        }
示例#6
0
 public static void ListFiles(Func <AbstractFileInfo, bool> filter = null)
 {
     if (CommandLineActions.SourcePath == null)
     {
         CommandLineLogger.LogFatal("Cannot list package without source path", 1);
     }
     else
     {
         ListPackageFiles(CommandLineActions.SourcePath, filter);
     }
 }
示例#7
0
        public static void BatchExtract()
        {
            string[] files = Directory.GetFiles(CommandLineActions.SourcePath, $"*.{Args.InputFormat}");

            foreach (string file in files)
            {
                string extractionPath = GetExtractionPath(file, CommandLineActions.DestinationPath);

                CommandLineLogger.LogDebug($"Extracting package: {file}");

                ExtractPackageResource(file, extractionPath);
            }
        }
示例#8
0
        public static void Extract(Func <AbstractFileInfo, bool> filter = null)
        {
            if (CommandLineActions.SourcePath == null)
            {
                CommandLineLogger.LogFatal("Cannot extract package without source path", 1);
            }
            else
            {
                string extractionPath = GetExtractionPath(CommandLineActions.SourcePath, CommandLineActions.DestinationPath);

                CommandLineLogger.LogInfo($"Extracting package: {CommandLineActions.SourcePath}");

                ExtractPackageResource(CommandLineActions.SourcePath, extractionPath, filter);
            }
        }
示例#9
0
        public static void Extract()
        {
            if (CommandLineActions.SourcePath == null)
            {
                CommandLineLogger.LogFatal("Cannot extract package without source path", 1);
            }
            else
            {
                string extractionPath = GetExtractionPath(CommandLineActions.SourcePath, CommandLineActions.DestinationPath);

                CommandLineLogger.LogDebug($"Extracting package: {CommandLineActions.SourcePath}");

                ExtractPackageResource(CommandLineActions.SourcePath, extractionPath);
            }
        }
        private static void BatchConvertResources(string sourcePath, string inputFormat)
        {
            string[] files = Directory.GetFiles(sourcePath, $"*.{inputFormat}");

            if (files.Length == 0)
            {
                CommandLineLogger.LogFatal($"Batch convert failed: *.{inputFormat} not found in source path", 1);
            }

            foreach (string file in files)
            {
                UpdateExporterSettings();
                Convert(file);
            }
        }
示例#11
0
        private static void BatchConvertResource(string sourcePath, string destinationPath, ResourceFormat inputFormat, ResourceFormat outputFormat, FileVersion fileVersion)
        {
            try
            {
                CommandLineLogger.LogDebug($"Using destination extension: {outputFormat}");

                ResourceUtils resourceUtils = new ResourceUtils();
                resourceUtils.ConvertResources(sourcePath, destinationPath, inputFormat, outputFormat, fileVersion);

                CommandLineLogger.LogInfo($"Wrote resources to: {destinationPath}");
            }
            catch (Exception e)
            {
                CommandLineLogger.LogFatal($"Failed to batch convert resources: {e.Message}", 2);
                CommandLineLogger.LogTrace($"{e.StackTrace}");
            }
        }
示例#12
0
        private static void ExtractSingleFile(string packagePath, string destinationPath, string packagedPath)
        {
            try
            {
                using (var reader = new PackageReader(packagePath))
                {
                    Package package = reader.Read();
                    // Try to match by full path
                    AbstractFileInfo file = package.Files.Find(fileInfo => string.Compare(fileInfo.Name, packagedPath, StringComparison.OrdinalIgnoreCase) == 0);
                    if (file == null)
                    {
                        // Try to match by filename only
                        file = package.Files.Find(fileInfo => string.Compare(Path.GetFileName(fileInfo.Name), packagedPath, StringComparison.OrdinalIgnoreCase) == 0);
                        if (file == null)
                        {
                            CommandLineLogger.LogError($"Package doesn't contain file named '{packagedPath}'");
                            return;
                        }
                    }

                    using (var fs = new FileStream(destinationPath, FileMode.Create, FileAccess.Write))
                    {
                        try
                        {
                            Stream stream = file.MakeStream();
                            stream.CopyTo(fs);
                        }
                        finally
                        {
                            file.ReleaseStream();
                        }
                    }
                }
            }
            catch (NotAPackageException)
            {
                CommandLineLogger.LogError("Failed to list package contents because the package is not an Original Sin package or savegame archive");
            }
            catch (Exception e)
            {
                CommandLineLogger.LogFatal($"Failed to list package: {e.Message}", 2);
                CommandLineLogger.LogTrace($"{e.StackTrace}");
            }
        }
示例#13
0
        private static void ConvertResource(string sourcePath, string destinationPath, FileVersion fileVersion)
        {
            try
            {
                ResourceFormat resourceFormat = ResourceUtils.ExtensionToResourceFormat(destinationPath);
                CommandLineLogger.LogDebug($"Using destination extension: {resourceFormat}");

                Resource resource = ResourceUtils.LoadResource(sourcePath);

                ResourceUtils.SaveResource(resource, destinationPath, resourceFormat, fileVersion);

                CommandLineLogger.LogInfo($"Wrote resource to: {destinationPath}");
            }
            catch (Exception e)
            {
                CommandLineLogger.LogFatal($"Failed to convert resource: {e.Message}", 2);
                CommandLineLogger.LogTrace($"{e.StackTrace}");
            }
        }
示例#14
0
        private static void CreatePackageResource(string file = "")
        {
            if (string.IsNullOrEmpty(file))
            {
                file = CommandLineActions.DestinationPath;
                CommandLineLogger.LogDebug($"Using destination path: {file}");
            }

            PackageVersion packageVersion = CommandLineActions.PackageVersion;
            Dictionary <string, object> compressionOptions = CommandLineArguments.GetCompressionOptions(Path.GetExtension(file)?.ToLower() == ".lsv" ? "zlib" : Args.CompressionMethod, packageVersion);

            CompressionMethod compressionMethod = (CompressionMethod)compressionOptions["Compression"];
            bool compressionSpeed = (bool)compressionOptions["FastCompression"];

            CommandLineLogger.LogDebug($"Using compression method: {compressionMethod.ToString()}");
            CommandLineLogger.LogDebug($"Using fast compression: {compressionSpeed}");

            Packager packager = new Packager();

            packager.CreatePackage(file, CommandLineActions.SourcePath, packageVersion, compressionMethod, compressionSpeed);

            CommandLineLogger.LogInfo("Package created successfully.");
        }
        private static void ConvertResource(string file)
        {
            Exporter exporter = new Exporter
            {
                Options = UpdateExporterSettings()
            };

            if (!string.IsNullOrEmpty(file))
            {
                exporter.Options.InputPath = file;
            }

            try
            {
                exporter.Export();
                CommandLineLogger.LogInfo("Export completed successfully.");
            }
            catch (Exception e)
            {
                CommandLineLogger.LogFatal($"Export failed: {e.Message}", 2);
                CommandLineLogger.LogTrace($"{e.StackTrace}");
            }
        }
示例#16
0
        private static void SetUpAndValidate(CommandLineArguments args)
        {
            string[] batchActions =
            {
                "extract-packages",
                "convert-models",
                "convert-resources"
            };

            string[] packageActions =
            {
                "create-package",
                "list-package",
                "extract-single-file",
                "extract-package",
                "extract-packages"
            };

            string[] graphicsActions =
            {
                "convert-model",
                "convert-models"
            };

            LogLevel = CommandLineArguments.GetLogLevelByString(args.LogLevel);
            CommandLineLogger.LogDebug($"Using log level: {LogLevel}");

            Game = CommandLineArguments.GetGameByString(args.Game);
            CommandLineLogger.LogDebug($"Using game: {Game}");

            if (batchActions.Any(args.Action.Contains))
            {
                if (args.InputFormat == null || args.OutputFormat == null)
                {
                    if (args.InputFormat == null && args.Action != "extract-packages")
                    {
                        CommandLineLogger.LogFatal("Cannot perform batch action without --input-format and --output-format arguments", 1);
                    }
                }

                InputFormat = CommandLineArguments.GetResourceFormatByString(args.InputFormat);
                CommandLineLogger.LogDebug($"Using input format: {InputFormat}");

                if (args.Action != "extract-packages")
                {
                    OutputFormat = CommandLineArguments.GetResourceFormatByString(args.OutputFormat);
                    CommandLineLogger.LogDebug($"Using output format: {OutputFormat}");
                }
            }

            if (args.Action == "create-package")
            {
                switch (Game)
                {
                case Game.DivinityOriginalSin:
                    PackageVersion = PackageVersion.V7;
                    break;

                case Game.DivinityOriginalSinEE:
                    PackageVersion = PackageVersion.V9;
                    break;

                case Game.DivinityOriginalSin2:
                    PackageVersion = PackageVersion.V10;
                    break;

                case Game.DivinityOriginalSin2DE:
                    PackageVersion = PackageVersion.V13;
                    break;

                case Game.BaldursGate3:
                    PackageVersion = PackageVersion.V16;
                    break;

                default:
                    throw new ArgumentException($"Unknown game: \"{Game}\"");
                }

                CommandLineLogger.LogDebug($"Using package version: {PackageVersion}");
            }

            if (graphicsActions.Any(args.Action.Contains))
            {
                GR2Options = CommandLineArguments.GetGR2Options(args.Options);

                if (LogLevel == LogLevel.DEBUG || LogLevel == LogLevel.ALL)
                {
                    CommandLineLogger.LogDebug("Using graphics options:");

                    foreach (KeyValuePair <string, bool> x in GR2Options)
                    {
                        CommandLineLogger.LogDebug($"   {x.Key} = {x.Value}");
                    }
                }

                if (GR2Options["conform"])
                {
                    ConformPath = TryToValidatePath(args.ConformPath);
                }
            }

            SourcePath = TryToValidatePath(args.Source);
            if (args.Action != "list-package")
            {
                DestinationPath = TryToValidatePath(args.Destination);
            }
            if (args.Action == "extract-single-file")
            {
                PackagedFilePath = args.PackagedPath;
            }
        }
示例#17
0
        private static void Process(CommandLineArguments args)
        {
            var expression = new Regex("^" + Regex.Escape(args.Expression).Replace(@"\*", ".*").Replace(@"\?", ".") + "$", RegexOptions.Singleline | RegexOptions.Compiled);

            if (args.UseRegex)
            {
                try
                {
                    expression = new Regex(args.Expression, RegexOptions.Singleline | RegexOptions.Compiled);
                }
                catch (ArgumentException)
                {
                    CommandLineLogger.LogFatal($"Cannot parse RegEx expression: {args.Expression}", -1);
                }
            }

            Func <AbstractFileInfo, bool> filter = obj => obj.Name.Like(expression);

            switch (args.Action)
            {
            case "create-package":
            {
                CommandLinePackageProcessor.Create();
                break;
            }

            case "extract-package":
            {
                CommandLinePackageProcessor.Extract(filter);
                break;
            }

            case "extract-single-file":
            {
                CommandLinePackageProcessor.ExtractSingleFile();
                break;
            }

            case "list-package":
            {
                CommandLinePackageProcessor.ListFiles(filter);
                break;
            }

            case "convert-model":
            {
                CommandLineGR2Processor.UpdateExporterSettings();
                CommandLineGR2Processor.Convert();
                break;
            }

            case "convert-resource":
            {
                CommandLineDataProcessor.Convert();
                break;
            }

            case "extract-packages":
            {
                CommandLinePackageProcessor.BatchExtract(filter);
                break;
            }

            case "convert-models":
            {
                CommandLineGR2Processor.BatchConvert();
                break;
            }

            case "convert-resources":
            {
                CommandLineDataProcessor.BatchConvert();
                break;
            }

            default:
            {
                throw new ArgumentException($"Unhandled action: {args.Action}");
            }
            }
        }
        private static void SetUpAndValidate(CommandLineArguments args)
        {
            string[] batchActions =
            {
                "extract-packages",
                "convert-models",
                "convert-resources"
            };

            string[] packageActions =
            {
                "create-package",
                "extract-package",
                "extract-packages"
            };

            string[] graphicsActions =
            {
                "convert-model",
                "convert-models"
            };

            LogLevel = CommandLineArguments.GetLogLevelByString(args.LogLevel);
            CommandLineLogger.LogDebug($"Using log level: {LogLevel}");

            Game = CommandLineArguments.GetGameByString(args.Game);
            CommandLineLogger.LogDebug($"Using game: {Game}");

            if (batchActions.Any(args.Action.Contains))
            {
                if (args.InputFormat == null || args.OutputFormat == null)
                {
                    if (args.InputFormat == null && args.Action != "extract-packages")
                    {
                        CommandLineLogger.LogFatal("Cannot perform batch action without --input-format and --output-format arguments", 1);
                    }
                }

                InputFormat = CommandLineArguments.GetResourceFormatByString(args.InputFormat);
                CommandLineLogger.LogDebug($"Using input format: {InputFormat}");

                if (args.Action != "extract-packages")
                {
                    OutputFormat = CommandLineArguments.GetResourceFormatByString(args.OutputFormat);
                    CommandLineLogger.LogDebug($"Using output format: {OutputFormat}");
                }
            }

            if (packageActions.Any(args.Action.Contains))
            {
                PackageVersion = CommandLineArguments.GetPackageVersion(args.PackageVersion);
                CommandLineLogger.LogDebug($"Using package version: {PackageVersion}");
            }

            if (graphicsActions.Any(args.Action.Contains))
            {
                GR2Options = CommandLineArguments.GetGR2Options(args.Options);
                CommandLineLogger.LogDebug($"Using graphics options: {GR2Options}");

                if (GR2Options["conform"])
                {
                    ConformPath = TryToValidatePath(args.ConformPath);
                }
            }

            SourcePath      = TryToValidatePath(args.Source);
            DestinationPath = TryToValidatePath(args.Destination);
        }