Exemplo n.º 1
0
        public static void LoadModules(string moduleDefinitionFile)
        {
            string directoryName = Path.GetDirectoryName(Assembly.GetCallingAssembly().Location);

            string moduleDefinitionFilePath = Path.Combine(directoryName, moduleDefinitionFile);

            XmlDocument document = new XmlDocument();

            document.Load(moduleDefinitionFilePath);

            XmlNamespaceManager namespaceManager = new XmlNamespaceManager(document.NameTable);

            namespaceManager.AddNamespace("home", "MyHomeUnity");

            XmlNodeList nodeList = document.SelectNodes("/home:ModuleCatalog/home:ModuleInfo", namespaceManager);

            if (nodeList == null)
            {
                return;
            }

            ClassFactoryRegister classFactoryRegister = new ClassFactoryRegister(_classFactory);

            foreach (XmlNode node in nodeList)
            {
                string moduleType = node.Attributes?["ModuleType"]?.Value ?? string.Empty;
                string refModule  = node.Attributes?["Ref"]?.Value ?? string.Empty;

                string refPath = Path.Combine(directoryName, refModule);

                var assembly = Assembly.LoadFrom(refPath);

                var type = assembly.GetType(moduleType);
                if (type == null)
                {
                    throw new Exception($"The {moduleType} is not found in {refPath}");
                }

                IModuleInit moduleInit = (IModuleInit)Activator.CreateInstance(type);
                moduleInit?.ClassFactoriesRegistration(classFactoryRegister);
            }
        }
Exemplo n.º 2
0
        private void initModule(IModuleInit module, string moduleName)
        {
            Robot.BeginInvokeOnMainThread(() =>
            {
                // This Invoke is on a different thread
                // so it's fire and forget, BUT, we can't run other parts of the
                // modules until everything is initialized.
                // This makes sure all modules are initialized
                // before setting the _hasInitialized flag

                try
                {
                    module.InitModule();
                }
                catch (SettingNotFound ex)
                {
                    this.Robot.Print("Setting not found InitModule:"
                                     + moduleName + ", " + ex.Message);
                }
                catch (Exception ex)
                {
                    this.Robot.Print("Error in InitModule:"
                                     + moduleName + ", " + ex.Message);
                }
                finally
                {
                    lock (locker)
                    {
                        _inflightInitializations--;
                    }
                    if (_inflightInitializations == 0)
                    {
                        _hasInitialized = true;
                    }
                }
            });
        }