aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Region/Environment/Scenes/scripting/Engines/JVMEngine/OpenSimJVM.cs
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--OpenSim/Region/Environment/Scenes/scripting/Engines/JVMEngine/OpenSimJVM.cs176
1 files changed, 176 insertions, 0 deletions
diff --git a/OpenSim/Region/Environment/Scenes/scripting/Engines/JVMEngine/OpenSimJVM.cs b/OpenSim/Region/Environment/Scenes/scripting/Engines/JVMEngine/OpenSimJVM.cs
new file mode 100644
index 0000000..7ed734b
--- /dev/null
+++ b/OpenSim/Region/Environment/Scenes/scripting/Engines/JVMEngine/OpenSimJVM.cs
@@ -0,0 +1,176 @@
1/*
2* Copyright (c) Contributors, http://www.openmetaverse.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*/
28using System;
29using System.Collections.Generic;
30using System.Text;
31using System.IO;
32using System.Threading;
33using OpenSim.Framework;
34using OpenSim.Framework.Interfaces;
35using OpenSim.Framework.Utilities;
36using OpenSim.Region.Environment.Scripting;
37using OpenSim.Region.Environment.Scenes;
38
39namespace OpenSim.Scripting.EmbeddedJVM
40{
41 public class OpenSimJVM : IScript
42 {
43 private List<Thread> _threads = new List<Thread>();
44 private BlockingQueue<CompileInfo> CompileScripts = new BlockingQueue<CompileInfo>();
45 private MainMemory _mainMemory;
46 private System.Threading.Thread compileThread;
47
48 ScriptInfo scriptInfo;
49
50 public OpenSimJVM()
51 {
52
53 }
54
55 public void Initialise(ScriptInfo info)
56 {
57 scriptInfo = info;
58 }
59
60 public string getName()
61 {
62 return "JVM Scripting Engine";
63 }
64
65 public bool Init(Scene world)
66 {
67 Console.WriteLine("Creating OpenSim JVM scripting engine");
68 _mainMemory = new MainMemory();
69 Thread.GlobalMemory = this._mainMemory;
70 Thread.World = world;
71 compileThread = new System.Threading.Thread(new ThreadStart(CompileScript));
72 compileThread.IsBackground = true;
73 compileThread.Start();
74 return true;
75 }
76
77 public string GetName()
78 {
79 return "OpenSimJVM";
80 }
81
82 public void LoadScript(string script, string scriptName, uint entityID)
83 {
84 Console.WriteLine("OpenSimJVM - loading new script: " + scriptName);
85 CompileInfo comp = new CompileInfo();
86 comp.entityId = entityID;
87 comp.script = script;
88 comp.scriptName = scriptName;
89 this.CompileScripts.Enqueue(comp);
90 }
91
92 public void CompileScript()
93 {
94 while (true)
95 {
96 CompileInfo comp = this.CompileScripts.Dequeue();
97 string script = comp.script;
98 string scriptName = comp.scriptName;
99 uint entityID = comp.entityId;
100 try
101 {
102 //need to compile the script into a java class file
103
104 //first save it to a java source file
105 TextWriter tw = new StreamWriter(scriptName + ".java");
106 tw.WriteLine(script);
107 tw.Close();
108
109 //now compile
110 System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo("javac.exe", "*.java");
111 // psi.RedirectStandardOutput = true;
112 psi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
113 psi.UseShellExecute = false;
114
115 System.Diagnostics.Process javacomp;
116 javacomp = System.Diagnostics.Process.Start(psi);
117 javacomp.WaitForExit();
118
119
120 //now load in class file
121 ClassRecord class1 = new ClassRecord();
122 class1.LoadClassFromFile(scriptName + ".class");
123 class1.PrintToConsole();
124 //Console.WriteLine();
125 this._mainMemory.MethodArea.Classes.Add(class1);
126 class1.AddMethodsToMemory(this._mainMemory.MethodArea);
127
128 Thread newThread = new Thread();
129 this._threads.Add(newThread);
130 newThread.EntityId = entityID;
131 newThread.currentClass = class1;
132 newThread.scriptInfo = scriptInfo;
133
134 //now delete the created files
135 System.IO.File.Delete(scriptName + ".java");
136 System.IO.File.Delete(scriptName + ".class");
137 //this.OnFrame();
138 }
139 catch (Exception e)
140 {
141 Console.WriteLine("exception");
142 Console.WriteLine(e.StackTrace);
143 Console.WriteLine(e.Message);
144 }
145 }
146 }
147
148 public void OnFrame()
149 {
150 for (int i = 0; i < this._threads.Count; i++)
151 {
152 if (!this._threads[i].running)
153 {
154 this._threads[i].StartMethod("OnFrame");
155 bool run = true;
156 while (run)
157 {
158 run = this._threads[i].Excute();
159 }
160 }
161 }
162 }
163
164 private class CompileInfo
165 {
166 public string script;
167 public string scriptName;
168 public uint entityId;
169
170 public CompileInfo()
171 {
172
173 }
174 }
175 }
176}