Exemplo n.º 1
0
        /// <summary>
        /// Print input flag error
        /// </summary>
        /// <param name="inputFlag">Input state</param>
        private static void PrintInputFlagError(EInputFlag inputFlag)
        {
            switch (inputFlag)
            {
            case EInputFlag.SpecifyOutputPath:
                Console.Error.WriteLine("Empty output flag");
                break;

            case EInputFlag.SpecifyOutputDirectoryPath:
                Console.Error.WriteLine("Empty output directory flag");
                break;
            }
        }
Exemplo n.º 2
0
 /// <summary>
 /// Constructs a new input flag
 /// </summary>
 /// <param name="description">Description</param>
 /// <param name="fullDescription">Full description</param>
 /// <param name="flag">Input flag</param>
 public InputFlag(string description, string fullDescription, EInputFlag flag)
 {
     Description     = description ?? throw new ArgumentNullException(nameof(description));
     FullDescription = fullDescription ?? throw new ArgumentNullException(nameof(fullDescription));
     Flag            = flag;
 }
Exemplo n.º 3
0
        /// <summary>
        /// Main entry point
        /// </summary>
        /// <param name="args">Command line arguments</param>
        private static void Main(string[] args)
        {
            Dictionary <string, string> files = new Dictionary <string, string>();
            string     output_directory       = string.Empty;
            bool       show_help          = false;
            bool       verbose            = false;
            EInputFlag current_input_flag = EInputFlag.Nothing;
            string     last_path          = null;
            string     help_topic         = null;

            foreach (string arg in args)
            {
                string trimmed_arg = arg.Trim().ToLower();
                if (trimmed_arg.Length > 0)
                {
                    if (trimmed_arg[0] == '-')
                    {
                        string    key        = ((trimmed_arg.Length > 1) ? trimmed_arg.Substring(1) : string.Empty);
                        InputFlag?input_flag = null;
                        if (inputFlags.ContainsKey(key))
                        {
                            input_flag = inputFlags[key];
                        }
                        else if (inputFlagAliases.ContainsKey(key))
                        {
                            input_flag = inputFlags[inputFlagAliases[key]];
                        }
                        if (input_flag == null)
                        {
                            Console.Error.Write("Invalid input flag \"");
                            Console.Error.Write(arg);
                            Console.Error.WriteLine("\"");
                        }
                        else
                        {
                            PrintInputFlagError(current_input_flag);
                            current_input_flag = input_flag.Value.Flag;
                            switch (current_input_flag)
                            {
                            case EInputFlag.ShowHelp:
                                show_help = true;
                                break;

                            case EInputFlag.Verbose:
                                verbose            = true;
                                current_input_flag = EInputFlag.Nothing;
                                break;
                            }
                        }
                    }
                    else
                    {
                        switch (current_input_flag)
                        {
                        case EInputFlag.Nothing:
                            if (!(files.ContainsKey(arg)))
                            {
                                files.Add(arg, arg);
                            }
                            last_path = arg;
                            break;

                        case EInputFlag.ShowHelp:
                            help_topic = trimmed_arg;
                            break;

                        case EInputFlag.SpecifyOutputPath:
                            if (last_path == null)
                            {
                                Console.Error.WriteLine("No input path is defined");
                            }
                            else
                            {
                                files[last_path] = arg;
                            }
                            current_input_flag = EInputFlag.Nothing;
                            break;

                        case EInputFlag.SpecifyOutputDirectoryPath:
                            output_directory   = arg;
                            current_input_flag = EInputFlag.Nothing;
                            break;
                        }
                        current_input_flag = EInputFlag.Nothing;
                    }
                }
            }
            PrintInputFlagError(current_input_flag);
            if (show_help || (files.Count <= 0))
            {
                if (help_topic == null)
                {
                    PrintInputFlags();
                }
                else
                {
                    InputFlag?input_flag = null;
                    if (inputFlags.ContainsKey(help_topic))
                    {
                        input_flag = inputFlags[help_topic];
                    }
                    else if (inputFlagAliases.ContainsKey(help_topic))
                    {
                        input_flag = inputFlags[inputFlagAliases[help_topic]];
                    }
                    if (input_flag == null)
                    {
                        Console.Error.Write("Invalid help topic \"");
                        Console.Error.Write(help_topic);
                        Console.Error.WriteLine("\".");
                        PrintInputFlags();
                    }
                    else
                    {
                        Console.WriteLine();
                        Console.Write("Help topic: ");
                        Console.WriteLine(input_flag.Value.Description);
                        Console.WriteLine();
                        string[] full_description = input_flag.Value.FullDescription.Split(Environment.NewLine);
                        if (full_description != null)
                        {
                            foreach (string full_description_line in full_description)
                            {
                                if (full_description_line != null)
                                {
                                    Console.Write("\t");
                                    Console.WriteLine(full_description_line);
                                }
                            }
                        }
                        Console.WriteLine();
                    }
                }
            }
            else
            {
                foreach (KeyValuePair <string, string> file in files)
                {
                    try
                    {
                        string input_file_path = Path.GetFullPath(file.Key);
                        if (File.Exists(input_file_path))
                        {
                            if (verbose)
                            {
                                Console.Write("Opening file \"");
                                Console.Write(input_file_path);
                                Console.WriteLine("\"...");
                            }
                            IDGF dgf = DGFIO.OpenDGF(input_file_path);
                            dgf.EditPassword = string.Empty;
                            dgf.PlayPassword = string.Empty;
                            dgf.ApplyPlayPasswordUntilGardenNumber = 1;
                            if (dgf == null)
                            {
                                Console.Error.Write("Could not open file \"");
                                Console.Error.Write(input_file_path);
                                Console.Error.WriteLine("\".");
                            }
                            else
                            {
                                string output_path    = Path.GetFullPath(Path.Combine(output_directory, file.Value));
                                string directory_path = Path.GetFullPath(Path.GetDirectoryName(output_path));
                                if (!(Directory.Exists(directory_path)))
                                {
                                    Directory.CreateDirectory(directory_path);
                                    if (verbose)
                                    {
                                        Console.Write("Created directory \"");
                                        Console.Write(directory_path);
                                        Console.WriteLine("\".");
                                    }
                                }
                                if (dgf.Save(output_path))
                                {
                                    if (verbose)
                                    {
                                        Console.Write("Created file \"");
                                        Console.Write(output_path);
                                        Console.WriteLine("\".");
                                    }
                                }
                                else
                                {
                                    Console.Error.Write("Failed to write output to \"");
                                    Console.Error.Write(output_path);
                                    Console.Error.WriteLine("\".");
                                }
                            }
                        }
                        else
                        {
                            Console.Error.Write("File \"");
                            Console.Error.Write(input_file_path);
                            Console.Error.WriteLine("\" does not exist.");
                        }
                    }
                    catch (Exception e)
                    {
                        Console.Error.WriteLine(e);
                    }
                }
            }
        }