Skip to content
Permalink
Browse files

Refractor the system

  • Loading branch information...
ferrybig committed Mar 9, 2019
1 parent a3a7a2c commit 7d6cd99c031149626d6b6680f0c5758a787437ca
Showing with 1,051 additions and 205 deletions.
  1. +28 −16 src/main/java/me/ferrybig/java/minecraft/overview/mapper/Main.java
  2. +140 −0 src/main/java/me/ferrybig/java/minecraft/overview/mapper/engine/SequentialEngine.java
  3. +28 −13 src/main/java/me/ferrybig/java/minecraft/overview/mapper/input/DirectoryInputSource.java
  4. +56 −0 src/main/java/me/ferrybig/java/minecraft/overview/mapper/input/InputInfo.java
  5. +3 −7 src/main/java/me/ferrybig/java/minecraft/overview/mapper/input/InputSource.java
  6. +62 −0 src/main/java/me/ferrybig/java/minecraft/overview/mapper/input/NamedThreadFactory.java
  7. +63 −12 src/main/java/me/ferrybig/java/minecraft/overview/mapper/input/PreparedFile.java
  8. +143 −0 src/main/java/me/ferrybig/java/minecraft/overview/mapper/input/SimpleInputInfo.java
  9. +51 −52 src/main/java/me/ferrybig/java/minecraft/overview/mapper/input/TarGzInputSource.java
  10. +364 −0 src/main/java/me/ferrybig/java/minecraft/overview/mapper/input/WorldFile.java
  11. +1 −1 src/main/java/me/ferrybig/java/minecraft/overview/mapper/render/ChunkReader.java
  12. +15 −9 src/main/java/me/ferrybig/java/minecraft/overview/mapper/render/ComplexImageOutputRenderer.java
  13. +3 −2 src/main/java/me/ferrybig/java/minecraft/overview/mapper/render/DefaultImageRenderer.java
  14. +10 −5 src/main/java/me/ferrybig/java/minecraft/overview/mapper/render/FlatImageRenderer.java
  15. +35 −0 src/main/java/me/ferrybig/java/minecraft/overview/mapper/render/ImageWriter.java
  16. +2 −2 src/main/java/me/ferrybig/java/minecraft/overview/mapper/render/RegionRenderer.java
  17. +0 −40 src/main/java/me/ferrybig/java/minecraft/overview/mapper/render/RenderEngine.java
  18. +6 −5 src/main/java/me/ferrybig/java/minecraft/overview/mapper/render/SimpleHTMLOutputRenderer.java
  19. +4 −3 src/main/java/me/ferrybig/java/minecraft/overview/mapper/render/SimpleImageOutputRenderer.java
  20. +25 −33 src/main/java/me/ferrybig/java/minecraft/overview/mapper/render/SimpleRenderer.java
  21. +11 −4 src/main/java/me/ferrybig/java/minecraft/overview/mapper/textures/TextureParser.java
  22. +1 −1 src/main/java/me/ferrybig/java/minecraft/overview/mapper/textures/blockstate/MultiBlockState.java
@@ -10,14 +10,15 @@
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import me.ferrybig.java.minecraft.overview.mapper.engine.SequentialEngine;
import me.ferrybig.java.minecraft.overview.mapper.input.DirectoryInputSource;
import me.ferrybig.java.minecraft.overview.mapper.input.InputSource;
import me.ferrybig.java.minecraft.overview.mapper.input.TarGzInputSource;
import me.ferrybig.java.minecraft.overview.mapper.render.BiomeMap;
import me.ferrybig.java.minecraft.overview.mapper.render.ComplexImageOutputRenderer;
import me.ferrybig.java.minecraft.overview.mapper.render.FlatImageRenderer;
import me.ferrybig.java.minecraft.overview.mapper.render.ImageWriter;
import me.ferrybig.java.minecraft.overview.mapper.render.RegionRenderer;
import me.ferrybig.java.minecraft.overview.mapper.render.RenderEngine;
import me.ferrybig.java.minecraft.overview.mapper.render.SimpleHTMLOutputRenderer;
import me.ferrybig.java.minecraft.overview.mapper.textures.TextureCache;
import me.ferrybig.java.minecraft.overview.mapper.textures.TextureParser;
@@ -68,32 +69,43 @@ public static void main(String[] args) throws IOException, ParseException {
return;
}

Path in = Paths.get(cmd.getOptionValue(input.getOpt()));
Path out = Paths.get(cmd.getOptionValue(output.getOpt()));

System.err.println("In: " + in.toAbsolutePath());
System.err.println("Out: " + out.toAbsolutePath());

System.out.println("Loading textures...");
TextureParser parser = new TextureParser();
parser.readAll(Arrays.asList(new File("C:\\Users\\Fernando\\AppData\\Roaming\\.minecraft\\versions\\1.13.2\\1.13.2.jar")));
final TextureCache textureCache = new TextureCache(parser);

