Search in sources :

Example 1 with Profiler

use of com.laytonsmith.core.profiler.Profiler in project CommandHelper by EngineHub.

the class Manager method start.

@SuppressWarnings("ResultOfObjectAllocationIgnored")
public static void start() throws IOException, DataSourceException, URISyntaxException, Profiles.InvalidProfileException {
    Implementation.useAbstractEnumThread(false);
    Implementation.forceServerType(Implementation.Type.BUKKIT);
    ConnectionMixinFactory.ConnectionMixinOptions options = new ConnectionMixinFactory.ConnectionMixinOptions();
    options.setWorkingDirectory(chDirectory);
    persistenceNetwork = new PersistenceNetwork(CommandHelperFileLocations.getDefault().getPersistenceConfig(), CommandHelperFileLocations.getDefault().getDefaultPersistenceDBFile().toURI(), options);
    Installer.Install(chDirectory);
    CHLog.initialize(chDirectory);
    profiler = new Profiler(CommandHelperFileLocations.getDefault().getProfilerConfigFile());
    gEnv = new GlobalEnv(new MethodScriptExecutionQueue("Manager", "default"), profiler, persistenceNetwork, chDirectory, new Profiles(MethodScriptFileLocations.getDefault().getProfilesFile()), new TaskManager());
    cls();
    pl("\n" + Static.Logo() + "\n\n" + Static.DataManagerLogo());
    pl("Starting the Data Manager...");
    try {
        Environment env = Environment.createEnvironment(gEnv, new CommandHelperEnvironment());
        MethodScriptCompiler.execute(MethodScriptCompiler.compile(MethodScriptCompiler.lex("player()", null, true)), env, null, null);
    } catch (ConfigCompileException | ConfigCompileGroupException ex) {
    }
    pl(GREEN + "Welcome to the CommandHelper " + CYAN + "Data Manager!");
    pl(BLINKON + RED + "Warning!" + BLINKOFF + YELLOW + " Be sure your server is not running before using this tool to make changes to your database!");
    pl("------------------------");
    boolean finished = false;
    do {
        pl(YELLOW + "What function would you like to run? Type \"help\" for a full list of options.");
        String input = prompt();
        pl();
        if (input.toLowerCase().startsWith("help")) {
            help(input.replaceFirst("help ?", "").toLowerCase().split(" "));
        } else if (input.equalsIgnoreCase("refactor")) {
            refactor();
        } else if (input.toLowerCase().startsWith("print")) {
            print(input.replaceFirst("print ?", "").toLowerCase().split(" "));
        } else if (input.equalsIgnoreCase("cleardb")) {
            cleardb();
        } else if (input.equalsIgnoreCase("edit")) {
            edit();
        } else if (input.equalsIgnoreCase("merge")) {
            merge();
        } else if (input.equalsIgnoreCase("interpreter")) {
            new Interpreter(null, System.getProperty("user.dir"));
        } else if (input.equalsIgnoreCase("hidden-keys")) {
            hiddenKeys();
        } else if (input.equalsIgnoreCase("exit")) {
            pl("Thanks for using the " + CYAN + BOLD + "Data Manager!" + reset());
            finished = true;
        } else {
            pl("I'm sorry, that's not a valid command. Here's the help:");
            help(new String[] {});
        }
    } while (finished == false);
    StreamUtils.GetSystemOut().println(TermColors.reset());
}
Also used : PersistenceNetwork(com.laytonsmith.persistence.PersistenceNetwork) ConfigCompileException(com.laytonsmith.core.exceptions.ConfigCompileException) TaskManager(com.laytonsmith.core.taskmanager.TaskManager) Profiler(com.laytonsmith.core.profiler.Profiler) Profiles(com.laytonsmith.core.Profiles) ConnectionMixinFactory(com.laytonsmith.persistence.io.ConnectionMixinFactory) MethodScriptExecutionQueue(com.laytonsmith.core.MethodScriptExecutionQueue) CommandHelperEnvironment(com.laytonsmith.core.environments.CommandHelperEnvironment) Environment(com.laytonsmith.core.environments.Environment) CommandHelperEnvironment(com.laytonsmith.core.environments.CommandHelperEnvironment) GlobalEnv(com.laytonsmith.core.environments.GlobalEnv) ConfigCompileGroupException(com.laytonsmith.core.exceptions.ConfigCompileGroupException)

Example 2 with Profiler

use of com.laytonsmith.core.profiler.Profiler in project CommandHelper by EngineHub.

the class AliasCore method reload.

/**
 * Loads the global alias file in from the file system. If a player is running the command, send a reference to
 * them, and they will see compile errors, otherwise, null.
 *
 * @param player
 * @param settings The argument list for the settings.
 * @param firstLoad Indicates that CH is loading
 */
