Skip to content

Commit

Permalink
Added -f switch, string tokenizer (ConsoleHelper) and made all consts…
Browse files Browse the repository at this point in the history
… PascalCase
  • Loading branch information
Bertie2011 committed Jan 21, 2021
1 parent 68cdafd commit 06a3c5b
Show file tree
Hide file tree
Showing 22 changed files with 100 additions and 48 deletions.
34 changes: 34 additions & 0 deletions DataPackChecker/ConsoleHelper.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DataPackChecker {
static class ConsoleHelper {
private static readonly char[] Quotes = new char[] { '"', '\'' };
private static readonly char[] Escapable = new char[] { '"', '\'' };
static public void WriteLine(string text, ConsoleColor color = ConsoleColor.White) {
Console.ForegroundColor = color;
Console.WriteLine(text);
Expand All @@ -25,5 +28,36 @@ static public void WriteError(Exception e, ConsoleColor color = ConsoleColor.Red
if (e.InnerException != null) WriteError(e.InnerException, color, depth + 1);
if (depth == 0) Console.ForegroundColor = ConsoleColor.White;
}

static public string[] CreateArgs(string line) {
StringBuilder arg = new StringBuilder();
char? quote = null;
bool escape = false;
List<string> args = new List<string>();
for (int i = 0; i < line.Length; i++) {
char c = line[i];

// Either process the created argument or record the next character.
if (c == ' ' && quote == null && !escape) {
args.Add(arg.ToString());
arg.Clear();
} else if (escape || !Quotes.Any(q => q == c) && c != '\\') {
if (escape && !Escapable.Any(e => e == c)) arg.Append('\\');
arg.Append(c);
}

// Change modifiers
if (escape) {
escape = false;
} else if (c == '\\') {
escape = true;
} else if (Quotes.Any(q => q == c)) {
quote = quote == null ? c : (char?)null;
}
}

if (arg.Length > 0) args.Add(arg.ToString());
return args.ToArray();
}
}
}
2 changes: 2 additions & 0 deletions DataPackChecker/Options.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

namespace DataPackChecker {
class Options {
[Option('f', "arguments-file", HelpText = "Read arguments from a file.", SetName = "Args File")]
public string ArgsPath { get; set; }
[Option('o', "keep-open", HelpText = "Keep process running after finishing.")]
public bool KeepOpen { get; set; }
[Option('d', "data-pack", HelpText = "Path to root folder of data pack, with data folder and pack.mcmeta file inside. Requires -c, --config.", SetName = "Check")]
Expand Down
4 changes: 2 additions & 2 deletions DataPackChecker/Parsers/AdvancementParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@

namespace DataPackChecker.Parsers {
static class AdvancementParser {
private static readonly Regex NAMESPACE_PATH_REGEX = new Regex(@"[\\/]advancements([\\/](?<path>.+?))?[\\/](?<name>[^\\/]+)\.json$");
private static readonly Regex NamespacePathRegex = new Regex(@"[\\/]advancements([\\/](?<path>.+?))?[\\/](?<name>[^\\/]+)\.json$");
static public Advancement TryParse(string absPath, string nsPath) {
var match = NAMESPACE_PATH_REGEX.Match(absPath, nsPath.Length);
var match = NamespacePathRegex.Match(absPath, nsPath.Length);
if (!match.Success) return null;
var advancement = new Advancement(match.Groups["path"].Value.Replace('\\', '/'), match.Groups["name"].Value);
using FileStream fs = new FileStream(absPath, FileMode.Open);
Expand Down
4 changes: 2 additions & 2 deletions DataPackChecker/Parsers/Dimensions/DimensionParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@

namespace DataPackChecker.Parsers.Dimensions {
static class DimensionParser {
private static readonly Regex NAMESPACE_PATH_REGEX = new Regex(@"[\\/]dimension([\\/](?<path>.+?))?[\\/](?<name>[^\\/]+)\.json$");
private static readonly Regex NamespacePathRegex = new Regex(@"[\\/]dimension([\\/](?<path>.+?))?[\\/](?<name>[^\\/]+)\.json$");
static public Dimension TryParse(string absPath, string nsPath) {
var match = NAMESPACE_PATH_REGEX.Match(absPath, nsPath.Length);
var match = NamespacePathRegex.Match(absPath, nsPath.Length);
if (!match.Success) return null;
var dimension = new Dimension(match.Groups["path"].Value.Replace('\\', '/'), match.Groups["name"].Value);
using FileStream fs = new FileStream(absPath, FileMode.Open);
Expand Down
4 changes: 2 additions & 2 deletions DataPackChecker/Parsers/Dimensions/DimensionTypeParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@

namespace DataPackChecker.Parsers.Dimensions {
static class DimensionTypeParser {
private static readonly Regex NAMESPACE_PATH_REGEX = new Regex(@"[\\/]dimension_type([\\/](?<path>.+?))?[\\/](?<name>[^\\/]+)\.json$");
private static readonly Regex NamespacePathRegex = new Regex(@"[\\/]dimension_type([\\/](?<path>.+?))?[\\/](?<name>[^\\/]+)\.json$");
static public DimensionType TryParse(string absPath, string nsPath) {
var match = NAMESPACE_PATH_REGEX.Match(absPath, nsPath.Length);
var match = NamespacePathRegex.Match(absPath, nsPath.Length);
if (!match.Success) return null;
var dimensionType = new DimensionType(match.Groups["path"].Value.Replace('\\', '/'), match.Groups["name"].Value);
using FileStream fs = new FileStream(absPath, FileMode.Open);
Expand Down
4 changes: 2 additions & 2 deletions DataPackChecker/Parsers/FunctionParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@

namespace DataPackChecker.Parsers {
static public class FunctionParser {
private static readonly Regex NAMESPACE_PATH_REGEX = new Regex(@"[\\/]functions([\\/](?<path>.+?))?[\\/](?<name>[^\\/]+)\.mcfunction$");
private static readonly Regex NamespacePathRegex = new Regex(@"[\\/]functions([\\/](?<path>.+?))?[\\/](?<name>[^\\/]+)\.mcfunction$");
static public Function TryParse(string absPath, string nsPath) {
var match = NAMESPACE_PATH_REGEX.Match(absPath, nsPath.Length);
var match = NamespacePathRegex.Match(absPath, nsPath.Length);
if (!match.Success) return null;
var function = new Function(match.Groups["path"].Value.Replace('\\', '/'), match.Groups["name"].Value);
function.Commands = File.ReadAllLines(absPath).Select((c, i) => new Command(i + 1, c)).ToList();
Expand Down
4 changes: 2 additions & 2 deletions DataPackChecker/Parsers/LootTableParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@

namespace DataPackChecker.Parsers {
static class LootTableParser {
private static readonly Regex NAMESPACE_PATH_REGEX = new Regex(@"[\\/]loot_tables([\\/](?<path>.+?))?[\\/](?<name>[^\\/]+)\.json$");
private static readonly Regex NamespacePathRegex = new Regex(@"[\\/]loot_tables([\\/](?<path>.+?))?[\\/](?<name>[^\\/]+)\.json$");
static public LootTable TryParse(string absPath, string nsPath) {
var match = NAMESPACE_PATH_REGEX.Match(absPath, nsPath.Length);
var match = NamespacePathRegex.Match(absPath, nsPath.Length);
if (!match.Success) return null;
var lootTable = new LootTable(match.Groups["path"].Value.Replace('\\', '/'), match.Groups["name"].Value);
using FileStream fs = new FileStream(absPath, FileMode.Open);
Expand Down
4 changes: 2 additions & 2 deletions DataPackChecker/Parsers/PredicateParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@

namespace DataPackChecker.Parsers {
static class PredicateParser {
private static readonly Regex NAMESPACE_PATH_REGEX = new Regex(@"[\\/]predicates([\\/](?<path>.+?))?[\\/](?<name>[^\\/]+)\.json$");
private static readonly Regex NamespacePathRegex = new Regex(@"[\\/]predicates([\\/](?<path>.+?))?[\\/](?<name>[^\\/]+)\.json$");
static public Predicate TryParse(string absPath, string nsPath) {
var match = NAMESPACE_PATH_REGEX.Match(absPath, nsPath.Length);
var match = NamespacePathRegex.Match(absPath, nsPath.Length);
if (!match.Success) return null;
var predicate = new Predicate(match.Groups["path"].Value.Replace('\\', '/'), match.Groups["name"].Value);
using FileStream fs = new FileStream(absPath, FileMode.Open);
Expand Down
4 changes: 2 additions & 2 deletions DataPackChecker/Parsers/RecipeParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@

namespace DataPackChecker.Parsers {
static class RecipeParser {
private static readonly Regex NAMESPACE_PATH_REGEX = new Regex(@"[\\/]recipes([\\/](?<path>.+?))?[\\/](?<name>[^\\/]+)\.json$");
private static readonly Regex NamespacePathRegex = new Regex(@"[\\/]recipes([\\/](?<path>.+?))?[\\/](?<name>[^\\/]+)\.json$");
static public Recipe TryParse(string absPath, string nsPath) {
var match = NAMESPACE_PATH_REGEX.Match(absPath, nsPath.Length);
var match = NamespacePathRegex.Match(absPath, nsPath.Length);
if (!match.Success) return null;
var recipe = new Recipe(match.Groups["path"].Value.Replace('\\', '/'), match.Groups["name"].Value);
using FileStream fs = new FileStream(absPath, FileMode.Open);
Expand Down
4 changes: 2 additions & 2 deletions DataPackChecker/Parsers/StructureParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@

namespace DataPackChecker.Parsers {
static class StructureParser {
private static readonly Regex NAMESPACE_PATH_REGEX = new Regex(@"[\\/]structures([\\/](?<path>.+?))?[\\/](?<name>[^\\/]+)\.nbt$");
private static readonly Regex NamespacePathRegex = new Regex(@"[\\/]structures([\\/](?<path>.+?))?[\\/](?<name>[^\\/]+)\.nbt$");
static public Structure TryParse(string absPath, string nsPath) {
var match = NAMESPACE_PATH_REGEX.Match(absPath, nsPath.Length);
var match = NamespacePathRegex.Match(absPath, nsPath.Length);
if (!match.Success) return null;
return new Structure(match.Groups["path"].Value.Replace('\\', '/'), match.Groups["name"].Value);
}
Expand Down
4 changes: 2 additions & 2 deletions DataPackChecker/Parsers/TagParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@

namespace DataPackChecker.Parsers {
static class TagParser {
private static readonly Regex NAMESPACE_PATH_REGEX = new Regex(@"[\\/]tags[\\/](?<type>[^\\/]+)([\\/](?<path>.+?))?[\\/](?<name>[^\\/]+)\.json$");
private static readonly Regex NamespacePathRegex = new Regex(@"[\\/]tags[\\/](?<type>[^\\/]+)([\\/](?<path>.+?))?[\\/](?<name>[^\\/]+)\.json$");
static public Tag TryParse(string absPath, string nsPath) {
var match = NAMESPACE_PATH_REGEX.Match(absPath, nsPath.Length);
var match = NamespacePathRegex.Match(absPath, nsPath.Length);
if (!match.Success) return null;
var type = Tag.TryGetType(match.Groups["type"].Value);
if (type == null) return null;
Expand Down
4 changes: 2 additions & 2 deletions DataPackChecker/Parsers/WorldGen/BiomeParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@

namespace DataPackChecker.Parsers.WorldGen {
static class BiomeParser {
private static readonly Regex NAMESPACE_PATH_REGEX = new Regex(@"[\\/]worldgen[\\/]biome([\\/](?<path>.+?))?[\\/](?<name>[^\\/]+)\.json$");
private static readonly Regex NamespacePathRegex = new Regex(@"[\\/]worldgen[\\/]biome([\\/](?<path>.+?))?[\\/](?<name>[^\\/]+)\.json$");
static public Biome TryParse(string absPath, string nsPath) {
var match = NAMESPACE_PATH_REGEX.Match(absPath, nsPath.Length);
var match = NamespacePathRegex.Match(absPath, nsPath.Length);
if (!match.Success) return null;
var biome = new Biome(match.Groups["path"].Value.Replace('\\', '/'), match.Groups["name"].Value);
using FileStream fs = new FileStream(absPath, FileMode.Open);
Expand Down
4 changes: 2 additions & 2 deletions DataPackChecker/Parsers/WorldGen/ConfiguredCarverParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@

namespace DataPackChecker.Parsers.WorldGen {
static class ConfiguredCarverParser {
private static readonly Regex NAMESPACE_PATH_REGEX = new Regex(@"[\\/]worldgen[\\/]configured_carver([\\/](?<path>.+?))?[\\/](?<name>[^\\/]+)\.json$");
private static readonly Regex NamespacePathRegex = new Regex(@"[\\/]worldgen[\\/]configured_carver([\\/](?<path>.+?))?[\\/](?<name>[^\\/]+)\.json$");
static public ConfiguredCarver TryParse(string absPath, string nsPath) {
var match = NAMESPACE_PATH_REGEX.Match(absPath, nsPath.Length);
var match = NamespacePathRegex.Match(absPath, nsPath.Length);
if (!match.Success) return null;
var configuredCarver = new ConfiguredCarver(match.Groups["path"].Value.Replace('\\', '/'), match.Groups["name"].Value);
using FileStream fs = new FileStream(absPath, FileMode.Open);
Expand Down
4 changes: 2 additions & 2 deletions DataPackChecker/Parsers/WorldGen/ConfiguredFeatureParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@

namespace DataPackChecker.Parsers.WorldGen {
static class ConfiguredFeatureParser {
private static readonly Regex NAMESPACE_PATH_REGEX = new Regex(@"[\\/]worldgen[\\/]configured_feature([\\/](?<path>.+?))?[\\/](?<name>[^\\/]+)\.json$");
private static readonly Regex NamespacePathRegex = new Regex(@"[\\/]worldgen[\\/]configured_feature([\\/](?<path>.+?))?[\\/](?<name>[^\\/]+)\.json$");
static public ConfiguredFeature TryParse(string absPath, string nsPath) {
var match = NAMESPACE_PATH_REGEX.Match(absPath, nsPath.Length);
var match = NamespacePathRegex.Match(absPath, nsPath.Length);
if (!match.Success) return null;
var configuredFeature = new ConfiguredFeature(match.Groups["path"].Value.Replace('\\', '/'), match.Groups["name"].Value);
using FileStream fs = new FileStream(absPath, FileMode.Open);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@

namespace DataPackChecker.Parsers.WorldGen {
static class ConfiguredStructureFeatureParser {
private static readonly Regex NAMESPACE_PATH_REGEX = new Regex(@"[\\/]worldgen[\\/]configured_structure_feature([\\/](?<path>.+?))?[\\/](?<name>[^\\/]+)\.json$");
private static readonly Regex NamespacePathRegex = new Regex(@"[\\/]worldgen[\\/]configured_structure_feature([\\/](?<path>.+?))?[\\/](?<name>[^\\/]+)\.json$");
static public ConfiguredStructureFeature TryParse(string absPath, string nsPath) {
var match = NAMESPACE_PATH_REGEX.Match(absPath, nsPath.Length);
var match = NamespacePathRegex.Match(absPath, nsPath.Length);
if (!match.Success) return null;
var configuredStructureFeature = new ConfiguredStructureFeature(match.Groups["path"].Value.Replace('\\', '/'), match.Groups["name"].Value);
using FileStream fs = new FileStream(absPath, FileMode.Open);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@

namespace DataPackChecker.Parsers.WorldGen {
static class ConfiguredSurfaceBuilderParser {
private static readonly Regex NAMESPACE_PATH_REGEX = new Regex(@"[\\/]worldgen[\\/]configured_surface_builder([\\/](?<path>.+?))?[\\/](?<name>[^\\/]+)\.json$");
private static readonly Regex NamespacePathRegex = new Regex(@"[\\/]worldgen[\\/]configured_surface_builder([\\/](?<path>.+?))?[\\/](?<name>[^\\/]+)\.json$");
static public ConfiguredSurfaceBuilder TryParse(string absPath, string nsPath) {
var match = NAMESPACE_PATH_REGEX.Match(absPath, nsPath.Length);
var match = NamespacePathRegex.Match(absPath, nsPath.Length);
if (!match.Success) return null;
var configuredSurfaceBuilder = new ConfiguredSurfaceBuilder(match.Groups["path"].Value.Replace('\\', '/'), match.Groups["name"].Value);
using FileStream fs = new FileStream(absPath, FileMode.Open);
Expand Down
4 changes: 2 additions & 2 deletions DataPackChecker/Parsers/WorldGen/NoiseSettingsParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@

namespace DataPackChecker.Parsers.WorldGen {
static class NoiseSettingsParser {
private static readonly Regex NAMESPACE_PATH_REGEX = new Regex(@"[\\/]worldgen[\\/]noise_settings([\\/](?<path>.+?))?[\\/](?<name>[^\\/]+)\.json$");
private static readonly Regex NamespacePathRegex = new Regex(@"[\\/]worldgen[\\/]noise_settings([\\/](?<path>.+?))?[\\/](?<name>[^\\/]+)\.json$");
static public NoiseSettings TryParse(string absPath, string nsPath) {
var match = NAMESPACE_PATH_REGEX.Match(absPath, nsPath.Length);
var match = NamespacePathRegex.Match(absPath, nsPath.Length);
if (!match.Success) return null;
var noiseSettings = new NoiseSettings(match.Groups["path"].Value.Replace('\\', '/'), match.Groups["name"].Value);
using FileStream fs = new FileStream(absPath, FileMode.Open);
Expand Down
4 changes: 2 additions & 2 deletions DataPackChecker/Parsers/WorldGen/ProcessorListParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@

namespace DataPackChecker.Parsers.WorldGen {
static class ProcessorListParser {
private static readonly Regex NAMESPACE_PATH_REGEX = new Regex(@"[\\/]worldgen[\\/]processor_list([\\/](?<path>.+?))?[\\/](?<name>[^\\/]+)\.json$");
private static readonly Regex NamespacePathRegex = new Regex(@"[\\/]worldgen[\\/]processor_list([\\/](?<path>.+?))?[\\/](?<name>[^\\/]+)\.json$");
static public ProcessorList TryParse(string absPath, string nsPath) {
var match = NAMESPACE_PATH_REGEX.Match(absPath, nsPath.Length);
var match = NamespacePathRegex.Match(absPath, nsPath.Length);
if (!match.Success) return null;
var processorList = new ProcessorList(match.Groups["path"].Value.Replace('\\', '/'), match.Groups["name"].Value);
using FileStream fs = new FileStream(absPath, FileMode.Open);
Expand Down
4 changes: 2 additions & 2 deletions DataPackChecker/Parsers/WorldGen/TemplatePoolParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@

namespace DataPackChecker.Parsers.WorldGen {
static class TemplatePoolParser {
private static readonly Regex NAMESPACE_PATH_REGEX = new Regex(@"[\\/]worldgen[\\/]template_pool([\\/](?<path>.+?))?[\\/](?<name>[^\\/]+)\.json$");
private static readonly Regex NamespacePathRegex = new Regex(@"[\\/]worldgen[\\/]template_pool([\\/](?<path>.+?))?[\\/](?<name>[^\\/]+)\.json$");
static public TemplatePool TryParse(string absPath, string nsPath) {
var match = NAMESPACE_PATH_REGEX.Match(absPath, nsPath.Length);
var match = NamespacePathRegex.Match(absPath, nsPath.Length);
if (!match.Success) return null;
var templatePool = new TemplatePool(match.Groups["path"].Value.Replace('\\', '/'), match.Groups["name"].Value);
using FileStream fs = new FileStream(absPath, FileMode.Open);
Expand Down
29 changes: 22 additions & 7 deletions DataPackChecker/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,43 +8,58 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;

namespace DataPackChecker {
class Program {
const string VERSION = "Data Pack Checker 3.0.0";
const string Version = "Data Pack Checker 3.0.0";

static void Main(string[] args) {
var parser = new Parser(with => {
with.EnableDashDash = true;
});

var parserResult = parser.ParseArguments<Options>(args);
parserResult.WithParsed<Options>(options => Run(parserResult, options))
parserResult.WithParsed(options => Run(parserResult, options))
.WithNotParsed(errs => DisplayErrors(parserResult, errs));
}

static void DisplayErrors<T>(ParserResult<T> result, IEnumerable<Error> errs) {
private static void DisplayErrors<T>(ParserResult<T> result, IEnumerable<Error> errs) {
if (errs.IsVersion()) {
DisplayVersion();
} else {
DisplayHelp(result);
}
}

static void DisplayHelp<T>(ParserResult<T> result) {
private static void DisplayHelp<T>(ParserResult<T> result) {
var helpText = HelpText.AutoBuild(result, h => {
h.Heading = VERSION;
h.Heading = Version;
h.Copyright = "Made by Bertie2011 / ThrownException / Bertiecrafter";
return HelpText.DefaultParsingErrorsHandler(result, h);
}, e => e);
Console.WriteLine(helpText);
}

static void DisplayVersion() {
Console.WriteLine(VERSION);
private static void DisplayVersion() {
Console.WriteLine(Version);
}

private static void FromFile(string path) {
try {
Main(ConsoleHelper.CreateArgs(File.ReadAllLines(path)[0]));
} catch (Exception e) {
ConsoleHelper.WriteError(e);
}
}

private static void Run<T>(ParserResult<T> result, Options options) {
if (!string.IsNullOrWhiteSpace(options.ArgsPath)) {
FromFile(options.ArgsPath);
return;
}

var (rules, errors) = RuleRegistry.FromDirectory(Path.Join(".", "Rules"));
var ruleInfo = new RuleInfoPrinter(rules);
var checker = new Checker(rules);
Expand Down
3 changes: 2 additions & 1 deletion DataPackChecker/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"profiles": {
"DataPackChecker": {
"commandName": "Project"
"commandName": "Project",
"commandLineArgs": "-f \"args.txt\""
}
}
}
Loading

0 comments on commit 06a3c5b

Please sign in to comment.