Search in sources :

Example 56 with JsonValue

use of com.badlogic.gdx.utils.JsonValue in project bladecoder-adventure-engine by bladecoder.

the class ActionUtils method readJson.

public static Action readJson(Json json, JsonValue jsonData) {
    String className = jsonData.getString("class", null);
    Action action = null;
    if (className != null) {
        jsonData.remove("class");
        try {
            action = ActionFactory.createByClass(className, null);
        } catch (ClassNotFoundException | ReflectionException e1) {
            throw new SerializationException(e1);
        }
        for (int j = 0; j < jsonData.size; j++) {
            JsonValue v = jsonData.get(j);
            try {
                if (v.isNull())
                    ActionUtils.setParam(action, v.name, null);
                else
                    ActionUtils.setParam(action, v.name, v.asString());
            } catch (NoSuchFieldException e) {
                EngineLogger.error("Action field not found - class: " + className + " field: " + v.name);
            } catch (IllegalArgumentException | IllegalAccessException e) {
                EngineLogger.error("Action field error - class: " + className + " field: " + v.name + " value: " + (v == null ? "null" : v.asString()));
            }
        }
    }
    return action;
}
Also used : ReflectionException(com.badlogic.gdx.utils.reflect.ReflectionException) Action(com.bladecoder.engine.actions.Action) SerializationException(com.badlogic.gdx.utils.SerializationException) JsonValue(com.badlogic.gdx.utils.JsonValue)

Example 57 with JsonValue

use of com.badlogic.gdx.utils.JsonValue in project bladecoder-adventure-engine by bladecoder.

the class World method loadWorldDesc.

/**
 * Load the world description in 'world.json'.
 *
 * @throws IOException
 */
public void loadWorldDesc() throws IOException {
    String worldFilename = EngineAssetManager.WORLD_FILENAME;
    if (!EngineAssetManager.getInstance().getModelFile(worldFilename).exists()) {
        // Search the world file with ".json" ext if not found.
        worldFilename = EngineAssetManager.WORLD_FILENAME + ".json";
        if (!EngineAssetManager.getInstance().getModelFile(worldFilename).exists()) {
            EngineLogger.error("ERROR LOADING WORLD: world file not found.");
            dispose();
            throw new IOException("ERROR LOADING WORLD: world file not found.");
        }
    }
    SerializationHelper.getInstance().setMode(Mode.MODEL);
    JsonValue root = new JsonReader().parse(EngineAssetManager.getInstance().getModelFile(worldFilename).reader("UTF-8"));
    Json json = new Json();
    json.setIgnoreUnknownFields(true);
    int width = json.readValue("width", Integer.class, root);
    int height = json.readValue("height", Integer.class, root);
    // We know the world width, so we can set the scale
    EngineAssetManager.getInstance().setScale(width, height);
    float scale = EngineAssetManager.getInstance().getScale();
    setWidth((int) (width * scale));
    setHeight((int) (height * scale));
    setInitChapter(json.readValue("initChapter", String.class, root));
    verbs.read(json, root);
    I18N.loadWorld(EngineAssetManager.MODEL_DIR + EngineAssetManager.WORLD_FILENAME);
}
Also used : JsonValue(com.badlogic.gdx.utils.JsonValue) JsonReader(com.badlogic.gdx.utils.JsonReader) IOException(java.io.IOException) Json(com.badlogic.gdx.utils.Json)

Example 58 with JsonValue

use of com.badlogic.gdx.utils.JsonValue in project bladecoder-adventure-engine by bladecoder.

the class ModelTools method extractInkTextsInternal.

