Пример #1
0
        public void Start()
        {
            if (m_httpServer != null)
            {
                string urlFriendlySceneName = WebUtil.UrlEncode(this.Name);
                string regionPath           = "/regions/" + urlFriendlySceneName;

                // Create a capability router for this scene
                string capsPath = regionPath + "/caps/";
                m_capabilityRouter = new CapabilityRouter(m_httpServer.HttpAddress.Combine(capsPath));
                m_httpServer.AddHandler(null, null, capsPath, false, false, m_capabilityRouter.RouteCapability);

                // Create the public seed capability for this scene
                AddPublicCapability("public_region_seed_capability", m_httpServer.HttpAddress.Combine("/regions/" + urlFriendlySceneName));
                m_httpServer.AddHandler("GET", null, "/regions/" + urlFriendlySceneName, true, true, PublicSeedHandler);
            }

            #region Scene Module Whitelist Loading

            // Load the scene module whitelist
            List <KeyValuePair <int, string> > whitelist = new List <KeyValuePair <int, string> >();

            IConfig config = m_configSource.Configs["SceneModules"];
            if (config != null)
            {
                foreach (string key in config.GetKeys())
                {
                    int runLevel = config.GetInt(key, -1);
                    if (runLevel >= 0)
                    {
                        whitelist.Add(new KeyValuePair <int, string>(runLevel, key));
                    }
                }
            }

            // Sort the list based on runlevel
            whitelist.Sort(delegate(KeyValuePair <int, string> lhs, KeyValuePair <int, string> rhs) { return(lhs.Key.CompareTo(rhs.Key)); });

            #endregion Scene Module Whitelist Loading

            #region Scene Module Loading

            IEnumerable <Lazy <object, object> >        exportEnumerable = m_simian.ModuleContainer.GetExports(typeof(ISceneModule), null, null);
            Dictionary <string, Lazy <object, object> > exports          = new Dictionary <string, Lazy <object, object> >();
            List <ISceneModule> imports   = new List <ISceneModule>();
            List <string>       notLoaded = new List <string>();

            // Reshuffle exportEnumerable into a dictionary mapping module names to their lazy instantiations
            foreach (Lazy <object, object> lazyExport in exportEnumerable)
            {
                IDictionary <string, object> metadata = (IDictionary <string, object>)lazyExport.Metadata;
                object nameObj;
                if (metadata.TryGetValue("Name", out nameObj))
                {
                    string name = (string)nameObj;

                    if (!exports.ContainsKey(name))
                    {
                        exports.Add(name, lazyExport);
                    }
                    else
                    {
                        m_log.Warn("Found an ISceneModule with a duplicate name: " + name);
                    }
                }
            }

            // Load modules in the order they appear in the whitelist
            foreach (KeyValuePair <int, string> kvp in whitelist)
            {
                string whitelisted = kvp.Value;

                Lazy <object, object> lazyExport;
                if (exports.TryGetValue(whitelisted, out lazyExport))
                {
                    // Instantiate a new copy of each scene module
                    ISceneModule module = (ISceneModule)Activator.CreateInstance(lazyExport.Value.GetType());

                    imports.Add(module);
                    exports.Remove(whitelisted);
                }
                else
                {
                    notLoaded.Add(whitelisted);
                }
            }

            // Populate m_sceneModules
            m_sceneModules = imports.ToArray();

            // Start the scene modules
            for (int i = 0; i < m_sceneModules.Length; i++)
            {
                ISceneModule module = m_sceneModules[i];
                module.Start(this);

                if (module is IScriptApi)
                {
                    RegisterScriptingApi((IScriptApi)module);
                }
            }

            // Fire the scene modules loaded event
            EventHandler callback = OnSceneModulesLoaded;
            if (callback != null)
            {
                callback(this, new EventArgs());
            }

            #endregion Scene Module Loading

            #region Logging

            m_log.InfoFormat("Loaded {0} scene modules for {1}", (m_sceneModules != null ? m_sceneModules.Length : 0), m_name);

            if (exports.Count > 0)
            {
                StringBuilder skippedStr = new StringBuilder("Skipped scene modules: ");
                foreach (string exportName in exports.Keys)
                {
                    skippedStr.Append(exportName + " ");
                }
                m_log.Info(skippedStr.ToString());
            }

            if (notLoaded.Count > 0)
            {
                StringBuilder notLoadedStr = new StringBuilder("Did not load whitelisted scene modules: ");
                foreach (string entry in notLoaded)
                {
                    notLoadedStr.Append(entry + " ");
                }
                m_log.Warn(notLoadedStr.ToString());
            }

            #endregion Logging

            // Add a few command handlers
            AddCommandHandler("presences", PresenceCommandHandler);
            AddCommandHandler("shutdown", ShutdownCommandHandler);
            AddCommandHandler("restart", RestartCommandHandler);

            m_running = true;
        }
