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