RegionRenderer rend = new FlatImageRenderer(new TextureCache(parser), BiomeMap.loadDefault());

System.out.println("Initizing input system");
Path in = Paths.get(cmd.getOptionValue(input.getOpt()));
InputSource inputSource;
if (in.toAbsolutePath().toString().endsWith(".tar.gz")) {
inputSource = new TarGzInputSource(in.toFile());
} else {
inputSource = new DirectoryInputSource(in.toFile());
inputSource = new DirectoryInputSource(in);
}
RenderEngine outputSource;

System.out.println("Initizing output system");
ImageWriter outputSource;
Path out = Paths.get(cmd.getOptionValue(output.getOpt()));
if (out.toAbsolutePath().toString().endsWith(".html")) {
outputSource = new SimpleHTMLOutputRenderer(rend, out.toFile(), "gif");
outputSource = new SimpleHTMLOutputRenderer(out.toFile(), "gif");
} else {
outputSource = new ComplexImageOutputRenderer(rend, out);
outputSource = new ComplexImageOutputRenderer(out);
}

System.err.println("In-type: " + inputSource.getClass());
System.err.println("Out-type: " + outputSource.getClass());
outputSource.forInputSource(inputSource, s -> System.out.println("Start:\t" + s), s -> System.out.println("End:\t" + s));
System.out.println("Initizing rendering system");
RegionRenderer renderer = new FlatImageRenderer(textureCache, BiomeMap.loadDefault());

System.out.println("Preparing engine...");
System.err.println("In-type: " + inputSource.getClass() + ": " + in.toAbsolutePath());
System.err.println("Render-type: " + renderer.getClass());
System.err.println("Out-type: " + outputSource.getClass() + ": " + out.toAbsolutePath());
SequentialEngine engine = new SequentialEngine(inputSource, renderer, outputSource);

System.out.println("Starting render...");
engine.render(
(s, p) -> System.out.println("Start:\t" + Math.round(p) + "\t" + s),
(s, p) -> System.out.println("End:\t" + Math.round(p) + "\t" + s)
);
System.out.println("Done!");
}
}
@@ -0,0 +1,140 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package me.ferrybig.java.minecraft.overview.mapper.engine;

import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Collection;
import java.util.Comparator;
import java.util.HashSet;
import java.util.Set;
import java.util.function.BiConsumer;
import java.util.stream.Collectors;
import me.ferrybig.java.minecraft.overview.mapper.input.InputInfo;
import me.ferrybig.java.minecraft.overview.mapper.input.PreparedFile;
import me.ferrybig.java.minecraft.overview.mapper.render.RegionRenderer;
import me.ferrybig.java.minecraft.overview.mapper.input.InputSource;
import me.ferrybig.java.minecraft.overview.mapper.input.WorldFile;
import me.ferrybig.java.minecraft.overview.mapper.render.ImageWriter;

public class SequentialEngine {

private static final Comparator<WorldFile> FILE_COMPARATOR = (WorldFile o1, WorldFile o2) -> {
int compare = o1.getType().compareTo(o2.getType());
if (compare != 0) {
return compare;
}
assert o1.getType() == o2.getType();
switch (o1.getType()) {
case REGION_MCA:
return Integer.compare(
Math.abs(o1.getX()) + Math.abs(o1.getZ()),
Math.abs(o2.getX()) + Math.abs(o2.getZ())
);
default:
return 0; // No specific order
}
};

private final InputSource files;
private final RegionRenderer renderer;
private final ImageWriter imageWriter;

public SequentialEngine(InputSource files, RegionRenderer renderer, ImageWriter imageWriter) {
this.files = files;
this.renderer = renderer;
this.imageWriter = imageWriter;
}

private boolean canConsume(WorldFile file) {
switch (file.getType()) {
case REGION_MCA:
return file.getDimension() == 0;
case LEVEL_DAT:
return true;
default:
return false;
}
}

public void render(BiConsumer<String, Double> beforeFile, BiConsumer<String, Double> afterFile) throws IOException {

try (InputInfo inputTask = this.files.generateFileListing()) {
System.out.println("Render start!");
imageWriter.startRender();

final Set<WorldFile> processedFiles;
final int totalFiles;
final boolean hasSendFullFileList;
{
Collection<WorldFile> fileNames = inputTask.getKnownFiles();
if (fileNames.isEmpty()) {
processedFiles = new HashSet<>();
System.out.println("Input files not known");
totalFiles = -1;
hasSendFullFileList = false;
} else {
System.out.println("We have a full list of files! " + fileNames.size());
imageWriter.addKnownFiles(fileNames);
processedFiles = fileNames.stream().filter(this::canConsume).collect(Collectors.toSet());
totalFiles = fileNames.size();
hasSendFullFileList = true;
for (Runnable run : imageWriter.filesKnown()) {
run.run();
}
}
}

inputTask.forAllFiles(new InputInfo.FileConsumer() {
private int processedFilesCount = 0;

@Override
public Comparator<WorldFile> getComparator() {
return FILE_COMPARATOR;
}

@Override
public boolean canConsume(WorldFile file) {
return SequentialEngine.this.canConsume(file);
}

@Override
public void consume(PreparedFile file) throws IOException {
if (!hasSendFullFileList) {
imageWriter.addKnownFile(file.getFile());
processedFiles.add(file.getFile());
} else {
assert processedFiles.contains(file.getFile());
}
beforeFile.accept(file.getFile().getOrignalName(), processedFilesCount * 100d / totalFiles);
switch (file.getFile().getType()) {
case REGION_MCA: {
BufferedImage render = SequentialEngine.this.renderer.renderFile(file);
SequentialEngine.this.imageWriter.addFile(file.getFile(), render);
}
break;
default: {
SequentialEngine.this.imageWriter.addFile(file.getFile(), file);
}
}
processedFilesCount++;
afterFile.accept(file.getFile().getOrignalName(), processedFilesCount * 100d / totalFiles);

}
});

if (!hasSendFullFileList) {
for (Runnable run : imageWriter.filesKnown()) {
run.run();
}
}

imageWriter.finishRender();
} finally {
imageWriter.close();
}
}
}
@@ -6,28 +6,43 @@
package me.ferrybig.java.minecraft.overview.mapper.input;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;

