1
0

CommandLineArguments.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics.CodeAnalysis;
  4. using Content.MapRenderer.Extensions;
  5. namespace Content.MapRenderer;
  6. public sealed class CommandLineArguments
  7. {
  8. public List<string> Maps { get; set; } = new();
  9. public OutputFormat Format { get; set; } = OutputFormat.png;
  10. public bool ExportViewerJson { get; set; } = false;
  11. public string OutputPath { get; set; } = DirectoryExtensions.MapImages().FullName;
  12. public bool ArgumentsAreFileNames { get; set; } = false;
  13. public static bool TryParse(IReadOnlyList<string> args, [NotNullWhen(true)] out CommandLineArguments? parsed)
  14. {
  15. parsed = new CommandLineArguments();
  16. if (args.Count == 0)
  17. {
  18. PrintHelp();
  19. //Returns true here so the user can select what maps they want to render
  20. return true;
  21. }
  22. using var enumerator = args.GetEnumerator();
  23. while (enumerator.MoveNext())
  24. {
  25. var argument = enumerator.Current;
  26. switch (argument)
  27. {
  28. case "--format":
  29. enumerator.MoveNext();
  30. if (!Enum.TryParse<OutputFormat>(enumerator.Current, out var format))
  31. {
  32. Console.WriteLine("Invalid format specified for option: {0}", argument);
  33. parsed = null;
  34. return false;
  35. }
  36. parsed.Format = format;
  37. break;
  38. case "--viewer":
  39. parsed.ExportViewerJson = true;
  40. break;
  41. case "-o":
  42. case "--output":
  43. enumerator.MoveNext();
  44. parsed.OutputPath = enumerator.Current;
  45. break;
  46. case "-f":
  47. case "--files":
  48. parsed.ArgumentsAreFileNames = true;
  49. break;
  50. case "-h":
  51. case "--help":
  52. PrintHelp();
  53. return false;
  54. default:
  55. parsed.Maps.Add(argument);
  56. break;
  57. }
  58. }
  59. if (parsed.ArgumentsAreFileNames && parsed.Maps.Count == 0)
  60. {
  61. Console.WriteLine("No file names specified!");
  62. PrintHelp();
  63. return false;
  64. }
  65. return true;
  66. }
  67. public static void PrintHelp()
  68. {
  69. Console.WriteLine(@"Content.MapRenderer <options> [map names]
  70. Options:
  71. --format <png|webp>
  72. Specifies the format the map images will be exported as.
  73. Defaults to: png
  74. --viewer
  75. Causes the map renderer to create the map.json files required for use with the map viewer.
  76. Also puts the maps in the required directory structure.
  77. -o / --output <output path>
  78. Changes the path the rendered maps will get saved to.
  79. Defaults to Resources/MapImages
  80. -f / --files
  81. This option tells the map renderer that you supplied a list of map file names instead of their ids.
  82. Example: Content.MapRenderer -f box.yml bagel.yml
  83. -h / --help
  84. Displays this help text");
  85. }
  86. }
  87. public sealed class CommandLineArgumentException : Exception
  88. {
  89. public CommandLineArgumentException(string? message) : base(message)
  90. {
  91. }
  92. }
  93. [SuppressMessage("ReSharper", "InconsistentNaming")]
  94. public enum OutputFormat
  95. {
  96. png,
  97. webp
  98. }