/// <summary> /// Convert a NameValueCollection into a query string. This is the /// inverse of HttpUtility.ParseQueryString() /// </summary> /// <param name="parameters">Collection of key/value pairs to convert</param> /// <returns>A query string with URL-escaped values</returns> public static string BuildQueryString(NameValueCollection parameters) { List <string> items = new List <string>(parameters.Count); foreach (string key in parameters.Keys) { string[] values = parameters.GetValues(key); if (values != null && values.Length > 0) { foreach (string value in values) { items.Add(String.Concat(key, "=", WebUtil.UrlEncode(value ?? String.Empty))); } } else { items.Add(key + "="); } } return(String.Join("&", items.ToArray())); }
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; }