create_ponder_wonder

Exports Create ponders to disk.
git clone git://git.oshgnacknak.de/create_ponder_wonder.git
Log | Files | Refs | README

PonderRenderScheduler.java (3054B)


      1 package de.oshgnacknak.create_ponder_wonder;
      2 
      3 import com.simibubi.create.foundation.ponder.PonderRegistry;
      4 import com.simibubi.create.foundation.ponder.PonderScene;
      5 
      6 import java.io.IOException;
      7 import java.nio.file.Files;
      8 import java.nio.file.Path;
      9 import java.nio.file.Paths;
     10 import java.util.List;
     11 import java.util.concurrent.ExecutorService;
     12 import java.util.concurrent.Executors;
     13 import java.util.concurrent.TimeUnit;
     14 
     15 public class PonderRenderScheduler {
     16 
     17 	private static final int GC_INTERVAL = 200;
     18 
     19 	private ExecutorService executorService;
     20 	private boolean rendering;
     21 
     22 	public void start(String basePath) {
     23 		if (rendering) {
     24 			CreatePonderWonder.chat("Error: cannot be done twice");
     25 			throw new IllegalStateException("Cannot start rendering twice");
     26 		}
     27 
     28 		rendering = true;
     29 		executorService = Executors.newSingleThreadExecutor();
     30 
     31 		CreatePonderWonder.LOGGER.info("Started rendering ponders");
     32 		CreatePonderWonder.chat("Started rendering ponders");
     33 
     34 		renderAllPonders(basePath);
     35 	}
     36 
     37 	private void renderAllPonders(String basePath) {
     38 		PonderRegistry.all
     39 			.values()
     40 			.stream()
     41 			.map(PonderRegistry::compile)
     42 			.flatMap(List::stream)
     43 			.forEach(ponder -> saveFrames(ponder, basePath));
     44 
     45 		CreatePonderWonder.LOGGER.info("All ponders rendered: {}", basePath);
     46 		CreatePonderWonder.chat("All ponders rendered: " + basePath);
     47 
     48 		finishRendering();
     49 	}
     50 
     51 	private void saveFrames(PonderScene ponder, String basePath) {
     52 		try {
     53 			Path path = getOutPath(ponder, basePath);
     54 
     55 			for (PonderRenderer.RenderResult result : new PonderRenderer(ponder)) {
     56 				Path out = path.resolve(String.format("%06d.png", result.frame));
     57 				result.image.write(out);
     58 				if (result.frame % GC_INTERVAL == 0)
     59 					System.gc();
     60 			}
     61 			System.gc();
     62 
     63 			CreatePonderWonder.chat("Finished rendering Ponder: " + path);
     64 			CreatePonderWonder.LOGGER.info("Finished rendering Ponder: {}", path);
     65 		} catch (Exception e) {
     66 			CreatePonderWonder.chat("Error: " + e.getMessage());
     67 			CreatePonderWonder.LOGGER.error("Could not save image", e);
     68 			e.printStackTrace();
     69 		}
     70 	}
     71 
     72 	private Path getOutPath(PonderScene ponder, String basePath) throws IOException {
     73 		Path path = Paths.get(
     74 			basePath,
     75 			CreatePonderWonder.MODID,
     76 			ponder.getString("out"));
     77 		Files.createDirectories(path);
     78 		return path;
     79 	}
     80 
     81 	public void stop() {
     82 		if (!rendering) {
     83 			CreatePonderWonder.chat("Aleady stopped...");
     84 			return;
     85 		}
     86 
     87 		CreatePonderWonder.LOGGER.warn("Stopping rendering ponders abruptly");
     88 		CreatePonderWonder.chat("Stopping rendering ponders abruptly");
     89 
     90 		try {
     91 			executorService.shutdown();
     92 			while (!executorService.awaitTermination(3, TimeUnit.SECONDS)) {
     93 				executorService.shutdownNow();
     94 			}
     95 
     96 			finishRendering();
     97 		} catch (InterruptedException e) {
     98 			e.printStackTrace();
     99 		}
    100 	}
    101 
    102 	private void finishRendering() {
    103 		executorService = null;
    104 		rendering = false;
    105 		System.gc();
    106 
    107 		CreatePonderWonder.LOGGER.info("Stopped rendering ponders");
    108 		CreatePonderWonder.chat("Stopped rendering ponders");
    109 	}
    110 
    111 	public boolean isRendering() {
    112 		return rendering;
    113 	}
    114 }