]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/commitdiff
core: sets plugin_path to loaded plugins
authorMatthias Hecker <mail@apoc.cc>
Thu, 16 Apr 2020 21:48:42 +0000 (23:48 +0200)
committerMatthias Hecker <mail@apoc.cc>
Thu, 16 Apr 2020 21:48:42 +0000 (23:48 +0200)
While loading a plugin in the manager using #load_botmodule_file this
will remember the directory of the file that is being loaded in
@next_plugin_path. In the plugin/BotModule initialization we set the
path in plugin_path of the plugin.

This was the only solution I could find to get this information in the
plugin. It is useful because this allows the plugin to know in which
directory it is located, it can then easily access data files stored
alongside the plugin.

Some built-in plugins (see #42) use the data/rbot/templates to copy
data files on bot load (see #repopulate_botclass_directory) into the
profile directory (~/.rbot/).

lib/rbot/plugins.rb

index 9589b202d46487fe811749a2a3d2a876dcf5e903..76c76ae199467d7537e90cd213b3807b89bbb45f 100644 (file)
@@ -161,6 +161,9 @@ module Plugins
     # the message map handler
     attr_reader :handler
 
+    # the directory in which the plugin is located
+    attr_reader :plugin_path
+
     # Initialise your bot module. Always call super if you override this method,
     # as important variables are set up for you:
     #
@@ -191,6 +194,7 @@ module Plugins
       @registry = @bot.registry_factory.create(@bot.path, self.class.to_s.gsub(/^.*::/, ''))
 
       @manager.add_botmodule(self)
+      @plugin_path = @manager.next_plugin_path
       if self.respond_to?('set_language')
         self.set_language(@bot.lang.language)
       end
@@ -413,6 +417,7 @@ module Plugins
 
     attr_reader :core_module_dirs
     attr_reader :plugin_dirs
+    attr_reader :next_plugin_path
 
     # This is the list of patterns commonly delegated to plugins.
     # A fast delegation lookup is enabled for them.
@@ -610,8 +615,14 @@ module Plugins
       begin
         plugin_string = IO.read(fname)
         debug "loading #{desc}#{fname}"
+
+        # set path of the plugin that will be loaded next (see BotModule#initialize)
+        @next_plugin_path = File.dirname fname
+
         plugin_module.module_eval(plugin_string, fname)
 
+        @next_plugin_path = nil
+
         return :loaded
       rescue Exception => err
         # rescue TimeoutError, StandardError, NameError, LoadError, SyntaxError => err