Search in sources :

Example 16 with InvalidExpressionException

use of buildcraft.lib.expression.api.InvalidExpressionException in project BuildCraft by BuildCraft.

the class NodeStack method popBoolean.

@Override
public INodeBoolean popBoolean() throws InvalidExpressionException {
    checkTypeMatch(boolean.class);
    IExpressionNode node = pop();
    if (node instanceof INodeBoolean) {
        return (INodeBoolean) node;
    } else {
        throw new InvalidExpressionException("Cannot cast " + node + " to a boolean!");
    }
}
Also used : InvalidExpressionException(buildcraft.lib.expression.api.InvalidExpressionException) INodeBoolean(buildcraft.lib.expression.api.IExpressionNode.INodeBoolean) IExpressionNode(buildcraft.lib.expression.api.IExpressionNode)

Example 17 with InvalidExpressionException

use of buildcraft.lib.expression.api.InvalidExpressionException in project BuildCraft by BuildCraft.

the class NodeCasting method castToDouble.

public static INodeFuncDouble castToDouble(INodeFunc func) throws InvalidExpressionException {
    if (func instanceof INodeFuncDouble) {
        return (INodeFuncDouble) func;
    }
    Class<?> type = NodeTypes.getType(func);
    FunctionContext ctx = NodeTypes.getContext(type);
    if (ctx == null) {
        throw new InvalidExpressionException("Cannot cast " + func + " to a double!");
    }
    INodeFunc caster = ctx.getFunction("(double)", Collections.singletonList(type));
    if (caster == null || NodeTypes.getType(caster) != double.class) {
        throw new InvalidExpressionException("Cannot cast " + func + " to a double!");
    }
    return (stack) -> (INodeDouble) caster.getNode(new NodeStack(func.getNode(stack)));
}
Also used : INodeObject(buildcraft.lib.expression.api.IExpressionNode.INodeObject) NodeTypes(buildcraft.lib.expression.api.NodeTypes) IExpressionNode(buildcraft.lib.expression.api.IExpressionNode) INodeFuncObject(buildcraft.lib.expression.api.INodeFunc.INodeFuncObject) INodeStack(buildcraft.lib.expression.api.INodeStack) INodeFunc(buildcraft.lib.expression.api.INodeFunc) INodeDouble(buildcraft.lib.expression.api.IExpressionNode.INodeDouble) INodeFuncDouble(buildcraft.lib.expression.api.INodeFunc.INodeFuncDouble) InvalidExpressionException(buildcraft.lib.expression.api.InvalidExpressionException) FunctionContext(buildcraft.lib.expression.FunctionContext) NodeStack(buildcraft.lib.expression.NodeStack) Collections(java.util.Collections) INodeFuncDouble(buildcraft.lib.expression.api.INodeFunc.INodeFuncDouble) InvalidExpressionException(buildcraft.lib.expression.api.InvalidExpressionException) INodeDouble(buildcraft.lib.expression.api.IExpressionNode.INodeDouble) INodeFunc(buildcraft.lib.expression.api.INodeFunc) INodeStack(buildcraft.lib.expression.api.INodeStack) NodeStack(buildcraft.lib.expression.NodeStack) FunctionContext(buildcraft.lib.expression.FunctionContext)

Example 18 with InvalidExpressionException

use of buildcraft.lib.expression.api.InvalidExpressionException in project BuildCraft by BuildCraft.

the class ElementType method createContext.

public static FunctionContext createContext(JsonGuiElement json) {
    FunctionContext ctx = json.context;
    // if json overrides variables then its ok
    ctx = new FunctionContext(ctx);
    Set<String> args = new HashSet<>();
    // Put in args first
    for (String key : json.properties.keySet()) {
        if (key.startsWith("args.")) {
            String argName = key.substring("args.".length());
            args.add(argName);
            String value = json.properties.get(key);
            try {
                IExpressionNode node = InternalCompiler.compileExpression(value, ctx);
                ctx.putVariable(argName, NodeTypes.createConstantNode(node));
            } catch (InvalidExpressionException e) {
                // Ignore the error
                BCLog.logger.info("Failed to compile expression for " + key + " because " + e.getMessage());
            }
        }
    }
    for (String key : json.properties.keySet()) {
        if (key.contains(".") || key.contains("[")) {
            continue;
        }
        String value = json.properties.get(key);
        try {
            IExpressionNode node = InternalCompiler.compileExpression(value, ctx);
            ctx.putVariable(key, NodeTypes.createConstantNode(node));
        } catch (InvalidExpressionException e) {
        // Ignore the error
        // BCLog.logger.info("Failed to compile expression for " + key + " because " + e.getMessage());
        }
    }
    return ctx;
}
Also used : InvalidExpressionException(buildcraft.lib.expression.api.InvalidExpressionException) FunctionContext(buildcraft.lib.expression.FunctionContext) IExpressionNode(buildcraft.lib.expression.api.IExpressionNode) HashSet(java.util.HashSet)

Example 19 with InvalidExpressionException

use of buildcraft.lib.expression.api.InvalidExpressionException in project BuildCraft by BuildCraft.

the class ElementTypeText method deserialize0.

