ScriptManager.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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.Collections.Generic;
  28. using System.Reflection;
  29. using log4net;
  30. using Nini.Config;
  31. using OpenSim.Region.Environment.Interfaces;
  32. using OpenSim.Region.Environment.Scenes;
  33. using OpenSim.Region.ExtensionsScriptModule.Engines.CSharp;
  34. using OpenSim.Region.ExtensionsScriptModule.Engines.JScript;
  35. using OpenSim.Region.ExtensionsScriptModule.Engines.JVMEngine;
  36. namespace OpenSim.Region.ExtensionsScriptModule
  37. {
  38. public class ScriptManager : IRegionModule, IExtensionScriptModule
  39. {
  40. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  41. private readonly List<IScript> scripts = new List<IScript>();
  42. private Scene m_scene;
  43. private readonly Dictionary<string, IScriptCompiler> compilers = new Dictionary<string, IScriptCompiler>();
  44. private void LoadFromCompiler(Dictionary<string, IScript> compiledscripts)
  45. {
  46. foreach (KeyValuePair<string, IScript> script in compiledscripts)
  47. {
  48. ScriptInfo scriptInfo = new ScriptInfo(m_scene);
  49. // Since each script could potentially corrupt their access with a stray assignment, making a new one for each script.
  50. m_log.Info("[SCRIPT]: Loading " + script.Key);
  51. script.Value.Initialise(scriptInfo);
  52. scripts.Add(script.Value);
  53. }
  54. m_log.Info("[SCRIPT]: " + string.Format("Finished loading {0} script(s)", compiledscripts.Count));
  55. }
  56. public ScriptManager()
  57. {
  58. // Default Engines
  59. CSharpScriptEngine csharpCompiler = new CSharpScriptEngine();
  60. compilers.Add(csharpCompiler.FileExt(), csharpCompiler);
  61. JScriptEngine jscriptCompiler = new JScriptEngine();
  62. compilers.Add(jscriptCompiler.FileExt(), jscriptCompiler);
  63. JavaEngine javaCompiler = new JavaEngine();
  64. compilers.Add(javaCompiler.FileExt(), javaCompiler);
  65. }
  66. public void Initialise(Scene scene, IConfigSource config)
  67. {
  68. m_log.Info("[SCRIPTMODULE]: Initialising Extensions Scripting Module");
  69. m_scene = scene;
  70. m_scene.RegisterModuleInterface<IExtensionScriptModule>(this);
  71. }
  72. public void PostInitialise()
  73. {
  74. }
  75. public void Close()
  76. {
  77. }
  78. public string Name
  79. {
  80. get { return "ExtensionsScriptingModule"; }
  81. }
  82. public bool IsSharedModule
  83. {
  84. get { return false; }
  85. }
  86. public bool Compile(string filename)
  87. {
  88. foreach (KeyValuePair<string, IScriptCompiler> compiler in compilers)
  89. {
  90. if (filename.EndsWith(compiler.Key))
  91. {
  92. LoadFromCompiler(compiler.Value.compile(filename));
  93. break;
  94. }
  95. }
  96. return true;
  97. }
  98. public void RunScriptCmd(string[] args)
  99. {
  100. switch (args[0])
  101. {
  102. case "load":
  103. Compile(args[1]);
  104. break;
  105. default:
  106. m_log.Error("Unknown script command");
  107. break;
  108. }
  109. }
  110. public bool AddPreCompiledScript(IScript script)
  111. {
  112. m_log.Info("[SCRIPT]: Loading script " + script.Name);
  113. ScriptInfo scriptInfo = new ScriptInfo(m_scene);
  114. // Since each script could potentially corrupt their access with a stray assignment, making a new one for each script.
  115. script.Initialise(scriptInfo);
  116. scripts.Add(script);
  117. return true;
  118. }
  119. }
  120. public interface IExtensionScriptModule
  121. {
  122. bool Compile(string filename);
  123. bool AddPreCompiledScript(IScript script);
  124. }
  125. internal interface IScriptCompiler
  126. {
  127. Dictionary<string, IScript> compile(string filename);
  128. string FileExt();
  129. }
  130. }