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:
parent
6521cb5749
commit
c88dea6030
@ -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;
|
||||||
|
@ -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();
|
||||||
|
@ -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() {
|
||||||
|
@ -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() {
|
||||||
|
@ -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);
|
||||||
|
@ -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,6 +48,7 @@ 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;
|
||||||
@ -54,40 +56,60 @@ public class TestWorldDiskIO {
|
|||||||
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;
|
||||||
|
private static int chunksPerRegion;
|
||||||
|
|
||||||
|
private static int currentFormat = -1;
|
||||||
|
private static String extension = ".null";
|
||||||
|
|
||||||
|
private static int natFromInt(int loc) {
|
||||||
if (loc < 0)
|
if (loc < 0)
|
||||||
return (-loc) << 1 + 1;
|
return (-loc) << 1 + 1;
|
||||||
return loc << 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,6 +117,8 @@ public class TestWorldDiskIO {
|
|||||||
return;
|
return;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
|
||||||
|
if (currentFormat == 0) {
|
||||||
LOG.debug(
|
LOG.debug(
|
||||||
"Saving {} {} {}",
|
"Saving {} {} {}",
|
||||||
chunk.getPosition().x,
|
chunk.getPosition().x,
|
||||||
@ -106,7 +130,7 @@ public class TestWorldDiskIO {
|
|||||||
|
|
||||||
Path path = SAVE_DIR.resolve(
|
Path path = SAVE_DIR.resolve(
|
||||||
String.format(
|
String.format(
|
||||||
"chunk_%+d_%+d_%+d.progressia_chunk",
|
"chunk_%+d_%+d_%+d" + extension,
|
||||||
chunk.getPosition().x,
|
chunk.getPosition().x,
|
||||||
chunk.getPosition().y,
|
chunk.getPosition().y,
|
||||||
chunk.getPosition().z
|
chunk.getPosition().z
|
||||||
@ -121,6 +145,37 @@ public class TestWorldDiskIO {
|
|||||||
ChunkIO.save(chunk, output, IOContext.SAVE);
|
ChunkIO.save(chunk, output, IOContext.SAVE);
|
||||||
writeGenerationHint(chunk, output, server);
|
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,9 +190,57 @@ public class TestWorldDiskIO {
|
|||||||
if (!ENABLE)
|
if (!ENABLE)
|
||||||
return null;
|
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(
|
Path path = SAVE_DIR.resolve(
|
||||||
String.format(
|
String.format(
|
||||||
"chunk_%+d_%+d_%+d.progressia_chunk",
|
"chunk_%+d_%+d_%+d" + extension,
|
||||||
chunkPos.x,
|
chunkPos.x,
|
||||||
chunkPos.y,
|
chunkPos.y,
|
||||||
chunkPos.z
|
chunkPos.z
|
||||||
@ -176,6 +279,52 @@ public class TestWorldDiskIO {
|
|||||||
);
|
);
|
||||||
return null;
|
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)
|
private static ChunkData load(Path path, Vec3i chunkPos, WorldData world, Server server)
|
||||||
|
Reference in New Issue
Block a user