Пример #2
0
        public bool LoadModules()
        {
            #region Application Module Whitelist Loading

            // Load the application module whitelist
            List <KeyValuePair <int, string> > whitelist = new List <KeyValuePair <int, string> >();

            IConfig config = Config.Configs["ApplicationModules"];
            if (config != null)
            {
                foreach (string key in config.GetKeys())
                {
                    int runLevel = config.GetInt(key, -1);
                    if (runLevel >= 0)
                    {
                        whitelist.Add(new KeyValuePair <int, string>(runLevel, key));
                    }
                }
            }

            // Sort the list based on runlevel
            whitelist.Sort(delegate(KeyValuePair <int, string> lhs, KeyValuePair <int, string> rhs) { return(lhs.Key.CompareTo(rhs.Key)); });

            #endregion Application Module Whitelist Loading

            #region Module Container Loading

            AggregateCatalog catalog = new AggregateCatalog();

            AssemblyCatalog  assemblyCatalog  = new AssemblyCatalog(System.Reflection.Assembly.GetExecutingAssembly());
            DirectoryCatalog directoryCatalog = new DirectoryCatalog(".", "Simian.*.dll");

            catalog.Catalogs.Add(assemblyCatalog);
            catalog.Catalogs.Add(directoryCatalog);

            m_moduleContainer = new CompositionContainer(catalog, true);

            try
            {
                m_log.InfoFormat("Found {0} modules in the current assembly and {1} modules in external assemblies",
                                 assemblyCatalog.Parts.Count(), directoryCatalog.Parts.Count());
            }
            catch (System.Reflection.ReflectionTypeLoadException ex)
            {
                StringBuilder error = new StringBuilder("Error(s) encountered loading extension modules. You may have an incompatible or out of date extension .dll in the current folder.");
                foreach (Exception loaderEx in ex.LoaderExceptions)
                {
                    error.Append("\n " + loaderEx.Message);
                }
                m_log.Error(error.ToString());

                return(false);
            }

            #endregion Module Container Loading

            #region Module Loading

            IEnumerable <Lazy <object, object> >        exportEnumerable = m_moduleContainer.GetExports(typeof(IApplicationModule), null, null);
            Dictionary <string, Lazy <object, object> > exports          = new Dictionary <string, Lazy <object, object> >();
            List <IApplicationModule> imports   = new List <IApplicationModule>();
            List <string>             notLoaded = new List <string>();

            // Reshuffle exportEnumerable into a dictionary mapping module names to their lazy instantiations
            foreach (Lazy <object, object> lazyExport in exportEnumerable)
            {
                IDictionary <string, object> metadata = (IDictionary <string, object>)lazyExport.Metadata;
                object nameObj;
                if (metadata.TryGetValue("Name", out nameObj))
                {
                    string name = (string)nameObj;

                    if (!exports.ContainsKey(name))
                    {
                        exports.Add(name, lazyExport);
                    }
                    else
                    {
                        m_log.Warn("Found an IApplicationModule with a duplicate name: " + name);
                    }
                }
            }

            // Load modules in the order they appear in the whitelist
            foreach (KeyValuePair <int, string> kvp in whitelist)
            {
                string whitelisted = kvp.Value;

                Lazy <object, object> lazyExport;
                if (exports.TryGetValue(whitelisted, out lazyExport))
                {
                    imports.Add((IApplicationModule)lazyExport.Value);
                    exports.Remove(whitelisted);
                }
                else
                {
                    notLoaded.Add(whitelisted);
                }
            }

            // Populate m_applicationModules
            m_applicationModules = imports.ToArray();

            // Start the application modules
            for (int i = 0; i < m_applicationModules.Length; i++)
            {
                IApplicationModule module = m_applicationModules[i];
                if (!(module is ISceneFactory))
                {
                    module.Start(this);
                }
            }

            // ISceneFactory modules are always started last
            for (int i = 0; i < m_applicationModules.Length; i++)
            {
                IApplicationModule module = m_applicationModules[i];
                if (module is ISceneFactory)
                {
                    module.Start(this);
                }
            }

            #endregion Module Loading

            #region Logging

            m_log.InfoFormat("Loaded {0} application modules", m_applicationModules.Length);

            if (exports.Count > 0)
            {
                StringBuilder skippedStr = new StringBuilder("Skipped application modules: ");
                foreach (string exportName in exports.Keys)
                {
                    skippedStr.Append(exportName + " ");
                }
                m_log.Info(skippedStr.ToString());
            }

            if (notLoaded.Count > 0)
            {
                StringBuilder notLoadedStr = new StringBuilder("Did not load whitelisted application modules: ");
                foreach (string entry in notLoaded)
                {
                    notLoadedStr.Append(entry + " ");
                }
                m_log.Warn(notLoadedStr.ToString());
            }

            #endregion Logging

            // Get a reference to the HTTP server if we have one
            m_httpServer = GetAppModule <IHttpServer>();
            if (m_httpServer != null)
            {
                m_capabilityRouter = new CapabilityRouter(m_httpServer.HttpAddress.Combine(CAPABILITY_PATH));
                m_httpServer.AddHandler(null, null, CAPABILITY_PATH, false, false, m_capabilityRouter.RouteCapability);
            }

            // Get a reference to the ISceneFactory if we have one
            m_sceneFactory = GetAppModule <ISceneFactory>();
            if (m_sceneFactory != null)
            {
                AddCommandHandler("scene", SceneHandler);
            }

            return(true);
        }