ScriptEngineLoader.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. */
  28. /* Original code: Tedd Hansen */
  29. using System;
  30. using System.IO;
  31. using System.Reflection;
  32. using OpenSim.Framework.Console;
  33. namespace OpenSim.Region.Environment.Scenes.Scripting
  34. {
  35. public class ScriptEngineLoader
  36. {
  37. private static readonly log4net.ILog m_log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
  38. public ScriptEngineInterface LoadScriptEngine(string EngineName)
  39. {
  40. ScriptEngineInterface ret = null;
  41. try
  42. {
  43. ret =
  44. LoadAndInitAssembly(
  45. Path.Combine("ScriptEngines", "OpenSim.Region.ScriptEngine." + EngineName + ".dll"),
  46. "OpenSim.Region.ScriptEngine." + EngineName + ".ScriptEngine");
  47. }
  48. catch (Exception e)
  49. {
  50. m_log.Error("[ScriptEngine]: " +
  51. "Error loading assembly \"" + EngineName + "\": " + e.Message + ", " +
  52. e.StackTrace.ToString());
  53. }
  54. return ret;
  55. }
  56. /// <summary>
  57. /// Does actual loading and initialization of script Assembly
  58. /// </summary>
  59. /// <param name="FreeAppDomain">AppDomain to load script into</param>
  60. /// <param name="FileName">FileName of script assembly (.dll)</param>
  61. /// <returns></returns>
  62. private ScriptEngineInterface LoadAndInitAssembly(string FileName, string NameSpace)
  63. {
  64. //Common.SendToDebug("Loading ScriptEngine Assembly " + FileName);
  65. // Load .Net Assembly (.dll)
  66. // Initialize and return it
  67. // TODO: Add error handling
  68. Assembly a;
  69. //try
  70. //{
  71. // Load to default appdomain (temporary)
  72. a = Assembly.LoadFrom(FileName);
  73. // Load to specified appdomain
  74. // TODO: Insert security
  75. //a = FreeAppDomain.Load(FileName);
  76. //}
  77. //catch (Exception e)
  78. //{
  79. // m_log.Error("[ScriptEngine]: Error loading assembly \String.Empty + FileName + "\": " + e.ToString());
  80. //}
  81. //Console.WriteLine("Loading: " + FileName);
  82. //foreach (Type _t in a.GetTypes())
  83. //{
  84. // Console.WriteLine("Type: " + _t.ToString());
  85. //}
  86. Type t;
  87. //try
  88. //{
  89. t = a.GetType(NameSpace, true);
  90. //}
  91. //catch (Exception e)
  92. //{
  93. // m_log.Error("[ScriptEngine]: Error initializing type \String.Empty + NameSpace + "\" from \String.Empty + FileName + "\": " + e.ToString());
  94. //}
  95. ScriptEngineInterface ret;
  96. //try
  97. //{
  98. ret = (ScriptEngineInterface) Activator.CreateInstance(t);
  99. //}
  100. //catch (Exception e)
  101. //{
  102. // m_log.Error("[ScriptEngine]: Error initializing type \String.Empty + NameSpace + "\" from \String.Empty + FileName + "\": " + e.ToString());
  103. //}
  104. return ret;
  105. }
  106. }
  107. }