From c88dea6030db43b7910a5cf5da91bb2de8d9fcc5 Mon Sep 17 00:00:00 2001 From: opfromthestart Date: Wed, 4 Aug 2021 16:57:21 -0400 Subject: [PATCH] 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 --- .../progressia/client/ClientState.java | 3 - .../progressia/client/graphics/GUI.java | 4 + .../progressia/server/ServerThread.java | 19 +- .../server/world/ticking/Ticker.java | 2 + .../progressia/test/LayerButtonTest.java | 1 + .../progressia/test/TestWorldDiskIO.java | 321 +++++++++++++----- 6 files changed, 254 insertions(+), 96 deletions(-) diff --git a/src/main/java/ru/windcorp/progressia/client/ClientState.java b/src/main/java/ru/windcorp/progressia/client/ClientState.java index f8a1dfa..1092203 100644 --- a/src/main/java/ru/windcorp/progressia/client/ClientState.java +++ b/src/main/java/ru/windcorp/progressia/client/ClientState.java @@ -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; diff --git a/src/main/java/ru/windcorp/progressia/client/graphics/GUI.java b/src/main/java/ru/windcorp/progressia/client/graphics/GUI.java index bb4d85b..56829cf 100644 --- a/src/main/java/ru/windcorp/progressia/client/graphics/GUI.java +++ b/src/main/java/ru/windcorp/progressia/client/graphics/GUI.java @@ -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(); diff --git a/src/main/java/ru/windcorp/progressia/server/ServerThread.java b/src/main/java/ru/windcorp/progressia/server/ServerThread.java index 5a1333a..d8d15a9 100644 --- a/src/main/java/ru/windcorp/progressia/server/ServerThread.java +++ b/src/main/java/ru/windcorp/progressia/server/ServerThread.java @@ -29,6 +29,8 @@ import ru.windcorp.progressia.server.world.ticking.TickerCoordinator; public class ServerThread implements Runnable { private static final ThreadLocal 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"); - } + + isShuttingDown = true; - getTicker().stop(); + //getTicker().stop(); } public Server getServer() { diff --git a/src/main/java/ru/windcorp/progressia/server/world/ticking/Ticker.java b/src/main/java/ru/windcorp/progressia/server/world/ticking/Ticker.java index 5c4b83e..8a102ac 100644 --- a/src/main/java/ru/windcorp/progressia/server/world/ticking/Ticker.java +++ b/src/main/java/ru/windcorp/progressia/server/world/ticking/Ticker.java @@ -115,6 +115,8 @@ class Ticker { } catch (Exception e) { getCoordinator().crash(e, this.name); } + + } private synchronized boolean sleep() { diff --git a/src/main/java/ru/windcorp/progressia/test/LayerButtonTest.java b/src/main/java/ru/windcorp/progressia/test/LayerButtonTest.java index 2b36bbd..c6e5d00 100644 --- a/src/main/java/ru/windcorp/progressia/test/LayerButtonTest.java +++ b/src/main/java/ru/windcorp/progressia/test/LayerButtonTest.java @@ -104,6 +104,7 @@ public class LayerButtonTest extends MenuLayer { //cm.unregisterAll(); alive = false; + //ServerState.getInstance().; ServerState.getInstance().shutdown("Safe Exit"); ServerState.setInstance(null); diff --git a/src/main/java/ru/windcorp/progressia/test/TestWorldDiskIO.java b/src/main/java/ru/windcorp/progressia/test/TestWorldDiskIO.java index 1b93323..71e4225 100644 --- a/src/main/java/ru/windcorp/progressia/test/TestWorldDiskIO.java +++ b/src/main/java/ru/windcorp/progressia/test/TestWorldDiskIO.java @@ -15,21 +15,22 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - + 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,47 +48,68 @@ 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; - + private static final int maxSize = 1048576; - private static final int sectorSize = maxSize/256; - - private Map regions = new HashMap(); - - private int natFromInt(int loc) - { - if (loc<0) - return (-loc)<<1 + 1; - return loc<<1; + private static final int sectorSize = maxSize / 256; + + private static final int bestFormat = 2; + + // private Map regions = new HashMap(); + 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; + if ((loc & 1) == 1) + return -loc >> 1; + return loc >> 1; } - - private Vec3i getRegionLoc(Vec3i chunkLoc) - { - return new Vec3i(natFromInt(chunkLoc.x),natFromInt(chunkLoc.y),natFromInt(chunkLoc.z)); + + 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,32 +117,65 @@ public class TestWorldDiskIO { return; try { - LOG.debug( - "Saving {} {} {}", - chunk.getPosition().x, - chunk.getPosition().y, - chunk.getPosition().z - ); - Files.createDirectories(SAVE_DIR); - - Path path = SAVE_DIR.resolve( - String.format( - "chunk_%+d_%+d_%+d.progressia_chunk", + if (currentFormat == 0) { + LOG.debug( + "Saving {} {} {}", chunk.getPosition().x, chunk.getPosition().y, chunk.getPosition().z - ) - ); + ); - try ( - DataOutputStream output = new DataOutputStream( - new DeflaterOutputStream(new BufferedOutputStream(Files.newOutputStream(path))) - ) - ) { - ChunkIO.save(chunk, output, IOContext.SAVE); - writeGenerationHint(chunk, output, server); + Files.createDirectories(SAVE_DIR); + + Path path = SAVE_DIR.resolve( + String.format( + "chunk_%+d_%+d_%+d" + extension, + chunk.getPosition().x, + chunk.getPosition().y, + chunk.getPosition().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 == 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,47 +190,141 @@ public class TestWorldDiskIO { if (!ENABLE) return null; - Path path = SAVE_DIR.resolve( - String.format( - "chunk_%+d_%+d_%+d.progressia_chunk", - chunkPos.x, - chunkPos.y, - chunkPos.z - ) - ); + if (currentFormat == -1) { + Path formatPath = SAVE_DIR.resolve(formatFile); + File format = formatPath.toFile(); - if (!Files.exists(path)) { - LOG.debug( - "Not found {} {} {}", - chunkPos.x, - chunkPos.y, - chunkPos.z - ); + if (format.exists()) { + String data = null; + try { + Scanner reader = new Scanner(format); - return null; + 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(); + } + + } } - try { - ChunkData result = load(path, chunkPos, world, server); + if (currentFormat == 0) { - LOG.debug( - "Loaded {} {} {}", - chunkPos.x, - chunkPos.y, - chunkPos.z + Path path = SAVE_DIR.resolve( + String.format( + "chunk_%+d_%+d_%+d" + extension, + chunkPos.x, + chunkPos.y, + chunkPos.z + ) ); - return result; - } catch (Exception e) { - e.printStackTrace(); - LOG.debug( - "Could not load {} {} {}", - chunkPos.x, - chunkPos.y, - chunkPos.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; + } + } 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 + ) ); - return null; + + 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)