public final void reload(MCPlayer player, String[] settings, boolean firstLoad) {
    ReloadOptions options;
    try {
        options = new ReloadOptions(settings);
    } catch (Exception ex) {
        if (player != null) {
            player.sendMessage(ex.getMessage());
        } else {
            StreamUtils.GetSystemOut().println(ex.getMessage());
        }
        return;
    }
    try {
        if (Prefs.AllowDynamicShell()) {
            CHLog.GetLogger().Log(CHLog.Tags.GENERAL, LogLevel.WARNING, "allow-dynamic-shell is set to true in " + CommandHelperFileLocations.getDefault().getProfilerConfigFile().getName() + " you should set this to false, except during development.", Target.UNKNOWN);
        }
        if (parent.profiler == null || options.reloadProfiler()) {
            parent.profiler = new Profiler(MethodScriptFileLocations.getDefault().getProfilerConfigFile());
        }
        ProfilePoint extensionPreReload = parent.profiler.start("Extension PreReloadAliases call", LogLevel.VERBOSE);
        try {
            // Allow new-style extensions know we are about to reload aliases.
            ExtensionManager.PreReloadAliases(options);
        } finally {
            extensionPreReload.stop();
        }
        ProfilePoint shutdownHooks = parent.profiler.start("Shutdown hooks call", LogLevel.VERBOSE);
        try {
            StaticLayer.GetConvertor().runShutdownHooks();
        } finally {
            shutdownHooks.stop();
        }
        if (!firstLoad && options.reloadExtensions()) {
            ProfilePoint extensionManagerShutdown = parent.profiler.start("Extension manager shutdown", LogLevel.VERBOSE);
            try {
                ExtensionManager.Shutdown();
            } finally {
                extensionManagerShutdown.stop();
            }
        }
        CHLog.initialize(MethodScriptFileLocations.getDefault().getConfigDirectory());
        // Clear out the data source cache
        DataSourceFactory.DisconnectAll();
        // PacketJumper.startup(); we're not using this yet
        if (options.reloadExtensions()) {
            ProfilePoint extensionManagerStartup = parent.profiler.start("Extension manager startup", LogLevel.VERBOSE);
            try {
                ExtensionManager.Startup();
            } finally {
                extensionManagerStartup.stop();
            }
        }
        CHLog.GetLogger().Log(CHLog.Tags.GENERAL, LogLevel.VERBOSE, "Scripts reloading...", Target.UNKNOWN);
        if (parent.persistenceNetwork == null || options.reloadPersistenceConfig()) {
            ProfilePoint persistenceConfigReload = parent.profiler.start("Reloading persistence configuration", LogLevel.VERBOSE);
            try {
                MemoryDataSource.ClearDatabases();
                ConnectionMixinFactory.ConnectionMixinOptions mixinOptions = new ConnectionMixinFactory.ConnectionMixinOptions();
                mixinOptions.setWorkingDirectory(MethodScriptFileLocations.getDefault().getConfigDirectory());
                parent.persistenceNetwork = new PersistenceNetwork(MethodScriptFileLocations.getDefault().getPersistenceConfig(), new URI("sqlite:/" + MethodScriptFileLocations.getDefault().getDefaultPersistenceDBFile().getCanonicalFile().toURI().getRawSchemeSpecificPart().replace('\\', '/')), mixinOptions);
            } finally {
                persistenceConfigReload.stop();
            }
        }
        try {
            parent.profiles = new Profiles(MethodScriptFileLocations.getDefault().getProfilesFile());
        } catch (IOException | Profiles.InvalidProfileException ex) {
            CHLog.GetLogger().e(CHLog.Tags.GENERAL, ex.getMessage(), Target.UNKNOWN);
            return;
        }
        GlobalEnv gEnv = new GlobalEnv(parent.executionQueue, parent.profiler, parent.persistenceNetwork, MethodScriptFileLocations.getDefault().getConfigDirectory(), parent.profiles, new TaskManager());
        gEnv.SetLabel(Static.GLOBAL_PERMISSION);
        if (options.reloadExecutionQueue()) {
            ProfilePoint stoppingExecutionQueue = parent.profiler.start("Stopping execution queues", LogLevel.VERBOSE);
            try {
                parent.executionQueue.stopAllNow();
            } finally {
                stoppingExecutionQueue.stop();
            }
        }
        CommandHelperEnvironment cEnv = new CommandHelperEnvironment();
        Environment env = Environment.createEnvironment(gEnv, cEnv);
        if (options.reloadGlobals()) {
            ProfilePoint clearingGlobals = parent.profiler.start("Clearing globals", LogLevel.VERBOSE);
            try {
                Globals.clear();
            } finally {
                clearingGlobals.stop();
            }
        }
        if (options.reloadTimeouts()) {
            ProfilePoint clearingTimeouts = parent.profiler.start("Clearing timeouts/intervals", LogLevel.VERBOSE);
            try {
                Scheduling.ClearScheduledRunners();
            } finally {
                clearingTimeouts.stop();
            }
        }
        if (!aliasConfig.exists()) {
            aliasConfig.getParentFile().mkdirs();
            aliasConfig.createNewFile();
            try {
                String samp_aliases = getStringResource(AliasCore.class.getResourceAsStream("/samp_aliases.txt"));
                // Because the sample config may have been written an a machine that isn't this type, replace all
                // line endings
                samp_aliases = samp_aliases.replaceAll("\n|\r\n", System.getProperty("line.separator"));
                file_put_contents(aliasConfig, samp_aliases, "o");
            } catch (Exception e) {
                logger.log(Level.WARNING, "CommandHelper: Could not write sample config file");
            }
        }
        if (!mainFile.exists()) {
            mainFile.getParentFile().mkdirs();
            mainFile.createNewFile();
            try {
                String samp_main = getStringResource(AliasCore.class.getResourceAsStream("/samp_main.txt"));
                samp_main = samp_main.replaceAll("\n|\r\n", System.getProperty("line.separator"));
                file_put_contents(mainFile, samp_main, "o");
            } catch (Exception e) {
                logger.log(Level.WARNING, "CommandHelper: Could not write sample main file");
            }
        }
        if (!Prefs.isInitialized()) {
            Prefs.init(prefFile);
        }
        if (options.reloadScripts()) {
            ProfilePoint unregisteringEvents = parent.profiler.start("Unregistering events", LogLevel.VERBOSE);
            try {
                EventUtils.UnregisterAll();
            } finally {
                unregisteringEvents.stop();
            }
            ProfilePoint runningExtensionHooks = parent.profiler.start("Running event hooks", LogLevel.VERBOSE);
            try {
                ExtensionManager.RunHooks();
            } finally {
                runningExtensionHooks.stop();
            }
            // Clear the include cache, so it re-pulls files
            IncludeCache.clearCache();
            // Close all channel messager channels registered by CH.
            Static.getServer().getMessenger().closeAllChannels();
            scripts = new ArrayList<Script>();
            LocalPackage localPackages = new LocalPackage();
            // Run the main file once
            String main = file_get_contents(mainFile.getAbsolutePath());
            localPackages.appendMS(main, mainFile);
            // get the file again
            String alias_config = file_get_contents(aliasConfig.getAbsolutePath());
            localPackages.appendMSA(alias_config, aliasConfig);
            File auto_include = new File(env.getEnv(GlobalEnv.class).GetRootFolder(), "auto_include.ms");
            if (auto_include.exists()) {
                localPackages.addAutoInclude(auto_include);
            }
            // Now that we've included the default files, search the local_packages directory
            GetAuxAliases(auxAliases, localPackages);
            autoIncludes = localPackages.getAutoIncludes();
            ProfilePoint compilerMS = parent.profiler.start("Compilation of MS files in Local Packages", LogLevel.VERBOSE);
            try {
                env.getEnv(CommandHelperEnvironment.class).SetCommandSender(Static.getServer().getConsole());
                MethodScriptCompiler.registerAutoIncludes(env, null);
                localPackages.compileMS(player, env);
            } finally {
                env.getEnv(CommandHelperEnvironment.class).SetCommandSender(null);
                compilerMS.stop();
            }
            ProfilePoint compilerMSA = parent.profiler.start("Compilation of MSA files in Local Packages", LogLevel.VERBOSE);
            try {
                localPackages.compileMSA(scripts, player);
            } finally {
                compilerMSA.stop();
            }
        }
    } catch (IOException ex) {
        logger.log(Level.SEVERE, "[CommandHelper]: Path to config file is not correct/accessable. Please" + " check the location and try loading the plugin again.");
    } catch (Throwable t) {
        t.printStackTrace();
    }
    ProfilePoint postReloadAliases = parent.profiler.start("Extension manager post reload aliases", LogLevel.VERBOSE);
    try {
        ExtensionManager.PostReloadAliases();
    } finally {
        postReloadAliases.stop();
    }
}
Also used : PersistenceNetwork(com.laytonsmith.persistence.PersistenceNetwork) IOException(java.io.IOException) URI(java.net.URI) ProgramFlowManipulationException(com.laytonsmith.core.exceptions.ProgramFlowManipulationException) CancelCommandException(com.laytonsmith.core.exceptions.CancelCommandException) ConfigCompileException(com.laytonsmith.core.exceptions.ConfigCompileException) ZipException(java.util.zip.ZipException) IOException(java.io.IOException) ConfigRuntimeException(com.laytonsmith.core.exceptions.ConfigRuntimeException) ConfigCompileGroupException(com.laytonsmith.core.exceptions.ConfigCompileGroupException) ProfilePoint(com.laytonsmith.core.profiler.ProfilePoint) TaskManager(com.laytonsmith.core.taskmanager.TaskManager) Profiler(com.laytonsmith.core.profiler.Profiler) ConnectionMixinFactory(com.laytonsmith.persistence.io.ConnectionMixinFactory) CommandHelperEnvironment(com.laytonsmith.core.environments.CommandHelperEnvironment) CommandHelperEnvironment(com.laytonsmith.core.environments.CommandHelperEnvironment) Environment(com.laytonsmith.core.environments.Environment) GlobalEnv(com.laytonsmith.core.environments.GlobalEnv) ZipFile(java.util.zip.ZipFile) File(java.io.File)

