Skip to content

Commit

Permalink
Optimization and code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Meeples10 committed Oct 5, 2023
1 parent ae300e4 commit 07ae7aa
Show file tree
Hide file tree
Showing 29 changed files with 1,746 additions and 1,981 deletions.
9 changes: 5 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
# Add any directories, files, or patterns you don't want to be tracked by version control
*.class
bin/
/bin/
*.jar
*.war
*.ear
*.zip
*.tar.gz
*.rar
hs_err_pid*
.classpath
.project
.settings/
/.classpath
/.project
/.settings/
*.csv
*.html
/target/
Expand All @@ -19,3 +19,4 @@ hs_err_pid*
/*.nbt
/*.mclevel
/*_stats.txt
/bedrock/
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

<groupId>io.github.meeples10.mcresourceanalyzer</groupId>
<artifactId>mc-resource-analyzer</artifactId>
<version>1.2.2</version>
<version>1.2.3-SNAPSHOT</version>
<packaging>jar</packaging>

<name>MCResourceAnalyzer</name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ public abstract class AnalyzerThread extends Thread {
public AnalyzerThread(RegionAnalyzer ra, Region region) {
this.ra = ra;
r = region;
setName(r.name);
}

public void run() {
Expand All @@ -26,24 +25,15 @@ private Analysis combineAnalyses() {
Analysis out = new Analysis();
for(Analysis a : analyses) {
for(String blockName : a.blocks.keySet()) {
long count = a.blocks.get(blockName);
if(out.blocks.containsKey(blockName)) {
out.blocks.put(blockName, out.blocks.get(blockName) + count);
} else {
out.blocks.put(blockName, count);
}
out.blocks.put(blockName, out.blocks.getOrDefault(blockName, 0L) + a.blocks.get(blockName));
}
for(String blockName : a.heights.keySet()) {
if(!out.heights.containsKey(blockName)) {
out.heights.put(blockName, new Hashtable<Integer, Long>());
}
for(int y : a.heights.get(blockName).keySet()) {
long count = a.heights.get(blockName).get(y);
if(out.heights.get(blockName).containsKey(y)) {
out.heights.get(blockName).put(y, out.heights.get(blockName).get(y) + count);
} else {
out.heights.get(blockName).put(y, count);
}
out.heights.get(blockName).put(y,
out.heights.get(blockName).getOrDefault(y, 0L) + a.heights.get(blockName).get(y));
}
}
}
Expand Down
36 changes: 14 additions & 22 deletions src/main/java/io/github/meeples10/mcresourceanalyzer/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.nio.file.Files;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -74,7 +75,7 @@ public static void main(String[] args) {
RegionAnalyzer analyzer;
try {
analyzer = selectedVersion.getAnalyzerInstance();
} catch(InstantiationException | IllegalAccessException e) {
} catch(Exception e) {
e.printStackTrace();
System.exit(1);
return;
Expand Down Expand Up @@ -192,41 +193,32 @@ private static int parseArgs(ParseResult pr) {
return 0;
}

public static String formatRegionName(File parent, File f) {
return f.getPath().split(parent.getName())[1].substring(1);
}

/**
* @author Estragon#9379
*
* <p>Many thanks to Estragon#9379 on Discord for this code.</p>
*/
public static int[] unstream(int bitsPerValue, int wordSize, boolean slack, long[] data) {
// in: bits per value, word size, ignore spare bits, data
// out: decoded array
List<Integer> out = new ArrayList<Integer>();
if(slack) {
wordSize = (int) Math.floor(wordSize / bitsPerValue) * bitsPerValue;
}
int bl = 0;
int v = 0;
int[] out = new int[data.length];
int size = 0;
for(int i = 0; i < data.length; i++) {
for(int n = 0; n < wordSize; n++) {
int bit = (int) ((data[i] >> n) & 0x01);
v = (bit << bl) | v;
bl++;
if(bl >= bitsPerValue) {
out.add(v);
out[size++] = v;
v = 0;
bl = 0;
}
}
}
int[] array = new int[out.size()];
for(int i = 0; i < out.size(); i++) {
array[i] = out.get(i);
}
return array;
return Arrays.copyOfRange(out, 0, size);
}

public static int bitLength(int i) {
Expand Down Expand Up @@ -273,14 +265,6 @@ private static void loadBlocksToMerge() throws IOException {
}
}

public static String getStringID(String id) {
return BLOCK_NAMES.getOrDefault(id, id);
}

public static String getOutputPrefix() {
return outputPrefix.equals("") ? "data" : outputPrefix;
}

