overhauling save and load routines
This commit is contained in:
@@ -14,11 +14,13 @@ import de.srsoftware.web4rail.tags.Checkbox;
|
||||
|
||||
public abstract class Block extends StretchableTile{
|
||||
private static final String NAME = "name";
|
||||
private static final String ALLOW_TURN = "allowTurn";
|
||||
public String name = "Block";
|
||||
private Train train;
|
||||
|
||||
private static final String ALLOW_TURN = "allowTurn";
|
||||
public boolean turnAllowed = false;
|
||||
|
||||
private static final String TRAIN = "train";
|
||||
|
||||
@Override
|
||||
public JSONObject config() {
|
||||
JSONObject config = super.config();
|
||||
@@ -26,17 +28,32 @@ public abstract class Block extends StretchableTile{
|
||||
return config;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void configure(JSONObject config) {
|
||||
super.configure(config);
|
||||
if (config.has(NAME)) name = config.getString(NAME);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean free() {
|
||||
return train == null && super.free();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public JSONObject json() {
|
||||
JSONObject json = super.json();
|
||||
json.put(NAME, name);
|
||||
json.put(ALLOW_TURN, turnAllowed);
|
||||
if (train != null) json.put(TRAIN, train.id);
|
||||
return json;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Tile load(JSONObject json) throws IOException {
|
||||
super.load(json);
|
||||
name = json.has(NAME) ? json.getString(NAME) : "Block";
|
||||
turnAllowed = json.has(ALLOW_TURN) && json.getBoolean(ALLOW_TURN);
|
||||
if (json.has(TRAIN)) {
|
||||
Train tr = Train.get(json.getLong(TRAIN));
|
||||
train(tr);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Tag propForm() {
|
||||
Tag form = super.propForm();
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package de.srsoftware.web4rail.tiles;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
@@ -19,9 +20,17 @@ public abstract class StretchableTile extends Tile {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void configure(JSONObject config) {
|
||||
super.configure(config);
|
||||
if (config.has(LENGTH)) setLength(config.getInt(LENGTH));
|
||||
public JSONObject json() {
|
||||
JSONObject json = super.json();
|
||||
if (length > 1) json.put(LENGTH, length);
|
||||
return json;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Tile load(JSONObject json) throws IOException {
|
||||
super.load(json);
|
||||
if (json.has(LENGTH)) length = json.getInt(LENGTH);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -1,9 +1,12 @@
|
||||
package de.srsoftware.web4rail.tiles;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.File;
|
||||
import java.io.FileReader;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
@@ -31,20 +34,20 @@ import de.srsoftware.web4rail.tags.Radio;
|
||||
|
||||
public abstract class Tile {
|
||||
|
||||
private static final String ID = "id";
|
||||
public static final String ID = "id";
|
||||
private static final String TYPE = "type";
|
||||
private static final String LOCKED = "locked";
|
||||
|
||||
private static final String POS = "pos";
|
||||
private static final String X = "x";
|
||||
private static final Object Y = "y";
|
||||
private static final String Y = "y";
|
||||
public int x = -1,y = -1;
|
||||
|
||||
private static final String ROUTE = "route";
|
||||
protected Route route;
|
||||
|
||||
private static final String OCCUPIED = "occupied";
|
||||
private Train train;
|
||||
protected Train train;
|
||||
|
||||
private static final String ONEW_WAY = "one_way";
|
||||
protected Direction oneWay = null;
|
||||
@@ -63,17 +66,6 @@ public abstract class Tile {
|
||||
if (train != null) classes.add(OCCUPIED);
|
||||
return classes;
|
||||
}
|
||||
|
||||
public JSONObject json() {
|
||||
JSONObject json = new JSONObject();
|
||||
json.put(ID, id());
|
||||
json.put(TYPE, getClass().getSimpleName());
|
||||
JSONObject pos = new JSONObject(Map.of(X,x,Y,y));
|
||||
json.put(POS, pos);
|
||||
if (route != null) json.put(ROUTE, route.id());
|
||||
if (oneWay != null) json.put(ONEW_WAY, oneWay);
|
||||
return json;
|
||||
}
|
||||
|
||||
public void add(Route route) {
|
||||
this.routes.add(route);
|
||||
@@ -95,9 +87,6 @@ public abstract class Tile {
|
||||
return new HashMap<>();
|
||||
}
|
||||
|
||||
|
||||
public void configure(JSONObject config) {}
|
||||
|
||||
public boolean free() {
|
||||
return route == null;
|
||||
}
|
||||
@@ -107,20 +96,64 @@ public abstract class Tile {
|
||||
}
|
||||
|
||||
public String id() {
|
||||
return "tile-"+x+"-"+y;
|
||||
return x+":"+y;
|
||||
}
|
||||
|
||||
private static void inflate(String clazz, JSONObject json, Plan plan) throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException, ClassNotFoundException, IOException {
|
||||
clazz = Tile.class.getName().replace(".Tile", "."+clazz);
|
||||
Tile tile = (Tile) Tile.class.getClassLoader().loadClass(clazz).getDeclaredConstructor().newInstance();
|
||||
tile.plan(plan);
|
||||
tile.load(json);
|
||||
plan.set(tile.x, tile.y, tile);
|
||||
}
|
||||
|
||||
public JSONObject json() {
|
||||
JSONObject json = new JSONObject();
|
||||
json.put(TYPE, getClass().getSimpleName());
|
||||
JSONObject pos = new JSONObject(Map.of(X,x,Y,y));
|
||||
json.put(POS, pos);
|
||||
if (route != null) json.put(ROUTE, route.id());
|
||||
if (oneWay != null) json.put(ONEW_WAY, oneWay);
|
||||
return json;
|
||||
}
|
||||
|
||||
public int len() {
|
||||
return 1;
|
||||
}
|
||||
|
||||
public static void loadAll(String filename, Plan plan) throws IOException {
|
||||
BufferedReader file = new BufferedReader(new FileReader(filename));
|
||||
String line = file.readLine();
|
||||
while (line != null) {
|
||||
JSONObject json = new JSONObject(line);
|
||||
String clazz = json.getString(TYPE);
|
||||
|
||||
try {
|
||||
Tile.inflate(clazz,json,plan);
|
||||
} catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException | ClassNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
line = file.readLine();
|
||||
}
|
||||
file.close();
|
||||
}
|
||||
|
||||
protected Tile load(JSONObject json) throws IOException {
|
||||
JSONObject pos = json.getJSONObject(POS);
|
||||
x = pos.getInt(X);
|
||||
y = pos.getInt(Y);
|
||||
return this;
|
||||
}
|
||||
|
||||
public void lock(Route route) throws IOException {
|
||||
this.route = route;
|
||||
plan.place(this);
|
||||
}
|
||||
|
||||
public void plan(Plan plan) {
|
||||
public Tile plan(Plan plan) {
|
||||
this.plan = plan;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Tile position(int x, int y) {
|
||||
@@ -309,4 +342,19 @@ public abstract class Tile {
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
/*
|
||||
if (clazz == null) throw new NullPointerException(TILE+" must not be null!");
|
||||
Class<Tile> tc = Tile.class;
|
||||
clazz = tc.getName().replace(".Tile", "."+clazz);
|
||||
Tile tile = (Tile) tc.getClassLoader().loadClass(clazz).getDeclaredConstructor().newInstance();
|
||||
if (tile instanceof Eraser) {
|
||||
Tile erased = get(x,y,true);
|
||||
remove(erased);
|
||||
return erased == null ? null : t("Removed {}.",erased);
|
||||
}
|
||||
if (configJson != null) tile.configure(new JSONObject(configJson));
|
||||
set(x, y, tile);
|
||||
return t("Added {}",tile.getClass().getSimpleName());
|
||||
}*/
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user