Skip to content

Commit

Permalink
Merge pull request #116 from Querz/section-import
Browse files Browse the repository at this point in the history
Section import
  • Loading branch information
Querz authored Jun 22, 2020
2 parents 90dfc10 + a43d469 commit 070afe3
Show file tree
Hide file tree
Showing 49 changed files with 618 additions and 169 deletions.
54 changes: 32 additions & 22 deletions README.md

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ apply plugin: 'idea'
apply plugin: 'css'

group 'net.querz.mcaselector'
version '1.10'
version '1.11'

sourceCompatibility = 1.8

Expand All @@ -25,8 +25,8 @@ repositories {
}

dependencies {
compile 'com.github.Querz:NBT:5.3'
shadow 'com.github.Querz:NBT:5.3'
compile 'com.github.Querz:NBT:5.5'
shadow 'com.github.Querz:NBT:5.5'
testCompile group: 'junit', name: 'junit', version: '4.12'
}

Expand Down
8 changes: 4 additions & 4 deletions src/main/java/net/querz/mcaselector/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,8 @@ public static void loadFromIni() {
}
config.put(elements[0], elements[1]);
});
} catch (IOException e) {
Debug.error(e);
} catch (IOException ex) {
Debug.dumpException("failed to read settings.ini", ex);
}

try {
Expand All @@ -186,7 +186,7 @@ public static void loadFromIni() {
shadeWater = Boolean.parseBoolean(config.getOrDefault("ShadeWater", DEFAULT_SHADE_WATER + ""));
debug = Boolean.parseBoolean(config.getOrDefault("Debug", DEFAULT_DEBUG + ""));
} catch (Exception ex) {
Debug.errorf("error loading settings.ini: %s", ex.getMessage());
Debug.dumpException("error loading settings.ini", ex);
}
}

