OpenSimJVM.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. _mainMemory = new MainMemory();
  23. Thread.GlobalMemory = this._mainMemory;
  24. Thread.OpenSimScriptAPI = api;
  25. compileThread = new System.Threading.Thread(new ThreadStart(CompileScript));
  26. compileThread.IsBackground = true;
  27. compileThread.Start();
  28. return true;
  29. }
  30. public string GetName()
  31. {
  32. return "OpenSimJVM";
  33. }
  34. public void LoadScript(string script, string scriptName, uint entityID)
  35. {
  36. CompileInfo comp = new CompileInfo();
  37. comp.entityId = entityID;
  38. comp.script = script;
  39. comp.scriptName = scriptName;
  40. this.CompileScripts.Enqueue(comp);
  41. }
  42. public void CompileScript()
  43. {
  44. while (true)
  45. {
  46. CompileInfo comp = this.CompileScripts.Dequeue();
  47. string script = comp.script;
  48. string scriptName = comp.scriptName;
  49. uint entityID = comp.entityId;
  50. try
  51. {
  52. //need to compile the script into a java class file
  53. //first save it to a java source file
  54. TextWriter tw = new StreamWriter(scriptName + ".java");
  55. tw.WriteLine(script);
  56. tw.Close();
  57. //now compile
  58. System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo(@"C:\Program Files\Java\jdk1.6.0_01\bin\javac.exe *.java");
  59. psi.RedirectStandardOutput = true;
  60. psi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
  61. psi.UseShellExecute = false;
  62. System.Diagnostics.Process javacomp;
  63. javacomp = System.Diagnostics.Process.Start(psi);
  64. javacomp.WaitForExit();
  65. //now load in class file
  66. ClassRecord class1 = new ClassRecord();
  67. class1.LoadClassFromFile(scriptName + ".class");
  68. class1.PrintToConsole();
  69. //Console.WriteLine();
  70. this._mainMemory.MethodArea.Classes.Add(class1);
  71. Thread newThread = new Thread();
  72. this._threads.Add(newThread);
  73. newThread.EntityId = entityID;
  74. newThread.currentClass = class1;
  75. //now delete the created files
  76. System.IO.File.Delete(scriptName + ".java");
  77. System.IO.File.Delete(scriptName + ".class");
  78. }
  79. catch (Exception e)
  80. {
  81. Console.WriteLine(e.Message);
  82. }
  83. }
  84. }
  85. public void OnFrame()
  86. {
  87. for (int i = 0; i < this._threads.Count; i++)
  88. {
  89. if (!this._threads[i].running)
  90. {
  91. this._threads[i].StartMethod("OnFrame");
  92. bool run = true;
  93. while (run)
  94. {
  95. run = this._threads[i].Excute();
  96. }
  97. }
  98. }
  99. }
  100. private class CompileInfo
  101. {
  102. public string script;
  103. public string scriptName;
  104. public uint entityId;
  105. public CompileInfo()
  106. {
  107. }
  108. }
  109. }
  110. }