OpenSimJVM.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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.Diagnostics;
  30. using System.IO;
  31. using OpenSim.Framework;
  32. using OpenSim.Region.Environment.Scenes;
  33. using OpenSim.Region.ExtensionsScriptModule.Engines.JVMEngine.JVM;
  34. namespace OpenSim.Region.ExtensionsScriptModule.Engines.JVMEngine
  35. {
  36. public class JVMScript : IScript
  37. {
  38. private List<Thread> _threads = new List<Thread>();
  39. private BlockingQueue<CompileInfo> CompileScripts = new BlockingQueue<CompileInfo>();
  40. private MainMemory _mainMemory;
  41. private ScriptInfo scriptInfo;
  42. public void Initialise(ScriptInfo info)
  43. {
  44. scriptInfo = info;
  45. _mainMemory = new MainMemory();
  46. Thread.GlobalMemory = _mainMemory;
  47. Thread.World = info.world;
  48. CompileScript();
  49. scriptInfo.events.OnFrame += new EventManager.OnFrameDelegate(events_OnFrame);
  50. scriptInfo.events.OnNewPresence += new EventManager.OnNewPresenceDelegate(events_OnNewPresence);
  51. }
  52. private void events_OnNewPresence(ScenePresence presence)
  53. {
  54. for (int i = 0; i < _threads.Count; i++)
  55. {
  56. if (!_threads[i].running)
  57. {
  58. _threads[i].StartMethod("OnNewPresence");
  59. bool run = true;
  60. while (run)
  61. {
  62. run = _threads[i].Excute();
  63. }
  64. }
  65. }
  66. }
  67. private void events_OnFrame()
  68. {
  69. for (int i = 0; i < _threads.Count; i++)
  70. {
  71. if (!_threads[i].running)
  72. {
  73. _threads[i].StartMethod("OnFrame");
  74. bool run = true;
  75. while (run)
  76. {
  77. run = _threads[i].Excute();
  78. }
  79. }
  80. }
  81. }
  82. public string Name
  83. {
  84. get { return "JVM Scripting Engine"; }
  85. }
  86. public void LoadScript(string script)
  87. {
  88. Console.WriteLine("OpenSimJVM - loading new script: " + script);
  89. CompileInfo comp = new CompileInfo();
  90. comp.script = script;
  91. comp.scriptName = script;
  92. CompileScripts.Enqueue(comp);
  93. }
  94. public void CompileScript()
  95. {
  96. CompileInfo comp = CompileScripts.Dequeue();
  97. string script = comp.script;
  98. string scriptName = comp.scriptName;
  99. try
  100. {
  101. //need to compile the script into a java class file
  102. //first save it to a java source file
  103. TextWriter tw = new StreamWriter(scriptName + ".java");
  104. tw.WriteLine(script);
  105. tw.Close();
  106. //now compile
  107. ProcessStartInfo psi = new ProcessStartInfo("javac.exe", "*.java");
  108. // psi.RedirectStandardOutput = true;
  109. psi.WindowStyle = ProcessWindowStyle.Hidden;
  110. psi.UseShellExecute = false;
  111. Process javacomp;
  112. javacomp = Process.Start(psi);
  113. javacomp.WaitForExit();
  114. //now load in class file
  115. ClassRecord class1 = new ClassRecord();
  116. class1.LoadClassFromFile(scriptName + ".class");
  117. class1.PrintToConsole();
  118. //Console.WriteLine();
  119. _mainMemory.MethodArea.Classes.Add(class1);
  120. class1.AddMethodsToMemory(_mainMemory.MethodArea);
  121. Thread newThread = new Thread();
  122. _threads.Add(newThread);
  123. newThread.currentClass = class1;
  124. newThread.scriptInfo = scriptInfo;
  125. //now delete the created files
  126. File.Delete(scriptName + ".java");
  127. File.Delete(scriptName + ".class");
  128. //this.OnFrame();
  129. }
  130. catch (Exception e)
  131. {
  132. Console.WriteLine("exception");
  133. Console.WriteLine(e.StackTrace);
  134. Console.WriteLine(e.Message);
  135. }
  136. }
  137. private class CompileInfo
  138. {
  139. public string script;
  140. public string scriptName;
  141. public CompileInfo()
  142. {
  143. }
  144. }
  145. }
  146. }