ScriptManager.cs 5.5 KB

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