Example 3 with Profiler

use of com.laytonsmith.core.profiler.Profiler in project CommandHelper by EngineHub.

the class Static method GenerateStandaloneEnvironment.

/**
 * Generates a new environment, assuming that the jar has a folder next to it named CommandHelper, and that folder
 * is the root.
 *
 * @return
 * @throws IOException
 * @throws DataSourceException
 * @throws URISyntaxException
 */
public static Environment GenerateStandaloneEnvironment(boolean install) throws IOException, DataSourceException, URISyntaxException, Profiles.InvalidProfileException {
    File platformFolder = MethodScriptFileLocations.getDefault().getConfigDirectory();
    if (install) {
        Installer.Install(platformFolder);
    }
    ConnectionMixinFactory.ConnectionMixinOptions options = new ConnectionMixinFactory.ConnectionMixinOptions();
    options.setWorkingDirectory(platformFolder);
    PersistenceNetwork persistenceNetwork = new PersistenceNetwork(MethodScriptFileLocations.getDefault().getPersistenceConfig(), new URI(URLEncoder.encode("sqlite://" + new File(platformFolder, "persistence.db").getCanonicalPath().replace('\\', '/'), "UTF-8")), options);
    GlobalEnv gEnv = new GlobalEnv(new MethodScriptExecutionQueue("MethodScriptExecutionQueue", "default"), new Profiler(MethodScriptFileLocations.getDefault().getProfilerConfigFile()), persistenceNetwork, platformFolder, new Profiles(MethodScriptFileLocations.getDefault().getProfilesFile()), new TaskManager());
    gEnv.SetLabel(GLOBAL_PERMISSION);
    return Environment.createEnvironment(gEnv, new CommandHelperEnvironment());
}
Also used : TaskManager(com.laytonsmith.core.taskmanager.TaskManager) Profiler(com.laytonsmith.core.profiler.Profiler) ConnectionMixinFactory(com.laytonsmith.persistence.io.ConnectionMixinFactory) CommandHelperEnvironment(com.laytonsmith.core.environments.CommandHelperEnvironment) PersistenceNetwork(com.laytonsmith.persistence.PersistenceNetwork) GlobalEnv(com.laytonsmith.core.environments.GlobalEnv) File(java.io.File) URI(java.net.URI)

