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