Class: ModSpox::PluginManager
- Inherits:
-
Object
- Object
- ModSpox::PluginManager
- Defined in:
- lib/mod_spox/PluginManager.rb
Defined Under Namespace
Classes: PluginFileNotFound, PluginMissing
Instance Attribute Summary collapse
-
#plugins ⇒ Object
readonly
Hash of plugins.
Instance Method Summary collapse
-
#destroy_plugins ⇒ Object
Destroys plugins.
-
#initialize(pipeline) ⇒ PluginManager
constructor
- pipeline
-
Pipeline for messages Create new PluginManager.
-
#load_plugin(message) ⇒ Object
- message
-
Messages::Internal::PluginLoadRequest Loads a plugin.
-
#plugin_request(message) ⇒ Object
- message
-
Messages::Internal::PluginRequest Returns a plugin to requesting object.
-
#reload_plugins(message = nil) ⇒ Object
- message
-
Messages::Internal::PluginReload Destroys and reinitializes plugins.
-
#send_modules(message) ⇒ Object
- message
-
Messages::Internal::PluginModuleRequest Sends the plugins module to the requester.
-
#unload_plugin(message) ⇒ Object
- message
-
Messages::Internal::PluginUnloadRequest Unloads a plugin.
Constructor Details
#initialize(pipeline) ⇒ PluginManager
- pipeline
-
Pipeline for messages
Create new PluginManager
17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/mod_spox/PluginManager.rb', line 17 def initialize(pipeline) @plugins = Hash.new @pipeline = pipeline @pipeline.hook(self, :load_plugin, :Internal_PluginLoadRequest) @pipeline.hook(self, :unload_plugin, :Internal_PluginUnloadRequest) @pipeline.hook(self, :reload_plugins, :Internal_PluginReload) @pipeline.hook(self, :send_modules, :Internal_PluginModuleRequest) @pipeline.hook(self, :plugin_request, :Internal_PluginRequest) @plugins_module = Module.new @plugin_lock = Mutex.new load_plugins end |
Instance Attribute Details
#plugins ⇒ Object (readonly)
Hash of plugins. Defined by class name symbol (i.e. Trivia class: plugins)
13 14 15 |
# File 'lib/mod_spox/PluginManager.rb', line 13 def plugins @plugins end |
Instance Method Details
#destroy_plugins ⇒ Object
Destroys plugins
49 50 51 |
# File 'lib/mod_spox/PluginManager.rb', line 49 def destroy_plugins unload_plugins end |
#load_plugin(message) ⇒ Object
- message
-
Messages::Internal::PluginLoadRequest
Loads a plugin
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/mod_spox/PluginManager.rb', line 55 def load_plugin() begin path = !.name ? "#{BotConfig[:userpluginpath]}/#{message.path.gsub(/^.+\//, '')}" : "#{BotConfig[:userpluginpath]}/#{message.name}" begin File.symlink(.path, path) rescue NotImplementedError => boom FileUtils.copy(.path, path) end do_load(path) @pipeline << Messages::Internal::PluginLoadResponse.new(.requester, true) Logger.log("Loaded new plugin: #{message.path}", 10) rescue Object => boom Logger.log("Failed to load plugin: #{message.path} Reason: #{boom}", 10) @pipeline << Messages::Internal::PluginLoadResponse.new(.requester, false) end @pipeline << Messages::Internal::SignaturesUpdate.new end |
#plugin_request(message) ⇒ Object
- message
-
Messages::Internal::PluginRequest
Returns a plugin to requesting object
101 102 103 104 105 106 107 108 |
# File 'lib/mod_spox/PluginManager.rb', line 101 def plugin_request() if(@plugins.has_key?(.plugin)) response = Messages::Internal::PluginResponse.new(.requester, @plugins[.plugin]) else response = Messages::Internal::PluginResponse.new(.requester, nil) end @pipeline << response end |
#reload_plugins(message = nil) ⇒ Object
- message
-
Messages::Internal::PluginReload
Destroys and reinitializes plugins
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/mod_spox/PluginManager.rb', line 32 def reload_plugins(=nil) @plugin_lock.synchronize do if(.fresh && .stale) do_unload(.stale) FileUtils.remove_file(.stale) FileUtils.copy(.fresh, BotConfig[:userpluginpath]) do_load(.stale) Logger.log("Completed reload of plugin: #{message.stale}") else unload_plugins load_plugins end @pipeline << Messages::Internal::SignaturesUpdate.new end end |
#send_modules(message) ⇒ Object
- message
-
Messages::Internal::PluginModuleRequest
Sends the plugins module to the requester
95 96 97 |
# File 'lib/mod_spox/PluginManager.rb', line 95 def send_modules() @pipeline << Messages::Internal::PluginModuleResponse.new(.requester, @plugins_module) end |
#unload_plugin(message) ⇒ Object
- message
-
Messages::Internal::PluginUnloadRequest
Unloads a plugin
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/mod_spox/PluginManager.rb', line 75 def unload_plugin() begin do_unload(.path) unless(File.symlink?(.path)) unless(.name.nil?) FileUtils.copy(.path, "#{BotConfig[:userpluginpath]}/#{message.name}") end end File.delete(.path) @pipeline << Messages::Internal::PluginUnloadResponse.new(.requester, true) Logger.log("Unloaded plugin: #{message.path}", 10) rescue Object => boom Logger.log("Failed to unload plugin: #{message.path} Reason: #{boom}", 10) @pipeline << Messages::Internal::PluginUnloadResponse.new(.requester, false) end @pipeline << Messages::Internal::SignaturesUpdate.new end |