private static void extractInkTextsInternal(JsonValue v, StringBuilder sbTSV, StringBuilder sbMD, OrderedProperties prop) {
    if (v.isArray() || v.isObject()) {
        if (v.name != null && v.isArray() && v.parent != null && v.parent.parent != null && v.parent.parent.parent != null) {
            if (v.name.contains("-"))
                sbMD.append('\n');
            else if (v.parent.parent.parent.parent == null)
                sbMD.append("\n==== " + v.name + " ====\n");
            else if (v.name.equals("s"))
                sbMD.append("  * ");
        // else
        // sbMD.append("\n-- " + v.name + " --\n");
        }
        for (int i = 0; i < v.size; i++) {
            JsonValue aValue = v.get(i);
            // Ignore string declr ej. "xxx"
            if (i > 0 && v.get(i - 1).isString() && v.get(i - 1).asString().equals("str")) {
                // comparison == or !=?
                if (v.size > i + 2 && v.get(i + 2).isString() && (v.get(i + 2).asString().equals("==") || v.get(i + 2).asString().equals("!=")))
                    continue;
                // find "/ev"
                boolean ev = false;
                int j = i + 2;
                while (j < v.size && !ev) {
                    JsonValue next = v.get(j);
                    if (!next.isObject() && next.asString().equals("/ev")) {
                        ev = true;
                    }
                    j++;
                }
                JsonValue next = v.get(j);
                if (!next.isObject() || next.get("*") == null)
                    continue;
            }
            // Ignore listInt checks
            if (v.size > i + 2 && v.get(i + 2).isString() && v.get(i + 2).asString().equals("listInt")) {
                continue;
            }
            extractInkTextsInternal(aValue, sbTSV, sbMD, prop);
        }
    } else if (v.isString() && !v.asString().isEmpty() && v.asString().charAt(0) == '^') {
        String value = v.asString().substring(1).trim();
        if (value.length() == 0 || value.charAt(0) == '>')
            return;
        // if we are inside an expression, exit
        int idx = value.indexOf('>');
        String charName = "";
        if (idx != -1) {
            charName = value.substring(0, idx).trim();
            value = value.substring(idx + 1).trim();
            if (value.length() == 0)
                return;
        }
        String key = null;
        try {
            MessageDigest md = MessageDigest.getInstance("SHA-1");
            byte[] bytes = value.getBytes(("UTF-8"));
            md.update(bytes);
            byte[] digest = md.digest();
            key = Base64Coder.encodeLines(digest).substring(0, InkManager.KEY_SIZE);
        } catch (NoSuchAlgorithmException | UnsupportedEncodingException e) {
            EditorLogger.error("Error encoding key." + e);
            return;
        }
        prop.setProperty(key, value);
        sbTSV.append(key + "\t" + charName + "\t" + value + "\n");
        sbMD.append(charName + (charName.isEmpty() ? "" : ": ") + value + " (" + key + ")\n");
        if (charName.isEmpty())
            v.set("^" + I18N.PREFIX + key);
        else
            v.set("^" + charName + '>' + I18N.PREFIX + key);
    }
}
Also used : JsonValue(com.badlogic.gdx.utils.JsonValue) MessageDigest(java.security.MessageDigest)

Example 59 with JsonValue

use of com.badlogic.gdx.utils.JsonValue in project bladecoder-adventure-engine by bladecoder.

the class WorldSerialization method loadGameState.

public void loadGameState(FileHandle savedFile) throws IOException {
    EngineLogger.debug("LOADING GAME STATE");
    if (savedFile.exists()) {
        JsonValue root = new JsonReader().parse(savedFile.reader("UTF-8"));
        Json json = new BladeJson(w, Mode.STATE);
        json.setIgnoreUnknownFields(true);
        read(json, root);
    } else {
        throw new IOException("LOADGAMESTATE: no saved game exists");
    }
}
Also used : JsonValue(com.badlogic.gdx.utils.JsonValue) JsonReader(com.badlogic.gdx.utils.JsonReader) Json(com.badlogic.gdx.utils.Json) IOException(java.io.IOException)

Example 60 with JsonValue

use of com.badlogic.gdx.utils.JsonValue in project bladecoder-adventure-engine by bladecoder.

the class WorldSerialization method read.

