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!");
}
}
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)));
}
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;
}
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;
}
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();
}
Aggregations