From 32dac207551f6ec60628b4531c8be9e477b1b094 Mon Sep 17 00:00:00 2001 From: Meeples10 Date: Thu, 12 Aug 2021 14:41:39 -0400 Subject: [PATCH] Allow input region directory to be specified as a command line argument --- README.md | 1 + pom.xml | 2 +- .../meeples10/mcresourceanalyzer/Main.java | 28 +++++++++++++------ .../mcresourceanalyzer/RegionAnalyzer.java | 21 ++++++++------ .../RegionAnalyzerAlpha.java | 5 ---- .../RegionAnalyzerAnvil2012.java | 2 +- .../RegionAnalyzerAnvil2018.java | 2 +- .../RegionAnalyzerAnvil2021.java | 2 +- .../RegionAnalyzerIndev.java | 7 +---- .../RegionAnalyzerMCRegion.java | 6 +--- 10 files changed, 40 insertions(+), 36 deletions(-) diff --git a/README.md b/README.md index b4cfab3..5807d48 100644 --- a/README.md +++ b/README.md @@ -32,6 +32,7 @@ Alternatively, to explicitly specify a version from the command line and skip th - `modernize-ids`: If analyzing regions saved before 1.13, numeric block IDs will be replaced with their modern string representations. If no string corresponding to the numeric ID is found, the numeric ID will be saved instead. - `blocks=`: When using the `modernize-ids` argument on a world with block IDs outside the range of 0-255, use this to specify the path to a file containing block IDs in the same format as [blocks.properties](https://github.com/Meeples10/MCResourceAnalyzer/blob/master/src/main/resources/blocks.properties). - `merge=`: When analyzing a world with block IDs outside the range of 0-255, use this to specify the path to a file containing block IDs in the same format as [merge.properties](https://github.com/Meeples10/MCResourceAnalyzer/blob/master/src/main/resources/merge.properties). Any block with an ID listed in this file will have all of its variants merged into a single value. +- `input=`: Use this argument to specify an input region directory or .mclevel file other than the default. Note that `` is relative to the program's working directory. ### Version compatibility diff --git a/pom.xml b/pom.xml index e71ecfa..192bd0d 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ io.github.meeples10.mcresourceanalyzer mc-resource-analyzer - 1.1.1 + 1.1.2 jar diff --git a/src/main/java/io/github/meeples10/mcresourceanalyzer/Main.java b/src/main/java/io/github/meeples10/mcresourceanalyzer/Main.java index 9dd21ac..910073b 100644 --- a/src/main/java/io/github/meeples10/mcresourceanalyzer/Main.java +++ b/src/main/java/io/github/meeples10/mcresourceanalyzer/Main.java @@ -27,12 +27,14 @@ public class Main { static boolean allowHack = true; static boolean generateTable = false; static boolean versionSelect = false; - static boolean versionSelectedExplicitly = false; + static boolean versionOverride = false; static boolean modernizeIDs = false; + static boolean inputOverride = false; public static void main(String[] args) { DECIMAL_FORMAT.setMaximumFractionDigits(10); RegionAnalyzer.Version selectedVersion = RegionAnalyzer.Version.values()[0]; + File inputFile = new File("region"); for(String arg : args) { if(arg.equalsIgnoreCase("statistics")) { saveStatistics = true; @@ -43,7 +45,7 @@ public static void main(String[] args) { } else if(arg.equalsIgnoreCase("version-select")) { versionSelect = true; } else if(arg.toLowerCase().startsWith("version-select=")) { - versionSelectedExplicitly = true; + versionOverride = true; String v = arg.split("=", 2)[1].toUpperCase(); try { selectedVersion = RegionAnalyzer.Version.valueOf(v); @@ -77,6 +79,9 @@ public static void main(String[] args) { } } else if(arg.equalsIgnoreCase("modernize-ids")) { modernizeIDs = true; + } else if(arg.toLowerCase().startsWith("input=")) { + inputOverride = true; + inputFile = new File(arg.split("=", 2)[1]); } else { System.err.println("Unknown argument: " + arg); } @@ -89,9 +94,9 @@ public static void main(String[] args) { } System.out.println("Save statistics: " + saveStatistics + "\nAllow empty section hack: " + allowHack + "\nGenerate HTML table: " + generateTable + "\nVersion select: " - + (versionSelectedExplicitly ? selectedVersion : versionSelect) + "\nModernize block IDs: " - + modernizeIDs + "\nBlock IDs: " + BLOCK_NAMES.size() + "\nBlock IDs to merge: " - + BLOCKS_TO_MERGE.size() + "\n--------------------------------"); + + (versionOverride ? selectedVersion : versionSelect) + "\nModernize block IDs: " + modernizeIDs + + "\nBlock IDs: " + BLOCK_NAMES.size() + "\nBlock IDs to merge: " + BLOCKS_TO_MERGE.size() + "\nInput: " + + inputFile.getPath() + "\n--------------------------------"); RegionAnalyzer analyzer; if(versionSelect) { Object returnedVersion = JOptionPane.showInputDialog(null, @@ -109,12 +114,19 @@ public static void main(String[] args) { return; } if(analyzer == null) analyzer = new RegionAnalyzerAnvil2021(); - analyzer.analyze(new File("region")); + if(inputOverride) { + if(inputFile.isDirectory() != selectedVersion.usesDirectory()) { + System.err.println("Input must be a " + (selectedVersion.usesDirectory() ? "directory" : "file") + ": " + + inputFile.getAbsolutePath()); + System.exit(1); + } + } + analyzer.analyze(inputFile); System.out.println("Completed after " + millisToHMS(System.currentTimeMillis() - analyzer.getStartTime())); } - public static String formatRegionName(File f) { - return f.getPath().split("region")[1].substring(1); + public static String formatRegionName(File parent, File f) { + return f.getPath().split(parent.getName())[1].substring(1); } /** diff --git a/src/main/java/io/github/meeples10/mcresourceanalyzer/RegionAnalyzer.java b/src/main/java/io/github/meeples10/mcresourceanalyzer/RegionAnalyzer.java index cd797dc..40d0f50 100644 --- a/src/main/java/io/github/meeples10/mcresourceanalyzer/RegionAnalyzer.java +++ b/src/main/java/io/github/meeples10/mcresourceanalyzer/RegionAnalyzer.java @@ -106,20 +106,21 @@ int getMaximumY() { } public enum Version { - ANVIL_2021("Anvil (1.16 to 1.17)", RegionAnalyzerAnvil2021.class), ANVIL_2018("Anvil (1.13 to 1.15)", - RegionAnalyzerAnvil2018.class), ANVIL_2012("Anvil (1.2 to 1.12)", - RegionAnalyzerAnvil2012.class), MCREGION("McRegion (Beta 1.3 to 1.1)", - RegionAnalyzerMCRegion.class), ALPHA("Alpha (Infdev 20100327 to Beta 1.2)", - RegionAnalyzerAlpha.class), INDEV( - "Indev (Indev 0.31 20100122 to Infdev 20100325)", - RegionAnalyzerIndev.class); + ANVIL_2021("Anvil (1.16 to 1.17)", RegionAnalyzerAnvil2021.class, true), ANVIL_2018("Anvil (1.13 to 1.15)", + RegionAnalyzerAnvil2018.class, + true), ANVIL_2012("Anvil (1.2 to 1.12)", RegionAnalyzerAnvil2012.class, true), MCREGION( + "McRegion (Beta 1.3 to 1.1)", RegionAnalyzerMCRegion.class, + true), ALPHA("Alpha (Infdev 20100327 to Beta 1.2)", RegionAnalyzerAlpha.class, true), INDEV( + "Indev (Indev 0.31 20100122 to Infdev 20100325)", RegionAnalyzerIndev.class, false); private final String versionName; private final Class analyzerClass; + private final boolean usesDirectory; - private Version(String versionName, Class analyzerClass) { + private Version(String versionName, Class analyzerClass, boolean usesDirectory) { this.versionName = versionName; this.analyzerClass = analyzerClass; + this.usesDirectory = usesDirectory; } public String toString() { @@ -129,5 +130,9 @@ public String toString() { public RegionAnalyzer getAnalyzerInstance() throws InstantiationException, IllegalAccessException { return analyzerClass.newInstance(); } + + public boolean usesDirectory() { + return usesDirectory; + } } } diff --git a/src/main/java/io/github/meeples10/mcresourceanalyzer/RegionAnalyzerAlpha.java b/src/main/java/io/github/meeples10/mcresourceanalyzer/RegionAnalyzerAlpha.java index 7d04ec9..5d2c47f 100644 --- a/src/main/java/io/github/meeples10/mcresourceanalyzer/RegionAnalyzerAlpha.java +++ b/src/main/java/io/github/meeples10/mcresourceanalyzer/RegionAnalyzerAlpha.java @@ -18,11 +18,6 @@ public class RegionAnalyzerAlpha extends RegionAnalyzer { @Override public void analyze(File world) { - if(!world.exists()) { - System.out.println("Error: No world directory found at " + world.getAbsolutePath()); - System.exit(1); - } - List chunkFiles = new ArrayList<>(); for(File f : world.listFiles()) { if(!f.isDirectory()) continue; diff --git a/src/main/java/io/github/meeples10/mcresourceanalyzer/RegionAnalyzerAnvil2012.java b/src/main/java/io/github/meeples10/mcresourceanalyzer/RegionAnalyzerAnvil2012.java index 90842aa..c766d2b 100644 --- a/src/main/java/io/github/meeples10/mcresourceanalyzer/RegionAnalyzerAnvil2012.java +++ b/src/main/java/io/github/meeples10/mcresourceanalyzer/RegionAnalyzerAnvil2012.java @@ -31,7 +31,7 @@ public void analyze(File regionDir) { int rnum = 1; for(File f : regionDir.listFiles()) { long startTime = System.currentTimeMillis(); - String name = Main.formatRegionName(f); + String name = Main.formatRegionName(regionDir, f); RegionFile r = new RegionFile(f); System.out.print("Scanning region " + name + " [" + rnum + "/" + totalRegions + "] (modified " + Main.DATE_FORMAT.format(new Date(r.lastModified())) + ")... "); diff --git a/src/main/java/io/github/meeples10/mcresourceanalyzer/RegionAnalyzerAnvil2018.java b/src/main/java/io/github/meeples10/mcresourceanalyzer/RegionAnalyzerAnvil2018.java index 1e9c40d..76c7824 100644 --- a/src/main/java/io/github/meeples10/mcresourceanalyzer/RegionAnalyzerAnvil2018.java +++ b/src/main/java/io/github/meeples10/mcresourceanalyzer/RegionAnalyzerAnvil2018.java @@ -34,7 +34,7 @@ public void analyze(File regionDir) { int rnum = 1; for(File f : regionDir.listFiles()) { long startTime = System.currentTimeMillis(); - String name = Main.formatRegionName(f); + String name = Main.formatRegionName(regionDir, f); RegionFile r = new RegionFile(f); System.out.print("Scanning region " + name + " [" + rnum + "/" + totalRegions + "] (modified " + Main.DATE_FORMAT.format(new Date(r.lastModified())) + ")... "); diff --git a/src/main/java/io/github/meeples10/mcresourceanalyzer/RegionAnalyzerAnvil2021.java b/src/main/java/io/github/meeples10/mcresourceanalyzer/RegionAnalyzerAnvil2021.java index 171da98..c8f6b62 100644 --- a/src/main/java/io/github/meeples10/mcresourceanalyzer/RegionAnalyzerAnvil2021.java +++ b/src/main/java/io/github/meeples10/mcresourceanalyzer/RegionAnalyzerAnvil2021.java @@ -32,7 +32,7 @@ public void analyze(File regionDir) { int rnum = 1; for(File f : regionDir.listFiles()) { long startTime = System.currentTimeMillis(); - String name = Main.formatRegionName(f); + String name = Main.formatRegionName(regionDir, f); RegionFile r = new RegionFile(f); System.out.print("Scanning region " + name + " [" + rnum + "/" + totalRegions + "] (modified " + Main.DATE_FORMAT.format(new Date(r.lastModified())) + ")... "); diff --git a/src/main/java/io/github/meeples10/mcresourceanalyzer/RegionAnalyzerIndev.java b/src/main/java/io/github/meeples10/mcresourceanalyzer/RegionAnalyzerIndev.java index 328461c..b876dc2 100644 --- a/src/main/java/io/github/meeples10/mcresourceanalyzer/RegionAnalyzerIndev.java +++ b/src/main/java/io/github/meeples10/mcresourceanalyzer/RegionAnalyzerIndev.java @@ -15,12 +15,7 @@ public class RegionAnalyzerIndev extends RegionAnalyzer { @Override - public void analyze(File ignore) { - File world = new File("world.mclevel"); - if(!world.exists()) { - System.out.println("Error: No world file found at " + world.getAbsolutePath()); - System.exit(1); - } + public void analyze(File world) { try { processWorld(world); } catch(Exception e) { diff --git a/src/main/java/io/github/meeples10/mcresourceanalyzer/RegionAnalyzerMCRegion.java b/src/main/java/io/github/meeples10/mcresourceanalyzer/RegionAnalyzerMCRegion.java index 180883a..3bf12a3 100644 --- a/src/main/java/io/github/meeples10/mcresourceanalyzer/RegionAnalyzerMCRegion.java +++ b/src/main/java/io/github/meeples10/mcresourceanalyzer/RegionAnalyzerMCRegion.java @@ -17,10 +17,6 @@ public class RegionAnalyzerMCRegion extends RegionAnalyzer { @Override public void analyze(File regionDir) { - if(!regionDir.exists()) { - System.out.println("Error: No region directory found at " + regionDir.getAbsolutePath()); - System.exit(1); - } int totalRegions = regionDir.listFiles().length; if(totalRegions == 0) { System.out.println("Error: Region directory is empty"); @@ -30,7 +26,7 @@ public void analyze(File regionDir) { int rnum = 1; for(File f : regionDir.listFiles()) { long startTime = System.currentTimeMillis(); - String name = Main.formatRegionName(f); + String name = Main.formatRegionName(regionDir, f); RegionFile r = new RegionFile(f); System.out.print("Scanning region " + name + " [" + rnum + "/" + totalRegions + "] (modified " + Main.DATE_FORMAT.format(new Date(r.lastModified())) + ")... ");