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