Fixing Bugs mostly
-Cleaning up imports -Better error detection -Actual thread deletion(still needs a bit of work to delete all conected objects) -Starting making format file and inplementing it
This commit is contained in:
		| @@ -18,9 +18,6 @@ | ||||
|   | ||||
| package ru.windcorp.progressia.client; | ||||
|  | ||||
| import java.util.Collection; | ||||
| import java.util.HashSet; | ||||
|  | ||||
| import ru.windcorp.progressia.client.comms.localhost.LocalServerCommsChannel; | ||||
| import ru.windcorp.progressia.client.graphics.GUI; | ||||
| import ru.windcorp.progressia.client.graphics.Layer; | ||||
|   | ||||
| @@ -21,6 +21,7 @@ package ru.windcorp.progressia.client.graphics; | ||||
| import java.util.ArrayList; | ||||
| import java.util.Collections; | ||||
| import java.util.List; | ||||
| import java.util.Objects; | ||||
|  | ||||
| import com.google.common.eventbus.Subscribe; | ||||
|  | ||||
| @@ -58,6 +59,7 @@ public class GUI { | ||||
| 	} | ||||
|  | ||||
| 	public static void addBottomLayer(Layer layer) { | ||||
| 		Objects.requireNonNull(layer, "layer"); | ||||
| 		modify(layers -> { | ||||
| 			layers.add(layer); | ||||
| 			layer.onAdded(); | ||||
| @@ -65,6 +67,7 @@ public class GUI { | ||||
| 	} | ||||
|  | ||||
| 	public static void addTopLayer(Layer layer) { | ||||
| 		Objects.requireNonNull(layer, "layer"); | ||||
| 		modify(layers -> { | ||||
| 			layers.add(0, layer); | ||||
| 			layer.onAdded(); | ||||
| @@ -72,6 +75,7 @@ public class GUI { | ||||
| 	} | ||||
|  | ||||
| 	public static void removeLayer(Layer layer) { | ||||
| 		Objects.requireNonNull(layer, "layer"); | ||||
| 		modify(layers -> { | ||||
| 			layers.remove(layer); | ||||
| 			layer.onRemoved(); | ||||
|   | ||||
| @@ -30,6 +30,8 @@ public class ServerThread implements Runnable { | ||||
|  | ||||
| 	private static final ThreadLocal<Server> SERVER_THREADS_MAP = new ThreadLocal<>(); | ||||
| 	 | ||||
| 	private static boolean isShuttingDown; | ||||
|  | ||||
| 	public static Server getCurrentServer() { | ||||
| 		return SERVER_THREADS_MAP.get(); | ||||
| 	} | ||||
| @@ -63,6 +65,7 @@ public class ServerThread implements Runnable { | ||||
| 	} | ||||
|  | ||||
| 	public void start() { | ||||
| 		isShuttingDown = false; | ||||
| 		ticker.start(); | ||||
| 		executor.scheduleAtFixedRate(this, 0, 1000 / 20, TimeUnit.MILLISECONDS); | ||||
| 	} | ||||
| @@ -70,6 +73,12 @@ public class ServerThread implements Runnable { | ||||
| 	@Override | ||||
| 	public void run() { | ||||
| 		try { | ||||
| 			if (isShuttingDown) | ||||
| 			{ | ||||
| 				getTicker().stop(); | ||||
| 				executor.shutdown(); | ||||
| 				return; | ||||
| 			} | ||||
| 			server.tick(); | ||||
| 			ticker.runOneTick(); | ||||
| 		} catch (Throwable e) { | ||||
| @@ -78,14 +87,10 @@ public class ServerThread implements Runnable { | ||||
| 	} | ||||
|  | ||||
| 	public void stop() { | ||||
| 		try { | ||||
| 			executor.shutdown(); | ||||
| 			executor.awaitTermination(10, TimeUnit.MINUTES); | ||||
| 		} catch (InterruptedException e) { | ||||
| 			LogManager.getLogger().warn("Received interrupt in ServerThread.stop(), aborting wait"); | ||||
| 		} | ||||
| 		 | ||||
| 		getTicker().stop(); | ||||
| 		isShuttingDown = true; | ||||
|  | ||||
| 		//getTicker().stop(); | ||||
| 	} | ||||
|  | ||||
| 	public Server getServer() { | ||||
|   | ||||
| @@ -115,6 +115,8 @@ class Ticker { | ||||
| 		} catch (Exception e) { | ||||
| 			getCoordinator().crash(e, this.name); | ||||
| 		} | ||||
| 		 | ||||
| 		 | ||||
| 	} | ||||
|  | ||||
| 	private synchronized boolean sleep() { | ||||
|   | ||||
| @@ -104,6 +104,7 @@ public class LayerButtonTest extends MenuLayer { | ||||
| 					//cm.unregisterAll(); | ||||
| 					alive = false; | ||||
| 					 | ||||
| 					//ServerState.getInstance().; | ||||
| 					ServerState.getInstance().shutdown("Safe Exit"); | ||||
|  | ||||
| 					ServerState.setInstance(null); | ||||
|   | ||||
| @@ -20,16 +20,17 @@ package ru.windcorp.progressia.test; | ||||
|  | ||||
| import java.io.BufferedInputStream; | ||||
| import java.io.BufferedOutputStream; | ||||
| import java.io.BufferedWriter; | ||||
| import java.io.DataInputStream; | ||||
| import java.io.DataOutputStream; | ||||
| import java.io.File; | ||||
| import java.io.FileNotFoundException; | ||||
| import java.io.FileWriter; | ||||
| import java.io.IOException; | ||||
| import java.nio.file.Files; | ||||
| import java.nio.file.Path; | ||||
| import java.nio.file.Paths; | ||||
| import java.util.Collection; | ||||
| import java.util.HashMap; | ||||
| import java.util.Map; | ||||
| import java.util.Map.Entry; | ||||
| import java.util.Scanner; | ||||
| import java.util.zip.DeflaterOutputStream; | ||||
| import java.util.zip.InflaterInputStream; | ||||
|  | ||||
| @@ -47,6 +48,7 @@ import ru.windcorp.progressia.server.Server; | ||||
| public class TestWorldDiskIO { | ||||
|  | ||||
| 	private static Path SAVE_DIR = Paths.get("tmp_world"); | ||||
| 	private static String formatFile = "world.format"; | ||||
| 	private static final Logger LOG = LogManager.getLogger("TestWorldDiskIO"); | ||||
|  | ||||
| 	private static final boolean ENABLE = true; | ||||
| @@ -54,40 +56,60 @@ public class TestWorldDiskIO { | ||||
| 	private static final int maxSize = 1048576; | ||||
| 	private static final int sectorSize = maxSize / 256; | ||||
|  | ||||
| 	private Map<Vec3i,Vec3i> regions = new HashMap<Vec3i,Vec3i>(); | ||||
| 	private static final int bestFormat = 2; | ||||
|  | ||||
| 	private int natFromInt(int loc) | ||||
| 	{ | ||||
| 	// private Map<Vec3i,Vec3i> regions = new HashMap<Vec3i,Vec3i>(); | ||||
| 	private static Vec3i regionSize; | ||||
| 	private static int chunksPerRegion; | ||||
|  | ||||
| 	private static int currentFormat = -1; | ||||
| 	private static String extension = ".null"; | ||||
|  | ||||
| 	private static int natFromInt(int loc) { | ||||
| 		if (loc < 0) | ||||
| 			return (-loc) << 1 + 1; | ||||
| 		return loc << 1; | ||||
| 	} | ||||
|  | ||||
| 	private int intFromNat(int loc) // Possibly unused | ||||
| 	private static int intFromNat(int loc) // Possibly unused | ||||
| 	{ | ||||
| 		if ((loc & 1) == 1) | ||||
| 			return -loc >> 1; | ||||
| 		return loc >> 1; | ||||
| 	} | ||||
|  | ||||
| 	private Vec3i getRegionLoc(Vec3i chunkLoc) | ||||
| 	{ | ||||
| 	private static Vec3i getRegionLoc(Vec3i chunkLoc) { | ||||
| 		return new Vec3i(natFromInt(chunkLoc.x), natFromInt(chunkLoc.y), natFromInt(chunkLoc.z)); | ||||
| 	} | ||||
|  | ||||
| 	public void initRegions() | ||||
| 	{ | ||||
| 	public static void initRegions() { | ||||
| 		initRegions(null); | ||||
| 	} | ||||
|  | ||||
| 	public void initRegions(Path worldPath) | ||||
| 	{ | ||||
| 		if (worldPath!=null) | ||||
| 		{ | ||||
| 	public static void initRegions(Path worldPath) { | ||||
| 		if (worldPath != null) { | ||||
| 			SAVE_DIR = worldPath; | ||||
| 		} | ||||
|  | ||||
| 		regions.put(new Vec3i(0,0,0), new Vec3i(1,1,1)); | ||||
| 		// regions.put(new Vec3i(0,0,0), new Vec3i(1,1,1)); | ||||
| 	} | ||||
|  | ||||
| 	private static void setRegionSize(int format) { | ||||
| 		switch (format) { | ||||
| 		case 0: | ||||
| 		case 1: | ||||
| 			regionSize = new Vec3i(1); | ||||
| 			chunksPerRegion = 1; | ||||
| 			currentFormat = format; | ||||
| 			extension = ".progressia_chunk"; | ||||
| 			break; | ||||
| 		case 2: | ||||
| 			regionSize = new Vec3i(16); | ||||
| 			chunksPerRegion = 16 * 16 * 16; | ||||
| 			currentFormat = 2; | ||||
| 			extension = ".progressia_region"; | ||||
| 			break; | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	public static void saveChunk(ChunkData chunk, Server server) { | ||||
| @@ -95,6 +117,8 @@ public class TestWorldDiskIO { | ||||
| 			return; | ||||
|  | ||||
| 		try { | ||||
|  | ||||
| 			if (currentFormat == 0) { | ||||
| 				LOG.debug( | ||||
| 					"Saving {} {} {}", | ||||
| 					chunk.getPosition().x, | ||||
| @@ -106,7 +130,7 @@ public class TestWorldDiskIO { | ||||
|  | ||||
| 				Path path = SAVE_DIR.resolve( | ||||
| 					String.format( | ||||
| 					"chunk_%+d_%+d_%+d.progressia_chunk", | ||||
| 						"chunk_%+d_%+d_%+d" + extension, | ||||
| 						chunk.getPosition().x, | ||||
| 						chunk.getPosition().y, | ||||
| 						chunk.getPosition().z | ||||
| @@ -121,6 +145,37 @@ public class TestWorldDiskIO { | ||||
| 					ChunkIO.save(chunk, output, IOContext.SAVE); | ||||
| 					writeGenerationHint(chunk, output, server); | ||||
| 				} | ||||
| 			} else if (currentFormat == 1) { | ||||
| 				LOG.debug( | ||||
| 					"Saving {} {} {}", | ||||
| 					chunk.getPosition().x, | ||||
| 					chunk.getPosition().y, | ||||
| 					chunk.getPosition().z | ||||
| 				); | ||||
|  | ||||
| 				Files.createDirectories(SAVE_DIR); | ||||
|  | ||||
| 				Vec3i saveCoords = getRegionLoc(chunk.getPosition()); | ||||
|  | ||||
| 				Path path = SAVE_DIR.resolve( | ||||
| 					String.format( | ||||
| 						"chunk_%+d_%+d_%+d" + extension, | ||||
| 						saveCoords.x, | ||||
| 						saveCoords.y, | ||||
| 						saveCoords.z | ||||
| 					) | ||||
| 				); | ||||
|  | ||||
| 				try ( | ||||
| 					DataOutputStream output = new DataOutputStream( | ||||
| 						new DeflaterOutputStream(new BufferedOutputStream(Files.newOutputStream(path))) | ||||
| 					) | ||||
| 				) { | ||||
| 					ChunkIO.save(chunk, output, IOContext.SAVE); | ||||
| 					writeGenerationHint(chunk, output, server); | ||||
| 				} | ||||
| 			} | ||||
| 			// else if (currentFormat) | ||||
| 		} catch (IOException e) { | ||||
| 			e.printStackTrace(); | ||||
| 		} | ||||
| @@ -135,9 +190,57 @@ public class TestWorldDiskIO { | ||||
| 		if (!ENABLE) | ||||
| 			return null; | ||||
|  | ||||
| 		if (currentFormat == -1) { | ||||
| 			Path formatPath = SAVE_DIR.resolve(formatFile); | ||||
| 			File format = formatPath.toFile(); | ||||
|  | ||||
| 			if (format.exists()) { | ||||
| 				String data = null; | ||||
| 				try { | ||||
| 					Scanner reader = new Scanner(format); | ||||
|  | ||||
| 					data = reader.next(); | ||||
|  | ||||
| 					reader.close(); | ||||
| 				} catch (FileNotFoundException e) { | ||||
| 					// TODO Auto-generated catch block | ||||
| 					e.printStackTrace(); | ||||
| 				} | ||||
|  | ||||
| 				byte[] formatBytes = data.getBytes(); | ||||
| 				int formatNum = formatBytes[0] * 256 * 256 * 256 + formatBytes[1] * 256 * 256 + formatBytes[2] * 256 | ||||
| 					+ formatBytes[3]; | ||||
|  | ||||
| 				setRegionSize(formatNum); | ||||
| 			} else { | ||||
| 				setRegionSize(bestFormat); | ||||
|  | ||||
| 				BufferedWriter bw; | ||||
| 				try { | ||||
| 					bw = new BufferedWriter(new FileWriter(format)); | ||||
|  | ||||
| 					bw.write( | ||||
| 						new char[] { | ||||
| 							bestFormat / (256 * 256 * 256), | ||||
| 							(bestFormat % 256) / (256 * 256), | ||||
| 							(bestFormat % (256 * 256)) / (256), | ||||
| 							bestFormat % (256 * 256 * 256) } | ||||
| 					); | ||||
|  | ||||
| 					bw.close(); | ||||
| 				} catch (IOException e) { | ||||
| 					// TODO Auto-generated catch block | ||||
| 					e.printStackTrace(); | ||||
| 				} | ||||
|  | ||||
| 			} | ||||
| 		} | ||||
|  | ||||
| 		if (currentFormat == 0) { | ||||
|  | ||||
| 			Path path = SAVE_DIR.resolve( | ||||
| 				String.format( | ||||
| 				"chunk_%+d_%+d_%+d.progressia_chunk", | ||||
| 					"chunk_%+d_%+d_%+d" + extension, | ||||
| 					chunkPos.x, | ||||
| 					chunkPos.y, | ||||
| 					chunkPos.z | ||||
| @@ -176,6 +279,52 @@ public class TestWorldDiskIO { | ||||
| 				); | ||||
| 				return null; | ||||
| 			} | ||||
| 		} else if (currentFormat == 1) { | ||||
| 			Vec3i saveCoords = getRegionLoc(chunkPos); | ||||
|  | ||||
| 			Path path = SAVE_DIR.resolve( | ||||
| 				String.format( | ||||
| 					"chunk_%+d_%+d_%+d" + extension, | ||||
| 					saveCoords.x, | ||||
| 					saveCoords.y, | ||||
| 					saveCoords.z | ||||
| 				) | ||||
| 			); | ||||
|  | ||||
| 			if (!Files.exists(path)) { | ||||
| 				LOG.debug( | ||||
| 					"Not found {} {} {}", | ||||
| 					chunkPos.x, | ||||
| 					chunkPos.y, | ||||
| 					chunkPos.z | ||||
| 				); | ||||
|  | ||||
| 				return null; | ||||
| 			} | ||||
|  | ||||
| 			try { | ||||
| 				ChunkData result = load(path, chunkPos, world, server); | ||||
|  | ||||
| 				LOG.debug( | ||||
| 					"Loaded {} {} {}", | ||||
| 					chunkPos.x, | ||||
| 					chunkPos.y, | ||||
| 					chunkPos.z | ||||
| 				); | ||||
|  | ||||
| 				return result; | ||||
| 			} catch (Exception e) { | ||||
| 				e.printStackTrace(); | ||||
| 				LOG.debug( | ||||
| 					"Could not load {} {} {}", | ||||
| 					chunkPos.x, | ||||
| 					chunkPos.y, | ||||
| 					chunkPos.z | ||||
| 				); | ||||
| 				return null; | ||||
| 			} | ||||
| 		} | ||||
| 		return null; | ||||
| 	} | ||||
|  | ||||
| 	private static ChunkData load(Path path, Vec3i chunkPos, WorldData world, Server server) | ||||
|   | ||||
		Reference in New Issue
	
	Block a user