diff options
Diffstat (limited to '')
-rw-r--r-- | OpenSim.Scripting.EmbeddedJVM/OpenSimJVM.cs | 133 |
1 files changed, 133 insertions, 0 deletions
diff --git a/OpenSim.Scripting.EmbeddedJVM/OpenSimJVM.cs b/OpenSim.Scripting.EmbeddedJVM/OpenSimJVM.cs new file mode 100644 index 0000000..77a92e5 --- /dev/null +++ b/OpenSim.Scripting.EmbeddedJVM/OpenSimJVM.cs | |||
@@ -0,0 +1,133 @@ | |||
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(@"C:\Program Files\Java\jdk1.6.0_01\bin\javac.exe", "*.java"); | ||
70 | psi.RedirectStandardOutput = true; | ||
71 | psi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; | ||
72 | psi.UseShellExecute = false; | ||
73 | System.Diagnostics.Process javacomp; | ||
74 | javacomp = System.Diagnostics.Process.Start(psi); | ||
75 | javacomp.WaitForExit(); | ||
76 | |||
77 | |||
78 | //now load in class file | ||
79 | ClassRecord class1 = new ClassRecord(); | ||
80 | class1.LoadClassFromFile(scriptName + ".class"); | ||
81 | class1.PrintToConsole(); | ||
82 | //Console.WriteLine(); | ||
83 | this._mainMemory.MethodArea.Classes.Add(class1); | ||
84 | class1.AddMethodsToMemory(this._mainMemory.MethodArea); | ||
85 | |||
86 | Thread newThread = new Thread(); | ||
87 | this._threads.Add(newThread); | ||
88 | newThread.EntityId = entityID; | ||
89 | newThread.currentClass = class1; | ||
90 | |||
91 | //now delete the created files | ||
92 | System.IO.File.Delete(scriptName + ".java"); | ||
93 | System.IO.File.Delete(scriptName + ".class"); | ||
94 | this.OnFrame(); | ||
95 | } | ||
96 | catch (Exception e) | ||
97 | { | ||
98 | Console.WriteLine("exception"); | ||
99 | Console.WriteLine(e.StackTrace); | ||
100 | Console.WriteLine(e.Message); | ||
101 | } | ||
102 | } | ||
103 | } | ||
104 | |||
105 | public void OnFrame() | ||
106 | { | ||
107 | for (int i = 0; i < this._threads.Count; i++) | ||
108 | { | ||
109 | if (!this._threads[i].running) | ||
110 | { | ||
111 | this._threads[i].StartMethod("OnFrame"); | ||
112 | bool run = true; | ||
113 | while (run) | ||
114 | { | ||
115 | run = this._threads[i].Excute(); | ||
116 | } | ||
117 | } | ||
118 | } | ||
119 | } | ||
120 | |||
121 | private class CompileInfo | ||
122 | { | ||
123 | public string script; | ||
124 | public string scriptName; | ||
125 | public uint entityId; | ||
126 | |||
127 | public CompileInfo() | ||
128 | { | ||
129 | |||
130 | } | ||
131 | } | ||
132 | } | ||
133 | } | ||