Search in sources :

Example 6 with ProfilePoint

use of com.laytonsmith.core.profiler.ProfilePoint 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)

Aggregations

GlobalEnv (com.laytonsmith.core.environments.GlobalEnv)6 ProfilePoint (com.laytonsmith.core.profiler.ProfilePoint)6 CancelCommandException (com.laytonsmith.core.exceptions.CancelCommandException)5 ConfigRuntimeException (com.laytonsmith.core.exceptions.ConfigRuntimeException)5 CommandHelperEnvironment (com.laytonsmith.core.environments.CommandHelperEnvironment)4 Environment (com.laytonsmith.core.environments.Environment)4 ProgramFlowManipulationException (com.laytonsmith.core.exceptions.ProgramFlowManipulationException)4 CArray (com.laytonsmith.core.constructs.CArray)3 CString (com.laytonsmith.core.constructs.CString)3 Construct (com.laytonsmith.core.constructs.Construct)3 IVariable (com.laytonsmith.core.constructs.IVariable)3 ConfigCompileException (com.laytonsmith.core.exceptions.ConfigCompileException)3 ConfigCompileGroupException (com.laytonsmith.core.exceptions.ConfigCompileGroupException)3 MCPlayer (com.laytonsmith.abstraction.MCPlayer)2 InvalidEnvironmentException (com.laytonsmith.core.environments.InvalidEnvironmentException)2 FunctionReturnException (com.laytonsmith.core.exceptions.FunctionReturnException)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 MutableObject (com.laytonsmith.PureUtilities.Common.MutableObject)1