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:
opfromthestart 2021-08-04 16:57:21 -04:00
parent 6521cb5749
commit c88dea6030
6 changed files with 254 additions and 96 deletions

View File

@ -18,9 +18,6 @@
package ru.windcorp.progressia.client; 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.comms.localhost.LocalServerCommsChannel;
import ru.windcorp.progressia.client.graphics.GUI; import ru.windcorp.progressia.client.graphics.GUI;
import ru.windcorp.progressia.client.graphics.Layer; import ru.windcorp.progressia.client.graphics.Layer;

View File

@ -21,6 +21,7 @@ package ru.windcorp.progressia.client.graphics;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Objects;
import com.google.common.eventbus.Subscribe; import com.google.common.eventbus.Subscribe;
@ -58,6 +59,7 @@ public class GUI {
} }
public static void addBottomLayer(Layer layer) { public static void addBottomLayer(Layer layer) {
Objects.requireNonNull(layer, "layer");
modify(layers -> { modify(layers -> {
layers.add(layer); layers.add(layer);
layer.onAdded(); layer.onAdded();
@ -65,6 +67,7 @@ public class GUI {
} }
public static void addTopLayer(Layer layer) { public static void addTopLayer(Layer layer) {
Objects.requireNonNull(layer, "layer");
modify(layers -> { modify(layers -> {
layers.add(0, layer); layers.add(0, layer);
layer.onAdded(); layer.onAdded();
@ -72,6 +75,7 @@ public class GUI {
} }
public static void removeLayer(Layer layer) { public static void removeLayer(Layer layer) {
Objects.requireNonNull(layer, "layer");
modify(layers -> { modify(layers -> {
layers.remove(layer); layers.remove(layer);
layer.onRemoved(); layer.onRemoved();

View File

@ -30,6 +30,8 @@ public class ServerThread implements Runnable {
private static final ThreadLocal<Server> SERVER_THREADS_MAP = new ThreadLocal<>(); private static final ThreadLocal<Server> SERVER_THREADS_MAP = new ThreadLocal<>();
private static boolean isShuttingDown;
public static Server getCurrentServer() { public static Server getCurrentServer() {
return SERVER_THREADS_MAP.get(); return SERVER_THREADS_MAP.get();
} }
@ -63,6 +65,7 @@ public class ServerThread implements Runnable {
} }
public void start() { public void start() {
isShuttingDown = false;
ticker.start(); ticker.start();
executor.scheduleAtFixedRate(this, 0, 1000 / 20, TimeUnit.MILLISECONDS); executor.scheduleAtFixedRate(this, 0, 1000 / 20, TimeUnit.MILLISECONDS);
} }
@ -70,6 +73,12 @@ public class ServerThread implements Runnable {
@Override @Override
public void run() { public void run() {
try { try {
if (isShuttingDown)
{
getTicker().stop();
executor.shutdown();
return;
}
server.tick(); server.tick();
ticker.runOneTick(); ticker.runOneTick();
} catch (Throwable e) { } catch (Throwable e) {
@ -78,14 +87,10 @@ public class ServerThread implements Runnable {
} }
public void stop() { 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() { public Server getServer() {

View File

@ -115,6 +115,8 @@ class Ticker {
} catch (Exception e) { } catch (Exception e) {
getCoordinator().crash(e, this.name); getCoordinator().crash(e, this.name);
} }
} }
private synchronized boolean sleep() { private synchronized boolean sleep() {

View File

@ -104,6 +104,7 @@ public class LayerButtonTest extends MenuLayer {
//cm.unregisterAll(); //cm.unregisterAll();
alive = false; alive = false;
//ServerState.getInstance().;
ServerState.getInstance().shutdown("Safe Exit"); ServerState.getInstance().shutdown("Safe Exit");
ServerState.setInstance(null); ServerState.setInstance(null);

View File

@ -20,16 +20,17 @@ package ru.windcorp.progressia.test;
import java.io.BufferedInputStream; import java.io.BufferedInputStream;
import java.io.BufferedOutputStream; import java.io.BufferedOutputStream;
import java.io.BufferedWriter;
import java.io.DataInputStream; import java.io.DataInputStream;
import java.io.DataOutputStream; import java.io.DataOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.util.Collection; import java.util.Scanner;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.zip.DeflaterOutputStream; import java.util.zip.DeflaterOutputStream;
import java.util.zip.InflaterInputStream; import java.util.zip.InflaterInputStream;
@ -47,47 +48,68 @@ import ru.windcorp.progressia.server.Server;
public class TestWorldDiskIO { public class TestWorldDiskIO {
private static Path SAVE_DIR = Paths.get("tmp_world"); 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 Logger LOG = LogManager.getLogger("TestWorldDiskIO");
private static final boolean ENABLE = true; private static final boolean ENABLE = true;
private static final int maxSize = 1048576; private static final int maxSize = 1048576;
private static final int sectorSize = maxSize/256; 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;
if (loc<0) private static int chunksPerRegion;
return (-loc)<<1 + 1;
return loc<<1; 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) if ((loc & 1) == 1)
return -loc>>1; return -loc >> 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));
return new Vec3i(natFromInt(chunkLoc.x),natFromInt(chunkLoc.y),natFromInt(chunkLoc.z));
} }
public void initRegions() public static void initRegions() {
{
initRegions(null); initRegions(null);
} }
public void initRegions(Path worldPath) public static void initRegions(Path worldPath) {
{ if (worldPath != null) {
if (worldPath!=null)
{
SAVE_DIR = worldPath; 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) { public static void saveChunk(ChunkData chunk, Server server) {
@ -95,32 +117,65 @@ public class TestWorldDiskIO {
return; return;
try { try {
LOG.debug(
"Saving {} {} {}",
chunk.getPosition().x,
chunk.getPosition().y,
chunk.getPosition().z
);
Files.createDirectories(SAVE_DIR); if (currentFormat == 0) {
LOG.debug(
Path path = SAVE_DIR.resolve( "Saving {} {} {}",
String.format(
"chunk_%+d_%+d_%+d.progressia_chunk",
chunk.getPosition().x, chunk.getPosition().x,
chunk.getPosition().y, chunk.getPosition().y,
chunk.getPosition().z chunk.getPosition().z
) );
);
try ( Files.createDirectories(SAVE_DIR);
DataOutputStream output = new DataOutputStream(
new DeflaterOutputStream(new BufferedOutputStream(Files.newOutputStream(path))) Path path = SAVE_DIR.resolve(
) String.format(
) { "chunk_%+d_%+d_%+d" + extension,
ChunkIO.save(chunk, output, IOContext.SAVE); chunk.getPosition().x,
writeGenerationHint(chunk, output, server); 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) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
@ -135,47 +190,141 @@ public class TestWorldDiskIO {
if (!ENABLE) if (!ENABLE)
return null; return null;
Path path = SAVE_DIR.resolve( if (currentFormat == -1) {
String.format( Path formatPath = SAVE_DIR.resolve(formatFile);
"chunk_%+d_%+d_%+d.progressia_chunk", File format = formatPath.toFile();
chunkPos.x,
chunkPos.y,
chunkPos.z
)
);
if (!Files.exists(path)) { if (format.exists()) {
LOG.debug( String data = null;
"Not found {} {} {}", try {
chunkPos.x, Scanner reader = new Scanner(format);
chunkPos.y,
chunkPos.z
);
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 { if (currentFormat == 0) {
ChunkData result = load(path, chunkPos, world, server);
LOG.debug( Path path = SAVE_DIR.resolve(
"Loaded {} {} {}", String.format(
chunkPos.x, "chunk_%+d_%+d_%+d" + extension,
chunkPos.y, chunkPos.x,
chunkPos.z chunkPos.y,
chunkPos.z
)
); );
return result; if (!Files.exists(path)) {
} catch (Exception e) { LOG.debug(
e.printStackTrace(); "Not found {} {} {}",
LOG.debug( chunkPos.x,
"Could not load {} {} {}", chunkPos.y,
chunkPos.x, chunkPos.z
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) private static ChunkData load(Path path, Vec3i chunkPos, WorldData world, Server server)