Aggregations

CommandHelperEnvironment (com.laytonsmith.core.environments.CommandHelperEnvironment)3 GlobalEnv (com.laytonsmith.core.environments.GlobalEnv)3 Profiler (com.laytonsmith.core.profiler.Profiler)3 TaskManager (com.laytonsmith.core.taskmanager.TaskManager)3 PersistenceNetwork (com.laytonsmith.persistence.PersistenceNetwork)3 ConnectionMixinFactory (com.laytonsmith.persistence.io.ConnectionMixinFactory)3 Environment (com.laytonsmith.core.environments.Environment)2 ConfigCompileException (com.laytonsmith.core.exceptions.ConfigCompileException)2 ConfigCompileGroupException (com.laytonsmith.core.exceptions.ConfigCompileGroupException)2 File (java.io.File)2 URI (java.net.URI)2 MethodScriptExecutionQueue (com.laytonsmith.core.MethodScriptExecutionQueue)1 Profiles (com.laytonsmith.core.Profiles)1 CancelCommandException (com.laytonsmith.core.exceptions.CancelCommandException)1 ConfigRuntimeException (com.laytonsmith.core.exceptions.ConfigRuntimeException)1 ProgramFlowManipulationException (com.laytonsmith.core.exceptions.ProgramFlowManipulationException)1 ProfilePoint (com.laytonsmith.core.profiler.ProfilePoint)1 IOException (java.io.IOException)1 ZipException (java.util.zip.ZipException)1 ZipFile (java.util.zip.ZipFile)1