Skip to content

Commit 06a3c5b

Browse files
committed
Added -f switch, string tokenizer (ConsoleHelper) and made all consts PascalCase
1 parent 68cdafd commit 06a3c5b

22 files changed

+100
-48
lines changed

DataPackChecker/ConsoleHelper.cs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Linq;
34
using System.Text;
45

56
namespace DataPackChecker {
67
static class ConsoleHelper {
8+
private static readonly char[] Quotes = new char[] { '"', '\'' };
9+
private static readonly char[] Escapable = new char[] { '"', '\'' };
710
static public void WriteLine(string text, ConsoleColor color = ConsoleColor.White) {
811
Console.ForegroundColor = color;
912
Console.WriteLine(text);
@@ -25,5 +28,36 @@ static public void WriteError(Exception e, ConsoleColor color = ConsoleColor.Red
2528
if (e.InnerException != null) WriteError(e.InnerException, color, depth + 1);
2629
if (depth == 0) Console.ForegroundColor = ConsoleColor.White;
2730
}
31+
32+
static public string[] CreateArgs(string line) {
33+
StringBuilder arg = new StringBuilder();
34+
char? quote = null;
35+
bool escape = false;
36+
List<string> args = new List<string>();
37+
for (int i = 0; i < line.Length; i++) {
38+
char c = line[i];
39+
40+
// Either process the created argument or record the next character.
41+
if (c == ' ' && quote == null && !escape) {
42+
args.Add(arg.ToString());
43+
arg.Clear();
44+
} else if (escape || !Quotes.Any(q => q == c) && c != '\\') {
45+
if (escape && !Escapable.Any(e => e == c)) arg.Append('\\');
46+
arg.Append(c);
47+
}
48+
49+
// Change modifiers
50+
if (escape) {
51+
escape = false;
52+
} else if (c == '\\') {
53+
escape = true;
54+
} else if (Quotes.Any(q => q == c)) {
55+
quote = quote == null ? c : (char?)null;
56+
}
57+
}
58+
59+
if (arg.Length > 0) args.Add(arg.ToString());
60+
return args.ToArray();
61+
}
2862
}
2963
}

DataPackChecker/Options.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
namespace DataPackChecker {
77
class Options {
8+
[Option('f', "arguments-file", HelpText = "Read arguments from a file.", SetName = "Args File")]
9+
public string ArgsPath { get; set; }
810
[Option('o', "keep-open", HelpText = "Keep process running after finishing.")]
911
public bool KeepOpen { get; set; }
1012
[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")]

DataPackChecker/Parsers/AdvancementParser.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88

99
namespace DataPackChecker.Parsers {
1010
static class AdvancementParser {
11-
private static readonly Regex NAMESPACE_PATH_REGEX = new Regex(@"[\\/]advancements([\\/](?<path>.+?))?[\\/](?<name>[^\\/]+)\.json$");
11+
private static readonly Regex NamespacePathRegex = new Regex(@"[\\/]advancements([\\/](?<path>.+?))?[\\/](?<name>[^\\/]+)\.json$");
1212
static public Advancement TryParse(string absPath, string nsPath) {
13-
var match = NAMESPACE_PATH_REGEX.Match(absPath, nsPath.Length);
13+
var match = NamespacePathRegex.Match(absPath, nsPath.Length);
1414
if (!match.Success) return null;
1515
var advancement = new Advancement(match.Groups["path"].Value.Replace('\\', '/'), match.Groups["name"].Value);
1616
using FileStream fs = new FileStream(absPath, FileMode.Open);

DataPackChecker/Parsers/Dimensions/DimensionParser.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88

99
namespace DataPackChecker.Parsers.Dimensions {
1010
static class DimensionParser {
11-
private static readonly Regex NAMESPACE_PATH_REGEX = new Regex(@"[\\/]dimension([\\/](?<path>.+?))?[\\/](?<name>[^\\/]+)\.json$");
11+
private static readonly Regex NamespacePathRegex = new Regex(@"[\\/]dimension([\\/](?<path>.+?))?[\\/](?<name>[^\\/]+)\.json$");
1212
static public Dimension TryParse(string absPath, string nsPath) {
13-
var match = NAMESPACE_PATH_REGEX.Match(absPath, nsPath.Length);
13+
var match = NamespacePathRegex.Match(absPath, nsPath.Length);
1414
if (!match.Success) return null;
1515
var dimension = new Dimension(match.Groups["path"].Value.Replace('\\', '/'), match.Groups["name"].Value);
1616
using FileStream fs = new FileStream(absPath, FileMode.Open);

DataPackChecker/Parsers/Dimensions/DimensionTypeParser.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88

99
namespace DataPackChecker.Parsers.Dimensions {
1010
static class DimensionTypeParser {
11-
private static readonly Regex NAMESPACE_PATH_REGEX = new Regex(@"[\\/]dimension_type([\\/](?<path>.+?))?[\\/](?<name>[^\\/]+)\.json$");
11+
private static readonly Regex NamespacePathRegex = new Regex(@"[\\/]dimension_type([\\/](?<path>.+?))?[\\/](?<name>[^\\/]+)\.json$");
1212
static public DimensionType TryParse(string absPath, string nsPath) {
13-
var match = NAMESPACE_PATH_REGEX.Match(absPath, nsPath.Length);
13+
var match = NamespacePathRegex.Match(absPath, nsPath.Length);
1414
if (!match.Success) return null;
1515
var dimensionType = new DimensionType(match.Groups["path"].Value.Replace('\\', '/'), match.Groups["name"].Value);
1616
using FileStream fs = new FileStream(absPath, FileMode.Open);

DataPackChecker/Parsers/FunctionParser.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55

66
namespace DataPackChecker.Parsers {
77
static public class FunctionParser {
8-
private static readonly Regex NAMESPACE_PATH_REGEX = new Regex(@"[\\/]functions([\\/](?<path>.+?))?[\\/](?<name>[^\\/]+)\.mcfunction$");
8+
private static readonly Regex NamespacePathRegex = new Regex(@"[\\/]functions([\\/](?<path>.+?))?[\\/](?<name>[^\\/]+)\.mcfunction$");
99
static public Function TryParse(string absPath, string nsPath) {
10-
var match = NAMESPACE_PATH_REGEX.Match(absPath, nsPath.Length);
10+
var match = NamespacePathRegex.Match(absPath, nsPath.Length);
1111
if (!match.Success) return null;
1212
var function = new Function(match.Groups["path"].Value.Replace('\\', '/'), match.Groups["name"].Value);
1313
function.Commands = File.ReadAllLines(absPath).Select((c, i) => new Command(i + 1, c)).ToList();

DataPackChecker/Parsers/LootTableParser.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88

99
namespace DataPackChecker.Parsers {
1010
static class LootTableParser {
11-
private static readonly Regex NAMESPACE_PATH_REGEX = new Regex(@"[\\/]loot_tables([\\/](?<path>.+?))?[\\/](?<name>[^\\/]+)\.json$");
11+
private static readonly Regex NamespacePathRegex = new Regex(@"[\\/]loot_tables([\\/](?<path>.+?))?[\\/](?<name>[^\\/]+)\.json$");
1212
static public LootTable TryParse(string absPath, string nsPath) {
13-
var match = NAMESPACE_PATH_REGEX.Match(absPath, nsPath.Length);
13+
var match = NamespacePathRegex.Match(absPath, nsPath.Length);
1414
if (!match.Success) return null;
1515
var lootTable = new LootTable(match.Groups["path"].Value.Replace('\\', '/'), match.Groups["name"].Value);
1616
using FileStream fs = new FileStream(absPath, FileMode.Open);

DataPackChecker/Parsers/PredicateParser.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88

99
namespace DataPackChecker.Parsers {
1010
static class PredicateParser {
11-
private static readonly Regex NAMESPACE_PATH_REGEX = new Regex(@"[\\/]predicates([\\/](?<path>.+?))?[\\/](?<name>[^\\/]+)\.json$");
11+
private static readonly Regex NamespacePathRegex = new Regex(@"[\\/]predicates([\\/](?<path>.+?))?[\\/](?<name>[^\\/]+)\.json$");
1212
static public Predicate TryParse(string absPath, string nsPath) {
13-
var match = NAMESPACE_PATH_REGEX.Match(absPath, nsPath.Length);
13+
var match = NamespacePathRegex.Match(absPath, nsPath.Length);
1414
if (!match.Success) return null;
1515
var predicate = new Predicate(match.Groups["path"].Value.Replace('\\', '/'), match.Groups["name"].Value);
1616
using FileStream fs = new FileStream(absPath, FileMode.Open);

DataPackChecker/Parsers/RecipeParser.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88

99
namespace DataPackChecker.Parsers {
1010
static class RecipeParser {
11-
private static readonly Regex NAMESPACE_PATH_REGEX = new Regex(@"[\\/]recipes([\\/](?<path>.+?))?[\\/](?<name>[^\\/]+)\.json$");
11+
private static readonly Regex NamespacePathRegex = new Regex(@"[\\/]recipes([\\/](?<path>.+?))?[\\/](?<name>[^\\/]+)\.json$");
1212
static public Recipe TryParse(string absPath, string nsPath) {
13-
var match = NAMESPACE_PATH_REGEX.Match(absPath, nsPath.Length);
13+
var match = NamespacePathRegex.Match(absPath, nsPath.Length);
1414
if (!match.Success) return null;
1515
var recipe = new Recipe(match.Groups["path"].Value.Replace('\\', '/'), match.Groups["name"].Value);
1616
using FileStream fs = new FileStream(absPath, FileMode.Open);

DataPackChecker/Parsers/StructureParser.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88

99
namespace DataPackChecker.Parsers {
1010
static class StructureParser {
11-
private static readonly Regex NAMESPACE_PATH_REGEX = new Regex(@"[\\/]structures([\\/](?<path>.+?))?[\\/](?<name>[^\\/]+)\.nbt$");
11+
private static readonly Regex NamespacePathRegex = new Regex(@"[\\/]structures([\\/](?<path>.+?))?[\\/](?<name>[^\\/]+)\.nbt$");
1212
static public Structure TryParse(string absPath, string nsPath) {
13-
var match = NAMESPACE_PATH_REGEX.Match(absPath, nsPath.Length);
13+
var match = NamespacePathRegex.Match(absPath, nsPath.Length);
1414
if (!match.Success) return null;
1515
return new Structure(match.Groups["path"].Value.Replace('\\', '/'), match.Groups["name"].Value);
1616
}

0 commit comments

Comments
 (0)