+ Added lua hot reload functionality

This commit is contained in:
Matthew Cech
2019-07-02 23:38:10 -07:00
parent 2ac0ff99ef
commit 5c21806db1
6 changed files with 63 additions and 16 deletions
+6 -4
View File
@@ -26,12 +26,14 @@ public class Plugin
public Plugin(Path filepath)
{
this.filepath = filepath;
StringBuilder contentBuilder = new StringBuilder();
reRead();
}
public void reRead()
{
try
{
StringBuilder contentBuilder = new StringBuilder();
Stream<String> stream = Files.lines(filepath, StandardCharsets.UTF_8);
stream.forEach(str -> contentBuilder.append(str).append("\n"));
stream.close();
+46 -9
View File
@@ -1,33 +1,31 @@
package core.lua;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;
import dataStructures.KittyUser;
import utils.GlobalLog;
import utils.LogFilter;
import utils.io.DirectoryMonitor;
// Reads in, handles, and manipulates plugins. Plugins are loaded in the order they appear in the folder.
public class PluginManager
{
// Variables
public final String pluginFolder;
public ArrayList<Plugin> plugins;
public DirectoryMonitor directoryMonitor;
// Constructor
public PluginManager(String folder)
{
this.pluginFolder = folder;
this.directoryMonitor = new DirectoryMonitor(folder);
plugins = new ArrayList<Plugin>();
try
{
try (Stream<Path> paths = Files.walk(Paths.get(this.pluginFolder)))
{
paths.filter(Files::isRegularFile).forEach((path)->{ addPlugin(path); });
}
directoryMonitor.getCurrentFiles().forEach((file) -> addPlugin(file.path));
}
catch(Exception e)
{
@@ -35,6 +33,43 @@ public class PluginManager
}
}
public void update()
{
directoryMonitor.update(
(addedFile) ->
{
addPlugin(addedFile.path);
GlobalLog.log(LogFilter.Plugin, "Found new plugin at " + addedFile.path);
},
(updatedFile) ->
{
for(Plugin plugin : plugins)
{
if(plugin.filepath.toString().equalsIgnoreCase(updatedFile.path.toString()))
{
plugin.reRead();
break;
}
}
GlobalLog.log(LogFilter.Plugin, "A plugin was updated " + updatedFile.path);
},
(deletedFile) ->
{
for(int i = plugins.size() - 1; i >= 0; i--)
{
Plugin plugin = plugins.get(i);
if(plugin.filepath.toString().equalsIgnoreCase(deletedFile.path.toString()))
plugins.remove(i);
}
GlobalLog.log(LogFilter.Plugin, "Plugin deleted at " + deletedFile.path);
});
}
// Registers a plugin based on the path
public void addPlugin(Path path)
{
plugins.add(new Plugin(path));
@@ -66,6 +101,8 @@ public class PluginManager
public void printAll()
{
for(int i = 0; i < plugins.size(); ++i)
{
PluginLog.log(plugins.get(i).contents.toString());
}
}
}
+4 -2
View File
@@ -86,7 +86,8 @@ public class Main extends ListenerAdapter
RPManager.instance.addLine(channel, user, input);
// Run any pre-emptive upkeep we need to
Superintendent.perCommandUpkeepPre();
if(!Superintendent.perCommandUpkeepPre(pluginManager))
return;
// Attempt to spin up a command. If the command doesn't exist.
// Run plugins right before invoking the commands but after all other setup.
@@ -111,6 +112,7 @@ public class Main extends ListenerAdapter
}
// Run any upkeep in post we need to
Superintendent.perCommandUpkeepPost(kittyCore, databaseManager);
if(!Superintendent.perCommandUpkeepPost(kittyCore, databaseManager))
return;
}
}
+4 -1
View File
@@ -6,6 +6,7 @@ import core.Config;
import core.DatabaseManager;
import core.RPManager;
import core.Stats;
import core.lua.PluginManager;
import dataStructures.KittyChannel;
import dataStructures.KittyCore;
import dataStructures.KittyGuild;
@@ -71,11 +72,13 @@ public class Superintendent
// Only called once per command. Good for lazily updating.
// Happens just before the command / plugin runs.
public static boolean perCommandUpkeepPre()
public static boolean perCommandUpkeepPre(PluginManager pluginManager)
{
// Upkeep the config file monitoring
Config.instance.upkeep();
pluginManager.update();
// Return if we succeeded or not. At this time, we always succeed.
return true;
}