@Override
public void read(Json json, JsonValue jsonData) {
    String bladeVersion = json.readValue(Config.BLADE_ENGINE_VERSION_PROP, String.class, jsonData);
    BladeJson bjson = (BladeJson) json;
    if (bjson.getMode() == Mode.MODEL) {
        if (bladeVersion != null && !bladeVersion.equals(Config.getInstance().getProperty(Config.BLADE_ENGINE_VERSION_PROP, ""))) {
            EngineLogger.debug("Model Engine Version v" + bladeVersion + " differs from Current Engine Version v" + Config.getInstance().getProperty(Config.BLADE_ENGINE_VERSION_PROP, ""));
        }
        // SOUNDS
        JsonValue jsonSounds = jsonData.get("sounds");
        HashMap<String, SoundDesc> sounds = w.getSounds();
        if (jsonSounds != null) {
            for (int i = 0; i < jsonSounds.size; i++) {
                JsonValue jsonValue = jsonSounds.get(i);
                SoundDesc s = json.readValue(SoundDesc.class, jsonValue);
                sounds.put(jsonValue.name, s);
            }
        }
        // SCENES
        JsonValue jsonScenes = jsonData.get("scenes");
        Map<String, Scene> scenes = w.getScenes();
        for (int i = 0; i < jsonScenes.size; i++) {
            JsonValue jsonValue = jsonScenes.get(i);
            Scene s = new Scene(w);
            scenes.put(jsonValue.name, s);
            s.read(json, jsonValue);
        }
        w.setInitScene(json.readValue("initScene", String.class, jsonData));
        if (w.getInitScene() == null && w.getScenes().size() > 0) {
            w.setInitScene(w.getScenes().keySet().toArray(new String[0])[0]);
        }
        for (Scene s : w.getScenes().values()) {
            s.resetCamera(w.getWidth(), w.getHeight());
        }
        // Load Ink story
        if (jsonData.get("inkManager") != null) {
            w.getInkManager().read(json, jsonData.get("inkManager"));
        }
        // Add sounds to cache
        cacheSounds();
    } else {
        if (bladeVersion != null && !bladeVersion.equals(Config.getInstance().getProperty(Config.BLADE_ENGINE_VERSION_PROP, ""))) {
            EngineLogger.debug("Saved Game Engine Version v" + bladeVersion + " differs from Current Engine Version v" + Config.getInstance().getProperty(Config.BLADE_ENGINE_VERSION_PROP, ""));
        }
        String currentChapter = json.readValue("chapter", String.class, jsonData);
        String currentScene = json.readValue("currentScene", String.class, jsonData);
        try {
            loadChapter(currentChapter, currentScene, false);
        } catch (IOException e1) {
            EngineLogger.error("Error Loading Chapter");
            return;
        }
        // scenes and verbs tweens
        if (jsonData.get("inkManager") != null) {
            w.getInkManager().read(json, jsonData.get("inkManager"));
        }
        // inventories have to be put in the hash to find the actors when
        // reading saved data
        w.setCurrentInventory(json.readValue("currentInventory", String.class, jsonData));
        JsonValue jsonInventories = jsonData.get("inventories");
        for (int i = 0; i < jsonInventories.size; i++) {
            JsonValue jsonValue = jsonInventories.get(i);
            Inventory inv = new Inventory();
            w.getInventories().put(jsonValue.name, inv);
            inv.read(json, jsonValue);
        }
        if (jsonData.get("uiActors") != null) {
            w.getUIActors().read(json, jsonData.get("uiActors"));
        }
        for (Scene s : w.getScenes().values()) {
            JsonValue jsonValue = jsonData.get("scenes").get(s.getId());
            if (jsonValue != null)
                s.read(json, jsonValue);
            else
                EngineLogger.debug("LOAD WARNING: Scene not found in saved game: " + s.getId());
        }
        w.setTimeOfGame(json.readValue("timeOfGame", long.class, 0L, jsonData));
        w.setCutMode(json.readValue("cutmode", boolean.class, false, jsonData));
        w.getVerbManager().read(json, jsonData);
        // CUSTOM PROPERTIES
        JsonValue jsonProperties = jsonData.get("customProperties");
        HashMap<String, String> props = w.getCustomProperties();
        for (int i = 0; i < jsonProperties.size; i++) {
            JsonValue jsonValue = jsonProperties.get(i);
            props.put(jsonValue.name, jsonValue.asString());
        }
        String version = json.readValue(Config.VERSION_PROP, String.class, jsonData);
        if (version == null)
            version = "TEST";
        props.put(WorldProperties.SAVED_GAME_VERSION.toString(), version);
        String actorId = json.readValue("dialogActor", String.class, jsonData);
        String dialogId = json.readValue("currentDialog", String.class, jsonData);
        if (dialogId != null) {
            CharacterActor actor = (CharacterActor) w.getCurrentScene().getActor(actorId, false);
            w.setCurrentDialog(actor.getDialog(dialogId));
        }
        w.getTransition().read(json, jsonData.get("transition"));
        w.getMusicManager().read(json, jsonData.get("musicEngine"));
        w.getI18N().loadChapter(EngineAssetManager.MODEL_DIR + w.getCurrentChapter());
    }
}
Also used : SoundDesc(com.bladecoder.engine.model.SoundDesc) JsonValue(com.badlogic.gdx.utils.JsonValue) IOException(java.io.IOException) Scene(com.bladecoder.engine.model.Scene) CharacterActor(com.bladecoder.engine.model.CharacterActor) Inventory(com.bladecoder.engine.model.Inventory)

Aggregations

JsonValue (com.badlogic.gdx.utils.JsonValue)148 JsonReader (com.badlogic.gdx.utils.JsonReader)27 IOException (java.io.IOException)21 Array (com.badlogic.gdx.utils.Array)20 Json (com.badlogic.gdx.utils.Json)15 GdxRuntimeException (com.badlogic.gdx.utils.GdxRuntimeException)14 FileHandle (com.badlogic.gdx.files.FileHandle)11 ReflectionException (com.badlogic.gdx.utils.reflect.ReflectionException)10 BladeJson (com.bladecoder.engine.serialization.BladeJson)9 HashMap (java.util.HashMap)8 Color (com.badlogic.gdx.graphics.Color)7 GraphBoxImpl (com.gempukku.libgdx.graph.ui.graph.GraphBoxImpl)7 ArrayList (java.util.ArrayList)7 Vector2 (com.badlogic.gdx.math.Vector2)6 Actor (com.badlogic.gdx.scenes.scene2d.Actor)6 ChangeListener (com.badlogic.gdx.scenes.scene2d.utils.ChangeListener)6 Action (com.bladecoder.engine.actions.Action)6 File (java.io.File)6 ObjectMap (com.badlogic.gdx.utils.ObjectMap)5 SerializationException (com.badlogic.gdx.utils.SerializationException)5