clockwerk-opensim-stable – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 vero 1 /*
2 * Copyright (c) Contributors, http://opensimulator.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 OpenSimulator 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.Runtime.Remoting;
30 using System.Runtime.Remoting.Lifetime;
31 using System.Security.Permissions;
32 using System.Threading;
33 using System.Reflection;
34 using System.Collections;
35 using System.Collections.Generic;
36 using OpenSim.Region.ScriptEngine.Interfaces;
37 using OpenSim.Region.ScriptEngine.Shared;
38 using OpenSim.Region.ScriptEngine.Shared.Api.Runtime;
39  
40 namespace OpenSim.Region.ScriptEngine.Shared.ScriptBase
41 {
42 public partial class ScriptBaseClass : MarshalByRefObject, IScript
43 {
44 private Dictionary<string, MethodInfo> inits = new Dictionary<string, MethodInfo>();
45 // private ScriptSponsor m_sponser;
46  
47 public override Object InitializeLifetimeService()
48 {
49 ILease lease = (ILease)base.InitializeLifetimeService();
50 if (lease.CurrentState == LeaseState.Initial)
51 {
52 // Infinite
53 lease.InitialLeaseTime = TimeSpan.FromMinutes(0);
54 // lease.RenewOnCallTime = TimeSpan.FromSeconds(10.0);
55 // lease.SponsorshipTimeout = TimeSpan.FromMinutes(1.0);
56 }
57 return lease;
58 }
59 #if DEBUG
60 // For tracing GC while debugging
61 public static bool GCDummy = false;
62 ~ScriptBaseClass()
63 {
64 GCDummy = true;
65 }
66 #endif
67  
68 public ScriptBaseClass()
69 {
70 m_Executor = new Executor(this);
71  
72 MethodInfo[] myArrayMethodInfo = GetType().GetMethods(BindingFlags.Public | BindingFlags.Instance);
73  
74 foreach (MethodInfo mi in myArrayMethodInfo)
75 {
76 if (mi.Name.Length > 7 && mi.Name.Substring(0, 7) == "ApiType")
77 {
78 string type = mi.Name.Substring(7);
79 inits[type] = mi;
80 }
81 }
82  
83 // m_sponser = new ScriptSponsor();
84 }
85  
86 private Executor m_Executor = null;
87  
88 public int GetStateEventFlags(string state)
89 {
90 return (int)m_Executor.GetStateEventFlags(state);
91 }
92  
93 public void ExecuteEvent(string state, string FunctionName, object[] args)
94 {
95 m_Executor.ExecuteEvent(state, FunctionName, args);
96 }
97  
98 public string[] GetApis()
99 {
100 string[] apis = new string[inits.Count];
101 inits.Keys.CopyTo(apis, 0);
102 return apis;
103 }
104  
105 private Dictionary<string, object> m_InitialValues =
106 new Dictionary<string, object>();
107 private Dictionary<string, FieldInfo> m_Fields =
108 new Dictionary<string, FieldInfo>();
109  
110 public void InitApi(string api, IScriptApi data)
111 {
112 if (!inits.ContainsKey(api))
113 return;
114  
115 //ILease lease = (ILease)RemotingServices.GetLifetimeService(data as MarshalByRefObject);
116 //RemotingServices.GetLifetimeService(data as MarshalByRefObject);
117 // lease.Register(m_sponser);
118  
119 MethodInfo mi = inits[api];
120  
121 Object[] args = new Object[1];
122 args[0] = data;
123  
124 mi.Invoke(this, args);
125  
126 m_InitialValues = GetVars();
127 }
128  
129 public virtual void StateChange(string newState)
130 {
131 }
132  
133 public void Close()
134 {
135 // m_sponser.Close();
136 }
137  
138 public Dictionary<string, object> GetVars()
139 {
140 Dictionary<string, object> vars = new Dictionary<string, object>();
141  
142 if (m_Fields == null)
143 return vars;
144  
145 m_Fields.Clear();
146  
147 Type t = GetType();
148  
149 FieldInfo[] fields = t.GetFields(BindingFlags.NonPublic |
150 BindingFlags.Public |
151 BindingFlags.Instance |
152 BindingFlags.DeclaredOnly);
153  
154 foreach (FieldInfo field in fields)
155 {
156 m_Fields[field.Name] = field;
157  
158 if (field.FieldType == typeof(LSL_Types.list)) // ref type, copy
159 {
160 LSL_Types.list v = (LSL_Types.list)field.GetValue(this);
161 Object[] data = new Object[v.Data.Length];
162 Array.Copy(v.Data, 0, data, 0, v.Data.Length);
163 LSL_Types.list c = new LSL_Types.list();
164 c.Data = data;
165 vars[field.Name] = c;
166 }
167 else if (field.FieldType == typeof(LSL_Types.LSLInteger) ||
168 field.FieldType == typeof(LSL_Types.LSLString) ||
169 field.FieldType == typeof(LSL_Types.LSLFloat) ||
170 field.FieldType == typeof(Int32) ||
171 field.FieldType == typeof(Double) ||
172 field.FieldType == typeof(Single) ||
173 field.FieldType == typeof(String) ||
174 field.FieldType == typeof(Byte) ||
175 field.FieldType == typeof(short) ||
176 field.FieldType == typeof(LSL_Types.Vector3) ||
177 field.FieldType == typeof(LSL_Types.Quaternion))
178 {
179 vars[field.Name] = field.GetValue(this);
180 }
181 }
182  
183 return vars;
184 }
185  
186 public void SetVars(Dictionary<string, object> vars)
187 {
188 foreach (KeyValuePair<string, object> var in vars)
189 {
190 if (m_Fields.ContainsKey(var.Key))
191 {
192 if (m_Fields[var.Key].FieldType == typeof(LSL_Types.list))
193 {
194 LSL_Types.list v = (LSL_Types.list)m_Fields[var.Key].GetValue(this);
195 Object[] data = ((LSL_Types.list)var.Value).Data;
196 v.Data = new Object[data.Length];
197 Array.Copy(data, 0, v.Data, 0, data.Length);
198 m_Fields[var.Key].SetValue(this, v);
199 }
200 else if (m_Fields[var.Key].FieldType == typeof(LSL_Types.LSLInteger) ||
201 m_Fields[var.Key].FieldType == typeof(LSL_Types.LSLString) ||
202 m_Fields[var.Key].FieldType == typeof(LSL_Types.LSLFloat) ||
203 m_Fields[var.Key].FieldType == typeof(Int32) ||
204 m_Fields[var.Key].FieldType == typeof(Double) ||
205 m_Fields[var.Key].FieldType == typeof(Single) ||
206 m_Fields[var.Key].FieldType == typeof(String) ||
207 m_Fields[var.Key].FieldType == typeof(Byte) ||
208 m_Fields[var.Key].FieldType == typeof(short) ||
209 m_Fields[var.Key].FieldType == typeof(LSL_Types.Vector3) ||
210 m_Fields[var.Key].FieldType == typeof(LSL_Types.Quaternion)
211 )
212 {
213 m_Fields[var.Key].SetValue(this, var.Value);
214 }
215 }
216 }
217 }
218  
219 public void ResetVars()
220 {
221 SetVars(m_InitialValues);
222 }
223  
224 public void NoOp()
225 {
226 // Does what is says on the packet. Nowt, nada, nothing.
227 // Required for insertion after a jump label to do what it says on the packet!
228 // With a bit of luck the compiler may even optimize it out.
229 }
230 }
231 }