Expand Down Expand Up @@ -217,7 +217,7 @@ public static void exportConfig() {
try {
Files.write(file.toPath(), lines);
} catch (IOException ex) {
Debug.errorf("error writing settings.ini: %s", ex.getMessage());
Debug.dumpException("error writing settings.ini", ex);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public class BiomeField extends Field<Integer> {
validIDs.add(id);
}
} catch (IOException ex) {
Debug.error("error reading biomes.csv: ", ex.getMessage());
Debug.dumpException("error reading biomes.csv for BiomeField", ex);
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/net/querz/mcaselector/changer/FieldType.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ public enum FieldType {
public Field<?> newInstance() {
try {
return clazz.newInstance();
} catch (InstantiationException | IllegalAccessException e) {
Debug.error(e);
} catch (InstantiationException | IllegalAccessException ex) {
Debug.dumpException("failed to create new change field instance", ex);
return null;
}
}
Expand Down
100 changes: 54 additions & 46 deletions src/main/java/net/querz/mcaselector/debug/Debug.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,31 @@
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Arrays;
import java.util.Map;
import java.util.Objects;
import java.util.Properties;
import java.util.concurrent.ConcurrentHashMap;

public final class Debug {

private Debug() {}

private static ExceptionInfo lastException = null;
private static final Map<ExceptionInfo, ExceptionInfo> lastExceptions = new ConcurrentHashMap<>();

public static void dump(Object... objects) {
if (Config.debug()) {
for (Object o : objects) {
if (o instanceof Exception) {
if (lastException == null) {
lastException = new ExceptionInfo((Exception) o);
ExceptionInfo info = new ExceptionInfo((Exception) o);
if (!lastExceptions.containsKey(info)) {
lastExceptions.put(info, info);
if (logWriter != null) {
appendLogFile(getStacktraceAsString((Exception) o));
}
((Exception) o).printStackTrace();
} else if (!lastException.equals((Exception) o)) {
lastException.flush();
lastException = new ExceptionInfo((Exception) o);
System.out.println(lastException.getOneLine());
} else {
lastException.poke();
// poke original instance
lastExceptions.get(info).poke();
}
} else {
if (logWriter != null) {
Expand All @@ -55,19 +56,16 @@ public static void dumpf(String format, Object... objects) {
public static void error(Object... objects) {
for (Object o : objects) {
if (o instanceof Exception) {
if (lastException == null) {
lastException = new ExceptionInfo((Exception) o);
ExceptionInfo info = new ExceptionInfo((Exception) o);
if (!lastExceptions.containsKey(info)) {
lastExceptions.put(info, info);
if (logWriter != null) {
appendLogFile(getStacktraceAsString((Exception) o));
}
((Exception) o).printStackTrace();
} else if (!lastException.equals((Exception) o)) {
lastException.flush();
lastException = new ExceptionInfo((Exception) o);
System.out.println(lastException.getOneLine());
} else {
System.out.println(lastException.getOneLine());
lastException.poke();
// poke original instance
lastExceptions.get(info).poke();
}
} else {
if (logWriter != null) {
Expand All @@ -78,6 +76,10 @@ public static void error(Object... objects) {
}
}

public static void dumpException(String msg, Exception ex) {
error(new Exception(msg), ex);
}

public static void errorf(String format, Object... objects) {
if (logWriter != null) {
appendLogFile(String.format(format, objects));
Expand Down Expand Up @@ -124,8 +126,11 @@ public synchronized void close() {
try {
System.out.println("closing log file");
if (br != null) {
if (lastException != null) {
lastException.flush();
if (!lastExceptions.isEmpty()) {
for (ExceptionInfo info : lastExceptions.keySet()) {
info.flush();
}
lastExceptions.clear();
}
try {
br.flush();
Expand All @@ -151,6 +156,17 @@ private void repeatedFlush() {
while (br != null) {
try {
Thread.sleep(10000);

long now = System.currentTimeMillis();

lastExceptions.entrySet().removeIf(e -> {
if (now - e.getValue().timestamp > 10000) {
e.getValue().flush();
return true;
}
return false;
});

if (br != null) {
br.flush();
}
Expand Down Expand Up @@ -226,6 +242,7 @@ private static class ExceptionInfo {
long timestamp;
String oneLine;
int count = 0;
int hashCode;

ExceptionInfo(Exception ex) {
exception = ex.getClass();
Expand All @@ -238,46 +255,37 @@ private static class ExceptionInfo {
timestamp = System.currentTimeMillis();
oneLine = getExceptionOneLine(ex);

new Thread(() -> {
try {
Thread.sleep(10000);
long remainingTime;
while ((remainingTime = System.currentTimeMillis() - timestamp) < 10000) {
Thread.sleep(remainingTime);
}
flush();
lastException = null;
} catch (InterruptedException e) {
System.out.println("failed to flush exception");
e.printStackTrace();
}
}).start();
hashCode = Objects.hash(exception, line, file);
}

@Override
public boolean equals(Object ex) {
return exception == ((ExceptionInfo) ex).exception
&& line == ((ExceptionInfo) ex).line
&& file.equals(((ExceptionInfo) ex).file);
}

boolean equals(Exception ex) {
return exception == ex.getClass()
&& (line > 0
&& ex.getStackTrace().length > 0
&& line == ex.getStackTrace()[0].getLineNumber()
&& file.equals(ex.getStackTrace()[0].getFileName())
|| line == -1
&& ex.getStackTrace().length == 0);
@Override
public String toString() {
return "ExceptionInfo: " + exception.getSimpleName() + " at " + file + "L" + line + " #" + hashCode;
}

@Override
public int hashCode() {
return hashCode;
}

void flush() {
if (count > 0) {
appendLogFile(" ... " + count + " more of " + lastException.getOneLine());
System.out.println(lastException.getOneLine());
String line = " ... " + count + " more of " + oneLine;
appendLogFile(line);
System.out.println(line);
}
}

void poke() {
timestamp = System.currentTimeMillis();
count++;
}

String getOneLine() {
return oneLine;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public class BiomeFilter extends TextFilter<List<Integer>> {
validIDs.add(id);
}
} catch (IOException ex) {
Debug.error("error reading biomes.csv: ", ex.getMessage());
Debug.dumpException("error reading biomes.csv for BiomeFilter", ex);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class EntityFilter extends TextFilter<List<String>> {
validNames.add("minecraft:" + line);
}
} catch (IOException ex) {
Debug.error("error reading entity-names.csv: ", ex.getMessage());
Debug.dumpException("error reading entity-names.csv", ex);
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/net/querz/mcaselector/filter/FilterType.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ public Format getFormat() {
public Filter<?> create() {
try {
return clazz.newInstance();
} catch (InstantiationException | IllegalAccessException e) {
Debug.error(e);
} catch (InstantiationException | IllegalAccessException ex) {
Debug.dumpException("failed to create new filter instance", ex);
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class PaletteFilter extends TextFilter<List<String>> {
validNames.add(line);
}
} catch (IOException ex) {
Debug.error("error reading block-names.csv: ", ex.getMessage());
Debug.dumpException("error reading block-names.csv", ex);
}
}

Expand Down
13 changes: 11 additions & 2 deletions src/main/java/net/querz/mcaselector/headless/ParamExecutor.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
import net.querz.mcaselector.debug.Debug;
import net.querz.mcaselector.io.FileHelper;
import net.querz.mcaselector.point.Point2i;
import net.querz.mcaselector.range.Range;
import net.querz.mcaselector.range.RangeParser;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
Expand Down Expand Up @@ -65,6 +68,7 @@ public Future<Boolean> parseAndRun() {
pi.registerDependencies("offset-x", null, new ActionKey("mode", "import"));
pi.registerDependencies("offset-z", null, new ActionKey("mode", "import"));
pi.registerDependencies("overwrite", null, new ActionKey("mode", "import"));
pi.registerDependencies("sections", null, new ActionKey("mode", "import"));
pi.registerDependencies("selection", null, new ActionKey("mode", "import"));
pi.registerDependencies("zoom-level", null, new ActionKey("mode", "cache"));
for (int z = Config.getMinZoomLevel(); z <= Config.getMaxZoomLevel(); z *= 2) {
Expand Down Expand Up @@ -96,7 +100,7 @@ public Future<Boolean> parseAndRun() {
} catch (Exception ex) {
Debug.error("Error: " + ex.getMessage());
if (params.get() != null && params.get().containsKey("debug")) {
Debug.error(ex);
Debug.dumpException("Error executing in headless mode", ex);
}
future.run();
return future;
Expand Down Expand Up @@ -216,12 +220,17 @@ private static void runModeImport(Map<String, String> params, FutureTask<Boolean
selection = SelectionUtil.importSelection(selectionFile);
}

List<Range> ranges = null;
if (params.containsKey("sections")) {
ranges = RangeParser.parseRanges(params.get("sections"), ",");
}

printHeadlessSettings();

ConsoleProgress progress = new ConsoleProgress();
progress.onDone(future);

ChunkImporter.importChunks(input, progress, true, overwrite, selection, new Point2i(offsetX, offsetZ));
ChunkImporter.importChunks(input, progress, true, overwrite, selection, ranges, new Point2i(offsetX, offsetZ));
}

private static void runModeExport(Map<String, String> params, FutureTask<Boolean> future) throws IOException {
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/net/querz/mcaselector/io/CacheHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,8 @@ private static String readVersionFromFile(File file) {
String version = null;
try (BufferedReader br = new BufferedReader(new FileReader(file))) {
version = br.readLine();
} catch (IOException e) {
Debug.error("failed to read version from file: " + e.getMessage());
} catch (IOException ex) {
Debug.dumpException("failed to read version from file " + file, ex);
}
return version;
}
Expand All @@ -147,7 +147,7 @@ private static void updateVersionFile() {
try (BufferedWriter bw = new BufferedWriter(new FileWriter(versionFile))) {
bw.write(applicationVersion);
} catch (IOException ex) {
Debug.error(ex);
Debug.dumpException("failed to write cache version file", ex);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ public void execute() {
Files.move(tmpFile.toPath(), getFile().toPath(), StandardCopyOption.REPLACE_EXISTING);
}
} catch (Exception ex) {
Debug.error(ex);
Debug.dumpException("failed to delete filtered chunks from " + getFile().getName(), ex);
}
progressChannel.incrementProgress(getFile().getName());
Debug.dumpf("took %s to save data to %s", t, getFile().getName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ public void execute() {
Files.move(tmpFile.toPath(), destination.toPath(), StandardCopyOption.REPLACE_EXISTING);
}
} catch (Exception ex) {
Debug.error(ex);
Debug.dumpException("failed to save exported filtered chunks in " + getFile().getName(), ex);
}
progressChannel.incrementProgress(getFile().getName());
Debug.dumpf("took %s to save data to %s", t, getFile().getName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public void execute() {
Debug.dumpf("took %s to delete chunk indices in %s", t, getFile().getName());
}
} catch (Exception ex) {
Debug.errorf("error selecting chunks in %s", getFile().getName());
Debug.dumpException("error selecting chunks in " + getFile().getName(), ex);
}
progressChannel.incrementProgress(getFile().getName());
}
Expand Down
Loading

0 comments on commit 070afe3

Please sign in to comment.