use of mudmap2.backend.WorldFileReader.Exception.WorldFileInvalidTypeException in project mudmap2 by Neop.
the class WorldFileJSON method readFile.
/**
* Read world file
* @return new world object or null if file is invalid
* @throws Exception
* @throws WorldFileInvalidTypeException
* @ throws WorldFileReadError
*/
@Override
public World readFile() throws Exception {
World world = null;
try {
JSONObject root = getJSONRoot();
// check file version
if (root.has("fileVer")) {
String[] fileVer = root.getString("fileVer").split("\\.");
if (versionMajor != Integer.parseInt(fileVer[0])) {
// version major not equal: different file format
throw new WorldFileInvalidTypeException(filename, "invalid world file version", null);
}
if (versionMinor < Integer.parseInt(fileVer[1])) {
// file was created by a newer MUD Map: might have unsupported features
int ret = JOptionPane.showConfirmDialog(null, "World file version is greater than the reader version. " + "Please update MUD Map. Continuing might cause data loss.", "Loading world", JOptionPane.OK_CANCEL_OPTION);
if (ret == JOptionPane.CANCEL_OPTION)
throw new WorldFileInvalidTypeException(filename, "Could not read world file", null);
}
} else {
throw new WorldFileInvalidTypeException(filename, "could not read world file version", null);
}
// getPlace world name
String worldName = "";
if (root.has("worldName"))
worldName = root.getString("worldName");
if (worldName.isEmpty()) {
Integer begin = worldName.lastIndexOf('/');
if (begin == -1)
begin = worldName.lastIndexOf('\\');
begin += 1;
worldName = worldName.substring(begin);
}
// create world root
world = new World(worldName);
world.setWorldFile(this);
// showPlaceID
if (root.has("showPlaceID")) {
world.setShowPlaceID(World.ShowPlaceID.valueOf(root.getString("showPlaceID")));
}
// tileCenterCol
if (root.has("tileCenterCol")) {
world.setTileCenterColor(hexToCol(root.getString("tileCenterCol")));
}
// pathCol
if (root.has("pathCol")) {
world.setPathColor(hexToCol(root.getString("pathCol")));
}
// pathColNonCardinal
if (root.has("pathColNonCardinal")) {
world.setPathColorNstd(hexToCol(root.getString("pathColNonCardinal")));
}
// pathColDefs
if (root.has("pathColDefs")) {
JSONArray pathColDefs = root.getJSONArray("pathColDefs");
Integer length = pathColDefs.length();
for (Integer i = 0; i < length; ++i) {
JSONObject pathColDef = pathColDefs.getJSONObject(i);
if (pathColDef.has("path") && pathColDef.has("col")) {
world.setPathColor(pathColDef.getString("path"), hexToCol(pathColDef.getString("col")));
}
}
}
// home
if (root.has("home")) {
JSONObject home = root.getJSONObject("home");
if (home.has("l") && home.has("x") && home.has("y")) {
Integer l = home.getInt("l");
Double x = home.getDouble("x");
Double y = home.getDouble("y");
world.setHome(new WorldCoordinate(l, x, y));
}
}
// riskLevels
if (root.has("riskLevels")) {
// remove existing risk levels
world.getRiskLevels().clear();
JSONArray riskLevels = root.getJSONArray("riskLevels");
Integer length = riskLevels.length();
for (Integer i = 0; i < length; ++i) {
JSONObject riskLevel = riskLevels.getJSONObject(i);
if (riskLevel.has("id") && riskLevel.has("desc") && riskLevel.has("col")) {
Integer id = riskLevel.getInt("id");
String desc = riskLevel.getString("desc");
Color col = hexToCol(riskLevel.getString("col"));
world.setRiskLevel(new RiskLevel(id, desc, col));
}
}
}
// areaArray
HashMap<Integer, PlaceGroup> areas = new HashMap<>();
if (root.has("areas")) {
JSONArray areaArray = root.getJSONArray("areas");
Integer length = areaArray.length();
for (Integer i = 0; i < length; ++i) {
JSONObject area = areaArray.getJSONObject(i);
if (area.has("id") && area.has("name") && area.has("col")) {
Integer id = area.getInt("id");
String name = area.getString("name");
Color col = hexToCol(area.getString("col"));
PlaceGroup a = new PlaceGroup(name, col);
areas.put(id, a);
world.addPlaceGroup(a);
}
}
}
// layers
if (root.has("layers")) {
JSONArray layers = root.getJSONArray("layers");
Integer length = layers.length();
for (Integer i = 0; i < length; ++i) {
JSONObject layer = layers.getJSONObject(i);
if (layer.has("id")) {
Integer id = layer.getInt("id");
// create layer
Layer l = new Layer(id, world);
if (layer.has("centerX") && layer.has("centerY")) {
// set quadtree center
Integer centerX = layer.getInt("centerX");
Integer centerY = layer.getInt("centerY");
l.setQuadtree(centerX, centerY);
}
if (layer.has("name")) {
// set layer name
l.setName(layer.getString("name"));
}
world.addLayer(l);
}
}
}
// places
HashMap<Integer, Place> places = new HashMap<>();
HashMap<Place, HashSet<Integer>> childrenMapping = new HashMap<>();
if (root.has("places")) {
JSONArray jPlaces = root.getJSONArray("places");
Integer length = jPlaces.length();
for (Integer i = 0; i < length; ++i) {
JSONObject jPlace = jPlaces.getJSONObject(i);
if (jPlace.has("id") && jPlace.has("n") && jPlace.has("l") && jPlace.has("x") && jPlace.has("y")) {
Integer id = jPlace.getInt("id");
String name = jPlace.getString("n");
Integer layerId = jPlace.getInt("l");
Integer x = jPlace.getInt("x");
Integer y = jPlace.getInt("y");
// get layer
Layer layer = world.getLayer(layerId);
if (layer == null) {
layer = new Layer(layerId, world);
world.addLayer(layer);
}
// create place
Place place = new Place(id, name, x, y, layer);
places.put(id, place);
// area
if (jPlace.has("a")) {
Integer area = jPlace.getInt("a");
place.setPlaceGroup(areas.get(area));
}
// risk level
if (jPlace.has("r")) {
Integer risk = jPlace.getInt("r");
place.setRiskLevel(world.getRiskLevel(risk));
}
// rec level
if (jPlace.has("lvlMin")) {
Integer lvlMin = jPlace.getInt("lvlMin");
place.setRecLevelMin(lvlMin);
}
if (jPlace.has("lvlMax")) {
Integer lvlMax = jPlace.getInt("lvlMax");
place.setRecLevelMin(lvlMax);
}
// children
if (jPlace.has("c")) {
JSONArray children = jPlace.getJSONArray("c");
HashSet<Integer> set = new HashSet<>();
childrenMapping.put(place, set);
Integer lc = children.length();
for (Integer c = 0; c < lc; ++c) {
set.add(children.getInt(c));
}
}
// flags
if (jPlace.has("f")) {
JSONArray flags = jPlace.getJSONArray("f");
Integer lf = flags.length();
for (Integer f = 0; f < lf; ++f) {
String flagname = flags.getString(f);
place.setFlag(flagname, true);
}
}
// comments
if (jPlace.has("co")) {
JSONArray comments = jPlace.getJSONArray("co");
Integer lc = comments.length();
for (Integer c = 0; c < lc; ++c) {
place.addComment(comments.getString(c));
}
}
layer.put(place);
}
}
}
// connect children
for (Entry<Place, HashSet<Integer>> entry : childrenMapping.entrySet()) {
Place place = entry.getKey();
for (Integer id : entry.getValue()) {
place.connectChild(places.get(id));
}
}
// paths
if (root.has("paths")) {
JSONArray paths = root.getJSONArray("paths");
Integer length = paths.length();
for (Integer i = 0; i < length; ++i) {
JSONArray path = paths.getJSONArray(i);
if (path.length() == 2) {
JSONObject p0 = path.getJSONObject(0);
JSONObject p1 = path.getJSONObject(1);
if (p0.has("p") && p0.has("e") && p1.has("p") && p1.has("e")) {
Place pl0 = places.get(p0.getInt("p"));
Place pl1 = places.get(p1.getInt("p"));
if (pl0 != null && pl1 != null) {
Path p = new Path(pl0, p0.getString("e"), pl1, p1.getString("e"));
pl0.connectPath(p);
}
}
}
}
}
// remember meta data for WorldTab
if (root.has("meta"))
metaData = root.getJSONObject("meta");
} catch (JSONException ex) {
System.out.println(ex.getLocalizedMessage());
throw new WorldFileReadError(filename, ex.getLocalizedMessage(), ex);
}
return world;
}
Aggregations