diff options
Diffstat (limited to '')
-rw-r--r-- | OpenSim/Region/Environment/Scenes/scripting/Engines/LSLEngine/LSLHandler/LSO_Parser.cs | 555 |
1 files changed, 287 insertions, 268 deletions
diff --git a/OpenSim/Region/Environment/Scenes/scripting/Engines/LSLEngine/LSLHandler/LSO_Parser.cs b/OpenSim/Region/Environment/Scenes/scripting/Engines/LSLEngine/LSLHandler/LSO_Parser.cs index ebe4465..18dca74 100644 --- a/OpenSim/Region/Environment/Scenes/scripting/Engines/LSLEngine/LSLHandler/LSO_Parser.cs +++ b/OpenSim/Region/Environment/Scenes/scripting/Engines/LSLEngine/LSLHandler/LSO_Parser.cs | |||
@@ -1,22 +1,48 @@ | |||
1 | using System; | 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 */ | ||
29 | using System; | ||
2 | using System.Collections.Generic; | 30 | using System.Collections.Generic; |
3 | using System.Text; | 31 | using System.Text; |
4 | using System.IO; | 32 | using System.IO; |
5 | using System.Reflection; | 33 | using System.Reflection; |
6 | using System.Reflection.Emit; | 34 | using System.Reflection.Emit; |
7 | using OpenSim.Region.Scripting; | ||
8 | 35 | ||
9 | namespace OpenSim.ScriptEngines.LSL | 36 | namespace OpenSim.Region.Scripting.LSL |
10 | { | 37 | { |
11 | class LSO_Parser | 38 | partial class LSO_Parser |
12 | { | 39 | { |
13 | private bool Debug = true; | ||
14 | private FileStream fs; | 40 | private FileStream fs; |
15 | private BinaryReader br; | 41 | private BinaryReader br; |
16 | private LSO_Struct.Header myHeader; | 42 | private LSO_Struct.Header myHeader; |
17 | 43 | ||
18 | private TypeBuilder typeBuilder; | 44 | private TypeBuilder typeBuilder; |
19 | private ScriptInfo WorldAPI; | 45 | private System.Collections.Generic.List<string> EventList = new System.Collections.Generic.List<string>(); |
20 | 46 | ||
21 | /// <summary> | 47 | /// <summary> |
22 | /// Parse LSO file. | 48 | /// Parse LSO file. |
@@ -24,12 +50,12 @@ using OpenSim.Region.Scripting; | |||
24 | /// TODO: What else does it do? | 50 | /// TODO: What else does it do? |
25 | /// </summary> | 51 | /// </summary> |
26 | /// <param name="FileName">FileName of LSO ByteCode file</param> | 52 | /// <param name="FileName">FileName of LSO ByteCode file</param> |
27 | public void ParseFile(string FileName, ScriptInfo _WorldAPI, ref TypeBuilder _typeBuilder) | 53 | public void ParseFile(string FileName, TypeBuilder _typeBuilder) |
28 | { | 54 | { |
29 | typeBuilder = _typeBuilder; | 55 | typeBuilder = _typeBuilder; |
30 | WorldAPI = _WorldAPI; | 56 | //WorldAPI = _WorldAPI; |
31 | // Open | 57 | // Open |
32 | SendToDebug("Opening filename: " + FileName); | 58 | Common.SendToDebug("Opening filename: " + FileName); |
33 | fs = File.Open(FileName, FileMode.Open, FileAccess.Read, FileShare.Read); | 59 | fs = File.Open(FileName, FileMode.Open, FileAccess.Read, FileShare.Read); |
34 | br = new BinaryReader(fs, Encoding.BigEndianUnicode); | 60 | br = new BinaryReader(fs, Encoding.BigEndianUnicode); |
35 | 61 | ||
@@ -38,7 +64,7 @@ using OpenSim.Region.Scripting; | |||
38 | 64 | ||
39 | 65 | ||
40 | // HEADER BLOCK | 66 | // HEADER BLOCK |
41 | SendToDebug("Reading HEADER BLOCK at: 0"); | 67 | Common.SendToDebug("Reading HEADER BLOCK at: 0"); |
42 | fs.Seek(0, SeekOrigin.Begin); | 68 | fs.Seek(0, SeekOrigin.Begin); |
43 | myHeader = new LSO_Struct.Header(); | 69 | myHeader = new LSO_Struct.Header(); |
44 | myHeader.TM = BitConverter.ToUInt32(br_read(4), 0); | 70 | myHeader.TM = BitConverter.ToUInt32(br_read(4), 0); |
@@ -65,50 +91,50 @@ using OpenSim.Region.Scripting; | |||
65 | myHeader.NER = BitConverter.ToUInt64(br_read(8), 0); | 91 | myHeader.NER = BitConverter.ToUInt64(br_read(8), 0); |
66 | 92 | ||
67 | // Print Header Block to debug | 93 | // Print Header Block to debug |
68 | SendToDebug("TM - Top of memory (size): " + myHeader.TM); | 94 | Common.SendToDebug("TM - Top of memory (size): " + myHeader.TM); |
69 | SendToDebug("IP - Instruction Pointer (0=not running): " + myHeader.IP); | 95 | Common.SendToDebug("IP - Instruction Pointer (0=not running): " + myHeader.IP); |
70 | SendToDebug("VN - Version number: " + myHeader.VN); | 96 | Common.SendToDebug("VN - Version number: " + myHeader.VN); |
71 | SendToDebug("BP - Local Frame Pointer: " + myHeader.BP); | 97 | Common.SendToDebug("BP - Local Frame Pointer: " + myHeader.BP); |
72 | SendToDebug("SP - Stack Pointer: " + myHeader.SP); | 98 | Common.SendToDebug("SP - Stack Pointer: " + myHeader.SP); |
73 | SendToDebug("HR - Heap Register: " + myHeader.HR); | 99 | Common.SendToDebug("HR - Heap Register: " + myHeader.HR); |
74 | SendToDebug("HP - Heap Pointer: " + myHeader.HP); | 100 | Common.SendToDebug("HP - Heap Pointer: " + myHeader.HP); |
75 | SendToDebug("CS - Current State: " + myHeader.CS); | 101 | Common.SendToDebug("CS - Current State: " + myHeader.CS); |
76 | SendToDebug("NS - Next State: " + myHeader.NS); | 102 | Common.SendToDebug("NS - Next State: " + myHeader.NS); |
77 | SendToDebug("CE - Current Events: " + myHeader.CE); | 103 | Common.SendToDebug("CE - Current Events: " + myHeader.CE); |
78 | SendToDebug("IE - In Event: " + myHeader.IE); | 104 | Common.SendToDebug("IE - In Event: " + myHeader.IE); |
79 | SendToDebug("ER - Event Register: " + myHeader.ER); | 105 | Common.SendToDebug("ER - Event Register: " + myHeader.ER); |
80 | SendToDebug("FR - Fault Register: " + myHeader.FR); | 106 | Common.SendToDebug("FR - Fault Register: " + myHeader.FR); |
81 | SendToDebug("SLR - Sleep Register: " + myHeader.SLR); | 107 | Common.SendToDebug("SLR - Sleep Register: " + myHeader.SLR); |
82 | SendToDebug("GVR - Global Variable Register: " + myHeader.GVR); | 108 | Common.SendToDebug("GVR - Global Variable Register: " + myHeader.GVR); |
83 | SendToDebug("GFR - Global Function Register: " + myHeader.GFR); | 109 | Common.SendToDebug("GFR - Global Function Register: " + myHeader.GFR); |
84 | SendToDebug("PR - Parameter Register: " + myHeader.PR); | 110 | Common.SendToDebug("PR - Parameter Register: " + myHeader.PR); |
85 | SendToDebug("ESR - Energy Supply Register: " + myHeader.ESR); | 111 | Common.SendToDebug("ESR - Energy Supply Register: " + myHeader.ESR); |
86 | SendToDebug("SR - State Register: " + myHeader.SR); | 112 | Common.SendToDebug("SR - State Register: " + myHeader.SR); |
87 | SendToDebug("NCE - 64-bit Current Events: " + myHeader.NCE); | 113 | Common.SendToDebug("NCE - 64-bit Current Events: " + myHeader.NCE); |
88 | SendToDebug("NIE - 64-bit In Events: " + myHeader.NIE); | 114 | Common.SendToDebug("NIE - 64-bit In Events: " + myHeader.NIE); |
89 | SendToDebug("NER - 64-bit Event Register: " + myHeader.NER); | 115 | Common.SendToDebug("NER - 64-bit Event Register: " + myHeader.NER); |
90 | SendToDebug("Read position when exiting HEADER BLOCK: " + fs.Position); | 116 | Common.SendToDebug("Read position when exiting HEADER BLOCK: " + fs.Position); |
91 | 117 | ||
92 | // STATIC BLOCK | 118 | // STATIC BLOCK |
93 | SendToDebug("Reading STATIC BLOCK at: " + myHeader.GVR); | 119 | Common.SendToDebug("Reading STATIC BLOCK at: " + myHeader.GVR); |
94 | fs.Seek(myHeader.GVR, SeekOrigin.Begin); | 120 | fs.Seek(myHeader.GVR, SeekOrigin.Begin); |
95 | int StaticBlockCount = 0; | 121 | int StaticBlockCount = 0; |
96 | // Read function blocks until we hit GFR | 122 | // Read function blocks until we hit GFR |
97 | while (fs.Position < myHeader.GFR) | 123 | while (fs.Position < myHeader.GFR) |
98 | { | 124 | { |
99 | StaticBlockCount++; | 125 | StaticBlockCount++; |
100 | SendToDebug("Reading Static Block " + StaticBlockCount + " at: " + fs.Position); | 126 | Common.SendToDebug("Reading Static Block " + StaticBlockCount + " at: " + fs.Position); |
101 | //fs.Seek(myHeader.GVR, SeekOrigin.Begin); | 127 | //fs.Seek(myHeader.GVR, SeekOrigin.Begin); |
102 | LSO_Struct.StaticBlock myStaticBlock = new LSO_Struct.StaticBlock(); | 128 | LSO_Struct.StaticBlock myStaticBlock = new LSO_Struct.StaticBlock(); |
103 | myStaticBlock.Static_Chunk_Header_Size = BitConverter.ToUInt32(br_read(4), 0); | 129 | myStaticBlock.Static_Chunk_Header_Size = BitConverter.ToUInt32(br_read(4), 0); |
104 | myStaticBlock.ObjectType = br_read(1)[0]; | 130 | myStaticBlock.ObjectType = br_read(1)[0]; |
105 | SendToDebug("Static Block ObjectType: " + ((LSO_Enums.Variable_Type_Codes)myStaticBlock.ObjectType).ToString()); | 131 | Common.SendToDebug("Static Block ObjectType: " + ((LSO_Enums.Variable_Type_Codes)myStaticBlock.ObjectType).ToString()); |
106 | myStaticBlock.Unknown = br_read(1)[0]; | 132 | myStaticBlock.Unknown = br_read(1)[0]; |
107 | // Size of datatype varies | 133 | // Size of datatype varies |
108 | if (myStaticBlock.ObjectType != 0) | 134 | if (myStaticBlock.ObjectType != 0) |
109 | myStaticBlock.BlockVariable = br_read(getObjectSize(myStaticBlock.ObjectType)); | 135 | myStaticBlock.BlockVariable = br_read(getObjectSize(myStaticBlock.ObjectType)); |
110 | } | 136 | } |
111 | SendToDebug("Number of Static Blocks read: " + StaticBlockCount); | 137 | Common.SendToDebug("Number of Static Blocks read: " + StaticBlockCount); |
112 | 138 | ||
113 | 139 | ||
114 | // FUNCTION BLOCK | 140 | // FUNCTION BLOCK |
@@ -117,22 +143,22 @@ using OpenSim.Region.Scripting; | |||
117 | if (myHeader.GFR == myHeader.SR) | 143 | if (myHeader.GFR == myHeader.SR) |
118 | { | 144 | { |
119 | // If GFR and SR are at same position then there is no fuction block | 145 | // If GFR and SR are at same position then there is no fuction block |
120 | SendToDebug("No FUNCTION BLOCK found"); | 146 | Common.SendToDebug("No FUNCTION BLOCK found"); |
121 | } else { | 147 | } else { |
122 | SendToDebug("Reading FUNCTION BLOCK at: " + myHeader.GFR); | 148 | Common.SendToDebug("Reading FUNCTION BLOCK at: " + myHeader.GFR); |
123 | fs.Seek(myHeader.GFR, SeekOrigin.Begin); | 149 | fs.Seek(myHeader.GFR, SeekOrigin.Begin); |
124 | myFunctionBlock.FunctionCount = BitConverter.ToUInt32(br_read(4), 0); | 150 | myFunctionBlock.FunctionCount = BitConverter.ToUInt32(br_read(4), 0); |
125 | SendToDebug("Number of functions in Fuction Block: " + myFunctionBlock.FunctionCount); | 151 | Common.SendToDebug("Number of functions in Fuction Block: " + myFunctionBlock.FunctionCount); |
126 | if (myFunctionBlock.FunctionCount > 0) | 152 | if (myFunctionBlock.FunctionCount > 0) |
127 | { | 153 | { |
128 | myFunctionBlock.CodeChunkPointer = new UInt32[myFunctionBlock.FunctionCount]; | 154 | myFunctionBlock.CodeChunkPointer = new UInt32[myFunctionBlock.FunctionCount]; |
129 | for (int i = 0; i < myFunctionBlock.FunctionCount; i++) | 155 | for (int i = 0; i < myFunctionBlock.FunctionCount; i++) |
130 | { | 156 | { |
131 | SendToDebug("Reading function " + i + " at: " + fs.Position); | 157 | Common.SendToDebug("Reading function " + i + " at: " + fs.Position); |
132 | // TODO: ADD TO FUNCTION LIST (How do we identify it later?) | 158 | // TODO: ADD TO FUNCTION LIST (How do we identify it later?) |
133 | // Note! Absolute position | 159 | // Note! Absolute position |
134 | myFunctionBlock.CodeChunkPointer[i] = BitConverter.ToUInt32(br_read(4), 0) + myHeader.GFR; | 160 | myFunctionBlock.CodeChunkPointer[i] = BitConverter.ToUInt32(br_read(4), 0) + myHeader.GFR; |
135 | SendToDebug("Fuction " + i + " code chunk position: " + myFunctionBlock.CodeChunkPointer[i]); | 161 | Common.SendToDebug("Fuction " + i + " code chunk position: " + myFunctionBlock.CodeChunkPointer[i]); |
136 | } | 162 | } |
137 | } | 163 | } |
138 | } | 164 | } |
@@ -140,7 +166,7 @@ using OpenSim.Region.Scripting; | |||
140 | 166 | ||
141 | // STATE FRAME BLOCK | 167 | // STATE FRAME BLOCK |
142 | // Always right after FUNCTION BLOCK | 168 | // Always right after FUNCTION BLOCK |
143 | SendToDebug("Reading STATE BLOCK at: " + myHeader.SR); | 169 | Common.SendToDebug("Reading STATE BLOCK at: " + myHeader.SR); |
144 | fs.Seek(myHeader.SR, SeekOrigin.Begin); | 170 | fs.Seek(myHeader.SR, SeekOrigin.Begin); |
145 | LSO_Struct.StateFrameBlock myStateFrameBlock = new LSO_Struct.StateFrameBlock(); | 171 | LSO_Struct.StateFrameBlock myStateFrameBlock = new LSO_Struct.StateFrameBlock(); |
146 | myStateFrameBlock.StateCount = BitConverter.ToUInt32(br_read(4), 0); | 172 | myStateFrameBlock.StateCount = BitConverter.ToUInt32(br_read(4), 0); |
@@ -150,12 +176,12 @@ using OpenSim.Region.Scripting; | |||
150 | myStateFrameBlock.StatePointer = new LSO_Struct.StatePointerBlock[myStateFrameBlock.StateCount]; | 176 | myStateFrameBlock.StatePointer = new LSO_Struct.StatePointerBlock[myStateFrameBlock.StateCount]; |
151 | for (int i = 0; i < myStateFrameBlock.StateCount; i++) | 177 | for (int i = 0; i < myStateFrameBlock.StateCount; i++) |
152 | { | 178 | { |
153 | SendToDebug("Reading STATE POINTER BLOCK " + (i+1) + " at: " + fs.Position); | 179 | Common.SendToDebug("Reading STATE POINTER BLOCK " + (i+1) + " at: " + fs.Position); |
154 | // Position is relative to state frame | 180 | // Position is relative to state frame |
155 | myStateFrameBlock.StatePointer[i].Location = myHeader.SR + BitConverter.ToUInt32(br_read(4), 0); | 181 | myStateFrameBlock.StatePointer[i].Location = myHeader.SR + BitConverter.ToUInt32(br_read(4), 0); |
156 | myStateFrameBlock.StatePointer[i].EventMask = new System.Collections.BitArray(br_read(8)); | 182 | myStateFrameBlock.StatePointer[i].EventMask = new System.Collections.BitArray(br_read(8)); |
157 | SendToDebug("Pointer: " + myStateFrameBlock.StatePointer[i].Location); | 183 | Common.SendToDebug("Pointer: " + myStateFrameBlock.StatePointer[i].Location); |
158 | SendToDebug("Total potential EventMask bits: " + myStateFrameBlock.StatePointer[i].EventMask.Count); | 184 | Common.SendToDebug("Total potential EventMask bits: " + myStateFrameBlock.StatePointer[i].EventMask.Count); |
159 | 185 | ||
160 | //// Read STATE BLOCK | 186 | //// Read STATE BLOCK |
161 | //long CurPos = fs.Position; | 187 | //long CurPos = fs.Position; |
@@ -175,7 +201,7 @@ using OpenSim.Region.Scripting; | |||
175 | { | 201 | { |
176 | 202 | ||
177 | fs.Seek(myStateFrameBlock.StatePointer[i].Location, SeekOrigin.Begin); | 203 | fs.Seek(myStateFrameBlock.StatePointer[i].Location, SeekOrigin.Begin); |
178 | SendToDebug("Reading STATE BLOCK " + (i + 1) + " at: " + fs.Position); | 204 | Common.SendToDebug("Reading STATE BLOCK " + (i + 1) + " at: " + fs.Position); |
179 | 205 | ||
180 | // READ: STATE BLOCK HEADER | 206 | // READ: STATE BLOCK HEADER |
181 | myStateFrameBlock.StatePointer[i].StateBlock = new LSO_Struct.StateBlock(); | 207 | myStateFrameBlock.StatePointer[i].StateBlock = new LSO_Struct.StateBlock(); |
@@ -183,9 +209,9 @@ using OpenSim.Region.Scripting; | |||
183 | myStateFrameBlock.StatePointer[i].StateBlock.HeaderSize = BitConverter.ToUInt32(br_read(4), 0); | 209 | myStateFrameBlock.StatePointer[i].StateBlock.HeaderSize = BitConverter.ToUInt32(br_read(4), 0); |
184 | myStateFrameBlock.StatePointer[i].StateBlock.Unknown = br_read(1)[0]; | 210 | myStateFrameBlock.StatePointer[i].StateBlock.Unknown = br_read(1)[0]; |
185 | myStateFrameBlock.StatePointer[i].StateBlock.EndPos = (UInt32)fs.Position; // Note | 211 | myStateFrameBlock.StatePointer[i].StateBlock.EndPos = (UInt32)fs.Position; // Note |
186 | SendToDebug("State block Start Pos: " + myStateFrameBlock.StatePointer[i].StateBlock.StartPos); | 212 | Common.SendToDebug("State block Start Pos: " + myStateFrameBlock.StatePointer[i].StateBlock.StartPos); |
187 | SendToDebug("State block Header Size: " + myStateFrameBlock.StatePointer[i].StateBlock.HeaderSize); | 213 | Common.SendToDebug("State block Header Size: " + myStateFrameBlock.StatePointer[i].StateBlock.HeaderSize); |
188 | SendToDebug("State block Header End Pos: " + myStateFrameBlock.StatePointer[i].StateBlock.EndPos); | 214 | Common.SendToDebug("State block Header End Pos: " + myStateFrameBlock.StatePointer[i].StateBlock.EndPos); |
189 | 215 | ||
190 | // We need to count number of bits flagged in EventMask? | 216 | // We need to count number of bits flagged in EventMask? |
191 | 217 | ||
@@ -202,11 +228,11 @@ using OpenSim.Region.Scripting; | |||
202 | { | 228 | { |
203 | // We got an event | 229 | // We got an event |
204 | // READ: STATE BLOCK HANDLER | 230 | // READ: STATE BLOCK HANDLER |
205 | SendToDebug("Reading STATE BLOCK " + (i + 1) + " HANDLER matching EVENT MASK " + ii + " (" + ((LSO_Enums.Event_Mask_Values)ii).ToString() + ") at: " + fs.Position); | 231 | Common.SendToDebug("Reading STATE BLOCK " + (i + 1) + " HANDLER matching EVENT MASK " + ii + " (" + ((LSO_Enums.Event_Mask_Values)ii).ToString() + ") at: " + fs.Position); |
206 | myStateFrameBlock.StatePointer[i].StateBlock.StateBlockHandlers[ii].CodeChunkPointer = myStateFrameBlock.StatePointer[i].StateBlock.EndPos + BitConverter.ToUInt32(br_read(4), 0); | 232 | myStateFrameBlock.StatePointer[i].StateBlock.StateBlockHandlers[ii].CodeChunkPointer = myStateFrameBlock.StatePointer[i].StateBlock.EndPos + BitConverter.ToUInt32(br_read(4), 0); |
207 | myStateFrameBlock.StatePointer[i].StateBlock.StateBlockHandlers[ii].CallFrameSize = BitConverter.ToUInt32(br_read(4), 0); | 233 | myStateFrameBlock.StatePointer[i].StateBlock.StateBlockHandlers[ii].CallFrameSize = BitConverter.ToUInt32(br_read(4), 0); |
208 | SendToDebug("Reading STATE BLOCK " + (i + 1) + " HANDLER EVENT MASK " + ii + " (" + ((LSO_Enums.Event_Mask_Values)ii).ToString() + ") Code Chunk Pointer: " + myStateFrameBlock.StatePointer[i].StateBlock.StateBlockHandlers[ii].CodeChunkPointer); | 234 | Common.SendToDebug("Reading STATE BLOCK " + (i + 1) + " HANDLER EVENT MASK " + ii + " (" + ((LSO_Enums.Event_Mask_Values)ii).ToString() + ") Code Chunk Pointer: " + myStateFrameBlock.StatePointer[i].StateBlock.StateBlockHandlers[ii].CodeChunkPointer); |
209 | SendToDebug("Reading STATE BLOCK " + (i + 1) + " HANDLER EVENT MASK " + ii + " (" + ((LSO_Enums.Event_Mask_Values)ii).ToString() + ") Call Frame Size: " + myStateFrameBlock.StatePointer[i].StateBlock.StateBlockHandlers[ii].CallFrameSize ); | 235 | Common.SendToDebug("Reading STATE BLOCK " + (i + 1) + " HANDLER EVENT MASK " + ii + " (" + ((LSO_Enums.Event_Mask_Values)ii).ToString() + ") Call Frame Size: " + myStateFrameBlock.StatePointer[i].StateBlock.StateBlockHandlers[ii].CallFrameSize ); |
210 | } | 236 | } |
211 | } | 237 | } |
212 | } | 238 | } |
@@ -224,7 +250,7 @@ using OpenSim.Region.Scripting; | |||
224 | // myFunctionCodeChunk = new LSO_Struct.CodeChunk[myFunctionBlock.FunctionCount]; | 250 | // myFunctionCodeChunk = new LSO_Struct.CodeChunk[myFunctionBlock.FunctionCount]; |
225 | // for (int i = 0; i < myFunctionBlock.FunctionCount; i++) | 251 | // for (int i = 0; i < myFunctionBlock.FunctionCount; i++) |
226 | // { | 252 | // { |
227 | // SendToDebug("Reading Function Code Chunk " + i); | 253 | // Common.SendToDebug("Reading Function Code Chunk " + i); |
228 | // myFunctionCodeChunk[i] = GetCodeChunk((UInt32)myFunctionBlock.CodeChunkPointer[i]); | 254 | // myFunctionCodeChunk[i] = GetCodeChunk((UInt32)myFunctionBlock.CodeChunkPointer[i]); |
229 | // } | 255 | // } |
230 | 256 | ||
@@ -244,32 +270,23 @@ using OpenSim.Region.Scripting; | |||
244 | 270 | ||
245 | if (myStateFrameBlock.StatePointer[i].StateBlock.StateBlockHandlers[ii].CodeChunkPointer > 0) | 271 | if (myStateFrameBlock.StatePointer[i].StateBlock.StateBlockHandlers[ii].CodeChunkPointer > 0) |
246 | { | 272 | { |
247 | SendToDebug("Reading Event Code Chunk state " + i + ", event " + (LSO_Enums.Event_Mask_Values)ii); | 273 | Common.SendToDebug("Reading Event Code Chunk state " + i + ", event " + (LSO_Enums.Event_Mask_Values)ii); |
248 | 274 | ||
249 | 275 | ||
250 | // Override a Method / Function | 276 | // Override a Method / Function |
251 | string eventname = "event_" + (LSO_Enums.Event_Mask_Values)ii; | 277 | string eventname = i + "_event_" + (LSO_Enums.Event_Mask_Values)ii; |
252 | SendToDebug("CLR:" + eventname + ":MethodBuilder methodBuilder = typeBuilder.DefineMethod..."); | 278 | Common.SendToDebug("Event Name: " + eventname); |
253 | MethodBuilder methodBuilder = typeBuilder.DefineMethod(eventname, | 279 | if (Common.IL_ProcessCodeChunks) |
254 | MethodAttributes.Private | MethodAttributes.Virtual, | 280 | { |
255 | typeof(void), | 281 | EventList.Add(eventname); |
256 | new Type[] { typeof(object) }); | 282 | |
257 | 283 | // JUMP TO CODE PROCESSOR | |
258 | SendToDebug("CLR:" + eventname + ":typeBuilder.DefineMethodOverride(methodBuilder..."); | 284 | ProcessCodeChunk(myStateFrameBlock.StatePointer[i].StateBlock.StateBlockHandlers[ii].CodeChunkPointer, typeBuilder, eventname); |
259 | typeBuilder.DefineMethodOverride(methodBuilder, | 285 | } |
260 | typeof(LSL_CLRInterface.LSLScript).GetMethod(eventname)); | ||
261 | |||
262 | // Create the IL generator | ||
263 | |||
264 | SendToDebug("CLR:" + eventname + ":ILGenerator il = methodBuilder.GetILGenerator();"); | ||
265 | ILGenerator il = methodBuilder.GetILGenerator(); | ||
266 | |||
267 | |||
268 | LSO_Struct.CodeChunk myECC = | ||
269 | GetCodeChunk(myStateFrameBlock.StatePointer[i].StateBlock.StateBlockHandlers[ii].CodeChunkPointer, il, eventname); | ||
270 | } | 286 | } |
271 | 287 | ||
272 | } | 288 | } |
289 | |||
273 | } | 290 | } |
274 | 291 | ||
275 | } | 292 | } |
@@ -279,13 +296,16 @@ using OpenSim.Region.Scripting; | |||
279 | br.Close(); | 296 | br.Close(); |
280 | fs.Close(); | 297 | fs.Close(); |
281 | 298 | ||
299 | if (Common.IL_CreateFunctionList) | ||
300 | IL_INSERT_FUNCTIONLIST(); | ||
301 | |||
282 | } | 302 | } |
283 | 303 | ||
284 | private LSO_Struct.HeapBlock GetHeap(UInt32 pos) | 304 | private LSO_Struct.HeapBlock GetHeap(UInt32 pos) |
285 | { | 305 | { |
286 | // HEAP BLOCK | 306 | // HEAP BLOCK |
287 | // TODO:? Special read for strings/keys (null terminated) and lists (pointers to other HEAP entries) | 307 | // TODO:? Special read for strings/keys (null terminated) and lists (pointers to other HEAP entries) |
288 | SendToDebug("Reading HEAP BLOCK at: " + pos); | 308 | Common.SendToDebug("Reading HEAP BLOCK at: " + pos); |
289 | fs.Seek(pos, SeekOrigin.Begin); | 309 | fs.Seek(pos, SeekOrigin.Begin); |
290 | 310 | ||
291 | LSO_Struct.HeapBlock myHeapBlock = new LSO_Struct.HeapBlock(); | 311 | LSO_Struct.HeapBlock myHeapBlock = new LSO_Struct.HeapBlock(); |
@@ -294,15 +314,12 @@ using OpenSim.Region.Scripting; | |||
294 | myHeapBlock.ReferenceCount = BitConverter.ToUInt16(br_read(2), 0); | 314 | myHeapBlock.ReferenceCount = BitConverter.ToUInt16(br_read(2), 0); |
295 | myHeapBlock.Data = br_read(getObjectSize(myHeapBlock.ObjectType)); | 315 | myHeapBlock.Data = br_read(getObjectSize(myHeapBlock.ObjectType)); |
296 | 316 | ||
297 | SendToDebug("Heap Block Data Block Size: " + myHeapBlock.DataBlockSize); | 317 | Common.SendToDebug("Heap Block Data Block Size: " + myHeapBlock.DataBlockSize); |
298 | SendToDebug("Heap Block ObjectType: " + ((LSO_Enums.Variable_Type_Codes)myHeapBlock.ObjectType).ToString()); | 318 | Common.SendToDebug("Heap Block ObjectType: " + ((LSO_Enums.Variable_Type_Codes)myHeapBlock.ObjectType).ToString()); |
299 | SendToDebug("Heap Block Reference Count: " + myHeapBlock.ReferenceCount); | 319 | Common.SendToDebug("Heap Block Reference Count: " + myHeapBlock.ReferenceCount); |
300 | 320 | ||
301 | return myHeapBlock; | 321 | return myHeapBlock; |
302 | } | 322 | } |
303 | |||
304 | |||
305 | |||
306 | private byte[] br_read(int len) | 323 | private byte[] br_read(int len) |
307 | { | 324 | { |
308 | if (len <= 0) | 325 | if (len <= 0) |
@@ -317,7 +334,7 @@ using OpenSim.Region.Scripting; | |||
317 | } | 334 | } |
318 | catch (Exception e) | 335 | catch (Exception e) |
319 | { | 336 | { |
320 | SendToDebug("Exception: " + e.ToString()); | 337 | Common.SendToDebug("Exception: " + e.ToString()); |
321 | throw (e); | 338 | throw (e); |
322 | } | 339 | } |
323 | } | 340 | } |
@@ -327,7 +344,25 @@ using OpenSim.Region.Scripting; | |||
327 | // br.Read(bytes,0, len); | 344 | // br.Read(bytes,0, len); |
328 | // return bytes; | 345 | // return bytes; |
329 | //} | 346 | //} |
330 | 347 | private Type getLLObjectType(byte objectCode) | |
348 | { | ||
349 | switch ((LSO_Enums.Variable_Type_Codes)objectCode) | ||
350 | { | ||
351 | case LSO_Enums.Variable_Type_Codes.Void: return typeof(void); | ||
352 | case LSO_Enums.Variable_Type_Codes.Integer: return typeof(UInt32); | ||
353 | case LSO_Enums.Variable_Type_Codes.Float: return typeof(float); | ||
354 | case LSO_Enums.Variable_Type_Codes.String: return typeof(string); | ||
355 | case LSO_Enums.Variable_Type_Codes.Key: return typeof(string); | ||
356 | case LSO_Enums.Variable_Type_Codes.Vector: return typeof(LSO_Enums.Vector); | ||
357 | case LSO_Enums.Variable_Type_Codes.Rotation: return typeof(LSO_Enums.Rotation); | ||
358 | case LSO_Enums.Variable_Type_Codes.List: | ||
359 | Common.SendToDebug("TODO: List datatype not implemented yet!"); | ||
360 | return typeof(System.Collections.ArrayList); | ||
361 | default: | ||
362 | Common.SendToDebug("Lookup of LSL datatype " + objectCode + " to .Net datatype failed: Unknown LSL datatype. Defaulting to object."); | ||
363 | return typeof(object); | ||
364 | } | ||
365 | } | ||
331 | private int getObjectSize(byte ObjectType) | 366 | private int getObjectSize(byte ObjectType) |
332 | { | 367 | { |
333 | switch (ObjectType) | 368 | switch (ObjectType) |
@@ -346,13 +381,6 @@ using OpenSim.Region.Scripting; | |||
346 | return 0; | 381 | return 0; |
347 | } | 382 | } |
348 | } | 383 | } |
349 | private void SendToDebug(string Message) | ||
350 | { | ||
351 | if (Debug == true) | ||
352 | Console.WriteLine("Debug: " + Message); | ||
353 | } | ||
354 | |||
355 | |||
356 | private string Read_String() | 384 | private string Read_String() |
357 | { | 385 | { |
358 | string ret = ""; | 386 | string ret = ""; |
@@ -365,244 +393,235 @@ using OpenSim.Region.Scripting; | |||
365 | return ret; | 393 | return ret; |
366 | } | 394 | } |
367 | 395 | ||
368 | /// <summary> | 396 | /// <summary> |
369 | /// Reads a code chunk into structure and returns it. | 397 | /// Reads a code chunk and creates IL |
370 | /// </summary> | 398 | /// </summary> |
371 | /// <param name="pos">Absolute position in file. REMEMBER TO ADD myHeader.GFR!</param> | 399 | /// <param name="pos">Absolute position in file. REMEMBER TO ADD myHeader.GFR!</param> |
372 | /// <returns></returns> | 400 | /// <param name="typeBuilder">TypeBuilder for assembly</param> |
373 | private LSO_Struct.CodeChunk GetCodeChunk(UInt32 pos, ILGenerator il, string eventname) | 401 | /// <param name="eventname">Name of event (function) to generate</param> |
402 | private void ProcessCodeChunk(UInt32 pos, TypeBuilder typeBuilder, string eventname) | ||
374 | { | 403 | { |
375 | 404 | ||
376 | /* | ||
377 | * CLR TRY | ||
378 | */ | ||
379 | //SendToDebug("CLR:" + eventname + ":il.BeginExceptionBlock()"); | ||
380 | il.BeginExceptionBlock(); | ||
381 | |||
382 | // Push "Hello World!" string to stack | ||
383 | //SendToDebug("CLR:" + eventname + ":il.Emit(OpCodes.Ldstr..."); | ||
384 | il.Emit(OpCodes.Ldstr, "Starting CLR dynamic execution of: " + eventname); | ||
385 | |||
386 | // Push Console.WriteLine command to stack ... Console.WriteLine("Hello World!"); | ||
387 | //SendToDebug("CLR:" + eventname + ":il.Emit(OpCodes.Call..."); | ||
388 | il.Emit(OpCodes.Call, typeof(Console).GetMethod | ||
389 | ("WriteLine", new Type[] { typeof(string) })); | ||
390 | |||
391 | |||
392 | LSO_Struct.CodeChunk myCodeChunk = new LSO_Struct.CodeChunk(); | 405 | LSO_Struct.CodeChunk myCodeChunk = new LSO_Struct.CodeChunk(); |
393 | 406 | ||
394 | SendToDebug("Reading Function Code Chunk at: " + pos); | 407 | Common.SendToDebug("Reading Function Code Chunk at: " + pos); |
395 | fs.Seek(pos, SeekOrigin.Begin); | 408 | fs.Seek(pos, SeekOrigin.Begin); |
396 | myCodeChunk.CodeChunkHeaderSize = BitConverter.ToUInt32(br_read(4), 0); | 409 | myCodeChunk.CodeChunkHeaderSize = BitConverter.ToUInt32(br_read(4), 0); |
397 | SendToDebug("CodeChunk Header Size: " + myCodeChunk.CodeChunkHeaderSize ); | 410 | Common.SendToDebug("CodeChunk Header Size: " + myCodeChunk.CodeChunkHeaderSize ); |
398 | // Read until null | 411 | // Read until null |
399 | myCodeChunk.Comment = Read_String(); | 412 | myCodeChunk.Comment = Read_String(); |
400 | SendToDebug("Function comment: " + myCodeChunk.Comment); | 413 | Common.SendToDebug("Function comment: " + myCodeChunk.Comment); |
401 | myCodeChunk.ReturnType = br_read(1)[0]; | 414 | myCodeChunk.ReturnType = br_read(1)[0]; |
402 | SendToDebug("Return type: " + (LSO_Enums.Variable_Type_Codes)myCodeChunk.ReturnType); | 415 | Common.SendToDebug("Return type #" + myCodeChunk.ReturnType + ": " + (getLLObjectType(myCodeChunk.ReturnType).ToString())); |
416 | |||
403 | // TODO: How to determine number of codechunks -- does this method work? | 417 | // TODO: How to determine number of codechunks -- does this method work? |
404 | myCodeChunk.CodeChunkArguments = new System.Collections.Generic.List<LSO_Struct.CodeChunkArgument>(); | 418 | myCodeChunk.CodeChunkArguments = new System.Collections.Generic.List<LSO_Struct.CodeChunkArgument>(); |
405 | byte reader = br_read(1)[0]; | 419 | byte reader = br_read(1)[0]; |
406 | reader = br_read(1)[0]; | 420 | reader = br_read(1)[0]; |
421 | |||
422 | // NOTE ON CODE CHUNK ARGUMENTS | ||
423 | // This determins type definition | ||
407 | int ccount = 0; | 424 | int ccount = 0; |
408 | while (reader != 0x000) | 425 | while (reader != 0x000) |
409 | { | 426 | { |
410 | ccount++; | 427 | ccount++; |
411 | SendToDebug("Reading Code Chunk Argument " + ccount); | 428 | Common.SendToDebug("Reading Code Chunk Argument " + ccount); |
412 | LSO_Struct.CodeChunkArgument CCA = new LSO_Struct.CodeChunkArgument(); | 429 | LSO_Struct.CodeChunkArgument CCA = new LSO_Struct.CodeChunkArgument(); |
413 | CCA.FunctionReturnType = reader; | 430 | CCA.FunctionReturnType = reader; |
414 | reader = br_read(1)[0]; | 431 | reader = br_read(1)[0]; |
415 | CCA.NullString = reader; | 432 | CCA.NullString = reader; |
416 | myCodeChunk.CodeChunkArguments.Add(CCA); | 433 | myCodeChunk.CodeChunkArguments.Add(CCA); |
417 | SendToDebug("Code Chunk Argument " + ccount + " return type: " + (LSO_Enums.Variable_Type_Codes)CCA.FunctionReturnType); | 434 | Common.SendToDebug("Code Chunk Argument " + ccount + " type: " + (LSO_Enums.Variable_Type_Codes)CCA.FunctionReturnType); |
435 | } | ||
436 | // Create string array | ||
437 | Type[] MethodArgs = new Type[myCodeChunk.CodeChunkArguments.Count]; | ||
438 | for (int _ic = 0; _ic < myCodeChunk.CodeChunkArguments.Count; _ic++) | ||
439 | { | ||
440 | MethodArgs[_ic] = getLLObjectType(myCodeChunk.CodeChunkArguments[_ic].FunctionReturnType); | ||
441 | Common.SendToDebug("Method argument " + _ic + ": " + getLLObjectType(myCodeChunk.CodeChunkArguments[_ic].FunctionReturnType).ToString()); | ||
418 | } | 442 | } |
419 | // End marker is 0x000 | 443 | // End marker is 0x000 |
420 | myCodeChunk.EndMarker = reader; | 444 | myCodeChunk.EndMarker = reader; |
421 | // TODO: How to read and identify following code | 445 | |
422 | // TODO: Code is read until a return of some sort is found | 446 | |
447 | // | ||
448 | // Emit: START OF METHOD (FUNCTION) | ||
449 | // | ||
450 | |||
451 | Common.SendToDebug("CLR:" + eventname + ":MethodBuilder methodBuilder = typeBuilder.DefineMethod..."); | ||
452 | MethodBuilder methodBuilder = typeBuilder.DefineMethod(eventname, | ||
453 | MethodAttributes.Public, | ||
454 | typeof(void), | ||
455 | MethodArgs); | ||
456 | //typeof(void), //getLLObjectType(myCodeChunk.ReturnType), | ||
457 | // new Type[] { typeof(object) }, //); | ||
458 | |||
459 | //Common.SendToDebug("CLR:" + eventname + ":typeBuilder.DefineMethodOverride(methodBuilder..."); | ||
460 | //typeBuilder.DefineMethodOverride(methodBuilder, | ||
461 | // typeof(LSL_CLRInterface.LSLScript).GetMethod(eventname)); | ||
462 | |||
463 | // Create the IL generator | ||
464 | |||
465 | Common.SendToDebug("CLR:" + eventname + ":ILGenerator il = methodBuilder.GetILGenerator();"); | ||
466 | ILGenerator il = methodBuilder.GetILGenerator(); | ||
467 | |||
468 | |||
469 | if (Common.IL_UseTryCatch) | ||
470 | IL_INSERT_TRY(il, eventname); | ||
471 | |||
472 | |||
473 | |||
474 | // Push Console.WriteLine command to stack ... Console.WriteLine("Hello World!"); | ||
475 | //Common.SendToDebug("CLR:" + eventname + ":il.Emit(OpCodes.Call..."); | ||
476 | //il.Emit(OpCodes.Call, typeof(Console).GetMethod | ||
477 | // ("WriteLine", new Type[] { typeof(string) })); | ||
478 | |||
479 | //Common.SendToDebug("STARTUP: il.Emit(OpCodes.Ldc_I4_S, 0);"); | ||
480 | |||
481 | //il.Emit(OpCodes.Ldc_I4_S, 0); | ||
482 | for (int _ic = 0; _ic < myCodeChunk.CodeChunkArguments.Count; _ic++) | ||
483 | { | ||
484 | Common.SendToDebug("PARAMS: il.Emit(OpCodes.Ldarg, " + _ic + ");"); | ||
485 | il.Emit(OpCodes.Ldarg, _ic); | ||
486 | } | ||
487 | |||
488 | |||
489 | |||
490 | // | ||
491 | // CALLING OPCODE PROCESSOR, one command at the time TO GENERATE IL | ||
492 | // | ||
423 | bool FoundRet = false; | 493 | bool FoundRet = false; |
424 | while (FoundRet == false) | 494 | while (FoundRet == false) |
425 | { | 495 | { |
426 | //reader = br_read(1)[0]; | 496 | FoundRet = LSL_PROCESS_OPCODE(il); |
427 | //UInt16 opcode = BitConverter.ToUInt16(br_read(1),0); | 497 | } |
428 | UInt16 opcode = br_read(1)[0]; | ||
429 | //long rPos = fs.Position; | ||
430 | SendToDebug("OPCODE: " + ((LSO_Enums.Operation_Table)opcode).ToString()); | ||
431 | switch (opcode) | ||
432 | { | ||
433 | // LONG | ||
434 | case (UInt16)LSO_Enums.Operation_Table.POPARG: | ||
435 | case (UInt16)LSO_Enums.Operation_Table.STORE: | ||
436 | case (UInt16)LSO_Enums.Operation_Table.STORES: | ||
437 | case (UInt16)LSO_Enums.Operation_Table.STOREL: | ||
438 | case (UInt16)LSO_Enums.Operation_Table.STOREV: | ||
439 | case (UInt16)LSO_Enums.Operation_Table.STOREQ: | ||
440 | case (UInt16)LSO_Enums.Operation_Table.STOREG: | ||
441 | case (UInt16)LSO_Enums.Operation_Table.STOREGS: | ||
442 | case (UInt16)LSO_Enums.Operation_Table.STOREGL: | ||
443 | case (UInt16)LSO_Enums.Operation_Table.STOREGV: | ||
444 | case (UInt16)LSO_Enums.Operation_Table.STOREGQ: | ||
445 | case (UInt16)LSO_Enums.Operation_Table.LOADP: | ||
446 | case (UInt16)LSO_Enums.Operation_Table.LOADSP: | ||
447 | case (UInt16)LSO_Enums.Operation_Table.LOADLP: | ||
448 | case (UInt16)LSO_Enums.Operation_Table.LOADVP: | ||
449 | case (UInt16)LSO_Enums.Operation_Table.LOADQP: | ||
450 | case (UInt16)LSO_Enums.Operation_Table.PUSH: | ||
451 | case (UInt16)LSO_Enums.Operation_Table.PUSHS: | ||
452 | case (UInt16)LSO_Enums.Operation_Table.PUSHL: | ||
453 | case (UInt16)LSO_Enums.Operation_Table.PUSHV: | ||
454 | case (UInt16)LSO_Enums.Operation_Table.PUSHQ: | ||
455 | case (UInt16)LSO_Enums.Operation_Table.PUSHG: | ||
456 | case (UInt16)LSO_Enums.Operation_Table.PUSHGS: | ||
457 | case (UInt16)LSO_Enums.Operation_Table.PUSHGL: | ||
458 | case (UInt16)LSO_Enums.Operation_Table.PUSHGV: | ||
459 | case (UInt16)LSO_Enums.Operation_Table.PUSHGQ: | ||
460 | SendToDebug("Param1: " + BitConverter.ToUInt32(br_read(4),0)); | ||
461 | break; | ||
462 | // BYTE | ||
463 | case (UInt16)LSO_Enums.Operation_Table.PUSHARGB: | ||
464 | SendToDebug("Param1: " + br_read(1)[0]); | ||
465 | break; | ||
466 | // INTEGER | ||
467 | case (UInt16)LSO_Enums.Operation_Table.PUSHARGI: | ||
468 | // TODO: What is size of integer? | ||
469 | SendToDebug("Param1: " + BitConverter.ToUInt32(br_read(4),0)); | ||
470 | break; | ||
471 | // FLOAT | ||
472 | case (UInt16)LSO_Enums.Operation_Table.PUSHARGF: | ||
473 | // TODO: What is size of float? | ||
474 | SendToDebug("Param1: " + BitConverter.ToUInt32(br_read(4),0)); | ||
475 | break; | ||
476 | // STRING | ||
477 | case (UInt16)LSO_Enums.Operation_Table.PUSHARGS: | ||
478 | string s = Read_String(); | ||
479 | SendToDebug("Param1: " + s); | ||
480 | il.Emit(OpCodes.Ldstr, s); | ||
481 | break; | ||
482 | // VECTOR z,y,x | ||
483 | case (UInt16)LSO_Enums.Operation_Table.PUSHARGV: | ||
484 | SendToDebug("Param1 Z: " + BitConverter.ToUInt32(br_read(4),0)); | ||
485 | SendToDebug("Param1 Y: " + BitConverter.ToUInt32(br_read(4),0)); | ||
486 | SendToDebug("Param1 X: " + BitConverter.ToUInt32(br_read(4),0)); | ||
487 | break; | ||
488 | // ROTATION s,z,y,x | ||
489 | case (UInt16)LSO_Enums.Operation_Table.PUSHARGQ: | ||
490 | SendToDebug("Param1 S: " + BitConverter.ToUInt32(br_read(4),0)); | ||
491 | SendToDebug("Param1 Z: " + BitConverter.ToUInt32(br_read(4),0)); | ||
492 | SendToDebug("Param1 Y: " + BitConverter.ToUInt32(br_read(4),0)); | ||
493 | SendToDebug("Param1 X: " + BitConverter.ToUInt32(br_read(4),0)); | ||
494 | break; | ||
495 | // LONG | ||
496 | case (UInt16)LSO_Enums.Operation_Table.PUSHARGE: | ||
497 | SendToDebug("Param1: " + BitConverter.ToUInt32(br_read(4),0)); | ||
498 | break; | ||
499 | // BYTE | ||
500 | case (UInt16)LSO_Enums.Operation_Table.ADD: | ||
501 | case (UInt16)LSO_Enums.Operation_Table.SUB: | ||
502 | case (UInt16)LSO_Enums.Operation_Table.MUL: | ||
503 | case (UInt16)LSO_Enums.Operation_Table.DIV: | ||
504 | case (UInt16)LSO_Enums.Operation_Table.MOD: | ||
505 | case (UInt16)LSO_Enums.Operation_Table.EQ: | ||
506 | case (UInt16)LSO_Enums.Operation_Table.NEQ: | ||
507 | case (UInt16)LSO_Enums.Operation_Table.LEQ: | ||
508 | case (UInt16)LSO_Enums.Operation_Table.GEQ: | ||
509 | case (UInt16)LSO_Enums.Operation_Table.LESS: | ||
510 | case (UInt16)LSO_Enums.Operation_Table.GREATER: | ||
511 | case (UInt16)LSO_Enums.Operation_Table.BOOLOR: | ||
512 | SendToDebug("Param1: " + br_read(1)[0]); | ||
513 | break; | ||
514 | // LONG | ||
515 | case (UInt16)LSO_Enums.Operation_Table.JUMP: | ||
516 | SendToDebug("Param1: " + BitConverter.ToUInt32(br_read(4),0)); | ||
517 | break; | ||
518 | // BYTE, LONG | ||
519 | case (UInt16)LSO_Enums.Operation_Table.JUMPIF: | ||
520 | case (UInt16)LSO_Enums.Operation_Table.JUMPNIF: | ||
521 | SendToDebug("Param1: " + br_read(1)[0]); | ||
522 | SendToDebug("Param2: " + BitConverter.ToUInt32(br_read(4),0)); | ||
523 | break; | ||
524 | // LONG | ||
525 | case (UInt16)LSO_Enums.Operation_Table.STATE: | ||
526 | case (UInt16)LSO_Enums.Operation_Table.CALL: | ||
527 | SendToDebug("Param1: " + BitConverter.ToUInt32(br_read(4),0)); | ||
528 | break; | ||
529 | // BYTE | ||
530 | case (UInt16)LSO_Enums.Operation_Table.CAST: | ||
531 | SendToDebug("Param1: " + br_read(1)[0]); | ||
532 | break; | ||
533 | // LONG | ||
534 | case (UInt16)LSO_Enums.Operation_Table.STACKTOS: | ||
535 | case (UInt16)LSO_Enums.Operation_Table.STACKTOL: | ||
536 | SendToDebug("Param1: " + BitConverter.ToUInt32(br_read(4),0)); | ||
537 | break; | ||
538 | // BYTE | ||
539 | case (UInt16)LSO_Enums.Operation_Table.PRINT: | ||
540 | case (UInt16)LSO_Enums.Operation_Table.CALLLIB: | ||
541 | SendToDebug("Param1: " + br_read(1)[0]); | ||
542 | break; | ||
543 | // SHORT | ||
544 | case (UInt16)LSO_Enums.Operation_Table.CALLLIB_TWO_BYTE: | ||
545 | // TODO: What is size of short? | ||
546 | UInt16 _i = BitConverter.ToUInt16(br_read(2), 0); | ||
547 | SendToDebug("Param1: " + _i); | ||
548 | switch (_i) | ||
549 | { | ||
550 | case (UInt16)LSO_Enums.BuiltIn_Functions.llSay: | ||
551 | il.Emit(OpCodes.Call, typeof(Console).GetMethod | ||
552 | ("WriteLine", new Type[] { typeof(string) })); | ||
553 | break; | ||
554 | } | ||
555 | break; | ||
556 | 498 | ||
557 | 499 | ||
558 | // RETURN | 500 | if (Common.IL_UseTryCatch) |
559 | case (UInt16)LSO_Enums.Operation_Table.RETURN: | 501 | IL_INSERT_END_TRY(il, eventname); |
560 | SendToDebug("Last OPCODE was return command. Code chunk execution complete."); | 502 | |
561 | FoundRet = true; | 503 | // Emit: RETURN FROM METHOD |
562 | break; | 504 | il.Emit(OpCodes.Ret); |
563 | } | 505 | |
564 | //fs.Seek(rPos, SeekOrigin.Begin); | 506 | return; |
507 | |||
508 | } | ||
509 | |||
510 | private void IL_INSERT_FUNCTIONLIST() | ||
511 | { | ||
512 | |||
513 | Common.SendToDebug("Creating function list"); | ||
514 | |||
515 | |||
516 | string eventname = "GetFunctions"; | ||
517 | |||
518 | Common.SendToDebug("Creating IL " + eventname); | ||
519 | // Define a private String field. | ||
520 | //FieldBuilder myField = myTypeBuilder.DefineField("EventList", typeof(String[]), FieldAttributes.Public); | ||
521 | |||
522 | |||
523 | //FieldBuilder mem = typeBuilder.DefineField("mem", typeof(Array), FieldAttributes.Private); | ||
524 | |||
525 | |||
526 | |||
527 | MethodBuilder methodBuilder = typeBuilder.DefineMethod(eventname, | ||
528 | MethodAttributes.Public, | ||
529 | typeof(string[]), | ||
530 | null); | ||
531 | |||
532 | //typeBuilder.DefineMethodOverride(methodBuilder, | ||
533 | // typeof(LSL_CLRInterface.LSLScript).GetMethod(eventname)); | ||
534 | |||
535 | ILGenerator il = methodBuilder.GetILGenerator(); | ||
536 | |||
537 | |||
538 | |||
539 | |||
540 | // IL_INSERT_TRY(il, eventname); | ||
541 | |||
542 | // // Push string to stack | ||
543 | // il.Emit(OpCodes.Ldstr, "Inside " + eventname); | ||
565 | 544 | ||
545 | //// Push Console.WriteLine command to stack ... Console.WriteLine("Hello World!"); | ||
546 | //il.Emit(OpCodes.Call, typeof(Console).GetMethod | ||
547 | // ("WriteLine", new Type[] { typeof(string) })); | ||
548 | |||
549 | //initIL.Emit(OpCodes.Newobj, typeof(string[])); | ||
550 | |||
551 | //string[] MyArray = new string[2] { "TestItem1" , "TestItem2" }; | ||
552 | |||
553 | il.DeclareLocal(typeof(string[])); | ||
554 | |||
555 | //il.Emit(OpCodes.Ldarg_0); | ||
556 | il.Emit(OpCodes.Ldc_I4, EventList.Count); // Specify array length | ||
557 | il.Emit(OpCodes.Newarr, typeof(String)); // create new string array | ||
558 | il.Emit(OpCodes.Stloc_0); // Store array as local variable 0 in stack | ||
559 | |||
560 | for (int lv = 0; lv < EventList.Count; lv++) | ||
561 | { | ||
562 | il.Emit(OpCodes.Ldloc_0); // Load local variable 0 onto stack | ||
563 | il.Emit(OpCodes.Ldc_I4, lv); // Push index position | ||
564 | il.Emit(OpCodes.Ldstr, EventList[lv]); // Push value | ||
565 | il.Emit(OpCodes.Stelem_Ref); // Perform array[index] = value | ||
566 | } | 566 | } |
567 | 567 | ||
568 | |||
569 | |||
570 | // IL_INSERT_END_TRY(il, eventname); | ||
571 | |||
572 | il.Emit(OpCodes.Ldloc_0); // Load local variable 0 onto stack | ||
573 | il.Emit(OpCodes.Ret); // Return | ||
574 | |||
575 | } | ||
576 | |||
577 | |||
578 | private void IL_INSERT_TRY(ILGenerator il, string eventname) | ||
579 | { | ||
580 | /* | ||
581 | * CLR TRY | ||
582 | */ | ||
583 | //Common.SendToDebug("CLR:" + eventname + ":il.BeginExceptionBlock()"); | ||
584 | il.BeginExceptionBlock(); | ||
585 | |||
586 | // Push "Hello World!" string to stack | ||
587 | //Common.SendToDebug("CLR:" + eventname + ":il.Emit(OpCodes.Ldstr..."); | ||
588 | il.Emit(OpCodes.Ldstr, "Starting CLR dynamic execution of: " + eventname); | ||
568 | 589 | ||
590 | } | ||
591 | |||
592 | private void IL_INSERT_END_TRY(ILGenerator il, string eventname) | ||
593 | { | ||
569 | /* | 594 | /* |
570 | * CATCH | 595 | * CATCH |
571 | */ | 596 | */ |
572 | SendToDebug("CLR:" + eventname + ":il.BeginCatchBlock(typeof(Exception));"); | 597 | Common.SendToDebug("CLR:" + eventname + ":il.BeginCatchBlock(typeof(Exception));"); |
573 | il.BeginCatchBlock(typeof(Exception)); | 598 | il.BeginCatchBlock(typeof(Exception)); |
574 | 599 | ||
575 | // Push "Hello World!" string to stack | 600 | // Push "Hello World!" string to stack |
576 | SendToDebug("CLR:" + eventname + ":il.Emit(OpCodes.Ldstr..."); | 601 | Common.SendToDebug("CLR:" + eventname + ":il.Emit(OpCodes.Ldstr..."); |
577 | il.Emit(OpCodes.Ldstr, "Execption executing dynamic CLR function " + eventname + ": "); | 602 | il.Emit(OpCodes.Ldstr, "Execption executing dynamic CLR function " + eventname + ": "); |
578 | 603 | ||
579 | //call void [mscorlib]System.Console::WriteLine(string) | 604 | //call void [mscorlib]System.Console::WriteLine(string) |
580 | SendToDebug("CLR:" + eventname + ":il.Emit(OpCodes.Call..."); | 605 | Common.SendToDebug("CLR:" + eventname + ":il.Emit(OpCodes.Call..."); |
581 | il.Emit(OpCodes.Call, typeof(Console).GetMethod | 606 | il.Emit(OpCodes.Call, typeof(Console).GetMethod |
582 | ("Write", new Type[] { typeof(string) })); | 607 | ("Write", new Type[] { typeof(string) })); |
583 | 608 | ||
584 | //callvirt instance string [mscorlib]System.Exception::get_Message() | 609 | //callvirt instance string [mscorlib]System.Exception::get_Message() |
585 | SendToDebug("CLR:" + eventname + ":il.Emit(OpCodes.Callvirt..."); | 610 | Common.SendToDebug("CLR:" + eventname + ":il.Emit(OpCodes.Callvirt..."); |
586 | il.Emit(OpCodes.Callvirt, typeof(Exception).GetMethod | 611 | il.Emit(OpCodes.Callvirt, typeof(Exception).GetMethod |
587 | ("get_Message")); | 612 | ("get_Message")); |
588 | 613 | ||
589 | //call void [mscorlib]System.Console::WriteLine(string) | 614 | //call void [mscorlib]System.Console::WriteLine(string) |
590 | SendToDebug("CLR:" + eventname + ":il.Emit(OpCodes.Call..."); | 615 | Common.SendToDebug("CLR:" + eventname + ":il.Emit(OpCodes.Call..."); |
591 | il.Emit(OpCodes.Call, typeof(Console).GetMethod | 616 | il.Emit(OpCodes.Call, typeof(Console).GetMethod |
592 | ("WriteLine", new Type[] { typeof(string) })); | 617 | ("WriteLine", new Type[] { typeof(string) })); |
593 | 618 | ||
594 | /* | 619 | /* |
595 | * CLR END TRY | 620 | * CLR END TRY |
596 | */ | 621 | */ |
597 | //SendToDebug("CLR:" + eventname + ":il.EndExceptionBlock();"); | 622 | //Common.SendToDebug("CLR:" + eventname + ":il.EndExceptionBlock();"); |
598 | il.EndExceptionBlock(); | 623 | il.EndExceptionBlock(); |
599 | // Push "Return from current method, with return value if present" to stack | ||
600 | il.Emit(OpCodes.Ret); | ||
601 | |||
602 | |||
603 | |||
604 | return myCodeChunk; | ||
605 | |||
606 | } | 624 | } |
625 | |||
607 | } | 626 | } |
608 | } | 627 | } |