aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Region/ScriptEngine/DotNetEngine/ScriptManager.cs
diff options
context:
space:
mode:
authorTedd Hansen2007-08-08 14:05:13 +0000
committerTedd Hansen2007-08-08 14:05:13 +0000
commit2a0e157985d790e6cbd83d61690da2709dfab9dd (patch)
treee9c38c11773796111fb4ff222429605bdd76fd5e /OpenSim/Region/ScriptEngine/DotNetEngine/ScriptManager.cs
parent* Got SimpleApp working again (diff)
downloadopensim-SC_OLD-2a0e157985d790e6cbd83d61690da2709dfab9dd.zip
opensim-SC_OLD-2a0e157985d790e6cbd83d61690da2709dfab9dd.tar.gz
opensim-SC_OLD-2a0e157985d790e6cbd83d61690da2709dfab9dd.tar.bz2
opensim-SC_OLD-2a0e157985d790e6cbd83d61690da2709dfab9dd.tar.xz
Added ScriptEngine.DotNetEngine
Diffstat (limited to 'OpenSim/Region/ScriptEngine/DotNetEngine/ScriptManager.cs')
-rw-r--r--OpenSim/Region/ScriptEngine/DotNetEngine/ScriptManager.cs232
1 files changed, 232 insertions, 0 deletions
diff --git a/OpenSim/Region/ScriptEngine/DotNetEngine/ScriptManager.cs b/OpenSim/Region/ScriptEngine/DotNetEngine/ScriptManager.cs
new file mode 100644
index 0000000..f2080eb
--- /dev/null
+++ b/OpenSim/Region/ScriptEngine/DotNetEngine/ScriptManager.cs
@@ -0,0 +1,232 @@
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/* Original code: Tedd Hansen */
29using System;
30using System.Collections.Generic;
31using System.Text;
32using System.Threading;
33using System.Reflection;
34
35namespace OpenSim.Region.ScriptEngine.DotNetEngine
36{
37 class ScriptManager
38 {
39
40 private ScriptEngine myScriptEngine;
41 public ScriptManager(ScriptEngine _ScriptEngine)
42 {
43 myScriptEngine = _ScriptEngine;
44 Common.SendToDebug("ScriptManager Start");
45 }
46
47
48 // Object<string, Script<string, script>>
49 internal Dictionary<string, Dictionary<string, OpenSim.Region.ScriptEngine.DotNetEngine.Compiler.LSL.LSL_BaseClass>> Scripts = new Dictionary<string, Dictionary<string, OpenSim.Region.ScriptEngine.DotNetEngine.Compiler.LSL.LSL_BaseClass>>();
50
51
52 internal Dictionary<string, OpenSim.Region.ScriptEngine.DotNetEngine.Compiler.LSL.LSL_BaseClass>.KeyCollection GetScriptKeys(string ObjectID)
53 {
54 if (Scripts.ContainsKey(ObjectID) == false)
55 return null;
56
57 Dictionary<string, OpenSim.Region.ScriptEngine.DotNetEngine.Compiler.LSL.LSL_BaseClass> Obj;
58 Scripts.TryGetValue(ObjectID, out Obj);
59
60 return Obj.Keys;
61
62 }
63
64 internal OpenSim.Region.ScriptEngine.DotNetEngine.Compiler.LSL.LSL_BaseClass GetScript(string ObjectID, string ScriptID)
65 {
66 if (Scripts.ContainsKey(ObjectID) == false)
67 return null;
68
69 Dictionary<string, OpenSim.Region.ScriptEngine.DotNetEngine.Compiler.LSL.LSL_BaseClass> Obj;
70 Scripts.TryGetValue(ObjectID, out Obj);
71 if (Obj.ContainsKey(ScriptID) == false)
72 return null;
73
74 // Get script
75 OpenSim.Region.ScriptEngine.DotNetEngine.Compiler.LSL.LSL_BaseClass Script;
76 Obj.TryGetValue(ScriptID, out Script);
77
78 return Script;
79
80 }
81 internal void SetScript(string ObjectID, string ScriptID, OpenSim.Region.ScriptEngine.DotNetEngine.Compiler.LSL.LSL_BaseClass Script)
82 {
83 // Create object if it doesn't exist
84 if (Scripts.ContainsKey(ObjectID) == false)
85 Scripts.Add(ObjectID, new Dictionary<string, OpenSim.Region.ScriptEngine.DotNetEngine.Compiler.LSL.LSL_BaseClass>());
86
87 // Delete script if it exists
88 Dictionary<string, OpenSim.Region.ScriptEngine.DotNetEngine.Compiler.LSL.LSL_BaseClass> Obj;
89 Scripts.TryGetValue(ObjectID, out Obj);
90 if (Obj.ContainsKey(ScriptID) == true)
91 Obj.Remove(ScriptID);
92
93 // Add to object
94 Obj.Add(ScriptID, Script);
95
96 }
97
98 /// <summary>
99 /// Fetches, loads and hooks up a script to an objects events
100 /// </summary>
101 /// <param name="ScriptID"></param>
102 /// <param name="ObjectID"></param>
103 public void StartScript(string ScriptID, string ObjectID)
104 {
105 Common.SendToDebug("ScriptManager StartScript: ScriptID: " + ScriptID + ", ObjectID: " + ObjectID);
106
107 // We will initialize and start the script.
108 // It will be up to the script itself to hook up the correct events.
109 string FileName;
110
111 // * Fetch script from server
112 // DEBUG - ScriptID is an actual filename during debug
113 // (therefore we can also check type by looking at extension)
114 FileName = ScriptID;
115
116 // * Does script need compile? Send it to LSL compiler first. (TODO: Use (and clean) compiler cache)
117 if (FileName.ToLower().EndsWith(".lso"))
118 {
119 Common.SendToDebug("ScriptManager Script is LSO, compiling to .Net Assembly");
120 // Create a new instance of the compiler (currently we don't want reuse)
121 OpenSim.Region.ScriptEngine.DotNetEngine.Compiler.LSL.Engine LSLCompiler = new OpenSim.Region.ScriptEngine.DotNetEngine.Compiler.LSL.Engine();
122 // Compile
123 FileName = LSLCompiler.Compile(FileName);
124 }
125
126 // * Insert yield into code
127 FileName = ProcessYield(FileName);
128
129 // * Find next available AppDomain to put it in
130 AppDomain FreeAppDomain = GetFreeAppDomain();
131
132 // * Load and start script
133 OpenSim.Region.ScriptEngine.DotNetEngine.Compiler.LSL.LSL_BaseClass Script = LoadAndInitAssembly(FreeAppDomain, FileName);
134 string FullScriptID = ScriptID + "." + ObjectID;
135 // Add it to our temporary active script keeper
136 //Scripts.Add(FullScriptID, Script);
137 SetScript(ObjectID, ScriptID, Script);
138 // We need to give (untrusted) assembly a private instance of BuiltIns
139 // this private copy will contain Read-Only FullScriptID so that it can bring that on to the server whenever needed.
140 OpenSim.Region.ScriptEngine.DotNetEngine.Compiler.LSL.LSL_BuiltIn_Commands_Interface LSLB = new OpenSim.Region.ScriptEngine.DotNetEngine.Compiler.LSL.LSL_BuiltIn_Commands_TestImplementation(FullScriptID);
141 // Start the script - giving it BuiltIns
142 Common.SendToDebug("ScriptManager initializing script, handing over private builtin command interface");
143 Script.Start(LSLB);
144
145
146 }
147 private string ProcessYield(string FileName)
148 {
149 // TODO: Create a new assembly and copy old but insert Yield Code
150 return FileName;
151 }
152
153 private AppDomain GetFreeAppDomain()
154 {
155 // TODO: Find an available AppDomain - if none, create one and add default security
156 return Thread.GetDomain();
157 }
158
159 /// <summary>
160 /// Does actual loading and initialization of script Assembly
161 /// </summary>
162 /// <param name="FreeAppDomain">AppDomain to load script into</param>
163 /// <param name="FileName">FileName of script assembly (.dll)</param>
164 /// <returns></returns>
165 private OpenSim.Region.ScriptEngine.DotNetEngine.Compiler.LSL.LSL_BaseClass LoadAndInitAssembly(AppDomain FreeAppDomain, string FileName)
166 {
167 Common.SendToDebug("ScriptManager Loading Assembly " + FileName);
168 // Load .Net Assembly (.dll)
169 // Initialize and return it
170
171 // TODO: Add error handling
172 // Script might not follow our rules since users can upload -anything-
173
174 Assembly a;
175 //try
176 //{
177
178
179 // Load to default appdomain (temporary)
180 a = Assembly.LoadFrom(FileName);
181 // Load to specified appdomain
182 // TODO: Insert security
183 //a = FreeAppDomain.Load(FileName);
184 //}
185 //catch (Exception e)
186 //{
187 //}
188
189
190 foreach (Type _t in a.GetTypes())
191 {
192 Console.WriteLine("Type: " + _t.ToString());
193 }
194
195 Type t;
196 //try
197 //{
198 t = a.GetType("LSL_ScriptObject", true);
199 //}
200 //catch (Exception e)
201 //{
202 //}
203
204 return (OpenSim.Region.ScriptEngine.DotNetEngine.Compiler.LSL.LSL_BaseClass)Activator.CreateInstance(t);
205
206
207 }
208
209 internal void ExecuteFunction(string ObjectID, string ScriptID, string FunctionName, object[] args)
210 {
211 Common.SendToDebug("Executing Function ObjectID: " + ObjectID + ", ScriptID: " + ScriptID + ", FunctionName: " + FunctionName);
212 OpenSim.Region.ScriptEngine.DotNetEngine.Compiler.LSL.LSL_BaseClass Script = myScriptEngine.myScriptManager.GetScript(ObjectID, ScriptID);
213
214 Type type = Script.GetType();
215 //object o = (object)Script;
216
217 //System.Collections.Generic.List<string> Functions = (System.Collections.Generic.List<string>)
218 //Type type = typeof(OpenSim.Region.ScriptEngine.DotNetEngine.Compiler.LSL.LSL_BaseClass);
219 Common.SendToDebug("Invoke: \"" + Script.State + "_event_" + FunctionName + "\"");
220 type.InvokeMember(Script.State + "_event_" + FunctionName, BindingFlags.InvokeMethod, null, Script, args);
221 //System.Collections.Generic.List<string> Functions = (System.Collections.Generic.List<string>)type.InvokeMember("GetFunctions", BindingFlags.InvokeMethod, null, Script, null);
222
223
224 //foreach (MemberInfo mi in type.GetMembers())
225 //{
226 // Common.SendToDebug("Member found: " + mi.ToString());
227 //}
228
229 }
230
231 }
232}