public static List<String> readLines(InputStream stream) throws IOException {
List<String> lines = new ArrayList<>();
BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
Expand All @@ -292,6 +276,14 @@ public static List<String> readLines(InputStream stream) throws IOException {
return lines;
}

public static String getStringID(String id) {
return BLOCK_NAMES.getOrDefault(id, id);
}

public static String getOutputPrefix() {
return outputPrefix.equals("") ? "data" : outputPrefix;
}

public static void print(Object s) {
if(!silent) System.out.print(s);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@

class Region {
public final RegionFile file;
public final String name;
public final Set<Chunk> chunks = new HashSet<>();

public Region(File file, String name) {
public Region(File file) {
this.file = new RegionFile(file);
this.name = name;
}

public void addChunk(int x, int z) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,54 +235,47 @@ static boolean mergeStates(byte id) {
return mergeStates((int) id);
}

/* THIS IS A HACK TO ACCOUNT FOR NONEXISTENT SECTIONS AT HIGH Y VALUES */
/* THIS IS A HACK TO ACCOUNT FOR MISSING SECTIONS AT HIGH Y VALUES */
static void airHack(Analysis a, int sectionY, String airID) {
if(Main.allowHack && sectionY < 15) {
if(!a.blocks.containsKey(airID)) a.blocks.put(airID, 0L);
if(!a.heights.containsKey(airID)) a.heights.put(airID, new Hashtable<Integer, Long>());
for(; sectionY < 16; sectionY++) {
a.blocks.put(airID, a.blocks.get(airID) + 4096L);
a.blocks.put(airID, a.blocks.getOrDefault(airID, 0L) + 4096L);
for(int y = sectionY * 16; y < sectionY * 16 + 16; y++) {
if(a.heights.get(airID).containsKey(y)) {
a.heights.get(airID).put(y, a.heights.get(airID).get(y) + 256L);
} else {
a.heights.get(airID).put(y, 256L);
}
a.heights.get(airID).put(y, a.heights.get(airID).getOrDefault(y, 0L) + 256L);
}
}
}
}

private int getMinimumY() {
if(version != Version.ANVIL_2021 && version != Version.ANVIL_118) {
return 0;
}
int min = Integer.MAX_VALUE;
for(Map<Integer, Long> map : heightCounter.values()) {
for(int i : map.keySet()) {
if(i < min) {
min = i;
for(int y : map.keySet()) {
if(y < min) {
min = y;
}
}
}
if(version == Version.ANVIL_2021 || version == Version.ANVIL_118) {
return min < 0 ? min : 0;
} else {
return 0;
}
return min < 0 ? min : 0;
}

private int getMaximumY() {
if(version == Version.INDEV || version == Version.ALPHA || version == Version.MCREGION) {
return 127;
}
int max = Integer.MIN_VALUE;
for(Map<Integer, Long> map : heightCounter.values()) {
for(int i : map.keySet()) {
if(i > max) {
max = i;
for(int y : map.keySet()) {
if(y > max) {
max = y;
}
}
}
if(version == Version.INDEV || version == Version.ALPHA || version == Version.MCREGION) {
return 127;
} else {
return max > 255 ? max : 255;
}
return max > 255 ? max : 255;
}

synchronized void add(Analysis a) {
Expand Down Expand Up @@ -319,8 +312,8 @@ private Version(Class<? extends RegionAnalyzer> analyzerClass, boolean usesDirec
this.usesDirectory = usesDirectory;
}

public RegionAnalyzer getAnalyzerInstance() throws InstantiationException, IllegalAccessException {
return analyzerClass.newInstance();
public RegionAnalyzer getAnalyzerInstance() throws Exception {
return analyzerClass.getDeclaredConstructor().newInstance();
}

public boolean usesDirectory() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,18 +83,16 @@ private void analyzeChunk(byte[] blocks, byte[] data) {
: Byte.toString(blockID) + ":" + Byte.toString(blockData);
}

if(a.blocks.containsKey(blockName)) {
a.blocks.put(blockName, a.blocks.get(blockName) + 1L);
a.blocks.put(blockName, a.blocks.getOrDefault(blockName, 0L) + 1L);
if(a.heights.containsKey(blockName)) {
a.heights.get(blockName).put(y, a.heights.get(blockName).getOrDefault(y, 0L) + 1L);
} else {
a.blocks.put(blockName, 1L);
}
if(!a.heights.containsKey(blockName)) {
a.heights.put(blockName, new Hashtable<Integer, Long>());
}
if(a.heights.get(blockName).containsKey(y)) {
a.heights.get(blockName).put(y, a.heights.get(blockName).get(y) + 1L);
} else {
a.heights.get(blockName).put(y, 1L);
final int yf = y;
a.heights.put(blockName, new Hashtable<Integer, Long>() {
{
put(yf, 1L);
}
});
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public void validateInput(File regionDir) {
@Override
public void findChunks(File regionDir) {
for(File f : regionDir.listFiles(Main.DS_STORE_FILTER)) {
Region r = new Region(f, Main.formatRegionName(regionDir, f));
Region r = new Region(f);
for(int x = 0; x < 32; x++) {
for(int z = 0; z < 32; z++) {
if(r.file.hasChunk(x, z)) r.addChunk(x, z);
Expand Down Expand Up @@ -59,50 +59,41 @@ public void process() {

private Analysis processRegion(RegionFile r, int x, int z) throws IOException {
DataInputStream chunkDataInputStream = r.getChunkDataInputStream(x, z);
if(chunkDataInputStream == null) {
// Skip malformed chunks
return null;
}
NBTTagList sections = CompressedStreamTools.read(r.getChunkDataInputStream(x, z)).getTagList("sections", 10);
return analyzeChunk(sections);
if(chunkDataInputStream == null) return null;
return analyzeChunk(CompressedStreamTools.read(r.getChunkDataInputStream(x, z)).getTagList("sections", 10));
}

private Analysis analyzeChunk(NBTTagList sections) {
Analysis a = new Analysis();
int i = 0;
for(; i < sections.tagCount(); i++) {
NBTTagCompound tag = sections.getCompoundTagAt(i);

NBTTagLongArray blockStatesTag = ((NBTTagLongArray) tag.getCompoundTag("block_states").getTag("data"));
long[] blockStates = blockStatesTag == null ? new long[0] : blockStatesTag.get();
NBTTagList palette = tag.getCompoundTag("block_states").getTagList("palette", 10);
NBTTagCompound section = sections.getCompoundTagAt(i);
NBTTagList palette = section.getCompoundTag("block_states").getTagList("palette", 10);
if(palette.hasNoTags()) continue;
NBTTagLongArray blockStatesTag = ((NBTTagLongArray) section.getCompoundTag("block_states").getTag("data"));
int bitLength = Main.bitLength(palette.tagCount() - 1);
bitLength = bitLength < 4 ? 4 : bitLength;
int[] blocks = Main.unstream(bitLength, 64, true, blockStates);
if(blocks.length == 0) continue; // skip empty sections
int sectionY = tag.getByte("Y");
int[] blocks = Main.unstream(bitLength < 4 ? 4 : bitLength, 64, true,
blockStatesTag == null ? new long[0] : blockStatesTag.get());
if(blocks.length == 0) continue;
int sectionY = section.getByte("Y");

for(int y = 0; y < 16; y++) {
for(int x = 0; x < 16; x++) {
for(int z = 0; z < 16; z++) {
int actualY = sectionY * 16 + y;
int blockIndex = y * 16 * 16 + z * 16 + x;
String block = palette.getStringTagAt(blocks[blockIndex]);
int startIndex = block.indexOf("Name:\"");
String blockName = block.substring(startIndex + 6, block.indexOf('"', startIndex + 6));
if(a.blocks.containsKey(blockName)) {
a.blocks.put(blockName, a.blocks.get(blockName) + 1L);
} else {
a.blocks.put(blockName, 1L);
}
if(!a.heights.containsKey(blockName)) {
a.heights.put(blockName, new Hashtable<Integer, Long>());
}
if(a.heights.get(blockName).containsKey(actualY)) {
a.heights.get(blockName).put(actualY, a.heights.get(blockName).get(actualY) + 1L);
String blockState = palette.getStringTagAt(blocks[y * 16 * 16 + z * 16 + x]);
int startIndex = blockState.indexOf("Name:\"") + 6;
String blockName = blockState.substring(startIndex, blockState.indexOf('"', startIndex));
a.blocks.put(blockName, a.blocks.getOrDefault(blockName, 0L) + 1L);
if(a.heights.containsKey(blockName)) {
a.heights.get(blockName).put(actualY,
a.heights.get(blockName).getOrDefault(actualY, 0L) + 1L);
} else {
a.heights.get(blockName).put(actualY, 1L);
a.heights.put(blockName, new Hashtable<Integer, Long>() {
{
put(actualY, 1L);
}
});
}
}
}
Expand Down
Loading

0 comments on commit 07ae7aa

Please sign in to comment.