Merge branch 'save-world'
Third time's the charm!
This commit is contained in:
commit
0ccc108ddd
@ -18,18 +18,29 @@
|
||||
|
||||
package ru.windcorp.progressia;
|
||||
|
||||
import ru.windcorp.progressia.client.ClientProxy;
|
||||
import ru.windcorp.progressia.client.graphics.GUI;
|
||||
import ru.windcorp.progressia.common.util.crash.CrashReports;
|
||||
import ru.windcorp.progressia.common.util.crash.analyzers.OutOfMemoryAnalyzer;
|
||||
import ru.windcorp.progressia.common.util.crash.providers.*;
|
||||
import ru.windcorp.progressia.test.LayerTitle;
|
||||
|
||||
public class ProgressiaLauncher {
|
||||
|
||||
public static String[] arguments;
|
||||
private static ClientProxy proxy;
|
||||
|
||||
public static void launch(String[] args, Proxy proxy) {
|
||||
public static void launch(String[] args, ClientProxy inProxy) {
|
||||
arguments = args.clone();
|
||||
setupCrashReports();
|
||||
proxy.initialize();
|
||||
|
||||
inProxy.initialize();
|
||||
proxy = inProxy;
|
||||
GUI.addTopLayer(new LayerTitle("Title"));
|
||||
}
|
||||
|
||||
public static void play() {
|
||||
proxy.setupServer();
|
||||
}
|
||||
|
||||
private static void setupCrashReports() {
|
||||
|
@ -38,7 +38,9 @@ public class ClientProxy implements Proxy {
|
||||
|
||||
@Override
|
||||
public void initialize() {
|
||||
|
||||
GraphicsBackend.initialize();
|
||||
|
||||
try {
|
||||
RenderTaskQueue.waitAndInvoke(FlatRenderProgram::init);
|
||||
RenderTaskQueue.waitAndInvoke(WorldRenderProgram::init);
|
||||
@ -58,10 +60,12 @@ public class ClientProxy implements Proxy {
|
||||
|
||||
AudioSystem.initialize();
|
||||
|
||||
ServerState.startServer();
|
||||
ClientState.connectToLocalServer();
|
||||
|
||||
TestMusicPlayer.start();
|
||||
}
|
||||
|
||||
public void setupServer() {
|
||||
ServerState.startServer();
|
||||
ClientState.connectToLocalServer();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -20,10 +20,13 @@ package ru.windcorp.progressia.client;
|
||||
|
||||
import ru.windcorp.progressia.client.comms.localhost.LocalServerCommsChannel;
|
||||
import ru.windcorp.progressia.client.graphics.GUI;
|
||||
import ru.windcorp.progressia.client.graphics.Layer;
|
||||
import ru.windcorp.progressia.client.graphics.world.LayerWorld;
|
||||
import ru.windcorp.progressia.common.world.DefaultWorldData;
|
||||
import ru.windcorp.progressia.client.localization.MutableStringLocalized;
|
||||
import ru.windcorp.progressia.server.ServerState;
|
||||
import ru.windcorp.progressia.test.LayerAbout;
|
||||
import ru.windcorp.progressia.test.LayerTestText;
|
||||
import ru.windcorp.progressia.test.LayerTestUI;
|
||||
import ru.windcorp.progressia.test.TestContent;
|
||||
|
||||
@ -52,11 +55,39 @@ public class ClientState {
|
||||
channel.connect(TestContent.PLAYER_LOGIN);
|
||||
|
||||
setInstance(client);
|
||||
displayLoadingScreen();
|
||||
|
||||
GUI.addBottomLayer(new LayerWorld(client));
|
||||
GUI.addTopLayer(new LayerTestUI());
|
||||
GUI.addTopLayer(new LayerAbout());
|
||||
}
|
||||
|
||||
private static void displayLoadingScreen() {
|
||||
GUI.addTopLayer(new LayerTestText("Text", new MutableStringLocalized("LayerText.Load"), layer -> {
|
||||
Client client = ClientState.getInstance();
|
||||
|
||||
// TODO refacetor and remove
|
||||
if (client != null) {
|
||||
client.getComms().processPackets();
|
||||
}
|
||||
|
||||
if (client != null && client.getLocalPlayer().hasEntity()) {
|
||||
GUI.removeLayer(layer);
|
||||
|
||||
// TODO refactor, this shouldn't be here
|
||||
LayerWorld layerWorld = new LayerWorld(client);
|
||||
LayerTestUI layerUI = new LayerTestUI();
|
||||
LayerAbout layerAbout = new LayerAbout();
|
||||
GUI.addBottomLayer(layerWorld);
|
||||
GUI.addTopLayer(layerUI);
|
||||
GUI.addTopLayer(layerAbout);
|
||||
}
|
||||
}));
|
||||
}
|
||||
|
||||
public static void disconnectFromLocalServer() {
|
||||
getInstance().getComms().disconnect();
|
||||
|
||||
for (Layer layer : GUI.getLayers()) {
|
||||
GUI.removeLayer(layer);
|
||||
}
|
||||
}
|
||||
|
||||
private ClientState() {
|
||||
|
@ -21,6 +21,7 @@ package ru.windcorp.progressia.client.comms.localhost;
|
||||
import ru.windcorp.progressia.client.comms.ServerCommsChannel;
|
||||
import ru.windcorp.progressia.common.comms.packets.Packet;
|
||||
import ru.windcorp.progressia.server.Server;
|
||||
import ru.windcorp.progressia.server.ServerState;
|
||||
|
||||
public class LocalServerCommsChannel extends ServerCommsChannel {
|
||||
|
||||
@ -54,7 +55,7 @@ public class LocalServerCommsChannel extends ServerCommsChannel {
|
||||
|
||||
@Override
|
||||
public void disconnect() {
|
||||
// Do nothing
|
||||
ServerState.getInstance().getClientManager().disconnectClient(localClient);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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();
|
||||
|
@ -43,9 +43,9 @@ public abstract class BasicButton extends Component {
|
||||
private boolean isPressed = false;
|
||||
private final Collection<Consumer<BasicButton>> actions = Collections.synchronizedCollection(new ArrayList<>());
|
||||
|
||||
public BasicButton(String name, String label, Font labelFont) {
|
||||
public BasicButton(String name, Label label) {
|
||||
super(name);
|
||||
this.label = new Label(name + ".Label", labelFont, label);
|
||||
this.label = label;
|
||||
|
||||
setLayout(new LayoutAlign(10));
|
||||
addChild(this.label);
|
||||
@ -104,6 +104,10 @@ public abstract class BasicButton extends Component {
|
||||
});
|
||||
}
|
||||
|
||||
public BasicButton(String name, String label, Font labelFont) {
|
||||
this(name, new Label(name + ".Label", labelFont, label));
|
||||
}
|
||||
|
||||
public BasicButton(String name, String label) {
|
||||
this(name, label, new Font());
|
||||
}
|
||||
|
@ -29,6 +29,10 @@ public class Button extends BasicButton {
|
||||
super(name, label, labelFont);
|
||||
}
|
||||
|
||||
public Button(String name, Label label) {
|
||||
super(name, label);
|
||||
}
|
||||
|
||||
public Button(String name, String label) {
|
||||
this(name, label, new Font());
|
||||
}
|
||||
|
@ -105,7 +105,8 @@ public class RadioButton extends BasicButton {
|
||||
addChild(group);
|
||||
|
||||
addListener(KeyEvent.class, e -> {
|
||||
if (e.isRelease()) return false;
|
||||
if (e.isRelease())
|
||||
return false;
|
||||
|
||||
if (e.getKey() == GLFW.GLFW_KEY_LEFT || e.getKey() == GLFW.GLFW_KEY_UP) {
|
||||
if (this.group != null) {
|
||||
@ -149,7 +150,8 @@ public class RadioButton extends BasicButton {
|
||||
group.selectNext();
|
||||
removeAction(group.listener);
|
||||
group.buttons.remove(this);
|
||||
group.getSelected(); // Clear reference if this was the only button in the group
|
||||
group.getSelected(); // Clear reference if this was the only button
|
||||
// in the group
|
||||
}
|
||||
|
||||
this.group = group;
|
||||
@ -178,7 +180,8 @@ public class RadioButton extends BasicButton {
|
||||
this.checked = checked;
|
||||
|
||||
if (group != null) {
|
||||
group.listener.accept(this); // Failsafe for manual invocations of setChecked()
|
||||
group.listener.accept(this); // Failsafe for manual invocations of
|
||||
// setChecked()
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -0,0 +1,35 @@
|
||||
package ru.windcorp.progressia.common.util;
|
||||
|
||||
import glm.vec._3.i.Vec3i;
|
||||
|
||||
public class HashableVec3i {
|
||||
|
||||
public Vec3i value;
|
||||
|
||||
public HashableVec3i(Vec3i inValue)
|
||||
{
|
||||
value = inValue;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() // Uses first 3 primes greater than 2**30
|
||||
{
|
||||
return 1073741827 * value.x + 1073741831 * value.y + 1073741833 * value.z;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object comparee)
|
||||
{
|
||||
if (comparee == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if (comparee.getClass() != HashableVec3i.class)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
HashableVec3i compareeCast = (HashableVec3i) comparee;
|
||||
return compareeCast.value == value;
|
||||
}
|
||||
|
||||
}
|
@ -27,6 +27,7 @@ import ru.windcorp.progressia.common.util.crash.CrashReports;
|
||||
import ru.windcorp.progressia.common.world.entity.EntityData;
|
||||
import ru.windcorp.progressia.common.world.entity.EntityDataRegistry;
|
||||
import ru.windcorp.progressia.server.events.PlayerJoinedEvent;
|
||||
import ru.windcorp.progressia.server.events.PlayerLeftEvent;
|
||||
import ru.windcorp.progressia.test.TestContent;
|
||||
|
||||
public class PlayerManager {
|
||||
@ -48,6 +49,11 @@ public class PlayerManager {
|
||||
getServer().postEvent(new PlayerJoinedEvent.Immutable(getServer(), player));
|
||||
}
|
||||
|
||||
public void removePlayer(Player player) {
|
||||
this.players.remove(player);
|
||||
getServer().postEvent(new PlayerLeftEvent.Immutable(getServer(), player));
|
||||
}
|
||||
|
||||
public EntityData conjurePlayerEntity(String login) {
|
||||
// TODO Live up to the name
|
||||
if (TestContent.PLAYER_LOGIN.equals(login)) {
|
||||
|
@ -26,10 +26,13 @@ import org.apache.logging.log4j.LogManager;
|
||||
import ru.windcorp.progressia.common.util.crash.CrashReports;
|
||||
import ru.windcorp.progressia.server.world.ticking.TickerCoordinator;
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
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 +66,7 @@ public class ServerThread implements Runnable {
|
||||
}
|
||||
|
||||
public void start() {
|
||||
isShuttingDown = false;
|
||||
ticker.start();
|
||||
executor.scheduleAtFixedRate(this, 0, 1000 / 20, TimeUnit.MILLISECONDS);
|
||||
}
|
||||
@ -70,6 +74,11 @@ 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,13 +87,10 @@ public class ServerThread implements Runnable {
|
||||
}
|
||||
|
||||
public void stop() {
|
||||
try {
|
||||
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() {
|
||||
|
@ -92,6 +92,9 @@ public class ClientManager {
|
||||
public void disconnectClient(Client client) {
|
||||
client.disconnect();
|
||||
clientsById.remove(client.getId());
|
||||
if (client instanceof ClientPlayer) {
|
||||
getServer().getPlayerManager().removePlayer(((ClientPlayer) client).getPlayer());
|
||||
}
|
||||
}
|
||||
|
||||
public void processPackets() {
|
||||
|
@ -17,7 +17,11 @@
|
||||
*/
|
||||
package ru.windcorp.progressia.test;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
import ru.windcorp.progressia.client.ClientState;
|
||||
import ru.windcorp.progressia.client.graphics.Colors;
|
||||
import ru.windcorp.progressia.client.graphics.GUI;
|
||||
import ru.windcorp.progressia.client.graphics.font.Font;
|
||||
import ru.windcorp.progressia.client.graphics.gui.Button;
|
||||
import ru.windcorp.progressia.client.graphics.gui.Checkbox;
|
||||
@ -25,9 +29,15 @@ import ru.windcorp.progressia.client.graphics.gui.Label;
|
||||
import ru.windcorp.progressia.client.graphics.gui.RadioButton;
|
||||
import ru.windcorp.progressia.client.graphics.gui.RadioButtonGroup;
|
||||
import ru.windcorp.progressia.client.graphics.gui.menu.MenuLayer;
|
||||
import ru.windcorp.progressia.client.localization.MutableStringLocalized;
|
||||
import ru.windcorp.progressia.server.Player;
|
||||
import ru.windcorp.progressia.server.Server;
|
||||
import ru.windcorp.progressia.server.ServerState;
|
||||
|
||||
public class LayerButtonTest extends MenuLayer {
|
||||
|
||||
boolean alive = true;
|
||||
|
||||
public LayerButtonTest() {
|
||||
super("ButtonTest");
|
||||
|
||||
@ -60,8 +70,28 @@ public class LayerButtonTest extends MenuLayer {
|
||||
getCloseAction().run();
|
||||
}));
|
||||
|
||||
getContent().addChild(new Button("Quit", "Quit").addAction(b -> {
|
||||
System.exit(0);
|
||||
getContent().addChild(new Button("Menu", "Back To Menu").addAction(b -> {
|
||||
getCloseAction().run();
|
||||
|
||||
Collection<Player> players = ServerState.getInstance().getPlayerManager().getPlayers();
|
||||
players.clear();
|
||||
|
||||
ClientState.disconnectFromLocalServer();
|
||||
|
||||
GUI.addTopLayer(new LayerTestText("Text", new MutableStringLocalized("LayerText.Save"), layer -> {
|
||||
Server server = ServerState.getInstance();
|
||||
if (server != null && server.getWorld().getChunks().isEmpty()) {
|
||||
GUI.removeLayer(layer);
|
||||
|
||||
// TODO Refactor, this shouldn't be here
|
||||
GUI.addTopLayer(new LayerTitle("Title"));
|
||||
ServerState.getInstance().shutdown("Safe Exit");
|
||||
ServerState.setInstance(null);
|
||||
TestPlayerControls.resetInstance();
|
||||
}
|
||||
}));
|
||||
|
||||
ClientState.setInstance(null);
|
||||
}));
|
||||
|
||||
getContent().takeFocus();
|
||||
|
29
src/main/java/ru/windcorp/progressia/test/LayerTestText.java
Normal file
29
src/main/java/ru/windcorp/progressia/test/LayerTestText.java
Normal file
@ -0,0 +1,29 @@
|
||||
package ru.windcorp.progressia.test;
|
||||
|
||||
import java.util.function.Consumer;
|
||||
import ru.windcorp.progressia.client.graphics.Colors;
|
||||
import ru.windcorp.progressia.client.graphics.font.Font;
|
||||
import ru.windcorp.progressia.client.graphics.gui.GUILayer;
|
||||
import ru.windcorp.progressia.client.graphics.gui.Label;
|
||||
import ru.windcorp.progressia.client.graphics.gui.layout.LayoutAlign;
|
||||
import ru.windcorp.progressia.client.localization.MutableString;
|
||||
|
||||
public class LayerTestText extends GUILayer {
|
||||
|
||||
private final Consumer<LayerTestText> remover;
|
||||
|
||||
public LayerTestText(String name, MutableString value, Consumer<LayerTestText> remover) {
|
||||
super(name, new LayoutAlign(15));
|
||||
this.remover = remover;
|
||||
|
||||
Font titleFont = new Font().deriveBold().withColor(Colors.BLACK).withAlign(0.5f);
|
||||
getRoot().addChild(new Label(name + ".Text", titleFont, value));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void doRender() {
|
||||
super.doRender();
|
||||
remover.accept(this);
|
||||
}
|
||||
|
||||
}
|
37
src/main/java/ru/windcorp/progressia/test/LayerTitle.java
Normal file
37
src/main/java/ru/windcorp/progressia/test/LayerTitle.java
Normal file
@ -0,0 +1,37 @@
|
||||
package ru.windcorp.progressia.test;
|
||||
|
||||
import ru.windcorp.progressia.ProgressiaLauncher;
|
||||
import ru.windcorp.progressia.client.graphics.Colors;
|
||||
import ru.windcorp.progressia.client.graphics.GUI;
|
||||
import ru.windcorp.progressia.client.graphics.font.Font;
|
||||
import ru.windcorp.progressia.client.graphics.gui.Button;
|
||||
import ru.windcorp.progressia.client.graphics.gui.GUILayer;
|
||||
import ru.windcorp.progressia.client.graphics.gui.Label;
|
||||
import ru.windcorp.progressia.client.graphics.gui.layout.LayoutVertical;
|
||||
import ru.windcorp.progressia.client.localization.MutableString;
|
||||
import ru.windcorp.progressia.client.localization.MutableStringLocalized;
|
||||
|
||||
public class LayerTitle extends GUILayer {
|
||||
|
||||
public LayerTitle(String name) {
|
||||
super(name, new LayoutVertical(20, 10));
|
||||
|
||||
MutableString title = new MutableStringLocalized("Layer" + name + ".Title");
|
||||
Font titleFont = new Font().deriveBold().withColor(Colors.BLACK).withAlign(0.5f);
|
||||
getRoot().addChild(new Label(name + ".Title", titleFont, title));
|
||||
|
||||
Font buttonFont = titleFont;
|
||||
MutableString playText = new MutableStringLocalized("Layer" + name + ".Play");
|
||||
getRoot().addChild(new Button(name + ".Play", new Label(name + ".Play", buttonFont, playText)).addAction(b -> {
|
||||
GUI.removeLayer(this);
|
||||
|
||||
ProgressiaLauncher.play();
|
||||
}));
|
||||
|
||||
MutableString quitText = new MutableStringLocalized("Layer" + name + ".Quit");
|
||||
getRoot().addChild(new Button(name + "Quit", new Label(name + ".Quit", buttonFont, quitText)).addAction(b -> {
|
||||
System.exit(0);
|
||||
}));
|
||||
}
|
||||
|
||||
}
|
@ -46,10 +46,10 @@ import ru.windcorp.progressia.server.ServerState;
|
||||
|
||||
public class TestPlayerControls {
|
||||
|
||||
private static final TestPlayerControls INSTANCE = new TestPlayerControls();
|
||||
private static TestPlayerControls instance = new TestPlayerControls();
|
||||
|
||||
public static TestPlayerControls getInstance() {
|
||||
return INSTANCE;
|
||||
return instance;
|
||||
}
|
||||
|
||||
private static final double MODE_SWITCH_MAX_DELAY = 300 * Units.MILLISECONDS;
|
||||
@ -91,6 +91,10 @@ public class TestPlayerControls {
|
||||
private LayerTestGUI debugLayer = null;
|
||||
private Runnable updateCallback = null;
|
||||
|
||||
public static void resetInstance() {
|
||||
instance = new TestPlayerControls();
|
||||
}
|
||||
|
||||
public void applyPlayerControls() {
|
||||
if (ClientState.getInstance() == null || !ClientState.getInstance().isReady()) {
|
||||
return;
|
||||
|
@ -20,12 +20,25 @@ package ru.windcorp.progressia.test;
|
||||
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.BufferedOutputStream;
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.DataInputStream;
|
||||
import java.io.DataOutputStream;
|
||||
import java.io.EOFException;
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.io.RandomAccessFile;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.MappedByteBuffer;
|
||||
import java.nio.channels.FileChannel;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.HashMap;
|
||||
import java.util.Scanner;
|
||||
import java.util.zip.DeflaterOutputStream;
|
||||
import java.util.zip.InflaterInputStream;
|
||||
|
||||
@ -34,6 +47,7 @@ import org.apache.logging.log4j.Logger;
|
||||
|
||||
import glm.vec._3.i.Vec3i;
|
||||
import ru.windcorp.progressia.common.state.IOContext;
|
||||
import ru.windcorp.progressia.common.util.HashableVec3i;
|
||||
import ru.windcorp.progressia.common.world.DefaultChunkData;
|
||||
import ru.windcorp.progressia.common.world.DecodingException;
|
||||
import ru.windcorp.progressia.common.world.DefaultWorldData;
|
||||
@ -42,16 +56,140 @@ import ru.windcorp.progressia.server.Server;
|
||||
|
||||
public class TestWorldDiskIO {
|
||||
|
||||
private static final Path SAVE_DIR = Paths.get("tmp_world");
|
||||
private static Path SAVE_DIR = Paths.get("tmp_world");
|
||||
private static final String formatFile = "world.format";
|
||||
private static final Logger LOG = LogManager.getLogger("TestWorldDiskIO");
|
||||
|
||||
private static final boolean ENABLE = false;
|
||||
private static HashMap<HashableVec3i, MappedByteBuffer> mappedByteMap;
|
||||
private static final boolean ENABLE = true;
|
||||
|
||||
private static int maxSize = 1048576;
|
||||
private static int sectorSize = maxSize / 256;
|
||||
|
||||
private static final int bestFormat = 65536;
|
||||
|
||||
// private Map<Vec3i,Vec3i> regions = new HashMap<Vec3i,Vec3i>();
|
||||
private static Vec3i regionSize;
|
||||
private static int chunksPerRegion;
|
||||
private static int offsetBytes;
|
||||
|
||||
private static int currentFormat = -1;
|
||||
private static String extension = ".null";
|
||||
|
||||
private static int natFromInt(int loc) {
|
||||
if (loc < 0)
|
||||
return -2*loc - 1;
|
||||
return 2*loc;
|
||||
}
|
||||
|
||||
/*
|
||||
* private static int intFromNat(int loc) // Possibly unused
|
||||
* {
|
||||
* if ((loc & 1) == 1)
|
||||
* return -loc >> 1;
|
||||
* return loc >> 1;
|
||||
* }
|
||||
*/
|
||||
|
||||
private static Vec3i getRegion(Vec3i chunkLoc) {
|
||||
int x = chunkLoc.x;
|
||||
if (x<0)
|
||||
{
|
||||
x /= regionSize.x;
|
||||
x--;
|
||||
}
|
||||
else
|
||||
{
|
||||
x /= regionSize.x;
|
||||
}
|
||||
int y = chunkLoc.y;
|
||||
if (y<0)
|
||||
{
|
||||
y /= regionSize.y;
|
||||
y--;
|
||||
}
|
||||
else
|
||||
{
|
||||
y /= regionSize.y;
|
||||
}
|
||||
int z = chunkLoc.z;
|
||||
if (z<0)
|
||||
{
|
||||
z /= regionSize.z;
|
||||
z--;
|
||||
}
|
||||
else
|
||||
{
|
||||
z /= regionSize.z;
|
||||
}
|
||||
return new Vec3i(
|
||||
natFromInt(x),
|
||||
natFromInt(y),
|
||||
natFromInt(z)
|
||||
);
|
||||
}
|
||||
|
||||
private static int mod(int a, int m) {
|
||||
return ((a % m) + m) % m;
|
||||
}
|
||||
|
||||
private static Vec3i getRegionLoc(Vec3i chunkLoc) {
|
||||
return new Vec3i(mod(chunkLoc.x, regionSize.x), mod(chunkLoc.y, regionSize.y), mod(chunkLoc.z, regionSize.z));
|
||||
}
|
||||
|
||||
public static void initRegions() {
|
||||
initRegions(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));
|
||||
}
|
||||
|
||||
public static int getAvailableSector(MappedByteBuffer mbb)
|
||||
{
|
||||
int sectorsUsed = 0;
|
||||
for (int i=offsetBytes; i<(offsetBytes+1)*chunksPerRegion; i+= (offsetBytes+1))
|
||||
{
|
||||
sectorsUsed += mbb.get(i);
|
||||
}
|
||||
return sectorsUsed;
|
||||
}
|
||||
|
||||
private static void setRegionSize(int format) {
|
||||
mappedByteMap = new HashMap<HashableVec3i, MappedByteBuffer>();
|
||||
switch (format) {
|
||||
case 0:
|
||||
case 1:
|
||||
regionSize = new Vec3i(1);
|
||||
chunksPerRegion = 1;
|
||||
currentFormat = format;
|
||||
extension = ".progressia_chunk";
|
||||
break;
|
||||
case 65536:
|
||||
regionSize = new Vec3i(16);
|
||||
chunksPerRegion = 16 * 16 * 16;
|
||||
currentFormat = 65536;
|
||||
offsetBytes = 3;
|
||||
extension = ".progressia_region";
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/*private static void expand(int sectors) {
|
||||
|
||||
}*/
|
||||
|
||||
public static void saveChunk(DefaultChunkData chunk, Server server) {
|
||||
if (!ENABLE)
|
||||
return;
|
||||
|
||||
try {
|
||||
|
||||
if (currentFormat == 0) {
|
||||
LOG.debug(
|
||||
"Saving {} {} {}",
|
||||
chunk.getPosition().x,
|
||||
@ -63,7 +201,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
|
||||
@ -78,11 +216,182 @@ 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 = getRegion(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 == 65536) {
|
||||
LOG.debug(
|
||||
"Saving {} {} {}",
|
||||
chunk.getPosition().x,
|
||||
chunk.getPosition().y,
|
||||
chunk.getPosition().z
|
||||
);
|
||||
|
||||
Files.createDirectories(SAVE_DIR);
|
||||
|
||||
Vec3i saveCoords = getRegion(chunk.getPosition());
|
||||
|
||||
Path path = SAVE_DIR.resolve(
|
||||
String.format(
|
||||
"%d_%d_%d" + extension,
|
||||
saveCoords.x,
|
||||
saveCoords.y,
|
||||
saveCoords.z
|
||||
)
|
||||
);
|
||||
|
||||
/*
|
||||
* if (!dosave)
|
||||
* {
|
||||
* return;
|
||||
* }
|
||||
* dosave = false;
|
||||
*/
|
||||
|
||||
|
||||
MappedByteBuffer output = mappedByteMap.get(new HashableVec3i(saveCoords));
|
||||
LOG.info("saveCoords {},{},{}", saveCoords.x, saveCoords.y, saveCoords.z);
|
||||
if (output == null)
|
||||
{
|
||||
output = makeNew(path, new HashableVec3i(saveCoords));
|
||||
}
|
||||
// LOG.debug(output.read());
|
||||
if (output.get() < 0) {
|
||||
LOG.info("Making header");
|
||||
ByteBuffer headerBytes = ByteBuffer.allocate((offsetBytes + 1) * chunksPerRegion);
|
||||
for (int i=0;i<(offsetBytes + 1) * chunksPerRegion;i++)
|
||||
{
|
||||
headerBytes.put(i, (byte) 0);
|
||||
}
|
||||
output.put(headerBytes);
|
||||
}
|
||||
|
||||
Vec3i pos = getRegionLoc(chunk.getPosition());
|
||||
int shortOffset = (offsetBytes + 1) * (pos.z + regionSize.z * (pos.y + regionSize.y * pos.x));
|
||||
int fullOffset = (offsetBytes + 1) * (chunksPerRegion);
|
||||
output.position(shortOffset);
|
||||
int offset = 0;
|
||||
for (int i = 0; i < offsetBytes; i++) {
|
||||
offset *= 256;
|
||||
offset += output.get();
|
||||
}
|
||||
ByteBuffer readOffset = ByteBuffer.allocate(offsetBytes);
|
||||
int sectorLength = output.get();
|
||||
if (sectorLength == 0) {
|
||||
//int outputLen = (int) output.size();
|
||||
//offset = (int) (outputLen - fullOffset) / sectorSize + 1;
|
||||
offset = getAvailableSector(output);
|
||||
output.position(shortOffset);
|
||||
|
||||
//readInt.putInt(offset<<8);
|
||||
for (int i=0;i<offsetBytes;i++)
|
||||
{
|
||||
readOffset.put(offsetBytes-i-1, (byte) (offset<<8));
|
||||
}
|
||||
output.put(readOffset);
|
||||
/*output.position(outputLen);
|
||||
|
||||
while (output.size()<fullOffset+sectorSize*offset)
|
||||
{
|
||||
output.write(ByteBuffer.wrap( new byte[]{0} ));
|
||||
}*/
|
||||
|
||||
//output.setLength(fullOffset + offset * sectorSize);
|
||||
// output.write(200);
|
||||
}
|
||||
|
||||
// int bytestoWrite = output.readInt();
|
||||
// output.mark(sectorSize*sectorLength);
|
||||
|
||||
// BufferedOutputStream counter = new
|
||||
// BufferedOutputStream(Files.newOutputStream(
|
||||
// SAVE_DIR.resolve(tempFile)));
|
||||
ByteArrayOutputStream tempDataStream = new ByteArrayOutputStream();
|
||||
DataOutputStream trueOutput = new DataOutputStream(
|
||||
new DeflaterOutputStream(
|
||||
new BufferedOutputStream(tempDataStream)
|
||||
)
|
||||
);
|
||||
// CountingOutputStream countOutput = new
|
||||
// CountingOutputStream(trueOutput);
|
||||
|
||||
// LOG.info("Before: {}",output.);
|
||||
// trueOutput.writeBytes("uh try this");
|
||||
// counter.
|
||||
ChunkIO.save(chunk, trueOutput, IOContext.SAVE);
|
||||
writeGenerationHint(chunk, trueOutput, server);
|
||||
|
||||
/*
|
||||
* while (counter.getCount()%sectorSize != 0) {
|
||||
* counter.write(0);
|
||||
* }
|
||||
*/
|
||||
|
||||
// LOG.info("Wrote {} bytes to
|
||||
// {},{},{}",trueOutput.size(),chunk.getPosition().x,chunk.getPosition().y,chunk.getPosition().z);
|
||||
|
||||
trueOutput.close();
|
||||
|
||||
byte tempData[] = tempDataStream.toByteArray();
|
||||
|
||||
output.position( fullOffset + sectorSize * offset);
|
||||
output.put(ByteBuffer.wrap( tempData));
|
||||
|
||||
output.position(shortOffset + offsetBytes);
|
||||
output.put(ByteBuffer.wrap( new byte[] {(byte) (tempData.length / sectorSize + 1)}));
|
||||
// LOG.info("Used {} sectors",(int)
|
||||
// tempData.length/sectorSize + 1);
|
||||
|
||||
}
|
||||
// else if (currentFormat)
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
private static MappedByteBuffer makeNew(Path path, Object hashObj) {
|
||||
try
|
||||
{
|
||||
RandomAccessFile raf = new RandomAccessFile(path.toFile(), "rw");
|
||||
FileChannel fc = raf.getChannel();
|
||||
MappedByteBuffer output = fc.map(FileChannel.MapMode.READ_WRITE, 0, maxSize*chunksPerRegion);
|
||||
output.limit(maxSize*chunksPerRegion);
|
||||
mappedByteMap.put((HashableVec3i) hashObj, output);
|
||||
return output;
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
LOG.warn("bad things");
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private static void writeGenerationHint(DefaultChunkData chunk, DataOutputStream output, Server server)
|
||||
throws IOException {
|
||||
server.getWorld().getGenerator().writeGenerationHint(output, chunk.getGenerationHint());
|
||||
@ -92,9 +401,69 @@ 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);
|
||||
|
||||
LOG.debug("Making new world with format {}", bestFormat);
|
||||
|
||||
BufferedWriter bw;
|
||||
try {
|
||||
bw = new BufferedWriter(new FileWriter(format));
|
||||
|
||||
int bfClone = bestFormat;
|
||||
|
||||
for (int i = 0; i < 4; i++) {
|
||||
bw.write(bfClone >> 24);
|
||||
LOG.debug(bfClone >> 24);
|
||||
bfClone = bfClone << 8;
|
||||
}
|
||||
|
||||
/*
|
||||
* bw.write(
|
||||
* new char[] {
|
||||
* (char) bestFormat / (256 * 256 * 256),
|
||||
* (char) (bestFormat % 256) / (256 * 256),
|
||||
* (char) (bestFormat % (256 * 256)) / (256),
|
||||
* (char) (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
|
||||
@ -133,6 +502,96 @@ public class TestWorldDiskIO {
|
||||
);
|
||||
return null;
|
||||
}
|
||||
} else if (currentFormat == 1) {
|
||||
Vec3i saveCoords = getRegion(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 {
|
||||
DefaultChunkData 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 == 65536) {
|
||||
Vec3i saveCoords = getRegion(chunkPos);
|
||||
|
||||
Path path = SAVE_DIR.resolve(
|
||||
String.format(
|
||||
"%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 {
|
||||
DefaultChunkData result = loadRegion(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 DefaultChunkData load(Path path, Vec3i chunkPos, DefaultWorldData world, Server server)
|
||||
@ -149,6 +608,56 @@ public class TestWorldDiskIO {
|
||||
}
|
||||
}
|
||||
|
||||
private static DefaultChunkData loadRegion(Path path, Vec3i chunkPos, DefaultWorldData world, Server server)
|
||||
throws IOException,
|
||||
DecodingException {
|
||||
try
|
||||
{
|
||||
Vec3i streamCoords = getRegion(chunkPos);
|
||||
|
||||
MappedByteBuffer input = mappedByteMap.get(new HashableVec3i(streamCoords));
|
||||
LOG.info("streamCoords {},{},{}", streamCoords.x,streamCoords.y,streamCoords.z);
|
||||
if (input == null)
|
||||
{
|
||||
//input = new RandomAccessFile(path.toFile(), "rw");
|
||||
//input = Files.newByteChannel(path);
|
||||
input = makeNew(path, new HashableVec3i(streamCoords));
|
||||
}
|
||||
|
||||
// LOG.info(path.toString());
|
||||
Vec3i pos = getRegionLoc(chunkPos);
|
||||
|
||||
int shortOffset = (offsetBytes + 1) * (pos.z + regionSize.z * (pos.y + regionSize.y * pos.x));
|
||||
int fullOffset = (offsetBytes + 1) * (chunksPerRegion);
|
||||
input.position(shortOffset);
|
||||
int offset = 0;
|
||||
for (int i = 0; i < offsetBytes; i++) {
|
||||
offset *= 256;
|
||||
offset += input.get();
|
||||
}
|
||||
int sectorLength = input.get();
|
||||
input.position(fullOffset + sectorSize * offset);
|
||||
|
||||
// LOG.info("Read {} sectors", sectorLength);
|
||||
|
||||
byte tempData[] = new byte[sectorSize * sectorLength];
|
||||
input.get(tempData);
|
||||
|
||||
DataInputStream trueInput = new DataInputStream(
|
||||
new InflaterInputStream(new BufferedInputStream(new ByteArrayInputStream(tempData)))
|
||||
);
|
||||
DefaultChunkData chunk = ChunkIO.load(world, chunkPos, trueInput, IOContext.SAVE);
|
||||
readGenerationHint(chunk, trueInput, server);
|
||||
return chunk;
|
||||
}
|
||||
catch (EOFException e)
|
||||
{
|
||||
LOG.warn("Reached end of file");
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private static void readGenerationHint(DefaultChunkData chunk, DataInputStream input, Server server)
|
||||
throws IOException,
|
||||
DecodingException {
|
||||
|
@ -22,3 +22,12 @@ LayerTestGUI.IsFullscreen = Fullscreen: %5s (F11)
|
||||
LayerTestGUI.IsVSync = VSync: %5s (F12)
|
||||
|
||||
LayerButtonTest.Title = Button Test
|
||||
LayerButtonTest.Return = Back To Menu
|
||||
|
||||
LayerTitle.Title = Progressia
|
||||
LayerTitle.Play = Play World
|
||||
LayerTitle.Options = Options
|
||||
LayerTitle.Quit = Quit
|
||||
|
||||
LayerText.Load = Loading...
|
||||
LayerText.Save = Saving...
|
@ -22,3 +22,12 @@ LayerTestGUI.IsFullscreen = Полный экран: %5s (F11)
|
||||
LayerTestGUI.IsVSync = Верт. синхр.: %5s (F12)
|
||||
|
||||
LayerButtonTest.Title = Тест Кнопок
|
||||
LayerButtonTest.Return = Back To Menu [[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[
|
||||
|
||||
LayerTitle.Title = Прогрессия
|
||||
LayerTitle.Play = ???????
|
||||
LayerTitle.Options = ????????
|
||||
LayerTitle.Quit = ????????
|
||||
|
||||
LayerText.Load = Loading... (Change)
|
||||
LayerText.Save = Saving...(Cahnsf)
|
@ -26,6 +26,7 @@
|
||||
<Logger name="Ticker Coordinator" level="DEBUG" />
|
||||
<Logger name="Ticker 0" level="DEBUG" />
|
||||
-->
|
||||
<!-- <Logger name="TestWorldDiskIO" level="DEBUG" /> -->
|
||||
|
||||
<Root level="info">
|
||||
<AppenderRef ref="FileLog" />
|
||||
|
Reference in New Issue
Block a user