move converter to server

This commit is contained in:
Sen 2025-05-12 18:52:03 +02:00
parent f89724e2aa
commit 95b5d0e3e3
7 changed files with 183 additions and 707 deletions

View file

@ -389,4 +389,12 @@ int utf_len(const char *str) {
public static double ftime() {
return ((double)rtime()) / 1000000.0;
}
public static String getRegionFolder(int x, int z) {
return String.format("%c%03X%c%03X", x < 0 ? 'n' : 'p', ((x < 0) ? -x : x) >> 9, z < 0 ? 'n' : 'p', ((z < 0) ? -z : z) >> 9);
}
public static String getRegionName(int x, int z) {
return String.format("r.%c%X%c%X.rgn", x < 0 ? 'n' : 'p', ((x < 0) ? -x : x) >> 3, z < 0 ? 'n' : 'p', ((z < 0) ? -z : z) >> 3);
}
}

View file

@ -22,9 +22,7 @@ import common.collect.Lists;
import common.collect.Maps;
import common.entity.Entity;
import common.init.BlockRegistry;
import common.init.Config;
import common.init.EntityRegistry;
import common.init.UniverseRegistry;
import common.log.Log;
import common.nbt.NBTLoader;
import common.nbt.NBTTagCompound;
@ -33,39 +31,9 @@ import common.tileentity.TileEntity;
import common.util.BlockPos;
import common.util.NextTickListEntry;
import common.util.NibbleArray;
import common.util.Util;
public class Region {
public static enum SaveVersion {
ALPHA_1_0("Alpha 1.0 - Beta 1.2"),
BETA_1_3("Beta 1.3 - Release 1.8.9"),
RELEASE_1_9("Release 1.9 - Release 1.12.2"),
RELEASE_1_13("Release 1.13 +");
private final String name;
private SaveVersion(String name) {
this.name = name;
}
public String toString() {
return this.name;
}
}
public static class FolderInfo {
public final long time;
public final long lastPlayed;
public final SaveVersion legacy;
public final String version;
public FolderInfo(long time, long lastPlayed, SaveVersion legacy, String version) {
this.time = time;
this.lastPlayed = lastPlayed;
this.legacy = legacy;
this.version = version;
}
}
private static class ChunkBuffer extends ByteArrayOutputStream {
public ChunkBuffer() {
super(8096);
@ -90,10 +58,7 @@ public class Region {
private static final Map<String, Region> CACHE = Maps.<String, Region>newHashMap();
private static final List<WorldServer> QUEUE = Collections.<WorldServer>synchronizedList(Lists.<WorldServer>newArrayList());
// public static long lastPlayed;
// public static int version;
// public static String owner;
private static volatile long queued;
private static volatile long saved;
private static volatile boolean waiting;
@ -113,10 +78,10 @@ public class Region {
private boolean modified;
public Region(File dir, int x, int z) {
File sdir = new File(dir, getRegionFolder(x << 3, z << 3));
File sdir = new File(dir, Util.getRegionFolder(x << 3, z << 3));
if(!sdir.exists())
sdir.mkdirs();
this.regFile = new File(sdir, getRegionName(x << 3, z << 3));
this.regFile = new File(sdir, Util.getRegionName(x << 3, z << 3));
this.folder = dir;
this.xPos = x;
this.zPos = z;
@ -313,13 +278,6 @@ public class Region {
this.write(x, z, buf.getData(), buf.size());
}
// public NBTTagCompound readTag(int x, int z) throws IOException {
// byte[] data = this.read(x, z);
// if(data == null)
// return null;
// return CompressedStreamTools.read(new DataInputStream(new BufferedInputStream(new InflaterInputStream(new ByteArrayInputStream(data)))));
// }
public File getFile() {
return this.regFile;
}
@ -336,7 +294,7 @@ public class Region {
}
private static File getExpansionFile(File dir, int x, int z) {
File sdir = new File(dir, getRegionFolder(x, z));
File sdir = new File(dir, Util.getRegionFolder(x, z));
if(!sdir.exists())
sdir.mkdirs();
return new File(sdir, String.format("c.%c%X%c%X.chk", x < 0 ? 'n' : 'p', (x < 0) ? -x : x, z < 0 ? 'n' : 'p', (z < 0) ? -z : z));
@ -399,14 +357,6 @@ public class Region {
// getRegionFile(dir, x >> 3, z >> 3).writeTag(x & 7, z & 7, tag);
}
public static String getRegionFolder(int x, int z) {
return String.format("%c%03X%c%03X", x < 0 ? 'n' : 'p', ((x < 0) ? -x : x) >> 9, z < 0 ? 'n' : 'p', ((z < 0) ? -z : z) >> 9);
}
public static String getRegionName(int x, int z) {
return String.format("r.%c%X%c%X.rgn", x < 0 ? 'n' : 'p', ((x < 0) ? -x : x) >> 3, z < 0 ? 'n' : 'p', ((z < 0) ? -z : z) >> 3);
}
public static Chunk readNbt(WorldServer world, int x, int z, NBTTagCompound tag) {
// if(!tag.hasKey("Level", 10)) {
// Log.error("Chunk-Datei bei " + x + "," + z + " hat keine Level-Daten, überspringe");
@ -680,93 +630,4 @@ public class Region {
public static void killIO() {
killed = true;
}
public static void saveWorldInfo(File worldDir, long time) {
NBTTagCompound data = new NBTTagCompound();
data.setLong("Time", time);
data.setLong("LastAccess", System.currentTimeMillis());
data.setString("Version", Config.VERSION);
NBTTagCompound cfg = new NBTTagCompound();
for(String cvar : Config.VARS.keySet()) {
cfg.setString(cvar, Config.VARS.get(cvar).getValue());
// Config.Value value = Config.VARS.get(cvar);
// switch(value.getType()) {
// case BOOLEAN:
// cfg.setString(cvar, "" + value.getBoolean());
// break;
// case INTEGER:
// cfg.setString(cvar, "" + value.getInt());
// break;
// case FLOAT:
// cfg.setString(cvar, "" + value.getFloat());
// break;
// case STRING:
// cfg.setString(cvar, value.getString());
// break;
// }
}
data.setTag("Config", cfg);
data.setTag("Universe", UniverseRegistry.saveNbt());
if(worldDir != null)
worldDir.mkdirs();
File nfile = new File(worldDir, "level.nbt.tmp");
File lfile = new File(worldDir, "level.nbt");
try {
// File ofile = new File(worldDir, "level.nbt_old");
NBTLoader.writeGZip(data, nfile);
// if(ofile.exists())
// ofile.delete();
// lfile.renameTo(ofile);
if(lfile.exists())
lfile.delete();
nfile.renameTo(lfile);
// if(nfile.exists())
// nfile.delete();
}
catch(Exception e) {
Log.JNI.error(e, "Fehler beim Schreiben von " + nfile);
}
}
public static FolderInfo loadWorldInfo(File worldDir) {
Config.clear();
UniverseRegistry.clear();
File file = new File(worldDir, "level.nbt");
if(!file.exists())
file = new File(worldDir, "level.nbt.tmp");
if(file.exists()) {
try {
NBTTagCompound tag = NBTLoader.readGZip(file);
NBTTagCompound cfg = tag.getCompoundTag("Config");
for(String key : cfg.getKeySet()) {
Config.set(key, cfg.getString(key), null);
}
UniverseRegistry.loadNbt(tag.getCompoundTag("Universe"));
// tag.getInteger("Version");
long lastPlayed = tag.getLong("LastAccess");
String version = tag.hasKey("Version", 8) ? tag.getString("Version") : null;
version = version != null && version.isEmpty() ? null : version;
long time = tag.hasKey("Time", 4) ? tag.getLong("Time") : World.START_TIME;
return new FolderInfo(time, lastPlayed, null, version);
}
catch(Exception e) {
Log.JNI.error(e, "Fehler beim Lesen von " + file);
}
}
return null;
}
// public static void reloadWorldInfo(File worldDir) {
// File file = new File(worldDir, "level.nbt");
// if(file.exists()) {
// Config.clear();
// try {
// Config.readFromNbt(NBTLoader.readGZip(file).getCompoundTag("Config"), true);
// }
// catch(Exception e) {
// Log.error("Fehler beim Lesen von " + file, e);
// return;
// }
// }
// }
}