Skip to content

Commit

Permalink
Allow input region directory to be specified as a command line argument
Browse files Browse the repository at this point in the history
  • Loading branch information
Meeples10 committed Aug 12, 2021
1 parent 1e375d6 commit 32dac20
Show file tree
Hide file tree
Showing 10 changed files with 40 additions and 36 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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=<path>`: 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=<path>`: 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=<path>`: Use this argument to specify an input region directory or .mclevel file other than the default. Note that `<path>` is relative to the program's working directory.

### Version compatibility

Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>io.github.meeples10.mcresourceanalyzer</groupId>
<artifactId>mc-resource-analyzer</artifactId>
<version>1.1.1</version>
<version>1.1.2</version>
<packaging>jar</packaging>

<properties>
Expand Down
28 changes: 20 additions & 8 deletions src/main/java/io/github/meeples10/mcresourceanalyzer/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down Expand Up @@ -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);
}
Expand All @@ -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,
Expand All @@ -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);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<? extends RegionAnalyzer> analyzerClass;
private final boolean usesDirectory;

private Version(String versionName, Class<? extends RegionAnalyzer> analyzerClass) {
private Version(String versionName, Class<? extends RegionAnalyzer> analyzerClass, boolean usesDirectory) {
this.versionName = versionName;
this.analyzerClass = analyzerClass;
this.usesDirectory = usesDirectory;
}

public String toString() {
Expand All @@ -129,5 +130,9 @@ public String toString() {
public RegionAnalyzer getAnalyzerInstance() throws InstantiationException, IllegalAccessException {
return analyzerClass.newInstance();
}

public boolean usesDirectory() {
return usesDirectory;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<File> chunkFiles = new ArrayList<>();
for(File f : world.listFiles()) {
if(!f.isDirectory()) continue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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())) + ")... ");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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())) + ")... ");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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())) + ")... ");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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())) + ")... ");
Expand Down

0 comments on commit 32dac20

Please sign in to comment.