public class DirectoryInputSource implements InputSource {

private final File root;
private final Path root;

public DirectoryInputSource(File root) {
public DirectoryInputSource(Path root) {
this.root = root;
}

@Override
public Stream<PreparedFile> stream() {
List<File> files = new ArrayList<>();
files.add(new File(root, "level.dat"));
files.addAll(Arrays.asList(new File(root, "region").listFiles()));
return files.stream().map(f
-> PreparedFile.of(f.getName(), () -> new FileInputStream(f))
);
public InputInfo generateFileListing() throws IOException {
ArrayList<WorldFile> files = new ArrayList<>();
Files.walkFileTree(root, new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
String path = root.relativize(file).toString();
if (!File.separator.equals("/")) {
path = path.replace(File.separator, "/");
}
files.add(WorldFile.of(path));
return FileVisitResult.CONTINUE;
}
});
files.forEach(System.out::println);
return new SimpleInputInfo(files) {
@Override
protected PreparedFile toPreparedFile(WorldFile file) throws IOException {
return PreparedFile.of(file, root.resolve(file.getOrignalName()));
}
};
}

}
@@ -0,0 +1,56 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package me.ferrybig.java.minecraft.overview.mapper.input;

import java.io.Closeable;
import java.io.IOException;
import java.util.Collection;
import java.util.Comparator;
import java.util.concurrent.ExecutorService;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;

public interface InputInfo extends Closeable {

@Nonnull
public Collection<WorldFile> getKnownFiles();

public boolean supportsSingleFile();

public void forSingleFile(@Nonnull WorldFile file, @Nonnull FileConsumer consumer) throws IOException;

public void forAllFiles(@Nonnull FileConsumer consumer) throws IOException;

public default boolean supportsParallelExecution() {
return false;
}

public default void forAllFilesParalel(@Nonnull FileConsumer consumer, int maxTasks) throws IOException, InterruptedException {
this.forAllFiles(consumer);
}

public default void forAllFilesParalel(@Nonnull FileConsumer consumer, int maxTasks, @Nonnull ExecutorService executor) throws IOException, InterruptedException {
this.forAllFiles(consumer);
}

@FunctionalInterface
public interface FileConsumer {

@Nullable
public static Comparator<WorldFile> NO_COMPARE = null;

@Nullable
public default Comparator<WorldFile> getComparator() {
return NO_COMPARE;
}

public default boolean canConsume(@Nonnull WorldFile file) {
return true;
}

public void consume(@Nonnull PreparedFile file) throws IOException;
}
}
@@ -6,14 +6,10 @@
package me.ferrybig.java.minecraft.overview.mapper.input;

import java.io.IOException;
import java.io.InputStream;
import java.util.stream.Stream;
import javax.annotation.Nonnull;

public interface InputSource {

public Stream<PreparedFile> stream() throws IOException;

public default void closeStreamIfNeeded(InputStream in) throws IOException {
in.close();
}
@Nonnull
public InputInfo generateFileListing() throws IOException;
}
Oops, something went wrong.

0 comments on commit 7d6cd99

Please sign in to comment.
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.