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