Hash Things
-Removed unused imports -Using HashMap to keep track of RandomAccessFile instances -Using those instances to do stuff -Made new HashableVec3i class that allows for hashing of 3d vectors.
This commit is contained in:
parent
b7dcbb0f30
commit
e4ced6507e
@ -33,7 +33,6 @@ import ru.windcorp.progressia.common.util.crash.CrashReports;
|
|||||||
import ru.windcorp.progressia.server.ServerState;
|
import ru.windcorp.progressia.server.ServerState;
|
||||||
import ru.windcorp.progressia.test.TestContent;
|
import ru.windcorp.progressia.test.TestContent;
|
||||||
import ru.windcorp.progressia.test.TestMusicPlayer;
|
import ru.windcorp.progressia.test.TestMusicPlayer;
|
||||||
import ru.windcorp.progressia.test.TestPlayerControls;
|
|
||||||
|
|
||||||
public class ClientProxy implements Proxy {
|
public class ClientProxy implements Proxy {
|
||||||
|
|
||||||
|
@ -0,0 +1,26 @@
|
|||||||
|
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)
|
||||||
|
{
|
||||||
|
return hashCode() == comparee.hashCode();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -26,6 +26,7 @@ import org.apache.logging.log4j.LogManager;
|
|||||||
import ru.windcorp.progressia.common.util.crash.CrashReports;
|
import ru.windcorp.progressia.common.util.crash.CrashReports;
|
||||||
import ru.windcorp.progressia.server.world.ticking.TickerCoordinator;
|
import ru.windcorp.progressia.server.world.ticking.TickerCoordinator;
|
||||||
|
|
||||||
|
@SuppressWarnings("unused")
|
||||||
public class ServerThread implements Runnable {
|
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<>();
|
||||||
|
@ -22,7 +22,6 @@ import java.util.Collection;
|
|||||||
import ru.windcorp.progressia.client.ClientState;
|
import ru.windcorp.progressia.client.ClientState;
|
||||||
import ru.windcorp.progressia.client.graphics.Colors;
|
import ru.windcorp.progressia.client.graphics.Colors;
|
||||||
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.font.Font;
|
import ru.windcorp.progressia.client.graphics.font.Font;
|
||||||
import ru.windcorp.progressia.client.graphics.gui.Button;
|
import ru.windcorp.progressia.client.graphics.gui.Button;
|
||||||
import ru.windcorp.progressia.client.graphics.gui.Checkbox;
|
import ru.windcorp.progressia.client.graphics.gui.Checkbox;
|
||||||
@ -34,7 +33,6 @@ import ru.windcorp.progressia.client.localization.MutableString;
|
|||||||
import ru.windcorp.progressia.client.localization.MutableStringLocalized;
|
import ru.windcorp.progressia.client.localization.MutableStringLocalized;
|
||||||
import ru.windcorp.progressia.server.ChunkManager;
|
import ru.windcorp.progressia.server.ChunkManager;
|
||||||
import ru.windcorp.progressia.server.ChunkManager.ChunksLoadFinishListener;
|
import ru.windcorp.progressia.server.ChunkManager.ChunksLoadFinishListener;
|
||||||
import ru.windcorp.progressia.server.ChunkManager.ChunksLoadListener;
|
|
||||||
import ru.windcorp.progressia.server.Player;
|
import ru.windcorp.progressia.server.Player;
|
||||||
import ru.windcorp.progressia.server.ServerState;
|
import ru.windcorp.progressia.server.ServerState;
|
||||||
|
|
||||||
|
@ -34,6 +34,7 @@ import java.nio.channels.Channels;
|
|||||||
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.HashMap;
|
||||||
import java.util.Scanner;
|
import java.util.Scanner;
|
||||||
import java.util.zip.DeflaterOutputStream;
|
import java.util.zip.DeflaterOutputStream;
|
||||||
import java.util.zip.InflaterInputStream;
|
import java.util.zip.InflaterInputStream;
|
||||||
@ -41,10 +42,9 @@ import java.util.zip.InflaterInputStream;
|
|||||||
import org.apache.logging.log4j.LogManager;
|
import org.apache.logging.log4j.LogManager;
|
||||||
import org.apache.logging.log4j.Logger;
|
import org.apache.logging.log4j.Logger;
|
||||||
|
|
||||||
import com.google.common.io.CountingOutputStream;
|
|
||||||
|
|
||||||
import glm.vec._3.i.Vec3i;
|
import glm.vec._3.i.Vec3i;
|
||||||
import ru.windcorp.progressia.common.state.IOContext;
|
import ru.windcorp.progressia.common.state.IOContext;
|
||||||
|
import ru.windcorp.progressia.common.util.HashableVec3i;
|
||||||
import ru.windcorp.progressia.common.world.ChunkData;
|
import ru.windcorp.progressia.common.world.ChunkData;
|
||||||
import ru.windcorp.progressia.common.world.DecodingException;
|
import ru.windcorp.progressia.common.world.DecodingException;
|
||||||
import ru.windcorp.progressia.common.world.WorldData;
|
import ru.windcorp.progressia.common.world.WorldData;
|
||||||
@ -57,6 +57,7 @@ public class TestWorldDiskIO {
|
|||||||
private static final String formatFile = "world.format";
|
private static final String formatFile = "world.format";
|
||||||
private static final Logger LOG = LogManager.getLogger("TestWorldDiskIO");
|
private static final Logger LOG = LogManager.getLogger("TestWorldDiskIO");
|
||||||
|
|
||||||
|
private static HashMap<HashableVec3i, RandomAccessFile> randomAccessMap;
|
||||||
private static final boolean ENABLE = true;
|
private static final boolean ENABLE = true;
|
||||||
|
|
||||||
private static int maxSize = 1048576;
|
private static int maxSize = 1048576;
|
||||||
@ -116,6 +117,7 @@ public class TestWorldDiskIO {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private static void setRegionSize(int format) {
|
private static void setRegionSize(int format) {
|
||||||
|
randomAccessMap = new HashMap<HashableVec3i, RandomAccessFile>();
|
||||||
switch (format) {
|
switch (format) {
|
||||||
case 0:
|
case 0:
|
||||||
case 1:
|
case 1:
|
||||||
@ -134,9 +136,9 @@ public class TestWorldDiskIO {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void expand(int sectors) {
|
/*private static void expand(int sectors) {
|
||||||
|
|
||||||
}
|
}*/
|
||||||
|
|
||||||
public static void saveChunk(ChunkData chunk, Server server) {
|
public static void saveChunk(ChunkData chunk, Server server) {
|
||||||
if (!ENABLE)
|
if (!ENABLE)
|
||||||
@ -229,9 +231,13 @@ public class TestWorldDiskIO {
|
|||||||
* dosave = false;
|
* dosave = false;
|
||||||
*/
|
*/
|
||||||
|
|
||||||
try (
|
|
||||||
RandomAccessFile output = new RandomAccessFile(path.toFile(), "rw")
|
RandomAccessFile output = randomAccessMap.get(new HashableVec3i(saveCoords));
|
||||||
) {
|
if (output == null)
|
||||||
|
{
|
||||||
|
output = new RandomAccessFile(path.toFile(), "rw");
|
||||||
|
randomAccessMap.put(new HashableVec3i(saveCoords), output);
|
||||||
|
}
|
||||||
// LOG.debug(output.read());
|
// LOG.debug(output.read());
|
||||||
if (output.read() < 0) {
|
if (output.read() < 0) {
|
||||||
LOG.info("Making header");
|
LOG.info("Making header");
|
||||||
@ -303,9 +309,6 @@ public class TestWorldDiskIO {
|
|||||||
// LOG.info("Used {} sectors",(int)
|
// LOG.info("Used {} sectors",(int)
|
||||||
// tempData.length/sectorSize + 1);
|
// tempData.length/sectorSize + 1);
|
||||||
|
|
||||||
output.close();
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
// else if (currentFormat)
|
// else if (currentFormat)
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
@ -532,36 +535,41 @@ public class TestWorldDiskIO {
|
|||||||
private static ChunkData loadRegion(Path path, Vec3i chunkPos, WorldData world, Server server)
|
private static ChunkData loadRegion(Path path, Vec3i chunkPos, WorldData world, Server server)
|
||||||
throws IOException,
|
throws IOException,
|
||||||
DecodingException {
|
DecodingException {
|
||||||
try (
|
Vec3i streamCoords = getRegion(chunkPos);
|
||||||
BufferedInputStream input = new BufferedInputStream(Files.newInputStream(path))
|
|
||||||
) {
|
RandomAccessFile input = randomAccessMap.get(new HashableVec3i(streamCoords));
|
||||||
|
if (input == null)
|
||||||
|
{
|
||||||
|
input = new RandomAccessFile(path.toFile(), "rw");
|
||||||
|
randomAccessMap.put(new HashableVec3i(streamCoords), input);
|
||||||
|
}
|
||||||
|
|
||||||
// LOG.info(path.toString());
|
// LOG.info(path.toString());
|
||||||
Vec3i pos = getRegionLoc(chunkPos);
|
Vec3i pos = getRegionLoc(chunkPos);
|
||||||
|
|
||||||
int shortOffset = (offsetBytes + 1) * (pos.z + regionSize.z * (pos.y + regionSize.y * pos.x));
|
int shortOffset = (offsetBytes + 1) * (pos.z + regionSize.z * (pos.y + regionSize.y * pos.x));
|
||||||
int fullOffset = (offsetBytes + 1) * (chunksPerRegion);
|
int fullOffset = (offsetBytes + 1) * (chunksPerRegion);
|
||||||
input.skipNBytes(shortOffset);
|
input.seek(shortOffset);
|
||||||
int offset = 0;
|
int offset = 0;
|
||||||
for (int i = 0; i < offsetBytes; i++) {
|
for (int i = 0; i < offsetBytes; i++) {
|
||||||
offset *= 256;
|
offset *= 256;
|
||||||
offset += input.read();
|
offset += input.read();
|
||||||
}
|
}
|
||||||
int sectorLength = input.read();
|
int sectorLength = input.read();
|
||||||
input.skipNBytes(fullOffset - shortOffset - offsetBytes - 1);
|
input.seek(fullOffset + sectorSize * offset);
|
||||||
input.skipNBytes(sectorSize * offset);
|
|
||||||
|
|
||||||
// LOG.info("Read {} sectors", sectorLength);
|
// LOG.info("Read {} sectors", sectorLength);
|
||||||
|
|
||||||
byte tempData[] = input.readNBytes(sectorSize * sectorLength);
|
byte tempData[] = new byte[sectorSize * sectorLength];
|
||||||
|
input.read(tempData);
|
||||||
|
|
||||||
DataInputStream trueInput = new DataInputStream(
|
DataInputStream trueInput = new DataInputStream(
|
||||||
new InflaterInputStream(new BufferedInputStream(new ByteArrayInputStream(tempData)))
|
new InflaterInputStream(new BufferedInputStream(new ByteArrayInputStream(tempData)))
|
||||||
);
|
);
|
||||||
ChunkData chunk = ChunkIO.load(world, chunkPos, trueInput, IOContext.SAVE);
|
ChunkData chunk = ChunkIO.load(world, chunkPos, trueInput, IOContext.SAVE);
|
||||||
readGenerationHint(chunk, trueInput, server);
|
readGenerationHint(chunk, trueInput, server);
|
||||||
trueInput.close();
|
|
||||||
return chunk;
|
return chunk;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
private static void readGenerationHint(ChunkData chunk, DataInputStream input, Server server)
|
private static void readGenerationHint(ChunkData chunk, DataInputStream input, Server server)
|
||||||
throws IOException,
|
throws IOException,
|
||||||
|
Reference in New Issue
Block a user