Search in sources :

Example 1 with NodeStateful

use of buildcraft.lib.expression.node.value.NodeStateful in project BuildCraft by BuildCraft.

the class JsonVariableObject method putVariables.

protected void putVariables(JsonObject values, FunctionContext fnCtx) {
    for (Entry<String, JsonElement> entry : values.entrySet()) {
        String name = entry.getKey();
        name = name.toLowerCase(Locale.ROOT);
        if (fnCtx.hasLocalVariable(name)) {
            throw new JsonSyntaxException("Duplicate local variable '" + name + "'");
        } else if (fnCtx.getVariable(name) != null) {
            // ...what? Doesn't this disallow overriding existing variables?
            continue;
        }
        JsonElement value = entry.getValue();
        String type = null, getter = null, rounder = null;
        if (value.isJsonObject()) {
            JsonObject objValue = value.getAsJsonObject();
            value = objValue.get("value");
            type = JsonUtils.getString(objValue, "type");
            getter = JsonUtils.getString(objValue, "getter");
            if (objValue.has("rounder")) {
                rounder = JsonUtils.getString(objValue, "rounder");
            }
        }
        if (!value.isJsonPrimitive()) {
            throw new JsonSyntaxException("Expected a primitive, got " + value + " for the variable '" + name + "'");
        }
        NodeStateful stateful = null;
        FunctionContext fnCtxValue = new FunctionContext("Value Object", fnCtx);
        if (getter != null) {
            // stateful node
            Class<?> nodeType;
            try {
                nodeType = NodeTypes.parseType(type);
            } catch (InvalidExpressionException iee) {
                throw new JsonSyntaxException("Could not parse node type for variable '" + name + "'", iee);
            }
            IGetterFunc getterFunc = parseGetterFunction(getter, fnCtx);
            try {
                stateful = new NodeStateful(name, nodeType, getterFunc);
            } catch (InvalidExpressionException iee) {
                throw new JsonSyntaxException("Could not create a getter for the variable '" + name + "'", iee);
            }
            fnCtx.putVariable(name, stateful.getter);
            fnCtxValue.putVariable(name, stateful.variable);
            if (rounder != null) {
                FunctionContext fnCtx2 = new FunctionContext("Rounding", fnCtx);
                fnCtx2.putVariable("last", stateful.last);
                fnCtx2.putVariable("current", stateful.variable);
                fnCtx2.putVariable("value", stateful.rounderValue);
                try {
                    IExpressionNode nodeRounder = InternalCompiler.compileExpression(rounder, fnCtx2);
                    stateful.setRounder(nodeRounder);
                } catch (InvalidExpressionException iee) {
                    throw new JsonSyntaxException("Could not compile a rounder for the variable '" + name + "'", iee);
                }
            }
        }
        String expression = value.getAsString();
        IExpressionNode node;
        try {
            node = InternalCompiler.compileExpression(expression, fnCtxValue);
        } catch (InvalidExpressionException e) {
            throw new JsonSyntaxException("Failed to compile variable " + name, e);
        }
        if (node instanceof IConstantNode) {
            // No point in adding it to variables
            fnCtx.putVariable(name, node);
            continue;
        }
        if (variables.containsKey(name)) {
            ITickableNode.Source existing = variables.get(name);
            existing.setSource(node);
        } else if (stateful != null) {
            stateful.setSource(node);
            variables.put(name, stateful);
        } else {
            NodeUpdatable nodeUpdatable = new NodeUpdatable(name, node);
            variables.put(name, nodeUpdatable);
            fnCtx.putVariable(name, nodeUpdatable.variable);
        }
    }
}
Also used : NodeStateful(buildcraft.lib.expression.node.value.NodeStateful) ITickableNode(buildcraft.lib.expression.node.value.ITickableNode) JsonObject(com.google.gson.JsonObject) IGetterFunc(buildcraft.lib.expression.node.value.NodeStateful.IGetterFunc) FunctionContext(buildcraft.lib.expression.FunctionContext) NodeUpdatable(buildcraft.lib.expression.node.value.NodeUpdatable) JsonSyntaxException(com.google.gson.JsonSyntaxException) InvalidExpressionException(buildcraft.lib.expression.api.InvalidExpressionException) JsonElement(com.google.gson.JsonElement) IConstantNode(buildcraft.lib.expression.api.IConstantNode) IExpressionNode(buildcraft.lib.expression.api.IExpressionNode)

Aggregations

FunctionContext (buildcraft.lib.expression.FunctionContext)1 IConstantNode (buildcraft.lib.expression.api.IConstantNode)1 IExpressionNode (buildcraft.lib.expression.api.IExpressionNode)1 InvalidExpressionException (buildcraft.lib.expression.api.InvalidExpressionException)1 ITickableNode (buildcraft.lib.expression.node.value.ITickableNode)1 NodeStateful (buildcraft.lib.expression.node.value.NodeStateful)1 IGetterFunc (buildcraft.lib.expression.node.value.NodeStateful.IGetterFunc)1 NodeUpdatable (buildcraft.lib.expression.node.value.NodeUpdatable)1 JsonElement (com.google.gson.JsonElement)1 JsonObject (com.google.gson.JsonObject)1 JsonSyntaxException (com.google.gson.JsonSyntaxException)1