Search in sources :

Example 36 with Construct

use of com.laytonsmith.core.constructs.Construct in project CommandHelper by EngineHub.

the class ObjectGenerator method itemMeta.

public Construct itemMeta(MCItemStack is, Target t) {
    if (!is.hasItemMeta()) {
        return CNull.NULL;
    } else {
        Construct display, lore;
        CArray ma = CArray.GetAssociativeArray(t);
        MCItemMeta meta = is.getItemMeta();
        if (meta.hasDisplayName()) {
            display = new CString(meta.getDisplayName(), t);
        } else {
            display = CNull.NULL;
        }
        if (meta.hasLore()) {
            lore = new CArray(t);
            for (String l : meta.getLore()) {
                ((CArray) lore).push(new CString(l, t), t);
            }
        } else {
            lore = CNull.NULL;
        }
        ma.set("display", display, t);
        ma.set("lore", lore, t);
        ma.set("enchants", enchants(meta.getEnchants(), t), t);
        ma.set("repair", new CInt(meta.getRepairCost(), t), t);
        // Version specific ItemMeta
        if (Static.getServer().getMinecraftVersion().gte(MCVersion.MC1_8)) {
            Set<MCItemFlag> itemFlags = meta.getItemFlags();
            CArray flagArray = new CArray(t);
            if (itemFlags.size() > 0) {
                for (MCItemFlag flag : itemFlags) {
                    flagArray.push(new CString(flag.name(), t), t);
                }
            }
            ma.set("flags", flagArray, t);
            if (Static.getServer().getMinecraftVersion().gte(MCVersion.MC1_11)) {
                ma.set("unbreakable", CBoolean.get(meta.isUnbreakable()), t);
            }
        }
        // Specific ItemMeta
        if (meta instanceof MCBlockStateMeta) {
            MCBlockState bs = ((MCBlockStateMeta) meta).getBlockState();
            if (bs instanceof MCShulkerBox) {
                MCShulkerBox mcsb = (MCShulkerBox) bs;
                MCInventory inv = mcsb.getInventory();
                CArray box = CArray.GetAssociativeArray(t);
                for (int i = 0; i < inv.getSize(); i++) {
                    Construct item = ObjectGenerator.GetGenerator().item(inv.getItem(i), t);
                    if (!(item instanceof CNull)) {
                        box.set(i, item, t);
                    }
                }
                ma.set("inventory", box, t);
            } else if (bs instanceof MCBanner) {
                MCBanner banner = (MCBanner) bs;
                CArray patterns = new CArray(t, banner.numberOfPatterns());
                for (MCPattern p : banner.getPatterns()) {
                    CArray pattern = CArray.GetAssociativeArray(t);
                    pattern.set("shape", new CString(p.getShape().toString(), t), t);
                    pattern.set("color", new CString(p.getColor().toString(), t), t);
                    patterns.push(pattern, t);
                }
                ma.set("patterns", patterns, t);
                MCDyeColor dyeColor = banner.getBaseColor();
                if (dyeColor != null) {
                    ma.set("basecolor", new CString(dyeColor.toString(), t), t);
                }
            } else if (bs instanceof MCCreatureSpawner) {
                MCCreatureSpawner mccs = (MCCreatureSpawner) bs;
                ma.set("spawntype", mccs.getSpawnedType().name());
            }
        } else if (meta instanceof MCFireworkEffectMeta) {
            MCFireworkEffectMeta mcfem = (MCFireworkEffectMeta) meta;
            MCFireworkEffect effect = mcfem.getEffect();
            if (effect == null) {
                ma.set("effect", CNull.NULL, t);
            } else {
                ma.set("effect", fireworkEffect(effect, t), t);
            }
        } else if (meta instanceof MCFireworkMeta) {
            MCFireworkMeta mcfm = (MCFireworkMeta) meta;
            CArray firework = CArray.GetAssociativeArray(t);
            firework.set("strength", new CInt(mcfm.getStrength(), t), t);
            CArray fe = new CArray(t);
            for (MCFireworkEffect effect : mcfm.getEffects()) {
                fe.push(fireworkEffect(effect, t), t);
            }
            firework.set("effects", fe, t);
            ma.set("firework", firework, t);
        } else if (meta instanceof MCLeatherArmorMeta) {
            CArray color = color(((MCLeatherArmorMeta) meta).getColor(), t);
            ma.set("color", color, t);
        } else if (meta instanceof MCBookMeta) {
            Construct title, author, pages;
            if (((MCBookMeta) meta).hasTitle()) {
                title = new CString(((MCBookMeta) meta).getTitle(), t);
            } else {
                title = CNull.NULL;
            }
            if (((MCBookMeta) meta).hasAuthor()) {
                author = new CString(((MCBookMeta) meta).getAuthor(), t);
            } else {
                author = CNull.NULL;
            }
            if (((MCBookMeta) meta).hasPages()) {
                pages = new CArray(t);
                for (String p : ((MCBookMeta) meta).getPages()) {
                    ((CArray) pages).push(new CString(p, t), t);
                }
            } else {
                pages = CNull.NULL;
            }
            ma.set("title", title, t);
            ma.set("author", author, t);
            ma.set("pages", pages, t);
        } else if (meta instanceof MCSkullMeta) {
            if (Static.getServer().getMinecraftVersion().gte(MCVersion.MC1_12_X)) {
                if (((MCSkullMeta) meta).hasOwner()) {
                    MCOfflinePlayer player = ((MCSkullMeta) meta).getOwningPlayer();
                    ma.set("owner", new CString(player.getName(), t), t);
                    ma.set("owneruuid", new CString(player.getUniqueID().toString(), t), t);
                } else {
                    ma.set("owner", CNull.NULL, t);
                    ma.set("owneruuid", CNull.NULL, t);
                }
            } else {
                if (((MCSkullMeta) meta).hasOwner()) {
                    ma.set("owner", new CString(((MCSkullMeta) meta).getOwner(), t), t);
                } else {
                    ma.set("owner", CNull.NULL, t);
                }
            }
        } else if (meta instanceof MCEnchantmentStorageMeta) {
            Construct stored;
            if (((MCEnchantmentStorageMeta) meta).hasStoredEnchants()) {
                stored = enchants(((MCEnchantmentStorageMeta) meta).getStoredEnchants(), t);
            } else {
                stored = CNull.NULL;
            }
            ma.set("stored", stored, t);
        } else if (meta instanceof MCPotionMeta) {
            MCPotionMeta potionmeta = (MCPotionMeta) meta;
            CArray effects = potions(potionmeta.getCustomEffects(), t);
            ma.set("potions", effects, t);
            if (Static.getServer().getMinecraftVersion().gte(MCVersion.MC1_9)) {
                MCPotionData potiondata = potionmeta.getBasePotionData();
                if (potiondata != null) {
                    ma.set("base", potionData(potiondata, t), t);
                }
            } else if (effects.size() > 0) {
                ma.set("main", ((CArray) effects.get(0, t)).get("id", t), t);
            }
        } else if (meta instanceof MCBannerMeta) {
            MCBannerMeta bannermeta = (MCBannerMeta) meta;
            CArray patterns = new CArray(t, bannermeta.numberOfPatterns());
            for (MCPattern p : bannermeta.getPatterns()) {
                CArray pattern = CArray.GetAssociativeArray(t);
                pattern.set("shape", new CString(p.getShape().toString(), t), t);
                pattern.set("color", new CString(p.getColor().toString(), t), t);
                patterns.push(pattern, t);
            }
            ma.set("patterns", patterns, t);
            MCDyeColor dyeColor = bannermeta.getBaseColor();
            if (dyeColor != null) {
                ma.set("basecolor", new CString(dyeColor.toString(), t), t);
            }
        } else if (meta instanceof MCSpawnEggMeta) {
            MCEntityType spawntype = ((MCSpawnEggMeta) meta).getSpawnedType();
            if (spawntype == null) {
                ma.set("spawntype", CNull.NULL, t);
            } else {
                ma.set("spawntype", new CString(spawntype.name(), t), t);
            }
        } else if (meta instanceof MCMapMeta && Static.getServer().getMinecraftVersion().gte(MCVersion.MC1_11)) {
            MCColor mapcolor = ((MCMapMeta) meta).getColor();
            Construct color;
            if (mapcolor == null) {
                color = CNull.NULL;
            } else {
                color = color(mapcolor, t);
            }
            ma.set("color", color, t);
        }
        return ma;
    }
}
Also used : MCBanner(com.laytonsmith.abstraction.blocks.MCBanner) MCEntityType(com.laytonsmith.abstraction.enums.MCEntityType) MCOfflinePlayer(com.laytonsmith.abstraction.MCOfflinePlayer) MCFireworkMeta(com.laytonsmith.abstraction.MCFireworkMeta) CArray(com.laytonsmith.core.constructs.CArray) MCLeatherArmorMeta(com.laytonsmith.abstraction.MCLeatherArmorMeta) MCSpawnEggMeta(com.laytonsmith.abstraction.MCSpawnEggMeta) CString(com.laytonsmith.core.constructs.CString) CString(com.laytonsmith.core.constructs.CString) MCPattern(com.laytonsmith.abstraction.MCPattern) MCFireworkEffect(com.laytonsmith.abstraction.MCFireworkEffect) MCPotionMeta(com.laytonsmith.abstraction.MCPotionMeta) MCMapMeta(com.laytonsmith.abstraction.MCMapMeta) MCColor(com.laytonsmith.abstraction.MCColor) MCItemFlag(com.laytonsmith.abstraction.enums.MCItemFlag) MCDyeColor(com.laytonsmith.abstraction.enums.MCDyeColor) MCBookMeta(com.laytonsmith.abstraction.MCBookMeta) MCBlockStateMeta(com.laytonsmith.abstraction.MCBlockStateMeta) MCFireworkEffectMeta(com.laytonsmith.abstraction.MCFireworkEffectMeta) MCBlockState(com.laytonsmith.abstraction.blocks.MCBlockState) MCBannerMeta(com.laytonsmith.abstraction.MCBannerMeta) MCInventory(com.laytonsmith.abstraction.MCInventory) MCSkullMeta(com.laytonsmith.abstraction.MCSkullMeta) MCItemMeta(com.laytonsmith.abstraction.MCItemMeta) MCPotionData(com.laytonsmith.abstraction.MCPotionData) MCEnchantmentStorageMeta(com.laytonsmith.abstraction.MCEnchantmentStorageMeta) CInt(com.laytonsmith.core.constructs.CInt) MCCreatureSpawner(com.laytonsmith.abstraction.MCCreatureSpawner) Construct(com.laytonsmith.core.constructs.Construct) MCShulkerBox(com.laytonsmith.abstraction.blocks.MCShulkerBox) CNull(com.laytonsmith.core.constructs.CNull)

