Exemplo n.º 1
0
        static ReturnCode ParseOptions([NotNull] string[] args)
        {
            if (args == null)
            {
                throw new ArgumentNullException("args");
            }
            string importerList = MapUtility.GetImporters().JoinToString(c => c.Format.ToString());
            string exporterList = MapUtility.GetExporters().JoinToString(c => c.Format.ToString());

            bool printHelp = false;

            opts = new OptionSet()
                   .Add("e=|exporter=",
                        "REQUIRED: Converter used for exporting/saving maps. " +
                        "Available exporters: " + exporterList,
                        o => exporterName = o)

                   .Add("f=|filter=",
                        "Optional: Pattern to filter input filenames, e.g. \"*.dat\" or \"builder*\". " +
                        "Applicable only when a directory name is given as input.",
                        o => inputFilter = o)

                   .Add("i=|importer=",
                        "Optional: Converter used for importing/loading maps. " +
                        "Available importers: Auto (default), " + importerList,
                        o => importerName = o)

                   .Add("o=|output=",
                        "Optional: Path to save converted map files to. " +
                        "If not specified, converted maps will be saved to the original maps' directory.",
                        o => outputDirName = o)

                   .Add("r|recursive",
                        "Optional: Look through all subdirectories, and convert map files there too. " +
                        "Applicable only when a directory name is given as input.",
                        o => recursive = (o != null))

                   .Add("t|tryhard",
                        "Try ALL the map converters on map files that cannot be loaded normally.",
                        o => tryHard = (o != null))

                   .Add("x|regex",
                        "Enable regular expessions in \"filter\".",
                        o => useRegex = (o != null))

                   .Add("y|overwrite",
                        "Optional: Do not ask for confirmation to overwrite existing files.",
                        o => overwrite = (o != null))

                   .Add("?|help|h",
                        "Prints usage information and a list of options.",
                        o => printHelp = (o != null));

            List <string> pathList;

            try {
                pathList = opts.Parse(args);
            } catch (OptionException ex) {
                Console.Error.Write("MapConverter: ");
                Console.Error.WriteLine(ex.Message);
                PrintHelp();
                return(ReturnCode.ArgumentError);
            }

            // Print help and break out
            if (printHelp)
            {
                PrintHelp();
                Environment.Exit((int)ReturnCode.Success);
            }

            if (pathList.Count != 1)
            {
                Console.Error.WriteLine("MapConverter: At least one file or directory name required.");
                PrintUsage();
                return(ReturnCode.ArgumentError);
            }
            inputPathList = pathList.ToArray();

            if (exporterName == null)
            {
                Console.Error.WriteLine("MapConverter: Export format required.");
                PrintUsage();
                return(ReturnCode.ArgumentError);
            }

            outputDirGiven = (outputDirName != null);

            return(ReturnCode.Success);
        }
