Search in sources :

Example 1 with UpgradeLog

use of com.laytonsmith.core.UpgradeLog in project CommandHelper by EngineHub.

the class CommandHelperPlugin method onLoad.

@Override
public void onLoad() {
    Implementation.setServerType(Implementation.Type.BUKKIT);
    CommandHelperFileLocations.setDefault(new CommandHelperFileLocations());
    CommandHelperFileLocations.getDefault().getCacheDirectory().mkdirs();
    CommandHelperFileLocations.getDefault().getPreferencesDirectory().mkdirs();
    UpgradeLog upgradeLog = new UpgradeLog(CommandHelperFileLocations.getDefault().getUpgradeLogFile());
    upgradeLog.addUpgradeTask(new UpgradeLog.UpgradeTask() {

        String version = null;

        @Override
        public boolean doRun() {
            try {
                version = "versionUpgrade-" + Main.loadSelfVersion();
                return !hasBreadcrumb(version);
            } catch (Exception ex) {
                Logger.getLogger(CommandHelperPlugin.class.getName()).log(Level.SEVERE, null, ex);
                return false;
            }
        }

        @Override
        public void run() {
            leaveBreadcrumb(version);
        }
    });
    upgradeLog.addUpgradeTask(new UpgradeLog.UpgradeTask() {

        File oldPreferences = new File(CommandHelperFileLocations.getDefault().getConfigDirectory(), "preferences.txt");

        @Override
        public boolean doRun() {
            return oldPreferences.exists() && !CommandHelperFileLocations.getDefault().getPreferencesFile().exists();
        }

        @Override
        public void run() {
            try {
                Prefs.init(oldPreferences);
                Prefs.SetColors();
                Logger.getLogger("Minecraft").log(Level.INFO, TermColors.YELLOW + "[" + Implementation.GetServerType().getBranding() + "] Old preferences.txt file detected. Moving preferences.txt to preferences.ini." + TermColors.reset());
                FileUtil.copy(oldPreferences, CommandHelperFileLocations.getDefault().getPreferencesFile(), true);
                oldPreferences.deleteOnExit();
            } catch (IOException ex) {
                Logger.getLogger(CommandHelperPlugin.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    });
    upgradeLog.addUpgradeTask(new UpgradeLog.UpgradeTask() {

        File cd = CommandHelperFileLocations.getDefault().getConfigDirectory();

        private final String breadcrumb = "move-preference-files-v1.0";

        @Override
        public boolean doRun() {
            return !hasBreadcrumb(breadcrumb) && new File(cd, "preferences.ini").exists();
        }

        @Override
        public void run() {
            // We need to move the following files:
            // 1. persistance.config to prefs/persistence.ini (note the correct spelling)
            // 2. preferences.ini to prefs/preferences.ini
            // 3. profiler.config to prefs/profiler.ini
            // 4. sql-profiles.xml to prefs/sql-profiles.xml
            // 5. We are not moving loggerPreferences.txt, instead just deleting it,
            // because the defaults have changed. Most people aren't using this feature
            // anyways. (The new one will write itself out upon installation.)
            // Other than the config/prefs directory, we are hardcoding all the values, so
            // we know they are correct (for old values). Any errors will be reported, but will not
            // stop the entire process.
            CommandHelperFileLocations p = CommandHelperFileLocations.getDefault();
            try {
                FileUtil.move(new File(cd, "persistance.config"), p.getPersistenceConfig());
            } catch (IOException ex) {
                Logger.getLogger(CommandHelperPlugin.class.getName()).log(Level.SEVERE, null, ex);
            }
            try {
                FileUtil.move(new File(cd, "preferences.ini"), p.getPreferencesFile());
            } catch (IOException ex) {
                Logger.getLogger(CommandHelperPlugin.class.getName()).log(Level.SEVERE, null, ex);
            }
            try {
                FileUtil.move(new File(cd, "profiler.config"), p.getProfilerConfigFile());
            } catch (IOException ex) {
                Logger.getLogger(CommandHelperPlugin.class.getName()).log(Level.SEVERE, null, ex);
            }
            try {
                FileUtil.move(new File(cd, "sql-profiles.xml"), p.getProfilesFile());
            } catch (IOException ex) {
                Logger.getLogger(CommandHelperPlugin.class.getName()).log(Level.SEVERE, null, ex);
            }
            new File(cd, "logs/debug/loggerPreferences.txt").delete();
            leaveBreadcrumb(breadcrumb);
            StreamUtils.GetSystemOut().println("CommandHelper: Your preferences files have all been relocated to " + p.getPreferencesDirectory());
            StreamUtils.GetSystemOut().println("CommandHelper: The loggerPreferences.txt file has been deleted and re-created, as the defaults have changed.");
        }
    });
    // Renames the sql-profiles.xml file to the new name.
    upgradeLog.addUpgradeTask(new UpgradeLog.UpgradeTask() {

        // This should never change
        private final File oldProfilesFile = new File(MethodScriptFileLocations.getDefault().getPreferencesDirectory(), "sql-profiles.xml");

        @Override
        public boolean doRun() {
            return oldProfilesFile.exists();
        }

        @Override
        public void run() {
            try {
                FileUtil.move(oldProfilesFile, MethodScriptFileLocations.getDefault().getProfilesFile());
                StreamUtils.GetSystemOut().println("CommandHelper: sql-profiles.xml has been renamed to " + MethodScriptFileLocations.getDefault().getProfilesFile().getName());
            } catch (IOException ex) {
                Logger.getLogger(CommandHelperPlugin.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    });
    try {
        upgradeLog.runTasks();
    } catch (IOException ex) {
        Logger.getLogger(CommandHelperPlugin.class.getName()).log(Level.SEVERE, null, ex);
    }
    try {
        Prefs.init(CommandHelperFileLocations.getDefault().getPreferencesFile());
    } catch (IOException ex) {
        Logger.getLogger(CommandHelperPlugin.class.getName()).log(Level.SEVERE, null, ex);
    }
    Prefs.SetColors();
    CHLog.initialize(CommandHelperFileLocations.getDefault().getConfigDirectory());
    Installer.Install(CommandHelperFileLocations.getDefault().getConfigDirectory());
    if (new SimpleVersion(System.getProperty("java.version")).lt(new SimpleVersion("1.8"))) {
        CHLog.GetLogger().w(CHLog.Tags.GENERAL, "You appear to be running a version of Java older than Java 8. You should have plans" + " to upgrade at some point, as " + Implementation.GetServerType().getBranding() + " may require it at some point.", Target.UNKNOWN);
    }
    self = this;
    ClassDiscoveryCache cdc = new ClassDiscoveryCache(CommandHelperFileLocations.getDefault().getCacheDirectory());
    cdc.setLogger(Logger.getLogger(CommandHelperPlugin.class.getName()));
    ClassDiscovery.getDefaultInstance().setClassDiscoveryCache(cdc);
    ClassDiscovery.getDefaultInstance().addDiscoveryLocation(ClassDiscovery.GetClassContainer(CommandHelperPlugin.class));
    StreamUtils.GetSystemOut().println("[CommandHelper] Running initial class discovery," + " this will probably take a few seconds...");
    StreamUtils.GetSystemOut().println("[CommandHelper] Loading extensions in the background...");
    loadingThread = new Thread("extensionloader") {

        @Override
        public void run() {
            ExtensionManager.AddDiscoveryLocation(CommandHelperFileLocations.getDefault().getExtensionsDirectory());
            if (OSUtils.GetOS() == OSUtils.OS.WINDOWS) {
                // Using StreamUtils.GetSystemOut() here instead of the logger as the logger doesn't
                // immediately print to the console.
                StreamUtils.GetSystemOut().println("[CommandHelper] Caching extensions...");
                ExtensionManager.Cache(CommandHelperFileLocations.getDefault().getExtensionCacheDirectory());
                StreamUtils.GetSystemOut().println("[CommandHelper] Extension caching complete.");
            }
            ExtensionManager.Initialize(ClassDiscovery.getDefaultInstance());
            StreamUtils.GetSystemOut().println("[CommandHelper] Extension loading complete.");
        }
    };
    loadingThread.start();
}
Also used : UpgradeLog(com.laytonsmith.core.UpgradeLog) SimpleVersion(com.laytonsmith.PureUtilities.SimpleVersion) IOException(java.io.IOException) File(java.io.File) InvocationTargetException(java.lang.reflect.InvocationTargetException) EventException(org.bukkit.event.EventException) IOException(java.io.IOException) ClassDiscoveryCache(com.laytonsmith.PureUtilities.ClassLoading.ClassDiscoveryCache)

Aggregations

ClassDiscoveryCache (com.laytonsmith.PureUtilities.ClassLoading.ClassDiscoveryCache)1 SimpleVersion (com.laytonsmith.PureUtilities.SimpleVersion)1 UpgradeLog (com.laytonsmith.core.UpgradeLog)1 File (java.io.File)1 IOException (java.io.IOException)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 EventException (org.bukkit.event.EventException)1