Example 37 with Construct

use of com.laytonsmith.core.constructs.Construct in project CommandHelper by EngineHub.

the class MethodScriptCompiler method optimize.

/**
 * Recurses down into the tree, attempting to optimize where possible. A few things have strong coupling, for
 * information on these items, see the documentation included in the source.
 *
 * @param tree
 * @return
 */
private static void optimize(ParseTree tree, Stack<List<Procedure>> procs, Set<ConfigCompileException> compilerErrors) {
    if (tree.isOptimized()) {
        // Don't need to re-run this
        return;
    }
    // }
    if (!(tree.getData() instanceof CFunction)) {
        // There's no way to optimize something that's not a function
        return;
    }
    // If it is a proc definition, we need to go ahead and see if we can add it to the const proc stack
    if (tree.getData().val().equals("proc")) {
        procs.push(new ArrayList<Procedure>());
    }
    CFunction cFunction = (CFunction) tree.getData();
    Function func;
    try {
        func = (Function) FunctionList.getFunction(cFunction);
    } catch (ConfigCompileException e) {
        func = null;
    }
    if (func != null) {
        if (func.getClass().getAnnotation(nolinking.class) != null) {
            // It's an unlinking function, so we need to stop at this point
            return;
        }
    }
    if (cFunction instanceof CIdentifier) {
        // Add the child to the identifier
        ParseTree c = ((CIdentifier) cFunction).contained();
        tree.addChild(c);
        c.getData().setWasIdentifier(true);
    }
    List<ParseTree> children = tree.getChildren();
    if (func instanceof Optimizable && ((Optimizable) func).optimizationOptions().contains(OptimizationOption.PRIORITY_OPTIMIZATION)) {
        // would cause an error, even though the user did in fact provide code in that section.
        try {
            ((Optimizable) func).optimizeDynamic(tree.getTarget(), children, tree.getFileOptions());
        } catch (ConfigCompileException ex) {
            // If an error occurs, we will skip the rest of this element
            compilerErrors.add(ex);
            return;
        } catch (ConfigRuntimeException ex) {
            compilerErrors.add(new ConfigCompileException(ex));
            return;
        }
    }
    for (int i = 0; i < children.size(); i++) {
        ParseTree t = children.get(i);
        if (t.getData() instanceof CFunction) {
            if (t.getData().val().startsWith("_") || (func != null && func.useSpecialExec())) {
                continue;
            }
            Function f;
            try {
                f = (Function) FunctionList.getFunction(t.getData());
            } catch (ConfigCompileException ex) {
                compilerErrors.add(ex);
                return;
            }
            Set<OptimizationOption> options = NO_OPTIMIZATIONS;
            if (f instanceof Optimizable) {
                options = ((Optimizable) f).optimizationOptions();
            }
            if (options.contains(OptimizationOption.TERMINAL)) {
                if (children.size() > i + 1) {
                    // First, a compiler warning
                    CHLog.GetLogger().Log(CHLog.Tags.COMPILER, LogLevel.WARNING, "Unreachable code. Consider removing this code.", children.get(i + 1).getTarget());
                    // Now, truncate the children
                    for (int j = children.size() - 1; j > i; j--) {
                        children.remove(j);
                    }
                    break;
                }
            }
        }
    }
    boolean fullyStatic = true;
    boolean hasIVars = false;
    for (ParseTree node : children) {
        if (node.getData() instanceof CFunction) {
            optimize(node, procs, compilerErrors);
        }
        if (node.getData().isDynamic() && !(node.getData() instanceof IVariable)) {
            fullyStatic = false;
        }
        if (node.getData() instanceof IVariable) {
            hasIVars = true;
        }
    }
    // In all cases, at this point, we are either unable to optimize, or we will
    // optimize, so set our optimized variable at this point.
    tree.setOptimized(true);
    if (func == null) {
        // It's a proc call. Let's see if we can optimize it
        Procedure p = null;
        loop: for (List<Procedure> proc : procs) {
            for (Procedure pp : proc) {
                if (pp.getName().equals(cFunction.val())) {
                    p = pp;
                    break loop;
                }
            }
        }
        if (p != null) {
            try {
                Construct c = DataHandling.proc.optimizeProcedure(p.getTarget(), p, children);
                if (c != null) {
                    tree.setData(c);
                    tree.removeChildren();
                    return;
                }
            // else Nope, couldn't optimize.
            } catch (ConfigRuntimeException ex) {
                // Cool. Caught a runtime error at compile time :D
                compilerErrors.add(new ConfigCompileException(ex));
            }
        }
        // so we can't for sure say, but we do know we can't optimize this
        return;
    }
    if (tree.getData().val().equals("proc")) {
        // Check for too few arguments
        if (children.size() < 2) {
            compilerErrors.add(new ConfigCompileException("Incorrect number of arguments passed to proc", tree.getData().getTarget()));
            return;
        }
        // We just went out of scope, so we need to pop the layer of Procedures that
        // are internal to us
        procs.pop();
        // Let's see.
        try {
            ParseTree root = new ParseTree(new CFunction(__autoconcat__, Target.UNKNOWN), tree.getFileOptions());
            Script fakeScript = Script.GenerateScript(root, "*");
            Environment env = null;
            try {
                if (Implementation.GetServerType().equals(Implementation.Type.BUKKIT)) {
                    CommandHelperPlugin plugin = CommandHelperPlugin.self;
                    GlobalEnv gEnv = new GlobalEnv(plugin.executionQueue, plugin.profiler, plugin.persistenceNetwork, MethodScriptFileLocations.getDefault().getConfigDirectory(), plugin.profiles, new TaskManager());
                    env = Environment.createEnvironment(gEnv, new CommandHelperEnvironment());
                } else {
                    env = Static.GenerateStandaloneEnvironment(false);
                }
            } catch (IOException | DataSourceException | URISyntaxException | Profiles.InvalidProfileException e) {
            // 
            }
            Procedure myProc = DataHandling.proc.getProcedure(tree.getTarget(), env, fakeScript, children.toArray(new ParseTree[children.size()]));
            // Yep. So, we can move on with our lives now, and if it's used later, it could possibly be static.
            procs.peek().add(myProc);
        } catch (ConfigRuntimeException e) {
            // Well, they have an error in there somewhere
            compilerErrors.add(new ConfigCompileException(e));
        } catch (NullPointerException e) {
            // Nope, can't optimize.
            return;
        }
    }
    // the compiler trick functions know how to deal with it specially, even if everything isn't
    // static, so do this first.
    String oldFunctionName = func.getName();
    Set<OptimizationOption> options = NO_OPTIMIZATIONS;
    if (func instanceof Optimizable) {
        options = ((Optimizable) func).optimizationOptions();
    }
    if (options.contains(OptimizationOption.OPTIMIZE_DYNAMIC)) {
        try {
            ParseTree tempNode;
            try {
                tempNode = ((Optimizable) func).optimizeDynamic(tree.getData().getTarget(), tree.getChildren(), tree.getFileOptions());
            } catch (ConfigRuntimeException e) {
                // Turn it into a compile exception, then rethrow
                throw new ConfigCompileException(e);
            }
            if (tempNode == Optimizable.PULL_ME_UP) {
                if (tree.hasChildren()) {
                    tempNode = tree.getChildAt(0);
                } else {
                    tempNode = null;
                }
            }
            if (tempNode == Optimizable.REMOVE_ME) {
                tree.setData(new CFunction("p", Target.UNKNOWN));
                tree.removeChildren();
            } else if (tempNode != null) {
                tree.setData(tempNode.getData());
                tree.setOptimized(tempNode.isOptimized());
                tree.setChildren(tempNode.getChildren());
                tree.getData().setWasIdentifier(tempNode.getData().wasIdentifier());
                optimize(tree, procs, compilerErrors);
                tree.setOptimized(true);
                // array, so if they have reversed this, make note of that now
                if (tempNode.hasBeenMadeStatic()) {
                    fullyStatic = true;
                }
            }
        // else it wasn't an optimization, but a compile check
        } catch (ConfigCompileException ex) {
            compilerErrors.add(ex);
        }
    }
    if (!fullyStatic) {
        return;
    }
    // specially from here forward
    if (func.preResolveVariables() && hasIVars) {
        // Well, this function isn't equipped to deal with IVariables.
        return;
    }
    // don't want to run this now
    if (tree.getData().getValue().equals(oldFunctionName) && (options.contains(OptimizationOption.OPTIMIZE_CONSTANT) || options.contains(OptimizationOption.CONSTANT_OFFLINE))) {
        Construct[] constructs = new Construct[tree.getChildren().size()];
        for (int i = 0; i < tree.getChildren().size(); i++) {
            constructs[i] = tree.getChildAt(i).getData();
        }
        try {
            try {
                Construct result;
                if (options.contains(OptimizationOption.CONSTANT_OFFLINE)) {
                    List<Integer> numArgsList = Arrays.asList(func.numArgs());
                    if (!numArgsList.contains(Integer.MAX_VALUE) && !numArgsList.contains(tree.getChildren().size())) {
                        compilerErrors.add(new ConfigCompileException("Incorrect number of arguments passed to " + tree.getData().val(), tree.getData().getTarget()));
                        result = null;
                    } else {
                        result = func.exec(tree.getData().getTarget(), null, constructs);
                    }
                } else {
                    result = ((Optimizable) func).optimize(tree.getData().getTarget(), constructs);
                }
                // If the result is null, it was just a check, it can't optimize further.
                if (result != null) {
                    result.setWasIdentifier(tree.getData().wasIdentifier());
                    tree.setData(result);
                    tree.removeChildren();
                }
            } catch (ConfigRuntimeException e) {
                // Turn this into a ConfigCompileException, then rethrow
                throw new ConfigCompileException(e);
            }
        } catch (ConfigCompileException ex) {
            compilerErrors.add(ex);
        }
    }
// It doesn't know how to optimize. Oh well.
}
Also used : CommandHelperPlugin(com.laytonsmith.commandhelper.CommandHelperPlugin) IVariable(com.laytonsmith.core.constructs.IVariable) ConfigRuntimeException(com.laytonsmith.core.exceptions.ConfigRuntimeException) URISyntaxException(java.net.URISyntaxException) CString(com.laytonsmith.core.constructs.CString) ConfigCompileException(com.laytonsmith.core.exceptions.ConfigCompileException) Function(com.laytonsmith.core.functions.Function) CFunction(com.laytonsmith.core.constructs.CFunction) CommandHelperEnvironment(com.laytonsmith.core.environments.CommandHelperEnvironment) KeywordList(com.laytonsmith.core.compiler.KeywordList) FunctionList(com.laytonsmith.core.functions.FunctionList) List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) com.laytonsmith.annotations.nolinking(com.laytonsmith.annotations.nolinking) OptimizationOption(com.laytonsmith.core.Optimizable.OptimizationOption) CFunction(com.laytonsmith.core.constructs.CFunction) IOException(java.io.IOException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TaskManager(com.laytonsmith.core.taskmanager.TaskManager) DataSourceException(com.laytonsmith.persistence.DataSourceException) CIdentifier(com.laytonsmith.core.constructs.CIdentifier) Construct(com.laytonsmith.core.constructs.Construct) CommandHelperEnvironment(com.laytonsmith.core.environments.CommandHelperEnvironment) Environment(com.laytonsmith.core.environments.Environment) GlobalEnv(com.laytonsmith.core.environments.GlobalEnv)

Example 38 with Construct

use of com.laytonsmith.core.constructs.Construct in project CommandHelper by EngineHub.

the class MethodScriptCompiler method execute.

/**
 * Executes a pre-compiled MethodScript, given the specified Script environment, but also provides a method to set
 * the constants in the script.
 *
 * @param root
 * @param env
 * @param done
 * @param script
 * @param vars
 * @return
 */
public static Construct execute(ParseTree root, Environment env, MethodScriptComplete done, Script script, List<Variable> vars) {
    if (root == null) {
        return CVoid.VOID;
    }
    if (script == null) {
        script = new Script(null, null, env.getEnv(GlobalEnv.class).GetLabel(), new FileOptions(new HashMap<>()));
    }
    if (vars != null) {
        Map<String, Variable> varMap = new HashMap<>();
        for (Variable v : vars) {
            varMap.put(v.getVariableName(), v);
        }
        for (Construct tempNode : root.getAllData()) {
            if (tempNode instanceof Variable) {
                Variable vv = varMap.get(((Variable) tempNode).getVariableName());
                if (vv != null) {
                    ((Variable) tempNode).setVal(vv.getDefault());
                } else {
                    // The variable is unset. I'm not quite sure what cases would cause this
                    ((Variable) tempNode).setVal("");
                }
            }
        }
    }
    StringBuilder b = new StringBuilder();
    Construct returnable = null;
    for (ParseTree gg : root.getChildren()) {
        Construct retc = script.eval(gg, env);
        if (root.numberOfChildren() == 1) {
            returnable = retc;
        }
        String ret = retc instanceof CNull ? "null" : retc.val();
        if (ret != null && !ret.trim().isEmpty()) {
            b.append(ret).append(" ");
        }
    }
    if (done != null) {
        done.done(b.toString().trim());
    }
    if (returnable != null) {
        return returnable;
    }
    return Static.resolveConstruct(b.toString().trim(), Target.UNKNOWN);
}
Also used : IVariable(com.laytonsmith.core.constructs.IVariable) Variable(com.laytonsmith.core.constructs.Variable) HashMap(java.util.HashMap) Construct(com.laytonsmith.core.constructs.Construct) GlobalEnv(com.laytonsmith.core.environments.GlobalEnv) CString(com.laytonsmith.core.constructs.CString) FileOptions(com.laytonsmith.core.compiler.FileOptions) CNull(com.laytonsmith.core.constructs.CNull)

Example 39 with Construct

use of com.laytonsmith.core.constructs.Construct in project CommandHelper by EngineHub.

the class Static method getMSObject.

/**
 * Given a java object, returns a MethodScript object.
 *
 * @param object
 * @param t
 * @return
 */
public static Construct getMSObject(Object object, Target t) {
    if (object == null) {
        return CNull.NULL;
    } else if (object instanceof Boolean) {
        return CBoolean.get((boolean) object);
    } else if ((object instanceof Byte) || (object instanceof Short) || (object instanceof Integer) || (object instanceof Long)) {
        return new CInt((long) object, t);
    } else if ((object instanceof Float) || (object instanceof Double)) {
        return new CDouble((double) object, t);
    } else if (object instanceof Character) {
        return new CString((char) object, t);
    } else if (object instanceof String) {
        return new CString((String) object, t);
    } else if (object instanceof StringBuffer) {
        return new CResource<>((StringBuffer) object, new CResource.ResourceToString() {

            @Override
            public String getString(CResource res) {
                return res.getResource().toString();
            }
        }, t);
    } else if (object instanceof XMLDocument) {
        return new CResource<>((XMLDocument) object, t);
    } else if (object instanceof Construct) {
        return (Construct) object;
    } else if (object instanceof boolean[]) {
        boolean[] array = (boolean[]) object;
        CArray r = new CArray(t);
        for (boolean b : array) {
            r.push(CBoolean.get(b), t);
        }
        return r;
    } else if (object instanceof byte[]) {
        return CByteArray.wrap((byte[]) object, t);
    } else if (object instanceof char[]) {
        char[] array = (char[]) object;
        CArray r = new CArray(t);
        for (char c : array) {
            r.push(new CString(c, t), t);
        }
        return r;
    } else if (object instanceof short[]) {
        short[] array = (short[]) object;
        CArray r = new CArray(t);
        for (short s : array) {
            r.push(new CInt(s, t), t);
        }
        return r;
    } else if (object instanceof int[]) {
        int[] array = (int[]) object;
        CArray r = new CArray(t);
        for (int i : array) {
            r.push(new CInt(i, t), t);
        }
        return r;
    } else if (object instanceof long[]) {
        long[] array = (long[]) object;
        CArray r = new CArray(t);
        for (long l : array) {
            r.push(new CInt(l, t), t);
        }
        return r;
    } else if (object instanceof float[]) {
        float[] array = (float[]) object;
        CArray r = new CArray(t);
        for (float f : array) {
            r.push(new CDouble(f, t), t);
        }
        return r;
    } else if (object instanceof double[]) {
        double[] array = (double[]) object;
        CArray r = new CArray(t);
        for (double d : array) {
            r.push(new CDouble(d, t), t);
        }
        return r;
    } else if (object instanceof Object[]) {
        CArray r = new CArray(t);
        for (Object o : (Object[]) object) {
            r.push((o == object) ? r : getMSObject(o, t), t);
        }
        return r;
    } else if (object instanceof Collection) {
        return getMSObject(((Collection) object).toArray(), t);
    } else if (object instanceof Map) {
        Map map = ((Map) object);
        CArray r = new CArray(t);
        for (Object key : map.keySet()) {
            Object o = map.get(key);
            r.set(key.toString(), (o == object) ? r : getMSObject(o, t), t);
        }
        return r;
    } else {
        return new CString(object.toString(), t);
    }
}
Also used : CArray(com.laytonsmith.core.constructs.CArray) CString(com.laytonsmith.core.constructs.CString) XMLDocument(com.laytonsmith.PureUtilities.XMLDocument) CString(com.laytonsmith.core.constructs.CString) CBoolean(com.laytonsmith.core.constructs.CBoolean) CResource(com.laytonsmith.core.constructs.CResource) CDouble(com.laytonsmith.core.constructs.CDouble) CDouble(com.laytonsmith.core.constructs.CDouble) CInt(com.laytonsmith.core.constructs.CInt) Construct(com.laytonsmith.core.constructs.Construct) Collection(java.util.Collection) Map(java.util.Map) HashMap(java.util.HashMap)

Example 40 with Construct

use of com.laytonsmith.core.constructs.Construct in project CommandHelper by EngineHub.

the class Static method getJavaObject.

/**
 * Given a MethodScript object, returns a java object.
 *
 * @param construct
 * @return
 */
public static Object getJavaObject(Construct construct) {
    if ((construct == null) || (construct instanceof CNull)) {
        return null;
    } else if (construct instanceof CVoid) {
        return "";
    } else if (construct instanceof CBoolean) {
        return ((CBoolean) construct).getBoolean();
    } else if (construct instanceof CInt) {
        return ((CInt) construct).getInt();
    } else if (construct instanceof CDouble) {
        return ((CDouble) construct).getDouble();
    } else if (construct instanceof CString) {
        return construct.val();
    } else if (construct instanceof CByteArray) {
        return ((CByteArray) construct).asByteArrayCopy();
    } else if (construct instanceof CResource) {
        return ((CResource) construct).getResource();
    } else if (construct instanceof CArray) {
        CArray array = (CArray) construct;
        if (array.isAssociative()) {
            HashMap<String, Object> map = new HashMap<>();
            for (Construct key : array.keySet()) {
                Construct c = array.get(key.val(), Target.UNKNOWN);
                map.put(key.val(), (c == array) ? map : getJavaObject(c));
            }
            return map;
        } else {
            Object[] a = new Object[(int) array.size()];
            boolean nullable = false;
            Class<?> clazz = null;
            for (int i = 0; i < array.size(); i++) {
                Construct c = array.get(i, Target.UNKNOWN);
                if (c == array) {
                    a[i] = a;
                } else {
                    a[i] = getJavaObject(array.get(i, Target.UNKNOWN));
                }
                if (a[i] != null) {
                    if (clazz == null) {
                        clazz = a[i].getClass();
                    } else if (!clazz.equals(Object.class)) {
                        // to test if it is possible to return something more specific than Object[]
                        Class<?> cl = a[i].getClass();
                        while (!clazz.isAssignableFrom(cl)) {
                            clazz = clazz.getSuperclass();
                        }
                    }
                } else {
                    nullable = true;
                }
            }
            if ((clazz != null) && (!clazz.equals(Object.class))) {
                if (clazz.equals(Boolean.class) && !nullable) {
                    boolean[] r = new boolean[a.length];
                    for (int i = 0; i < a.length; i++) {
                        r[i] = (boolean) a[i];
                    }
                    return r;
                }
                if (clazz.equals(Long.class) && !nullable) {
                    long[] r = new long[a.length];
                    for (int i = 0; i < a.length; i++) {
                        r[i] = (long) a[i];
                    }
                    return r;
                } else if (clazz.equals(Double.class) && !nullable) {
                    double[] r = new double[a.length];
                    for (int i = 0; i < a.length; i++) {
                        r[i] = (double) a[i];
                    }
                    return r;
                } else {
                    Object[] r = (Object[]) Array.newInstance(clazz, a.length);
                    System.arraycopy(a, 0, r, 0, a.length);
                    return r;
                }
            } else {
                return a;
            }
        }
    } else {
        return construct;
    }
}
Also used : HashMap(java.util.HashMap) CBoolean(com.laytonsmith.core.constructs.CBoolean) CArray(com.laytonsmith.core.constructs.CArray) CDouble(com.laytonsmith.core.constructs.CDouble) CString(com.laytonsmith.core.constructs.CString) CVoid(com.laytonsmith.core.constructs.CVoid) CString(com.laytonsmith.core.constructs.CString) CByteArray(com.laytonsmith.core.constructs.CByteArray) CInt(com.laytonsmith.core.constructs.CInt) Construct(com.laytonsmith.core.constructs.Construct) CBoolean(com.laytonsmith.core.constructs.CBoolean) CResource(com.laytonsmith.core.constructs.CResource) CNull(com.laytonsmith.core.constructs.CNull)

Aggregations

Construct (com.laytonsmith.core.constructs.Construct)42 CString (com.laytonsmith.core.constructs.CString)25 CArray (com.laytonsmith.core.constructs.CArray)23 IVariable (com.laytonsmith.core.constructs.IVariable)14 CInt (com.laytonsmith.core.constructs.CInt)11 CNull (com.laytonsmith.core.constructs.CNull)11 GlobalEnv (com.laytonsmith.core.environments.GlobalEnv)10 ConfigCompileException (com.laytonsmith.core.exceptions.ConfigCompileException)9 ConfigRuntimeException (com.laytonsmith.core.exceptions.ConfigRuntimeException)9 ArrayList (java.util.ArrayList)9 HashMap (java.util.HashMap)9 Variable (com.laytonsmith.core.constructs.Variable)8 CDouble (com.laytonsmith.core.constructs.CDouble)7 CommandHelperEnvironment (com.laytonsmith.core.environments.CommandHelperEnvironment)7 FunctionReturnException (com.laytonsmith.core.exceptions.FunctionReturnException)7 CBoolean (com.laytonsmith.core.constructs.CBoolean)6 Target (com.laytonsmith.core.constructs.Target)6 Environment (com.laytonsmith.core.environments.Environment)6 CRECastException (com.laytonsmith.core.exceptions.CRE.CRECastException)6 CREFormatException (com.laytonsmith.core.exceptions.CRE.CREFormatException)6