Exemplo n.º 2
0
        static ReturnCode ParseOptions([NotNull] string[] args)
        {
            if (args == null)
            {
                throw new ArgumentNullException("args");
            }
            string jpegQualityString = null,
                   imageFormatName   = null,
                   importerName      = null,
                   angleString       = null,
                   isoCatModeName    = null,
                   regionString      = null;

            string importerList = MapUtility.GetImporters().JoinToString(c => c.Format.ToString());

            bool printHelp = false;

            opts = new OptionSet()
                   .Add("a=|angle=",
                        "Angle (orientation) from which the map is drawn. May be -90, 0, 90, 180, or 270. Default is 0.",
                        o => angleString = o)

                   .Add("f=|filter=",
                        "Pattern to filter input filenames, e.g. \"*.dat\" or \"builder*\". " +
                        "Applicable only when a directory name is given as input.",
                        o => inputFilter = o)

                   .Add("i=|importer=",
                        "Optional: Converter used for importing/loading maps. " +
                        "Available importers: Auto (default), " + importerList,
                        o => importerName = o)

                   .Add("e=|export=",
                        "Image format to use for exporting. " +
                        "Supported formats: PNG (default), BMP, GIF, JPEG, TIFF.",
                        o => imageFormatName = o)

                   .Add("m=|mode=",
                        "Rendering mode. May be \"normal\" (default), \"cut\" (cuts out a quarter of the map, revealing inside), " +
                        "\"peeled\" (strips the outer-most layer of blocks), \"chunk\" (renders only a specified region of the map).",
                        o => isoCatModeName = o)

                   .Add("g|nogradient",
                        "Disables altitude-based gradient/shading on terrain.",
                        o => noGradient = (o != null))

                   .Add("s|noshadows",
                        "Disables rendering of shadows.",
                        o => noShadows = (o != null))

                   .Add("o=|output=",
                        "Path to save images to. " +
                        "If not specified, images will be saved to the maps' directories.",
                        o => outputDirName = o)

                   .Add("y|overwrite",
                        "Do not ask for confirmation to overwrite existing files.",
                        o => overwrite = (o != null))

                   .Add("q=|quality=",
                        "Sets JPEG compression quality. Between 0 and 100. Default is 80. " +
                        "Applicable only when exporting images to .jpg or .jpeg.",
                        o => jpegQualityString = o)

                   .Add("r|recursive",
                        "Look through all subdirectories for map files. " +
                        "Applicable only when a directory name is given as input.",
                        o => recursive = (o != null))

                   .Add("region=",
                        "Region of the map to render. Should be given in following format: \"region=x1,y1,z1,x2,y2,z2\" " +
                        "Applicable only when rendering mode is set to \"chunk\".",
                        o => regionString = o)

                   .Add("w|seethroughwater",
                        "Makes all water see-through, instead of mostly opaque.",
                        o => seeThroughWater = (o != null))

                   .Add("l|seethroughlava",
                        "Makes all lava partially see-through, instead of opaque.",
                        o => seeThroughLava = (o != null))

                   .Add("u|uncropped",
                        "Does not crop the finished map image, leaving some empty space around the edges.",
                        o => uncropped = (o != null))

                   .Add("?|h|help",
                        "Prints out the options.",
                        o => printHelp = (o != null));

            List <string> pathList;

            try {
                pathList = opts.Parse(args);
            } catch (OptionException ex) {
                Console.Error.Write("MapRenderer: ");
                Console.Error.WriteLine(ex.Message);
                PrintHelp();
                return(ReturnCode.ArgumentError);
            }

            if (printHelp)
            {
                PrintHelp();
                Environment.Exit((int)ReturnCode.Success);
            }

            if (pathList.Count != 1)
            {
                Console.Error.WriteLine("MapRenderer: At least one file or directory name required.");
                PrintUsage();
                return(ReturnCode.ArgumentError);
            }
            inputPath = pathList[0];

            // Parse angle
            if (angleString != null && (!Int32.TryParse(angleString, out angle) ||
                                        angle != -90 && angle != 0 && angle != 180 && angle != 270))
            {
                Console.Error.WriteLine("MapRenderer: Angle must be a number: -90, 0, 90, 180, or 270");
                return(ReturnCode.ArgumentError);
            }

            // Parse mode
            if (isoCatModeName != null && !EnumUtil.TryParse(isoCatModeName, out mode, true))
            {
                Console.Error.WriteLine(
                    "MapRenderer: Rendering mode should be: \"normal\", \"cut\", \"peeled\", or \"chunk\".");
                return(ReturnCode.ArgumentError);
            }

            // Parse region (if in chunk mode)
            if (mode == IsoCatMode.Chunk)
            {
                if (regionString == null)
                {
                    Console.Error.WriteLine("MapRenderer: Region parameter is required when mode is set to \"chunk\"");
                    return(ReturnCode.ArgumentError);
                }
                try {
                    string[] regionParts = regionString.Split(',');
                    region = new BoundingBox(Int32.Parse(regionParts[0]), Int32.Parse(regionParts[1]),
                                             Int32.Parse(regionParts[2]),
                                             Int32.Parse(regionParts[3]), Int32.Parse(regionParts[4]),
                                             Int32.Parse(regionParts[5]));
                } catch {
                    Console.Error.WriteLine(
                        "MapRenderer: Region should be specified in the following format: \"--region=x1,y1,z1,x2,y2,z2\"");
                }
            }
            else if (regionString != null)
            {
                Console.Error.WriteLine(
                    "MapRenderer: Region parameter is given, but rendering mode was not set to \"chunk\"");
            }

            // Parse given image format
            if (imageFormatName != null)
            {
                if (imageFormatName.Equals("BMP", StringComparison.OrdinalIgnoreCase))
                {
                    exportFormat       = ImageFormat.Bmp;
                    imageFileExtension = ".bmp";
                }
                else if (imageFormatName.Equals("GIF", StringComparison.OrdinalIgnoreCase))
                {
                    exportFormat       = ImageFormat.Gif;
                    imageFileExtension = ".gif";
                }
                else if (imageFormatName.Equals("JPEG", StringComparison.OrdinalIgnoreCase) ||
                         imageFormatName.Equals("JPG", StringComparison.OrdinalIgnoreCase))
                {
                    exportFormat       = ImageFormat.Jpeg;
                    imageFileExtension = ".jpg";
                }
                else if (imageFormatName.Equals("PNG", StringComparison.OrdinalIgnoreCase))
                {
                    exportFormat       = ImageFormat.Png;
                    imageFileExtension = ".png";
                }
                else if (imageFormatName.Equals("TIFF", StringComparison.OrdinalIgnoreCase) ||
                         imageFormatName.Equals("TIF", StringComparison.OrdinalIgnoreCase))
                {
                    exportFormat       = ImageFormat.Tiff;
                    imageFileExtension = ".tif";
                }
                else
                {
                    Console.Error.WriteLine(
                        "MapRenderer: Image file format should be: BMP, GIF, JPEG, PNG, or TIFF");
                    return(ReturnCode.ArgumentError);
                }
            }

            // Parse JPEG quality
            if (jpegQualityString != null)
            {
                if (exportFormat == ImageFormat.Jpeg)
                {
                    if (!Int32.TryParse(jpegQualityString, out jpegQuality) || jpegQuality < 0 || jpegQuality > 100)
                    {
                        Console.Error.WriteLine(
                            "MapRenderer: JpegQuality parameter should be a number between 0 and 100");
                        return(ReturnCode.ArgumentError);
                    }
                }
                else
                {
                    Console.Error.WriteLine(
                        "MapRenderer: JpegQuality parameter given, but image export format was not set to \"JPEG\".");
                }
            }

            // parse importer name
            if (importerName != null && !importerName.Equals("auto", StringComparison.OrdinalIgnoreCase))
            {
                MapFormat importFormat;
                if (!EnumUtil.TryParse(importerName, out importFormat, true) ||
                    (mapImporter = MapUtility.GetImporter(importFormat)) == null)
                {
                    Console.Error.WriteLine("Unsupported importer \"{0}\"", importerName);
                    PrintUsage();
                    return(ReturnCode.UnrecognizedImporter);
                }
            }

            return(ReturnCode.Success);
        }