// pos: the position of the text
// text: The text to be drawn. Will be localised first, and used as a fallback
// expression: A replacement for text -- uses an expression rather than as a literal.
// colour: Default colour to be drawn
// centered: If true then the text will be centered around pos
@Override
public IGuiElement deserialize0(BuildCraftJsonGui gui, IGuiPosition parent, JsonGuiInfo info, JsonGuiElement json) {
    FunctionContext ctx = createContext(json);
    IGuiPosition pos = resolvePosition(json, "pos", parent, ctx);
    INodeObject<String> text;
    String prop;
    if ((prop = json.properties.get("text")) != null) {
        String localized = LocaleUtil.localize(prop);
        text = new NodeConstantObject<>(String.class, localized);
    } else if ((prop = json.properties.get("expression")) != null) {
        try {
            text = GenericExpressionCompiler.compileExpressionString(prop, ctx);
        } catch (InvalidExpressionException e) {
            throw new JsonSyntaxException("Invalid expression for '" + json.name + "'", e);
        }
    } else {
        throw new JsonSyntaxException("Require either 'text' or 'expression'!");
    }
    int colour;
    if (json.properties.containsKey("colour")) {
        colour = resolveEquationInt(json, "colour", ctx);
    } else {
        colour = resolveEquationInt(json, "color", ctx);
    }
    GuiElementText element = new GuiElementText(gui, pos, text, colour);
    element.setCentered("true".equals(json.properties.get("centered")));
    element.setDropShadow("true".equals(json.properties.get("shadow")));
    element.setForeground("true".equals(json.properties.get("foreground")));
    return element;
}
Also used : JsonSyntaxException(com.google.gson.JsonSyntaxException) InvalidExpressionException(buildcraft.lib.expression.api.InvalidExpressionException) GuiElementText(buildcraft.lib.gui.elem.GuiElementText) FunctionContext(buildcraft.lib.expression.FunctionContext) IGuiPosition(buildcraft.lib.gui.pos.IGuiPosition)

Example 20 with InvalidExpressionException

use of buildcraft.lib.expression.api.InvalidExpressionException in project BuildCraft by BuildCraft.

the class BuildCraftJsonGui method load.

public final void load() {
    ResourceLoaderContext loadHistory = new ResourceLoaderContext();
    try (InputStreamReader reader = loadHistory.startLoading(jsonGuiDefinition)) {
        JsonObject obj = new Gson().fromJson(reader, JsonObject.class);
        JsonGuiInfo info = new JsonGuiInfo(obj, context, loadHistory);
        sizeX = (int) GenericExpressionCompiler.compileExpressionLong(info.sizeX, context).evaluate();
        sizeY = (int) GenericExpressionCompiler.compileExpressionLong(info.sizeY, context).evaluate();
        varData.setNodes(info.createTickableNodes());
        for (JsonGuiElement elem : info.elements) {
            String typeName = elem.properties.get("type");
            ElementType type = JsonGuiTypeRegistry.TYPES.get(typeName);
            if (type == null) {
                BCLog.logger.warn("Unknown type " + typeName);
            } else {
                IGuiElement e = type.deserialize(this, rootElement, info, elem);
                String parent = elem.properties.get("parent");
                IContainingElement p = properties.get("custom." + parent, IContainingElement.class);
                properties.put("custom." + elem.name, e);
                if (p == null) {
                    shownElements.add(e);
                } else {
                    p.getChildElements().add(e);
                    p.calculateSizes();
                }
            }
        }
    } catch (InvalidExpressionException iee) {
        throw new JsonSyntaxException("Failed to resolve the size of " + jsonGuiDefinition, iee);
    } catch (IOException e) {
        throw new Error(e);
    }
    loadHistory.finishLoading();
}
Also used : ResourceLoaderContext(buildcraft.lib.client.model.ResourceLoaderContext) InputStreamReader(java.io.InputStreamReader) JsonObject(com.google.gson.JsonObject) Gson(com.google.gson.Gson) IOException(java.io.IOException) IContainingElement(buildcraft.lib.gui.IContainingElement) JsonSyntaxException(com.google.gson.JsonSyntaxException) InvalidExpressionException(buildcraft.lib.expression.api.InvalidExpressionException) IGuiElement(buildcraft.lib.gui.IGuiElement)

Aggregations

InvalidExpressionException (buildcraft.lib.expression.api.InvalidExpressionException)21 IExpressionNode (buildcraft.lib.expression.api.IExpressionNode)12 FunctionContext (buildcraft.lib.expression.FunctionContext)8 INodeDouble (buildcraft.lib.expression.api.IExpressionNode.INodeDouble)6 INodeFunc (buildcraft.lib.expression.api.INodeFunc)6 INodeStack (buildcraft.lib.expression.api.INodeStack)6 JsonSyntaxException (com.google.gson.JsonSyntaxException)6 INodeObject (buildcraft.lib.expression.api.IExpressionNode.INodeObject)5 NodeStack (buildcraft.lib.expression.NodeStack)4 INodeBoolean (buildcraft.lib.expression.api.IExpressionNode.INodeBoolean)3 INodeLong (buildcraft.lib.expression.api.IExpressionNode.INodeLong)3 ArrayList (java.util.ArrayList)3 INodeFuncObject (buildcraft.lib.expression.api.INodeFunc.INodeFuncObject)2 IGuiArea (buildcraft.lib.gui.pos.IGuiArea)2 IGuiPosition (buildcraft.lib.gui.pos.IGuiPosition)2 JsonObject (com.google.gson.JsonObject)2 ResourceLoaderContext (buildcraft.lib.client.model.ResourceLoaderContext)1 IConstantNode (buildcraft.lib.expression.api.IConstantNode)1 INodeFuncDouble (buildcraft.lib.expression.api.INodeFunc.INodeFuncDouble)1 NodeTypes (buildcraft.lib.expression.api.NodeTypes)1