Exemplo n.º 1
0
        public static ModuleV1[] LoadModules(Config config, ChatboxConnector connector)
        {
            var ret = new List <ModuleV1>();

            foreach (var moduleConfig in config.Modules)
            {
                // load assembly
                var ass = Assembly.Load(moduleConfig.Assembly);
                if (ass == null)
                {
                    Logger.ErrorFormat("module {0}: failed to load assembly {1}", moduleConfig.ModuleClass, moduleConfig.Assembly);
                    continue;
                }

                // get the module class
                var cls = ass.GetType(moduleConfig.ModuleClass);
                if (cls == null)
                {
                    Logger.ErrorFormat("module {0}: failed to load module class", moduleConfig.ModuleClass);
                    continue;
                }

                // find the right constructor
                var ctor = cls.GetConstructor(
                    BindingFlags.Public | BindingFlags.Instance,
                    null,
                    new [] { typeof(ChatboxConnector), typeof(JObject) },
                    null
                    );
                if (ctor == null)
                {
                    Logger.ErrorFormat("module {0}: failed to find correct constructor", moduleConfig.ModuleClass);
                    continue;
                }

                Logger.DebugFormat("loading module {0}...", moduleConfig.ModuleClass);

                // invoke it
                var moduleObject = ctor.Invoke(new object[] { connector, moduleConfig.Config });
                var module       = moduleObject as ModuleV1;

                if (module == null)
                {
                    Logger.ErrorFormat("module {0}: module instance is not an instance of ModuleV1", moduleConfig.ModuleClass);

                    // attempt cleanup
                    var moduleDispo = moduleObject as IDisposable;
                    if (moduleDispo != null)
                    {
                        moduleDispo.Dispose();
                    }

                    continue;
                }

                ret.Add(module);
            }

            return(ret.ToArray());
        }
Exemplo n.º 2
0
 public ModuleV1(ChatboxConnector connector)
 {
     Connector = connector;
     Connector.MessageUpdated += MessageUpdated;
 }