diff options
author | Tedd Hansen | 2008-01-12 15:49:41 +0000 |
---|---|---|
committer | Tedd Hansen | 2008-01-12 15:49:41 +0000 |
commit | 27859faf7abdee7874819539dc518ea9e44437a5 (patch) | |
tree | a612281fe31a9c2d99e2756410f97395961b03b0 /OpenSim/Region/ScriptEngine/LSOEngine/LSO/LSO_Parser.cs | |
parent | Set eol (diff) | |
download | opensim-SC-27859faf7abdee7874819539dc518ea9e44437a5.zip opensim-SC-27859faf7abdee7874819539dc518ea9e44437a5.tar.gz opensim-SC-27859faf7abdee7874819539dc518ea9e44437a5.tar.bz2 opensim-SC-27859faf7abdee7874819539dc518ea9e44437a5.tar.xz |
Welcome back LSO.
Added old LSO code, will use it as base to create new LSO VM.
Diffstat (limited to 'OpenSim/Region/ScriptEngine/LSOEngine/LSO/LSO_Parser.cs')
-rw-r--r-- | OpenSim/Region/ScriptEngine/LSOEngine/LSO/LSO_Parser.cs | 729 |
1 files changed, 729 insertions, 0 deletions
diff --git a/OpenSim/Region/ScriptEngine/LSOEngine/LSO/LSO_Parser.cs b/OpenSim/Region/ScriptEngine/LSOEngine/LSO/LSO_Parser.cs new file mode 100644 index 0000000..1d3992f --- /dev/null +++ b/OpenSim/Region/ScriptEngine/LSOEngine/LSO/LSO_Parser.cs | |||
@@ -0,0 +1,729 @@ | |||
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 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; | ||
30 | using System.Collections; | ||
31 | using System.Collections.Generic; | ||
32 | using System.IO; | ||
33 | using System.Reflection; | ||
34 | using System.Reflection.Emit; | ||
35 | using System.Text; | ||
36 | using OpenSim.Region.ScriptEngine.LSOEngine.LSO; | ||
37 | |||
38 | namespace OpenSim.Region.ScriptEngine.LSOEngine.LSO | ||
39 | { | ||
40 | internal partial class LSO_Parser | ||
41 | { | ||
42 | private string FileName; | ||
43 | private FileStream fs; | ||
44 | private BinaryReader br; | ||
45 | internal LSO_Struct.Header myHeader; | ||
46 | internal Dictionary<long, LSO_Struct.StaticBlock> StaticBlocks = new Dictionary<long, LSO_Struct.StaticBlock>(); | ||
47 | //private System.Collections.Hashtable StaticBlocks = new System.Collections.Hashtable(); | ||
48 | |||
49 | private TypeBuilder typeBuilder; | ||
50 | private List<string> EventList = new List<string>(); | ||
51 | |||
52 | public LSO_Parser(string _FileName, TypeBuilder _typeBuilder) | ||
53 | { | ||
54 | FileName = _FileName; | ||
55 | typeBuilder = _typeBuilder; | ||
56 | } | ||
57 | |||
58 | internal void OpenFile() | ||
59 | { | ||
60 | // Open | ||
61 | Common.SendToDebug("Opening filename: " + FileName); | ||
62 | fs = File.Open(FileName, FileMode.Open, FileAccess.Read, FileShare.Read); | ||
63 | br = new BinaryReader(fs, Encoding.BigEndianUnicode); | ||
64 | } | ||
65 | |||
66 | internal void CloseFile() | ||
67 | { | ||
68 | // Close | ||
69 | br.Close(); | ||
70 | fs.Close(); | ||
71 | } | ||
72 | |||
73 | |||
74 | /// <summary> | ||
75 | /// Parse LSO file. | ||
76 | /// </summary> | ||
77 | public void Parse() | ||
78 | { | ||
79 | // The LSO Format consist of 6 major blocks: header, statics, functions, states, heap, and stack. | ||
80 | |||
81 | |||
82 | // HEADER BLOCK | ||
83 | Common.SendToDebug("Reading HEADER BLOCK at: 0"); | ||
84 | fs.Seek(0, SeekOrigin.Begin); | ||
85 | myHeader = new LSO_Struct.Header(); | ||
86 | myHeader.TM = BitConverter.ToUInt32(br_read(4), 0); | ||
87 | myHeader.IP = BitConverter.ToUInt32(br_read(4), 0); | ||
88 | myHeader.VN = BitConverter.ToUInt32(br_read(4), 0); | ||
89 | myHeader.BP = BitConverter.ToUInt32(br_read(4), 0); | ||
90 | myHeader.SP = BitConverter.ToUInt32(br_read(4), 0); | ||
91 | myHeader.HR = BitConverter.ToUInt32(br_read(4), 0); | ||
92 | myHeader.HP = BitConverter.ToUInt32(br_read(4), 0); | ||
93 | myHeader.CS = BitConverter.ToUInt32(br_read(4), 0); | ||
94 | myHeader.NS = BitConverter.ToUInt32(br_read(4), 0); | ||
95 | myHeader.CE = BitConverter.ToUInt32(br_read(4), 0); | ||
96 | myHeader.IE = BitConverter.ToUInt32(br_read(4), 0); | ||
97 | myHeader.ER = BitConverter.ToUInt32(br_read(4), 0); | ||
98 | myHeader.FR = BitConverter.ToUInt32(br_read(4), 0); | ||
99 | myHeader.SLR = BitConverter.ToUInt32(br_read(4), 0); | ||
100 | myHeader.GVR = BitConverter.ToUInt32(br_read(4), 0); | ||
101 | myHeader.GFR = BitConverter.ToUInt32(br_read(4), 0); | ||
102 | myHeader.PR = BitConverter.ToUInt32(br_read(4), 0); | ||
103 | myHeader.ESR = BitConverter.ToUInt32(br_read(4), 0); | ||
104 | myHeader.SR = BitConverter.ToUInt32(br_read(4), 0); | ||
105 | myHeader.NCE = BitConverter.ToUInt64(br_read(8), 0); | ||
106 | myHeader.NIE = BitConverter.ToUInt64(br_read(8), 0); | ||
107 | myHeader.NER = BitConverter.ToUInt64(br_read(8), 0); | ||
108 | |||
109 | // Print Header Block to debug | ||
110 | Common.SendToDebug("TM - Top of memory (size): " + myHeader.TM); | ||
111 | Common.SendToDebug("IP - Instruction Pointer (0=not running): " + myHeader.IP); | ||
112 | Common.SendToDebug("VN - Version number: " + myHeader.VN); | ||
113 | Common.SendToDebug("BP - Local Frame Pointer: " + myHeader.BP); | ||
114 | Common.SendToDebug("SP - Stack Pointer: " + myHeader.SP); | ||
115 | Common.SendToDebug("HR - Heap Register: " + myHeader.HR); | ||
116 | Common.SendToDebug("HP - Heap Pointer: " + myHeader.HP); | ||
117 | Common.SendToDebug("CS - Current State: " + myHeader.CS); | ||
118 | Common.SendToDebug("NS - Next State: " + myHeader.NS); | ||
119 | Common.SendToDebug("CE - Current Events: " + myHeader.CE); | ||
120 | Common.SendToDebug("IE - In Event: " + myHeader.IE); | ||
121 | Common.SendToDebug("ER - Event Register: " + myHeader.ER); | ||
122 | Common.SendToDebug("FR - Fault Register: " + myHeader.FR); | ||
123 | Common.SendToDebug("SLR - Sleep Register: " + myHeader.SLR); | ||
124 | Common.SendToDebug("GVR - Global Variable Register: " + myHeader.GVR); | ||
125 | Common.SendToDebug("GFR - Global Function Register: " + myHeader.GFR); | ||
126 | Common.SendToDebug("PR - Parameter Register: " + myHeader.PR); | ||
127 | Common.SendToDebug("ESR - Energy Supply Register: " + myHeader.ESR); | ||
128 | Common.SendToDebug("SR - State Register: " + myHeader.SR); | ||
129 | Common.SendToDebug("NCE - 64-bit Current Events: " + myHeader.NCE); | ||
130 | Common.SendToDebug("NIE - 64-bit In Events: " + myHeader.NIE); | ||
131 | Common.SendToDebug("NER - 64-bit Event Register: " + myHeader.NER); | ||
132 | Common.SendToDebug("Read position when exiting HEADER BLOCK: " + fs.Position); | ||
133 | |||
134 | // STATIC BLOCK | ||
135 | Common.SendToDebug("Reading STATIC BLOCK at: " + myHeader.GVR); | ||
136 | fs.Seek(myHeader.GVR, SeekOrigin.Begin); | ||
137 | int StaticBlockCount = 0; | ||
138 | // Read function blocks until we hit GFR | ||
139 | while (fs.Position < myHeader.GFR) | ||
140 | { | ||
141 | StaticBlockCount++; | ||
142 | long startReadPos = fs.Position; | ||
143 | Common.SendToDebug("Reading Static Block " + StaticBlockCount + " at: " + startReadPos); | ||
144 | |||
145 | //fs.Seek(myHeader.GVR, SeekOrigin.Begin); | ||
146 | LSO_Struct.StaticBlock myStaticBlock = new LSO_Struct.StaticBlock(); | ||
147 | myStaticBlock.Static_Chunk_Header_Size = BitConverter.ToUInt32(br_read(4), 0); | ||
148 | myStaticBlock.ObjectType = br_read(1)[0]; | ||
149 | Common.SendToDebug("Static Block ObjectType: " + | ||
150 | ((LSO_Enums.Variable_Type_Codes) myStaticBlock.ObjectType).ToString()); | ||
151 | myStaticBlock.Unknown = br_read(1)[0]; | ||
152 | // Size of datatype varies -- what about strings? | ||
153 | if (myStaticBlock.ObjectType != 0) | ||
154 | myStaticBlock.BlockVariable = br_read(getObjectSize(myStaticBlock.ObjectType)); | ||
155 | |||
156 | StaticBlocks.Add((UInt32) startReadPos, myStaticBlock); | ||
157 | } | ||
158 | Common.SendToDebug("Number of Static Blocks read: " + StaticBlockCount); | ||
159 | |||
160 | |||
161 | // FUNCTION BLOCK | ||
162 | // Always right after STATIC BLOCK | ||
163 | LSO_Struct.FunctionBlock myFunctionBlock = new LSO_Struct.FunctionBlock(); | ||
164 | if (myHeader.GFR == myHeader.SR) | ||
165 | { | ||
166 | // If GFR and SR are at same position then there is no fuction block | ||
167 | Common.SendToDebug("No FUNCTION BLOCK found"); | ||
168 | } | ||
169 | else | ||
170 | { | ||
171 | Common.SendToDebug("Reading FUNCTION BLOCK at: " + myHeader.GFR); | ||
172 | fs.Seek(myHeader.GFR, SeekOrigin.Begin); | ||
173 | myFunctionBlock.FunctionCount = BitConverter.ToUInt32(br_read(4), 0); | ||
174 | Common.SendToDebug("Number of functions in Fuction Block: " + myFunctionBlock.FunctionCount); | ||
175 | if (myFunctionBlock.FunctionCount > 0) | ||
176 | { | ||
177 | myFunctionBlock.CodeChunkPointer = new UInt32[myFunctionBlock.FunctionCount]; | ||
178 | for (int i = 0; i < myFunctionBlock.FunctionCount; i++) | ||
179 | { | ||
180 | Common.SendToDebug("Reading function " + i + " at: " + fs.Position); | ||
181 | // TODO: ADD TO FUNCTION LIST (How do we identify it later?) | ||
182 | // Note! Absolute position | ||
183 | myFunctionBlock.CodeChunkPointer[i] = BitConverter.ToUInt32(br_read(4), 0) + myHeader.GFR; | ||
184 | Common.SendToDebug("Fuction " + i + " code chunk position: " + | ||
185 | myFunctionBlock.CodeChunkPointer[i]); | ||
186 | } | ||
187 | } | ||
188 | } | ||
189 | |||
190 | |||
191 | // STATE FRAME BLOCK | ||
192 | // Always right after FUNCTION BLOCK | ||
193 | Common.SendToDebug("Reading STATE BLOCK at: " + myHeader.SR); | ||
194 | fs.Seek(myHeader.SR, SeekOrigin.Begin); | ||
195 | LSO_Struct.StateFrameBlock myStateFrameBlock = new LSO_Struct.StateFrameBlock(); | ||
196 | myStateFrameBlock.StateCount = BitConverter.ToUInt32(br_read(4), 0); | ||
197 | if (myStateFrameBlock.StateCount > 0) | ||
198 | { | ||
199 | // Initialize array | ||
200 | myStateFrameBlock.StatePointer = new LSO_Struct.StatePointerBlock[myStateFrameBlock.StateCount]; | ||
201 | for (int i = 0; i < myStateFrameBlock.StateCount; i++) | ||
202 | { | ||
203 | Common.SendToDebug("Reading STATE POINTER BLOCK " + (i + 1) + " at: " + fs.Position); | ||
204 | // Position is relative to state frame | ||
205 | myStateFrameBlock.StatePointer[i].Location = myHeader.SR + BitConverter.ToUInt32(br_read(4), 0); | ||
206 | myStateFrameBlock.StatePointer[i].EventMask = new BitArray(br_read(8)); | ||
207 | Common.SendToDebug("Pointer: " + myStateFrameBlock.StatePointer[i].Location); | ||
208 | Common.SendToDebug("Total potential EventMask bits: " + | ||
209 | myStateFrameBlock.StatePointer[i].EventMask.Count); | ||
210 | |||
211 | //// Read STATE BLOCK | ||
212 | //long CurPos = fs.Position; | ||
213 | //fs.Seek(CurPos, SeekOrigin.Begin); | ||
214 | } | ||
215 | } | ||
216 | |||
217 | |||
218 | // STATE BLOCK | ||
219 | // For each StateFrameBlock there is one StateBlock with multiple event handlers | ||
220 | |||
221 | if (myStateFrameBlock.StateCount > 0) | ||
222 | { | ||
223 | // Go through all State Frame Pointers found | ||
224 | for (int i = 0; i < myStateFrameBlock.StateCount; i++) | ||
225 | { | ||
226 | fs.Seek(myStateFrameBlock.StatePointer[i].Location, SeekOrigin.Begin); | ||
227 | Common.SendToDebug("Reading STATE BLOCK " + (i + 1) + " at: " + fs.Position); | ||
228 | |||
229 | // READ: STATE BLOCK HEADER | ||
230 | myStateFrameBlock.StatePointer[i].StateBlock = new LSO_Struct.StateBlock(); | ||
231 | myStateFrameBlock.StatePointer[i].StateBlock.StartPos = (UInt32) fs.Position; // Note | ||
232 | myStateFrameBlock.StatePointer[i].StateBlock.HeaderSize = BitConverter.ToUInt32(br_read(4), 0); | ||
233 | myStateFrameBlock.StatePointer[i].StateBlock.Unknown = br_read(1)[0]; | ||
234 | myStateFrameBlock.StatePointer[i].StateBlock.EndPos = (UInt32) fs.Position; // Note | ||
235 | Common.SendToDebug("State block Start Pos: " + myStateFrameBlock.StatePointer[i].StateBlock.StartPos); | ||
236 | Common.SendToDebug("State block Header Size: " + | ||
237 | myStateFrameBlock.StatePointer[i].StateBlock.HeaderSize); | ||
238 | Common.SendToDebug("State block Header End Pos: " + | ||
239 | myStateFrameBlock.StatePointer[i].StateBlock.EndPos); | ||
240 | |||
241 | // We need to count number of bits flagged in EventMask? | ||
242 | |||
243 | |||
244 | // for each bit in myStateFrameBlock.StatePointer[i].EventMask | ||
245 | |||
246 | // ADDING TO ALL RIGHT NOW, SHOULD LIMIT TO ONLY THE ONES IN USE | ||
247 | //TODO: Create event hooks | ||
248 | myStateFrameBlock.StatePointer[i].StateBlock.StateBlockHandlers = | ||
249 | new LSO_Struct.StateBlockHandler[myStateFrameBlock.StatePointer[i].EventMask.Count - 1]; | ||
250 | for (int ii = 0; ii < myStateFrameBlock.StatePointer[i].EventMask.Count - 1; ii++) | ||
251 | { | ||
252 | if (myStateFrameBlock.StatePointer[i].EventMask.Get(ii) == true) | ||
253 | { | ||
254 | // We got an event | ||
255 | // READ: STATE BLOCK HANDLER | ||
256 | Common.SendToDebug("Reading STATE BLOCK " + (i + 1) + " HANDLER matching EVENT MASK " + ii + | ||
257 | " (" + ((LSO_Enums.Event_Mask_Values) ii).ToString() + ") at: " + | ||
258 | fs.Position); | ||
259 | myStateFrameBlock.StatePointer[i].StateBlock.StateBlockHandlers[ii].CodeChunkPointer = | ||
260 | myStateFrameBlock.StatePointer[i].StateBlock.EndPos + | ||
261 | BitConverter.ToUInt32(br_read(4), 0); | ||
262 | myStateFrameBlock.StatePointer[i].StateBlock.StateBlockHandlers[ii].CallFrameSize = | ||
263 | BitConverter.ToUInt32(br_read(4), 0); | ||
264 | Common.SendToDebug("Reading STATE BLOCK " + (i + 1) + " HANDLER EVENT MASK " + ii + " (" + | ||
265 | ((LSO_Enums.Event_Mask_Values) ii).ToString() + ") Code Chunk Pointer: " + | ||
266 | myStateFrameBlock.StatePointer[i].StateBlock.StateBlockHandlers[ii]. | ||
267 | CodeChunkPointer); | ||
268 | Common.SendToDebug("Reading STATE BLOCK " + (i + 1) + " HANDLER EVENT MASK " + ii + " (" + | ||
269 | ((LSO_Enums.Event_Mask_Values) ii).ToString() + ") Call Frame Size: " + | ||
270 | myStateFrameBlock.StatePointer[i].StateBlock.StateBlockHandlers[ii]. | ||
271 | CallFrameSize); | ||
272 | } | ||
273 | } | ||
274 | } | ||
275 | } | ||
276 | |||
277 | |||
278 | //// READ FUNCTION CODE CHUNKS | ||
279 | //// Functions + Function start pos (GFR) | ||
280 | //// TODO: Somehow be able to identify and reference this | ||
281 | //LSO_Struct.CodeChunk[] myFunctionCodeChunk; | ||
282 | //if (myFunctionBlock.FunctionCount > 0) | ||
283 | //{ | ||
284 | // myFunctionCodeChunk = new LSO_Struct.CodeChunk[myFunctionBlock.FunctionCount]; | ||
285 | // for (int i = 0; i < myFunctionBlock.FunctionCount; i++) | ||
286 | // { | ||
287 | // Common.SendToDebug("Reading Function Code Chunk " + i); | ||
288 | // myFunctionCodeChunk[i] = GetCodeChunk((UInt32)myFunctionBlock.CodeChunkPointer[i]); | ||
289 | // } | ||
290 | |||
291 | //} | ||
292 | // READ EVENT CODE CHUNKS | ||
293 | LSO_Struct.CodeChunk[] myEventCodeChunk; | ||
294 | if (myStateFrameBlock.StateCount > 0) | ||
295 | { | ||
296 | myEventCodeChunk = new LSO_Struct.CodeChunk[myStateFrameBlock.StateCount]; | ||
297 | for (int i = 0; i < myStateFrameBlock.StateCount; i++) | ||
298 | { | ||
299 | // TODO: Somehow organize events and functions so they can be found again, | ||
300 | // two level search ain't no good | ||
301 | for (int ii = 0; ii < myStateFrameBlock.StatePointer[i].EventMask.Count - 1; ii++) | ||
302 | { | ||
303 | if (myStateFrameBlock.StatePointer[i].StateBlock.StateBlockHandlers[ii].CodeChunkPointer > 0) | ||
304 | { | ||
305 | Common.SendToDebug("Reading Event Code Chunk state " + i + ", event " + | ||
306 | (LSO_Enums.Event_Mask_Values) ii); | ||
307 | |||
308 | |||
309 | // Override a Method / Function | ||
310 | string eventname = i + "_event_" + (LSO_Enums.Event_Mask_Values) ii; | ||
311 | Common.SendToDebug("Event Name: " + eventname); | ||
312 | if (Common.IL_ProcessCodeChunks) | ||
313 | { | ||
314 | EventList.Add(eventname); | ||
315 | |||
316 | // JUMP TO CODE PROCESSOR | ||
317 | ProcessCodeChunk( | ||
318 | myStateFrameBlock.StatePointer[i].StateBlock.StateBlockHandlers[ii].CodeChunkPointer, | ||
319 | typeBuilder, eventname); | ||
320 | } | ||
321 | } | ||
322 | } | ||
323 | } | ||
324 | } | ||
325 | |||
326 | |||
327 | if (Common.IL_CreateFunctionList) | ||
328 | IL_INSERT_FUNCTIONLIST(); | ||
329 | } | ||
330 | |||
331 | internal LSO_Struct.HeapBlock GetHeap(UInt32 pos) | ||
332 | { | ||
333 | // HEAP BLOCK | ||
334 | // TODO:? Special read for strings/keys (null terminated) and lists (pointers to other HEAP entries) | ||
335 | Common.SendToDebug("Reading HEAP BLOCK at: " + pos); | ||
336 | fs.Seek(pos, SeekOrigin.Begin); | ||
337 | |||
338 | LSO_Struct.HeapBlock myHeapBlock = new LSO_Struct.HeapBlock(); | ||
339 | myHeapBlock.DataBlockSize = BitConverter.ToInt32(br_read(4), 0); | ||
340 | myHeapBlock.ObjectType = br_read(1)[0]; | ||
341 | myHeapBlock.ReferenceCount = BitConverter.ToUInt16(br_read(2), 0); | ||
342 | //myHeapBlock.Data = br_read(getObjectSize(myHeapBlock.ObjectType)); | ||
343 | // Don't read it reversed | ||
344 | myHeapBlock.Data = new byte[myHeapBlock.DataBlockSize - 1]; | ||
345 | br.Read(myHeapBlock.Data, 0, myHeapBlock.DataBlockSize - 1); | ||
346 | |||
347 | |||
348 | Common.SendToDebug("Heap Block Data Block Size: " + myHeapBlock.DataBlockSize); | ||
349 | Common.SendToDebug("Heap Block ObjectType: " + | ||
350 | ((LSO_Enums.Variable_Type_Codes) myHeapBlock.ObjectType).ToString()); | ||
351 | Common.SendToDebug("Heap Block Reference Count: " + myHeapBlock.ReferenceCount); | ||
352 | |||
353 | return myHeapBlock; | ||
354 | } | ||
355 | |||
356 | private byte[] br_read(int len) | ||
357 | { | ||
358 | if (len <= 0) | ||
359 | return null; | ||
360 | |||
361 | try | ||
362 | { | ||
363 | byte[] bytes = new byte[len]; | ||
364 | for (int i = len - 1; i > -1; i--) | ||
365 | bytes[i] = br.ReadByte(); | ||
366 | return bytes; | ||
367 | } | ||
368 | catch (Exception e) | ||
369 | { | ||
370 | Common.SendToDebug("Exception: " + e.ToString()); | ||
371 | throw (e); | ||
372 | } | ||
373 | } | ||
374 | |||
375 | //private byte[] br_read_smallendian(int len) | ||
376 | //{ | ||
377 | // byte[] bytes = new byte[len]; | ||
378 | // br.Read(bytes,0, len); | ||
379 | // return bytes; | ||
380 | //} | ||
381 | private Type getLLObjectType(byte objectCode) | ||
382 | { | ||
383 | switch ((LSO_Enums.Variable_Type_Codes) objectCode) | ||
384 | { | ||
385 | case LSO_Enums.Variable_Type_Codes.Void: | ||
386 | return typeof (void); | ||
387 | case LSO_Enums.Variable_Type_Codes.Integer: | ||
388 | return typeof (UInt32); | ||
389 | case LSO_Enums.Variable_Type_Codes.Float: | ||
390 | return typeof (float); | ||
391 | case LSO_Enums.Variable_Type_Codes.String: | ||
392 | return typeof (string); | ||
393 | case LSO_Enums.Variable_Type_Codes.Key: | ||
394 | return typeof (string); | ||
395 | case LSO_Enums.Variable_Type_Codes.Vector: | ||
396 | return typeof (LSO_Enums.Vector); | ||
397 | case LSO_Enums.Variable_Type_Codes.Rotation: | ||
398 | return typeof (LSO_Enums.Rotation); | ||
399 | case LSO_Enums.Variable_Type_Codes.List: | ||
400 | Common.SendToDebug("TODO: List datatype not implemented yet!"); | ||
401 | return typeof (ArrayList); | ||
402 | case LSO_Enums.Variable_Type_Codes.Null: | ||
403 | Common.SendToDebug("TODO: Datatype null is not implemented, using string instead.!"); | ||
404 | return typeof (string); | ||
405 | default: | ||
406 | Common.SendToDebug("Lookup of LSL datatype " + objectCode + | ||
407 | " to .Net datatype failed: Unknown LSL datatype. Defaulting to object."); | ||
408 | return typeof (object); | ||
409 | } | ||
410 | } | ||
411 | |||
412 | private int getObjectSize(byte ObjectType) | ||
413 | { | ||
414 | switch ((LSO_Enums.Variable_Type_Codes) ObjectType) | ||
415 | { | ||
416 | case LSO_Enums.Variable_Type_Codes.Integer: | ||
417 | case LSO_Enums.Variable_Type_Codes.Float: | ||
418 | case LSO_Enums.Variable_Type_Codes.String: | ||
419 | case LSO_Enums.Variable_Type_Codes.Key: | ||
420 | case LSO_Enums.Variable_Type_Codes.List: | ||
421 | return 4; | ||
422 | case LSO_Enums.Variable_Type_Codes.Vector: | ||
423 | return 12; | ||
424 | case LSO_Enums.Variable_Type_Codes.Rotation: | ||
425 | return 16; | ||
426 | default: | ||
427 | return 0; | ||
428 | } | ||
429 | } | ||
430 | |||
431 | private string Read_String() | ||
432 | { | ||
433 | string ret = ""; | ||
434 | byte reader = br_read(1)[0]; | ||
435 | while (reader != 0x000) | ||
436 | { | ||
437 | ret += (char) reader; | ||
438 | reader = br_read(1)[0]; | ||
439 | } | ||
440 | return ret; | ||
441 | } | ||
442 | |||
443 | /// <summary> | ||
444 | /// Reads a code chunk and creates IL | ||
445 | /// </summary> | ||
446 | /// <param name="pos">Absolute position in file. REMEMBER TO ADD myHeader.GFR!</param> | ||
447 | /// <param name="typeBuilder">TypeBuilder for assembly</param> | ||
448 | /// <param name="eventname">Name of event (function) to generate</param> | ||
449 | private void ProcessCodeChunk(UInt32 pos, TypeBuilder typeBuilder, string eventname) | ||
450 | { | ||
451 | LSO_Struct.CodeChunk myCodeChunk = new LSO_Struct.CodeChunk(); | ||
452 | |||
453 | Common.SendToDebug("Reading Function Code Chunk at: " + pos); | ||
454 | fs.Seek(pos, SeekOrigin.Begin); | ||
455 | myCodeChunk.CodeChunkHeaderSize = BitConverter.ToUInt32(br_read(4), 0); | ||
456 | Common.SendToDebug("CodeChunk Header Size: " + myCodeChunk.CodeChunkHeaderSize); | ||
457 | // Read until null | ||
458 | myCodeChunk.Comment = Read_String(); | ||
459 | Common.SendToDebug("Function comment: " + myCodeChunk.Comment); | ||
460 | myCodeChunk.ReturnTypePos = br_read(1)[0]; | ||
461 | myCodeChunk.ReturnType = GetStaticBlock((long) myCodeChunk.ReturnTypePos + (long) myHeader.GVR); | ||
462 | Common.SendToDebug("Return type #" + myCodeChunk.ReturnType.ObjectType + ": " + | ||
463 | ((LSO_Enums.Variable_Type_Codes) myCodeChunk.ReturnType.ObjectType).ToString()); | ||
464 | |||
465 | |||
466 | // TODO: How to determine number of codechunks -- does this method work? | ||
467 | myCodeChunk.CodeChunkArguments = new List<LSO_Struct.CodeChunkArgument>(); | ||
468 | byte reader = br_read(1)[0]; | ||
469 | reader = br_read(1)[0]; | ||
470 | |||
471 | // NOTE ON CODE CHUNK ARGUMENTS | ||
472 | // This determins type definition | ||
473 | int ccount = 0; | ||
474 | while (reader != 0x000) | ||
475 | { | ||
476 | ccount++; | ||
477 | Common.SendToDebug("Reading Code Chunk Argument " + ccount); | ||
478 | LSO_Struct.CodeChunkArgument CCA = new LSO_Struct.CodeChunkArgument(); | ||
479 | CCA.FunctionReturnTypePos = reader; | ||
480 | reader = br_read(1)[0]; | ||
481 | CCA.NullString = reader; | ||
482 | CCA.FunctionReturnType = GetStaticBlock(CCA.FunctionReturnTypePos + myHeader.GVR); | ||
483 | myCodeChunk.CodeChunkArguments.Add(CCA); | ||
484 | Common.SendToDebug("Code Chunk Argument " + ccount + " type #" + CCA.FunctionReturnType.ObjectType + | ||
485 | ": " + (LSO_Enums.Variable_Type_Codes) CCA.FunctionReturnType.ObjectType); | ||
486 | } | ||
487 | // Create string array | ||
488 | Type[] MethodArgs = new Type[myCodeChunk.CodeChunkArguments.Count]; | ||
489 | for (int _ic = 0; _ic < myCodeChunk.CodeChunkArguments.Count; _ic++) | ||
490 | { | ||
491 | MethodArgs[_ic] = getLLObjectType(myCodeChunk.CodeChunkArguments[_ic].FunctionReturnType.ObjectType); | ||
492 | Common.SendToDebug("Method argument " + _ic + ": " + | ||
493 | getLLObjectType(myCodeChunk.CodeChunkArguments[_ic].FunctionReturnType.ObjectType). | ||
494 | ToString()); | ||
495 | } | ||
496 | // End marker is 0x000 | ||
497 | myCodeChunk.EndMarker = reader; | ||
498 | |||
499 | |||
500 | // | ||
501 | // Emit: START OF METHOD (FUNCTION) | ||
502 | // | ||
503 | |||
504 | Common.SendToDebug("CLR:" + eventname + ":MethodBuilder methodBuilder = typeBuilder.DefineMethod..."); | ||
505 | MethodBuilder methodBuilder = typeBuilder.DefineMethod(eventname, | ||
506 | MethodAttributes.Public, | ||
507 | typeof (void), | ||
508 | new Type[] {typeof (object)}); | ||
509 | //MethodArgs); | ||
510 | //typeof(void), //getLLObjectType(myCodeChunk.ReturnType), | ||
511 | // new Type[] { typeof(object) }, //); | ||
512 | |||
513 | //Common.SendToDebug("CLR:" + eventname + ":typeBuilder.DefineMethodOverride(methodBuilder..."); | ||
514 | //typeBuilder.DefineMethodOverride(methodBuilder, | ||
515 | // typeof(LSL_CLRInterface.LSLScript).GetMethod(eventname)); | ||
516 | |||
517 | // Create the IL generator | ||
518 | |||
519 | Common.SendToDebug("CLR:" + eventname + ":ILGenerator il = methodBuilder.GetILGenerator();"); | ||
520 | ILGenerator il = methodBuilder.GetILGenerator(); | ||
521 | |||
522 | |||
523 | if (Common.IL_UseTryCatch) | ||
524 | IL_INSERT_TRY(il, eventname); | ||
525 | |||
526 | |||
527 | // Push Console.WriteLine command to stack ... Console.WriteLine("Hello World!"); | ||
528 | //Common.SendToDebug("CLR:" + eventname + ":il.Emit(OpCodes.Call..."); | ||
529 | //il.Emit(OpCodes.Call, typeof(Console).GetMethod | ||
530 | // ("WriteLine", new Type[] { typeof(string) })); | ||
531 | |||
532 | //Common.SendToDebug("STARTUP: il.Emit(OpCodes.Ldc_I4_S, 0);"); | ||
533 | |||
534 | //il.Emit(OpCodes.Ldc_I4_S, 0); | ||
535 | for (int _ic = 0; _ic < myCodeChunk.CodeChunkArguments.Count; _ic++) | ||
536 | { | ||
537 | Common.SendToDebug("PARAMS: il.Emit(OpCodes.Ldarg, " + _ic + ");"); | ||
538 | il.Emit(OpCodes.Ldarg, _ic); | ||
539 | } | ||
540 | |||
541 | |||
542 | // | ||
543 | // CALLING OPCODE PROCESSOR, one command at the time TO GENERATE IL | ||
544 | // | ||
545 | bool FoundRet = false; | ||
546 | while (FoundRet == false) | ||
547 | { | ||
548 | FoundRet = LSL_PROCESS_OPCODE(il); | ||
549 | } | ||
550 | |||
551 | |||
552 | if (Common.IL_UseTryCatch) | ||
553 | IL_INSERT_END_TRY(il, eventname); | ||
554 | |||
555 | // Emit: RETURN FROM METHOD | ||
556 | il.Emit(OpCodes.Ret); | ||
557 | |||
558 | return; | ||
559 | } | ||
560 | |||
561 | private void IL_INSERT_FUNCTIONLIST() | ||
562 | { | ||
563 | Common.SendToDebug("Creating function list"); | ||
564 | |||
565 | |||
566 | string eventname = "GetFunctions"; | ||
567 | |||
568 | Common.SendToDebug("Creating IL " + eventname); | ||
569 | // Define a private String field. | ||
570 | //FieldBuilder myField = myTypeBuilder.DefineField("EventList", typeof(String[]), FieldAttributes.Public); | ||
571 | |||
572 | |||
573 | //FieldBuilder mem = typeBuilder.DefineField("mem", typeof(Array), FieldAttributes.Private); | ||
574 | |||
575 | |||
576 | MethodBuilder methodBuilder = typeBuilder.DefineMethod(eventname, | ||
577 | MethodAttributes.Public, | ||
578 | typeof (string[]), | ||
579 | null); | ||
580 | |||
581 | //typeBuilder.DefineMethodOverride(methodBuilder, | ||
582 | // typeof(LSL_CLRInterface.LSLScript).GetMethod(eventname)); | ||
583 | |||
584 | ILGenerator il = methodBuilder.GetILGenerator(); | ||
585 | |||
586 | |||
587 | // IL_INSERT_TRY(il, eventname); | ||
588 | |||
589 | // // Push string to stack | ||
590 | // il.Emit(OpCodes.Ldstr, "Inside " + eventname); | ||
591 | |||
592 | //// Push Console.WriteLine command to stack ... Console.WriteLine("Hello World!"); | ||
593 | //il.Emit(OpCodes.Call, typeof(Console).GetMethod | ||
594 | // ("WriteLine", new Type[] { typeof(string) })); | ||
595 | |||
596 | //initIL.Emit(OpCodes.Newobj, typeof(string[])); | ||
597 | |||
598 | //string[] MyArray = new string[2] { "TestItem1" , "TestItem2" }; | ||
599 | |||
600 | ////il.Emit(OpCodes.Ldarg_0); | ||
601 | |||
602 | il.DeclareLocal(typeof (string[])); | ||
603 | |||
604 | ////il.Emit(OpCodes.Ldarg_0); | ||
605 | il.Emit(OpCodes.Ldc_I4, EventList.Count); // Specify array length | ||
606 | il.Emit(OpCodes.Newarr, typeof (String)); // create new string array | ||
607 | il.Emit(OpCodes.Stloc_0); // Store array as local variable 0 in stack | ||
608 | ////SetFunctionList | ||
609 | |||
610 | for (int lv = 0; lv < EventList.Count; lv++) | ||
611 | { | ||
612 | il.Emit(OpCodes.Ldloc_0); // Load local variable 0 onto stack | ||
613 | il.Emit(OpCodes.Ldc_I4, lv); // Push index position | ||
614 | il.Emit(OpCodes.Ldstr, EventList[lv]); // Push value | ||
615 | il.Emit(OpCodes.Stelem_Ref); // Perform array[index] = value | ||
616 | |||
617 | //il.Emit(OpCodes.Ldarg_0); | ||
618 | //il.Emit(OpCodes.Ldstr, EventList[lv]); // Push value | ||
619 | //il.Emit(OpCodes.Call, typeof(LSL_BaseClass).GetMethod("AddFunction", new Type[] { typeof(string) })); | ||
620 | } | ||
621 | |||
622 | |||
623 | // IL_INSERT_END_TRY(il, eventname); | ||
624 | |||
625 | |||
626 | il.Emit(OpCodes.Ldloc_0); // Load local variable 0 onto stack | ||
627 | // il.Emit(OpCodes.Call, typeof(LSL_BaseClass).GetMethod("SetFunctionList", new Type[] { typeof(Array) })); | ||
628 | |||
629 | il.Emit(OpCodes.Ret); // Return | ||
630 | } | ||
631 | |||
632 | |||
633 | private void IL_INSERT_TRY(ILGenerator il, string eventname) | ||
634 | { | ||
635 | /* | ||
636 | * CLR TRY | ||
637 | */ | ||
638 | //Common.SendToDebug("CLR:" + eventname + ":il.BeginExceptionBlock()"); | ||
639 | il.BeginExceptionBlock(); | ||
640 | |||
641 | // Push "Hello World!" string to stack | ||
642 | //Common.SendToDebug("CLR:" + eventname + ":il.Emit(OpCodes.Ldstr..."); | ||
643 | //il.Emit(OpCodes.Ldstr, "Starting CLR dynamic execution of: " + eventname); | ||
644 | } | ||
645 | |||
646 | private void IL_INSERT_END_TRY(ILGenerator il, string eventname) | ||
647 | { | ||
648 | /* | ||
649 | * CATCH | ||
650 | */ | ||
651 | Common.SendToDebug("CLR:" + eventname + ":il.BeginCatchBlock(typeof(Exception));"); | ||
652 | il.BeginCatchBlock(typeof (Exception)); | ||
653 | |||
654 | // Push "Hello World!" string to stack | ||
655 | Common.SendToDebug("CLR:" + eventname + ":il.Emit(OpCodes.Ldstr..."); | ||
656 | il.Emit(OpCodes.Ldstr, "Execption executing dynamic CLR function " + eventname + ": "); | ||
657 | |||
658 | //call void [mscorlib]System.Console::WriteLine(string) | ||
659 | Common.SendToDebug("CLR:" + eventname + ":il.Emit(OpCodes.Call..."); | ||
660 | il.Emit(OpCodes.Call, typeof (Console).GetMethod | ||
661 | ("Write", new Type[] {typeof (string)})); | ||
662 | |||
663 | //callvirt instance string [mscorlib]System.Exception::get_Message() | ||
664 | Common.SendToDebug("CLR:" + eventname + ":il.Emit(OpCodes.Callvirt..."); | ||
665 | il.Emit(OpCodes.Callvirt, typeof (Exception).GetMethod | ||
666 | ("get_Message")); | ||
667 | |||
668 | //call void [mscorlib]System.Console::WriteLine(string) | ||
669 | Common.SendToDebug("CLR:" + eventname + ":il.Emit(OpCodes.Call..."); | ||
670 | il.Emit(OpCodes.Call, typeof (Console).GetMethod | ||
671 | ("WriteLine", new Type[] {typeof (string)})); | ||
672 | |||
673 | /* | ||
674 | * CLR END TRY | ||
675 | */ | ||
676 | //Common.SendToDebug("CLR:" + eventname + ":il.EndExceptionBlock();"); | ||
677 | il.EndExceptionBlock(); | ||
678 | } | ||
679 | |||
680 | private LSO_Struct.StaticBlock GetStaticBlock(long pos) | ||
681 | { | ||
682 | long FirstPos = fs.Position; | ||
683 | try | ||
684 | { | ||
685 | UInt32 position = (UInt32) pos; | ||
686 | // STATIC BLOCK | ||
687 | Common.SendToDebug("Reading STATIC BLOCK at: " + position); | ||
688 | fs.Seek(position, SeekOrigin.Begin); | ||
689 | |||
690 | if (StaticBlocks.ContainsKey(position) == true) | ||
691 | { | ||
692 | Common.SendToDebug("Found cached STATIC BLOCK"); | ||
693 | |||
694 | |||
695 | return StaticBlocks[pos]; | ||
696 | } | ||
697 | |||
698 | //int StaticBlockCount = 0; | ||
699 | // Read function blocks until we hit GFR | ||
700 | //while (fs.Position < myHeader.GFR) | ||
701 | //{ | ||
702 | //StaticBlockCount++; | ||
703 | |||
704 | //Common.SendToDebug("Reading Static Block at: " + position); | ||
705 | |||
706 | //fs.Seek(myHeader.GVR, SeekOrigin.Begin); | ||
707 | LSO_Struct.StaticBlock myStaticBlock = new LSO_Struct.StaticBlock(); | ||
708 | myStaticBlock.Static_Chunk_Header_Size = BitConverter.ToUInt32(br_read(4), 0); | ||
709 | myStaticBlock.ObjectType = br_read(1)[0]; | ||
710 | Common.SendToDebug("Static Block ObjectType: " + | ||
711 | ((LSO_Enums.Variable_Type_Codes) myStaticBlock.ObjectType).ToString()); | ||
712 | myStaticBlock.Unknown = br_read(1)[0]; | ||
713 | // Size of datatype varies | ||
714 | if (myStaticBlock.ObjectType != 0) | ||
715 | myStaticBlock.BlockVariable = br_read(getObjectSize(myStaticBlock.ObjectType)); | ||
716 | |||
717 | StaticBlocks.Add(position, myStaticBlock); | ||
718 | //} | ||
719 | Common.SendToDebug("Done reading Static Block."); | ||
720 | return myStaticBlock; | ||
721 | } | ||
722 | finally | ||
723 | { | ||
724 | // Go back to original read pos | ||
725 | fs.Seek(FirstPos, SeekOrigin.Begin); | ||
726 | } | ||
727 | } | ||
728 | } | ||
729 | } \ No newline at end of file | ||