ComponentFactory.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSim Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. using System;
  28. using System.Collections.Generic;
  29. using System.IO;
  30. using System.Reflection;
  31. using log4net;
  32. using OpenSim.ScriptEngine.Shared;
  33. namespace OpenSim.ApplicationPlugins.ScriptEngine
  34. {
  35. public static class ComponentFactory
  36. {
  37. // Component providers are registered here wit a name (string)
  38. // When a script engine is created the components are instanciated
  39. public static Dictionary<string, Type> providers = new Dictionary<string, Type>();
  40. public static Dictionary<string, Type> scriptEngines = new Dictionary<string, Type>();
  41. internal static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  42. private readonly static string nameIScriptEngineComponent = typeof(IScriptEngineComponent).Name; // keep interface name in managed code
  43. private readonly static string nameIScriptEngine = typeof(IScriptEngine).Name; // keep interface name in managed code
  44. public static string Name
  45. {
  46. get { return "SECS.ComponentFactory"; }
  47. }
  48. /// <summary>
  49. /// Load components from directory
  50. /// </summary>
  51. /// <param name="directory"></param>
  52. internal static void Load(string directory, string filter)
  53. {
  54. // We may want to change how this functions as currently it required unique class names for each component
  55. foreach (string file in Directory.GetFiles(directory, filter))
  56. {
  57. //m_log.DebugFormat("[ScriptEngine]: Loading: [{0}].", file);
  58. Assembly componentAssembly = null;
  59. try
  60. {
  61. componentAssembly = Assembly.LoadFrom(file);
  62. }
  63. catch
  64. {
  65. m_log.ErrorFormat("[{0}] Error loading: \"{1}\".", Name, file);
  66. }
  67. if (componentAssembly != null)
  68. {
  69. try
  70. {
  71. // Go through all types in the assembly
  72. foreach (Type componentType in componentAssembly.GetTypes())
  73. {
  74. if (componentType.IsPublic
  75. && !componentType.IsAbstract)
  76. {
  77. //if (componentType.IsSubclassOf(typeof(ComponentBase)))
  78. if (componentType.GetInterface(nameIScriptEngineComponent) != null)
  79. {
  80. // We have found an type which is derived from ProdiverBase, add it to provider list
  81. m_log.InfoFormat("[{0}] Adding component: {1}", Name, componentType.Name);
  82. lock (providers)
  83. {
  84. providers.Add(componentType.Name, componentType);
  85. }
  86. }
  87. //if (componentType.IsSubclassOf(typeof(ScriptEngineBase)))
  88. if (componentType.GetInterface(nameIScriptEngine) != null)
  89. {
  90. // We have found an type which is derived from RegionScriptEngineBase, add it to engine list
  91. m_log.InfoFormat("[{0}] Adding script engine: {1}", Name, componentType.Name);
  92. lock (scriptEngines)
  93. {
  94. scriptEngines.Add(componentType.Name, componentType);
  95. }
  96. }
  97. }
  98. }
  99. }
  100. catch
  101. (ReflectionTypeLoadException re)
  102. {
  103. m_log.ErrorFormat("[{0}] Could not load component \"{1}\": {2}", Name, componentAssembly.FullName, re.ToString());
  104. int c = 0;
  105. foreach (Exception e in re.LoaderExceptions)
  106. {
  107. c++;
  108. m_log.ErrorFormat("[{0}] LoaderException {1}: {2}", Name, c, e.ToString());
  109. }
  110. }
  111. } //if
  112. } //foreach
  113. }
  114. public static IScriptEngineComponent GetComponentInstance(string name, params Object[] args)
  115. {
  116. lock (providers)
  117. {
  118. if (!providers.ContainsKey(name))
  119. throw new Exception("ScriptEngine requested component named \"" + name +
  120. "\" that does not exist.");
  121. return Activator.CreateInstance(providers[name], args) as IScriptEngineComponent;
  122. }
  123. }
  124. private readonly static string nameIScriptEngineRegionComponent = typeof(IScriptEngineRegionComponent).Name; // keep interface name in managed code
  125. public static IScriptEngineComponent GetComponentInstance(RegionInfoStructure info, string name, params Object[] args)
  126. {
  127. IScriptEngineComponent c = GetComponentInstance(name, args);
  128. // If module is IScriptEngineRegionComponent then it will have one instance per region and we will initialize it
  129. if (c.GetType().GetInterface(nameIScriptEngineRegionComponent) != null)
  130. ((IScriptEngineRegionComponent)c).Initialize(info);
  131. return c;
  132. }
  133. }
  134. }