change svar implementation
This commit is contained in:
parent
367e6f5bfd
commit
126ce64a24
7 changed files with 171 additions and 214 deletions
|
@ -5,6 +5,8 @@ import java.io.BufferedReader;
|
|||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.net.InetAddress;
|
||||
import java.security.KeyPair;
|
||||
import java.security.PrivateKey;
|
||||
|
@ -87,6 +89,7 @@ import common.util.LazyLoader;
|
|||
import common.util.PortalType;
|
||||
import common.util.Position;
|
||||
import common.util.Util;
|
||||
import common.util.Var;
|
||||
import common.util.WorldPos;
|
||||
import common.world.World;
|
||||
import server.biome.GenBiome;
|
||||
|
@ -97,6 +100,7 @@ import server.command.FixedExecutor;
|
|||
import server.network.HandshakeHandler;
|
||||
import server.network.Player;
|
||||
import server.network.User;
|
||||
import server.util.SVar;
|
||||
import server.world.Converter;
|
||||
import server.world.Region;
|
||||
import server.world.WorldServer;
|
||||
|
@ -109,6 +113,7 @@ public final class Server implements IThreadListener {
|
|||
};
|
||||
|
||||
private final Thread serverThread = Thread.currentThread();
|
||||
private final Map<String, SVar> variables = Maps.newTreeMap();
|
||||
private final List<NetConnection> clients = Collections.<NetConnection>synchronizedList(Lists.<NetConnection>newArrayList());
|
||||
private final List<Player> players = Lists.<Player>newArrayList();
|
||||
private final Map<String, Player> online = Maps.<String, Player>newHashMap();
|
||||
|
@ -161,6 +166,7 @@ public final class Server implements IThreadListener {
|
|||
Registry.addShutdownHook(new Runnable() {
|
||||
public void run() {
|
||||
server.stopServer();
|
||||
Log.flushLog();
|
||||
}
|
||||
});
|
||||
Log.setSync(server);
|
||||
|
@ -169,22 +175,22 @@ public final class Server implements IThreadListener {
|
|||
Log.flushLog();
|
||||
}
|
||||
|
||||
public static void saveServerConfig(long time, Server server) {
|
||||
private void saveServerConfig(long time) {
|
||||
TagObject data = new TagObject();
|
||||
data.setLong("Time", time);
|
||||
data.setLong("LastAccess", System.currentTimeMillis());
|
||||
data.setString("Version", Util.VERSION);
|
||||
TagObject cfg = new TagObject();
|
||||
for(String cvar : Config.VARS.keySet()) {
|
||||
Config.Value value = Config.VARS.get(cvar);
|
||||
if(!value.noDef || !value.def.equals(value.getValue()))
|
||||
cfg.setString(cvar, value.getValue());
|
||||
for(String cvar : this.variables.keySet()) {
|
||||
SVar value = this.variables.get(cvar);
|
||||
if(!value.noDef || !value.def.equals(value.get()))
|
||||
cfg.setString(cvar, value.get());
|
||||
}
|
||||
data.setObject("Config", cfg);
|
||||
data.setObject("Universe", UniverseRegistry.toTags());
|
||||
if(server != null) {
|
||||
data.setByteArray("PrivateKey", server.getPrivateKey().getEncoded());
|
||||
data.setByteArray("PublicKey", server.getPublicKey().getEncoded());
|
||||
if(this.keyPair != null) {
|
||||
data.setByteArray("PrivateKey", this.keyPair.getPrivate().getEncoded());
|
||||
data.setByteArray("PublicKey", this.keyPair.getPublic().getEncoded());
|
||||
}
|
||||
File nfile = new File("server.cdt.tmp");
|
||||
File lfile = new File("server.cdt");
|
||||
|
@ -199,9 +205,7 @@ public final class Server implements IThreadListener {
|
|||
}
|
||||
}
|
||||
|
||||
public long loadServerConfig() {
|
||||
Config.clear();
|
||||
UniverseRegistry.clear();
|
||||
private long loadServerConfig() {
|
||||
File file = new File("server.cdt");
|
||||
if(!file.exists())
|
||||
file = new File("server.cdt.tmp");
|
||||
|
@ -210,7 +214,9 @@ public final class Server implements IThreadListener {
|
|||
TagObject tag = TagObject.readGZip(file);
|
||||
TagObject cfg = tag.getObject("Config");
|
||||
for(String key : cfg.keySet()) {
|
||||
Config.set(key, cfg.getString(key), false);
|
||||
SVar svar = this.variables.get(key);
|
||||
if(svar != null)
|
||||
svar.set(cfg.getString(key), false, false);
|
||||
}
|
||||
UniverseRegistry.fromTags(tag.getObject("Universe"));
|
||||
long lastPlayed = tag.getLong("LastAccess");
|
||||
|
@ -230,16 +236,40 @@ public final class Server implements IThreadListener {
|
|||
}
|
||||
catch(Exception e) {
|
||||
Log.IO.error(e, "Fehler beim Lesen von " + file);
|
||||
Config.clear();
|
||||
for(SVar svar : this.variables.values()) {
|
||||
svar.set(svar.def, svar.noDef, false);
|
||||
}
|
||||
UniverseRegistry.clear();
|
||||
}
|
||||
}
|
||||
Log.IO.info("Erstelle neue Welt und Konfiguration");
|
||||
return World.START_TIME;
|
||||
}
|
||||
|
||||
private void setCallback(Runnable callback, String ... vars) {
|
||||
for(String key : vars) {
|
||||
this.variables.get(key).setCallback(callback);
|
||||
}
|
||||
}
|
||||
|
||||
public Map<String, SVar> getVariables() {
|
||||
return this.variables;
|
||||
}
|
||||
|
||||
private Server() {
|
||||
Config.setCallback(new Runnable() {
|
||||
for(Field field : Config.class.getDeclaredFields()) {
|
||||
if(field.isAnnotationPresent(Var.class)) {
|
||||
if(!Modifier.isStatic(field.getModifiers()) || Modifier.isFinal(field.getModifiers()))
|
||||
throw new IllegalArgumentException("Feld für Variable " + field + " muss statisch und änderbar sein!");
|
||||
Var value = field.getAnnotation(Var.class);
|
||||
if(value.name().isEmpty())
|
||||
throw new IllegalArgumentException("Variablenname von " + field + " kann nicht leer sein!");
|
||||
if(this.variables.containsKey(value.name()))
|
||||
throw new IllegalArgumentException("Variable " + value.name() + " existiert bereits!");
|
||||
this.variables.put(value.name(), new SVar(field, value));
|
||||
}
|
||||
}
|
||||
this.setCallback(new Runnable() {
|
||||
public void run() {
|
||||
for(WorldServer world : Server.this.getWorlds()) {
|
||||
world.updatePhysics();
|
||||
|
@ -247,24 +277,22 @@ public final class Server implements IThreadListener {
|
|||
Server.this.sendPacket(new SPacketWorld(WorldServer.clampGravity(), Config.dayCycle, Config.timeFlow));
|
||||
}
|
||||
}, "daylightCycle", "timeFlow", "gravity");
|
||||
Config.setCallback(new Runnable() {
|
||||
this.setCallback(new Runnable() {
|
||||
public void run() {
|
||||
for(WorldServer world : Server.this.getWorlds()) {
|
||||
world.updateViewRadius();
|
||||
}
|
||||
}
|
||||
}, "viewDistance");
|
||||
Config.setCallback(new Runnable() {
|
||||
this.setCallback(new Runnable() {
|
||||
public void run() {
|
||||
Server.this.bind(Config.port);
|
||||
}
|
||||
}, "port");
|
||||
Config.setCallback(new Runnable() {
|
||||
this.setCallback(new Runnable() {
|
||||
public void run() {
|
||||
if((!Config.password.isEmpty() && Config.password.length() < 8) || Config.password.length() > IPlayer.MAX_PASS_LENGTH) {
|
||||
Log.IO.error("Passwort muss aus 8-" + IPlayer.MAX_PASS_LENGTH + " Zeichen bestehen");
|
||||
Config.set("password", "", false);
|
||||
}
|
||||
if((!Config.password.isEmpty() && Config.password.length() < 8) || Config.password.length() > IPlayer.MAX_PASS_LENGTH)
|
||||
Log.IO.warn("Passwort muss aus 8-" + IPlayer.MAX_PASS_LENGTH + " Zeichen bestehen, Login wird nicht möglich sein");
|
||||
}
|
||||
}, "password");
|
||||
}
|
||||
|
@ -289,7 +317,7 @@ public final class Server implements IThreadListener {
|
|||
}
|
||||
|
||||
public void saveWorldInfo() {
|
||||
saveServerConfig(this.space.getDayTime(), this);
|
||||
this.saveServerConfig(this.space.getDayTime());
|
||||
WorldServer.saveWarps(this.warps);
|
||||
}
|
||||
|
||||
|
@ -365,7 +393,7 @@ public final class Server implements IThreadListener {
|
|||
}
|
||||
|
||||
public void run(long time) {
|
||||
Converter.convert();
|
||||
Converter.convert(this);
|
||||
long wtime = this.loadServerConfig();
|
||||
if(this.keyPair == null) {
|
||||
Log.SYSTEM.info("Generiere neues Schlüsselpaar");
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue