OpenSimJVM.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.IO;
  5. using System.Threading;
  6. using OpenSim.Framework;
  7. using OpenSim.Framework.Interfaces;
  8. using OpenSim.Framework.Utilities;
  9. namespace OpenSim.Scripting.EmbeddedJVM
  10. {
  11. public class OpenSimJVM : IScriptEngine
  12. {
  13. private List<Thread> _threads = new List<Thread>();
  14. private BlockingQueue<CompileInfo> CompileScripts = new BlockingQueue<CompileInfo>();
  15. private MainMemory _mainMemory;
  16. private System.Threading.Thread compileThread;
  17. public OpenSimJVM()
  18. {
  19. }
  20. public bool Init(IScriptAPI api)
  21. {
  22. Console.WriteLine("Creating OpenSim JVM scripting engine");
  23. _mainMemory = new MainMemory();
  24. Thread.GlobalMemory = this._mainMemory;
  25. Thread.OpenSimScriptAPI = api;
  26. compileThread = new System.Threading.Thread(new ThreadStart(CompileScript));
  27. compileThread.IsBackground = true;
  28. compileThread.Start();
  29. return true;
  30. }
  31. public string GetName()
  32. {
  33. return "OpenSimJVM";
  34. }
  35. public void LoadScript(string script, string scriptName, uint entityID)
  36. {
  37. Console.WriteLine("OpenSimJVM - loading new script: " + scriptName);
  38. CompileInfo comp = new CompileInfo();
  39. comp.entityId = entityID;
  40. comp.script = script;
  41. comp.scriptName = scriptName;
  42. this.CompileScripts.Enqueue(comp);
  43. }
  44. public void CompileScript()
  45. {
  46. while (true)
  47. {
  48. CompileInfo comp = this.CompileScripts.Dequeue();
  49. string script = comp.script;
  50. string scriptName = comp.scriptName;
  51. uint entityID = comp.entityId;
  52. try
  53. {
  54. //need to compile the script into a java class file
  55. //first save it to a java source file
  56. TextWriter tw = new StreamWriter(scriptName + ".java");
  57. tw.WriteLine(script);
  58. tw.Close();
  59. //now compile
  60. System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo("javac.exe", "*.java");
  61. // psi.RedirectStandardOutput = true;
  62. psi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
  63. psi.UseShellExecute = false;
  64. System.Diagnostics.Process javacomp;
  65. javacomp = System.Diagnostics.Process.Start(psi);
  66. javacomp.WaitForExit();
  67. //now load in class file
  68. ClassRecord class1 = new ClassRecord();
  69. class1.LoadClassFromFile(scriptName + ".class");
  70. class1.PrintToConsole();
  71. //Console.WriteLine();
  72. this._mainMemory.MethodArea.Classes.Add(class1);
  73. class1.AddMethodsToMemory(this._mainMemory.MethodArea);
  74. Thread newThread = new Thread();
  75. this._threads.Add(newThread);
  76. newThread.EntityId = entityID;
  77. newThread.currentClass = class1;
  78. //now delete the created files
  79. System.IO.File.Delete(scriptName + ".java");
  80. System.IO.File.Delete(scriptName + ".class");
  81. //this.OnFrame();
  82. }
  83. catch (Exception e)
  84. {
  85. Console.WriteLine("exception");
  86. Console.WriteLine(e.StackTrace);
  87. Console.WriteLine(e.Message);
  88. }
  89. }
  90. }
  91. public void OnFrame()
  92. {
  93. for (int i = 0; i < this._threads.Count; i++)
  94. {
  95. if (!this._threads[i].running)
  96. {
  97. this._threads[i].StartMethod("OnFrame");
  98. bool run = true;
  99. while (run)
  100. {
  101. run = this._threads[i].Excute();
  102. }
  103. }
  104. }
  105. }
  106. private class CompileInfo
  107. {
  108. public string script;
  109. public string scriptName;
  110. public uint entityId;
  111. public CompileInfo()
  112. {
  113. }
  114. }
  115. }
  116. }