Search in sources :

Example 1 with ZenModule

use of stanhebben.zenscript.ZenModule in project CraftTweaker by CraftTweaker.

the class CrTTweaker method loadScript.

@Override
public boolean loadScript(boolean isSyntaxCommand, String loaderName) {
    CraftTweakerAPI.logInfo("Loading scripts");
    CRT_LOADING_STARTED_EVENT_EVENT_LIST.publish(new CrTLoadingStartedEvent(loaderName, isSyntaxCommand, networkSide));
    preprocessorManager.clean();
    Set<String> executed = new HashSet<>();
    boolean loadSuccessful = true;
    List<ScriptFile> scriptFiles = collectScriptFiles(isSyntaxCommand);
    // preprocessor magic
    for (ScriptFile scriptFile : scriptFiles) {
        scriptFile.addAll(preprocessorManager.checkFileForPreprocessors(scriptFile));
    }
    scriptFiles.sort(PreprocessorManager.SCRIPT_FILE_COMPARATOR);
    Map<String, byte[]> classes = new HashMap<>();
    IEnvironmentGlobal environmentGlobal = GlobalRegistry.makeGlobalEnvironment(classes);
    // ZS magic
    long totalTime = System.currentTimeMillis();
    for (ScriptFile scriptFile : scriptFiles) {
        if (!scriptFile.getLoaderName().equals(loaderName) && !isSyntaxCommand) {
            CraftTweakerAPI.logInfo(getTweakerDescriptor(loaderName) + ": Skipping file " + scriptFile + " as we are currently loading with a different loader");
            continue;
        }
        if (!scriptFile.shouldBeLoadedOn(networkSide)) {
            CraftTweakerAPI.logInfo(getTweakerDescriptor(loaderName) + ": Skipping file " + scriptFile + " as we are on the wrong side of the Network");
            continue;
        }
        if (!executed.contains(scriptFile.getEffectiveName())) {
            // long time = System.currentTimeMillis();
            executed.add(scriptFile.getEffectiveName());
            CraftTweakerAPI.logInfo(getTweakerDescriptor(loaderName) + ": Loading Script: " + scriptFile);
            ZenParsedFile zenParsedFile = null;
            String filename = scriptFile.getEffectiveName();
            String className = extractClassName(filename);
            CRT_LOADING_SCRIPT_PRE_EVENT_LIST.publish(new CrTLoadingScriptEventPre(filename));
            Reader reader = null;
            try {
                reader = new InputStreamReader(new BufferedInputStream(scriptFile.open()), "UTF-8");
                CrTScriptLoadEvent loadEvent = new CrTScriptLoadEvent(scriptFile);
                preprocessorManager.postLoadEvent(loadEvent);
                // blocks the parsing of the script
                if (scriptFile.isParsingBlocked())
                    continue;
                ZenTokener parser = new ZenTokener(reader, environmentGlobal.getEnvironment(), filename, scriptFile.areBracketErrorsIgnored());
                zenParsedFile = new ZenParsedFile(filename, className, parser, environmentGlobal);
            } catch (IOException ex) {
                CraftTweakerAPI.logError(getTweakerDescriptor(loaderName) + ": Could not load script " + scriptFile + ": " + ex.getMessage());
                loadSuccessful = false;
            } catch (ParseException ex) {
                CraftTweakerAPI.logError(getTweakerDescriptor(loaderName) + ": Error parsing " + ex.getFile().getFileName() + ":" + ex.getLine() + " -- " + ex.getExplanation());
                loadSuccessful = false;
            } catch (Exception ex) {
                CraftTweakerAPI.logError(getTweakerDescriptor(loaderName) + ": Error loading " + scriptFile + ": " + ex.toString(), ex);
                loadSuccessful = false;
            } finally {
                if (reader != null) {
                    try {
                        reader.close();
                    } catch (IOException ignored) {
                    }
                }
            }
            try {
                // Stops if the compile is disabled
                if (zenParsedFile == null || scriptFile.isCompileBlocked())
                    continue;
                compileScripts(className, Collections.singletonList(zenParsedFile), environmentGlobal, scriptFile.isDebugEnabled() || DEBUG);
                // stops if the execution is disabled
                if (scriptFile.isExecutionBlocked() || isSyntaxCommand)
                    continue;
                ZenModule module = new ZenModule(classes, CraftTweakerAPI.class.getClassLoader());
                Runnable runnable = module.getMain();
                if (runnable != null)
                    runnable.run();
            } catch (Throwable ex) {
                CraftTweakerAPI.logError("[" + loaderName + "]: Error executing " + scriptFile + ": " + ex.getMessage(), ex);
            }
            CRT_LOADING_SCRIPT_POST_EVENT_LIST.publish(new CrTLoadingScriptEventPost(filename));
        // CraftTweakerAPI.logInfo("Completed file: " + filename +" in: " + (System.currentTimeMillis() - time) + "ms");
        }
    }
    CraftTweakerAPI.logInfo("Completed script loading in: " + (System.currentTimeMillis() - totalTime) + "ms");
    return loadSuccessful;
}
Also used : ZenModule(stanhebben.zenscript.ZenModule) ParseException(stanhebben.zenscript.parser.ParseException) IEnvironmentGlobal(stanhebben.zenscript.compiler.IEnvironmentGlobal) ParseException(stanhebben.zenscript.parser.ParseException)

Aggregations

ZenModule (stanhebben.zenscript.ZenModule)1 IEnvironmentGlobal (stanhebben.zenscript.compiler.IEnvironmentGlobal)1 ParseException (stanhebben.zenscript.parser.ParseException)1