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