Exemplo n.º 3
0
        static ReturnCode ParseOptions([NotNull] string[] args)
        {
            if (args == null)
            {
                throw new ArgumentNullException("args");
            }
            string jpegQualityString     = null,
                   imageFormatName       = null,
                   angleString           = null,
                   isoCatModeName        = null,
                   regionString          = null,
                   threadCountString     = null,
                   overwritePolicyString = null;

            string importerList = MapUtility.GetImporters().JoinToString(c => c.Format.ToString());

            bool printHelp = false;

            opts = new OptionSet()
                   .Add("a=|angle=",
                        "Angle (orientation) from which the map is drawn. May be -90, 0, 90, 180, or 270. Default is 0.",
                        o => angleString = o)
                   .Add("tryhard",
                        "Try ALL the map converters on map files that cannot be loaded normally.",
                        o => p.TryHard = (o != null))
                   .Add("e=|export=",
                        "Image format to use for exporting. " +
                        "Supported formats: PNG (default), BMP, GIF, JPEG, TIFF.",
                        o => imageFormatName = o)
                   .Add("f=|filter=",
                        "Pattern to filter input file names, e.g. \"*.dat\" or \"builder*\". " +
                        "Applicable only when a directory name is given as input.",
                        o => p.InputFilter = o)
                   .Add("g|nogradient",
                        "Disables altitude-based gradient/shading on terrain.",
                        o => p.NoGradient = (o != null))
                   .Add("i=|importer=",
                        "Optional: Converter used for importing/loading maps. " +
                        "Available importers: Auto (default), " + importerList,
                        o => importerName = o)
                   .Add("l|seethroughlava",
                        "Makes all lava partially see-through, instead of opaque.",
                        o => p.SeeThroughLava = (o != null))
                   .Add("m=|mode=",
                        "Rendering mode. May be \"normal\" (default), \"cut\" (cuts out a quarter of the map, revealing inside), " +
                        "\"peeled\" (strips the outer-most layer of blocks), \"chunk\" (renders only a specified region of the map).",
                        o => isoCatModeName = o)
                   .Add("o=|output=",
                        "Path to save images to. " +
                        "If not specified, images will be saved to the maps' directories.",
                        o => p.OutputDirName = o)
                   .Add("q=|quality=",
                        "Sets JPEG compression quality. Between 0 and 100. Default is 80. " +
                        "Applicable only when exporting images to .jpg or .jpeg.",
                        o => jpegQualityString = o)
                   .Add("r|recursive",
                        "Look through all sub-directories for map files. " +
                        "Applicable only when a directory name is given as input.",
                        o => p.Recursive = (o != null))
                   .Add("t=|threads=",
                        "Number of threads to use, to render multiple files in parallel. Default is CPU count.",
                        o => threadCountString = o)
                   .Add("region=",
                        "Region of the map to render. Should be given in following format: \"region=x1,y1,z1,x2,y2,z2\" " +
                        "Applicable only when rendering mode is set to \"chunk\".",
                        o => regionString = o)
                   .Add("s|noshadows",
                        "Disables rendering of shadows.",
                        o => p.NoShadows = (o != null))
                   .Add("u|uncropped",
                        "Does not crop the finished map image, leaving some empty space around the edges.",
                        o => p.Uncropped = (o != null))
                   .Add("w|seethroughwater",
                        "Makes all water see-through, instead of mostly opaque.",
                        o => p.SeeThroughWater = (o != null))
                   .Add("x|regex",
                        "Enable regular expressions in \"filter\".",
                        o => p.UseRegex = (o != null))
                   .Add("y|overwrite=",
                        "When to overwrite existing image files: Never, Ask (default), IfNewer, Always",
                        o => overwritePolicyString = o)
                   .Add("?|h|help",
                        "Prints out the options.",
                        o => printHelp = (o != null));

            List <string> pathList;

            try {
                pathList = opts.Parse(args);
            } catch (OptionException ex) {
                Console.Error.Write("MapRenderer: ");
                Console.Error.WriteLine(ex.Message);
                PrintHelp();
                return(ReturnCode.ArgumentError);
            }

            if (printHelp)
            {
                PrintHelp();
                Environment.Exit((int)ReturnCode.Success);
            }

            if (pathList.Count == 0)
            {
                Console.Error.WriteLine("MapRenderer: At least one file or directory name required.");
                PrintUsage();
                return(ReturnCode.ArgumentError);
            }
            p.InputPathList = pathList.ToArray();

            // Parse angle
            int angle = 0;

            if (angleString != null && (!Int32.TryParse(angleString, out angle) ||
                                        angle != -90 && angle != 0 && angle != 180 && angle != 270))
            {
                Console.Error.WriteLine("MapRenderer: Angle must be a number: -90, 0, 90, 180, or 270");
                return(ReturnCode.ArgumentError);
            }
            p.Angle = angle;

            // Parse mode
            IsoCatMode mode = IsoCatMode.Normal;

            if (isoCatModeName != null && !EnumUtil.TryParse(isoCatModeName, out mode, true))
            {
                Console.Error.WriteLine(
                    "MapRenderer: Rendering mode should be: \"normal\", \"cut\", \"peeled\", or \"chunk\".");
                return(ReturnCode.ArgumentError);
            }
            p.Mode = mode;

            // Parse region (if in chunk mode)
            if (mode == IsoCatMode.Chunk)
            {
                if (regionString == null)
                {
                    Console.Error.WriteLine("MapRenderer: Region parameter is required when mode is set to \"chunk\"");
                    return(ReturnCode.ArgumentError);
                }
                try {
                    string[] regionParts = regionString.Split(',');
                    p.Region = new BoundingBox(Int32.Parse(regionParts[0]),
                                               Int32.Parse(regionParts[1]),
                                               Int32.Parse(regionParts[2]),
                                               Int32.Parse(regionParts[3]),
                                               Int32.Parse(regionParts[4]),
                                               Int32.Parse(regionParts[5]));
                } catch {
                    Console.Error.WriteLine(
                        "MapRenderer: Region should be specified in the following format: \"--region=x1,y1,z1,x2,y2,z2\"");
                }
            }
            else if (regionString != null)
            {
                Console.Error.WriteLine(
                    "MapRenderer: Region parameter is given, but rendering mode was not set to \"chunk\"");
            }

            // Parse given image format
            if (imageFormatName != null)
            {
                if (imageFormatName.Equals("BMP", StringComparison.OrdinalIgnoreCase))
                {
                    p.ExportFormat       = ImageFormat.Bmp;
                    p.ImageFileExtension = ".bmp";
                }
                else if (imageFormatName.Equals("GIF", StringComparison.OrdinalIgnoreCase))
                {
                    p.ExportFormat       = ImageFormat.Gif;
                    p.ImageFileExtension = ".gif";
                }
                else if (imageFormatName.Equals("JPEG", StringComparison.OrdinalIgnoreCase) ||
                         imageFormatName.Equals("JPG", StringComparison.OrdinalIgnoreCase))
                {
                    p.ExportFormat       = ImageFormat.Jpeg;
                    p.ImageFileExtension = ".jpg";
                }
                else if (imageFormatName.Equals("PNG", StringComparison.OrdinalIgnoreCase))
                {
                    p.ExportFormat       = ImageFormat.Png;
                    p.ImageFileExtension = ".png";
                }
                else if (imageFormatName.Equals("TIFF", StringComparison.OrdinalIgnoreCase) ||
                         imageFormatName.Equals("TIF", StringComparison.OrdinalIgnoreCase))
                {
                    p.ExportFormat       = ImageFormat.Tiff;
                    p.ImageFileExtension = ".tif";
                }
                else
                {
                    Console.Error.WriteLine(
                        "MapRenderer: Image file format should be: BMP, GIF, JPEG, PNG, or TIFF");
                    return(ReturnCode.ArgumentError);
                }
            }

            // Parse JPEG quality
            if (jpegQualityString != null)
            {
                if (p.ExportFormat.Guid == ImageFormat.Jpeg.Guid)
                {
                    int jpegQuality;
                    if (!Int32.TryParse(jpegQualityString, out jpegQuality) || jpegQuality < 0 || jpegQuality > 100)
                    {
                        Console.Error.WriteLine(
                            "MapRenderer: JpegQuality parameter should be a number between 0 and 100");
                        return(ReturnCode.ArgumentError);
                    }
                    p.JpegQuality = jpegQuality;
                }
                else
                {
                    Console.Error.WriteLine(
                        "MapRenderer: JpegQuality parameter given, but image export format was not set to \"JPEG\".");
                }
            }

            if (p.MapImporter != null && p.TryHard)
            {
                Console.Error.WriteLine("MapRenderer: --tryhard flag can only be used when importer is \"auto\".");
                return(ReturnCode.ArgumentError);
            }

            if (p.InputFilter == null && p.UseRegex)
            {
                Console.Error.WriteLine("MapRenderer: --regex flag can only be used when --filter is specified.");
                return(ReturnCode.ArgumentError);
            }

            // Parse theread count
            byte tempThreadCount = 2;

            if (threadCountString != null &&
                (!Byte.TryParse(threadCountString, out tempThreadCount) || tempThreadCount < 1))
            {
                Console.Error.WriteLine("MapRenderer: --threads flag must be a number between 1 and 255");
                return(ReturnCode.ArgumentError);
            }
            p.ThreadCount = tempThreadCount;

            p.OutputDirGiven = (p.OutputDirName != null);

            // Parse OverwritePolicy
            OverwritePolicy op = OverwritePolicy.Ask;

            if (overwritePolicyString != null && !Enum.TryParse(overwritePolicyString, true, out op))
            {
                Console.Error.WriteLine(
                    "MapRenderer: Overwrite mode should be: \"never\", \"ask\", \"ifNewer\", or \"always\".");
                return(ReturnCode.ArgumentError);
            }
            p.OverwritePolicy = op;

            return(ReturnCode.Success);
        }