diff options
Diffstat (limited to 'OpenSim/Region/Environment/Scenes/Scripting/Engines/JVMEngine/JVM/ClassRecord.cs')
-rw-r--r-- | OpenSim/Region/Environment/Scenes/Scripting/Engines/JVMEngine/JVM/ClassRecord.cs | 640 |
1 files changed, 640 insertions, 0 deletions
diff --git a/OpenSim/Region/Environment/Scenes/Scripting/Engines/JVMEngine/JVM/ClassRecord.cs b/OpenSim/Region/Environment/Scenes/Scripting/Engines/JVMEngine/JVM/ClassRecord.cs new file mode 100644 index 0000000..4b2aec3 --- /dev/null +++ b/OpenSim/Region/Environment/Scenes/Scripting/Engines/JVMEngine/JVM/ClassRecord.cs | |||
@@ -0,0 +1,640 @@ | |||
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 | using System; | ||
29 | using System.IO; | ||
30 | using System.Collections.Generic; | ||
31 | using System.Text; | ||
32 | using OpenSim.Region.Scripting.EmbeddedJVM.Types; | ||
33 | using OpenSim.Region.Scripting.EmbeddedJVM.Types.PrimitiveTypes; | ||
34 | |||
35 | namespace OpenSim.Region.Scripting.EmbeddedJVM | ||
36 | { | ||
37 | public class ClassRecord | ||
38 | { | ||
39 | private ushort m_majorVersion; | ||
40 | private ushort m_minorVersion; | ||
41 | private ushort m_constantPoolCount; | ||
42 | private ushort m_accessFlags; | ||
43 | private ushort m_thisClass; | ||
44 | private ushort m_supperClass; | ||
45 | private ushort m_interfaceCount; | ||
46 | private ushort m_fieldCount; | ||
47 | private ushort m_methodCount; | ||
48 | //private ushort _attributeCount; | ||
49 | //private string _name; | ||
50 | public Dictionary<string, BaseType> StaticFields = new Dictionary<string, BaseType>(); | ||
51 | public PoolClass MClass; | ||
52 | |||
53 | public List<PoolItem> m_constantsPool = new List<PoolItem>(); | ||
54 | private List<MethodInfo> m_methodsList = new List<MethodInfo>(); | ||
55 | private List<FieldInfo> m_fieldList = new List<FieldInfo>(); | ||
56 | |||
57 | public ClassRecord() | ||
58 | { | ||
59 | |||
60 | } | ||
61 | |||
62 | public ClassInstance CreateNewInstance() | ||
63 | { | ||
64 | ClassInstance classInst = new ClassInstance(); | ||
65 | classInst.ClassRec = this; | ||
66 | //TODO: set fields | ||
67 | |||
68 | return classInst; | ||
69 | } | ||
70 | |||
71 | public void LoadClassFromFile(string fileName) | ||
72 | { | ||
73 | Console.WriteLine("loading script " + fileName); | ||
74 | FileStream fs = File.OpenRead(fileName); | ||
75 | this.LoadClassFromBytes(ReadFully(fs)); | ||
76 | fs.Close(); | ||
77 | } | ||
78 | |||
79 | public void LoadClassFromBytes(byte[] data) | ||
80 | { | ||
81 | int i = 0; | ||
82 | i += 4; | ||
83 | m_minorVersion = (ushort)((data[i++] << 8) + data[i++]); | ||
84 | m_majorVersion = (ushort)((data[i++] << 8) + data[i++]); | ||
85 | m_constantPoolCount = (ushort)((data[i++] << 8) + data[i++]); | ||
86 | Console.WriteLine("there should be " + m_constantPoolCount + " items in the pool"); | ||
87 | for (int count = 0; count < (m_constantPoolCount - 1); count++) | ||
88 | { | ||
89 | //read in the constant pool | ||
90 | byte pooltype = data[i++]; | ||
91 | Console.WriteLine("#" + count + ": new constant type = " + pooltype); | ||
92 | //Console.WriteLine("start position is: " + i); | ||
93 | switch (pooltype) | ||
94 | { | ||
95 | case 1: //Utf8 | ||
96 | ushort uLength = (ushort)((data[i++] << 8) + data[i++]); | ||
97 | |||
98 | // Console.WriteLine("new utf8 type, length is " + uLength); | ||
99 | PoolUtf8 utf8 = new PoolUtf8(); | ||
100 | utf8.readValue(data, ref i, uLength); | ||
101 | this.m_constantsPool.Add(utf8); | ||
102 | break; | ||
103 | case 3: //Int | ||
104 | break; | ||
105 | case 4: //Float | ||
106 | break; | ||
107 | case 7: //Class | ||
108 | PoolClass pClass = new PoolClass(this); | ||
109 | pClass.readValue(data, ref i); | ||
110 | this.m_constantsPool.Add(pClass); | ||
111 | break; | ||
112 | case 9: //FieldRef | ||
113 | PoolFieldRef pField = new PoolFieldRef(this); | ||
114 | pField.readValue(data, ref i); | ||
115 | this.m_constantsPool.Add(pField); | ||
116 | break; | ||
117 | case 10: //Method | ||
118 | PoolMethodRef pMeth = new PoolMethodRef(this); | ||
119 | pMeth.readValue(data, ref i); | ||
120 | this.m_constantsPool.Add(pMeth); | ||
121 | break; | ||
122 | case 12: //NamedType | ||
123 | PoolNamedType pNamed = new PoolNamedType(this); | ||
124 | pNamed.readValue(data, ref i); | ||
125 | this.m_constantsPool.Add(pNamed); | ||
126 | break; | ||
127 | } | ||
128 | } | ||
129 | |||
130 | m_accessFlags = (ushort)((data[i++] << 8) + data[i++]); | ||
131 | m_thisClass = (ushort)((data[i++] << 8) + data[i++]); | ||
132 | m_supperClass = (ushort)((data[i++] << 8) + data[i++]); | ||
133 | |||
134 | if (this.m_constantsPool[this.m_thisClass - 1] is PoolClass) | ||
135 | { | ||
136 | this.MClass = ((PoolClass)this.m_constantsPool[this.m_thisClass - 1]); | ||
137 | } | ||
138 | |||
139 | m_interfaceCount = (ushort)((data[i++] << 8) + data[i++]); | ||
140 | //should now read in the info for each interface | ||
141 | |||
142 | m_fieldCount = (ushort)((data[i++] << 8) + data[i++]); | ||
143 | //should now read in the info for each field | ||
144 | for (int count = 0; count < m_fieldCount; count++) | ||
145 | { | ||
146 | FieldInfo fieldInf = new FieldInfo(this); | ||
147 | fieldInf.ReadData(data, ref i); | ||
148 | this.m_fieldList.Add(fieldInf); | ||
149 | } | ||
150 | |||
151 | m_methodCount = (ushort)((data[i++] << 8) + data[i++]); | ||
152 | for (int count = 0; count < m_methodCount; count++) | ||
153 | { | ||
154 | MethodInfo methInf = new MethodInfo(this); | ||
155 | methInf.ReadData(data, ref i); | ||
156 | this.m_methodsList.Add(methInf); | ||
157 | } | ||
158 | } | ||
159 | |||
160 | public void AddMethodsToMemory(MethodMemory memory) | ||
161 | { | ||
162 | for (int count = 0; count < m_methodCount; count++) | ||
163 | { | ||
164 | this.m_methodsList[count].AddMethodCode(memory); | ||
165 | } | ||
166 | } | ||
167 | |||
168 | public bool StartMethod(Thread thread, string methodName) | ||
169 | { | ||
170 | for (int count = 0; count < m_methodCount; count++) | ||
171 | { | ||
172 | if (this.m_constantsPool[this.m_methodsList[count].NameIndex - 1] is PoolUtf8) | ||
173 | { | ||
174 | if (((PoolUtf8)this.m_constantsPool[this.m_methodsList[count].NameIndex - 1]).Value == methodName) | ||
175 | { | ||
176 | //Console.WriteLine("found method: " + ((PoolUtf8)this._constantsPool[this._methodsList[count].NameIndex - 1]).Value); | ||
177 | thread.SetPC(this.m_methodsList[count].CodePointer); | ||
178 | return true; | ||
179 | } | ||
180 | } | ||
181 | } | ||
182 | return false; | ||
183 | } | ||
184 | |||
185 | public void PrintToConsole() | ||
186 | { | ||
187 | Console.WriteLine("Class File:"); | ||
188 | Console.WriteLine("Major version: " + m_majorVersion); | ||
189 | Console.WriteLine("Minor version: " + m_minorVersion); | ||
190 | Console.WriteLine("Pool size: " + m_constantPoolCount); | ||
191 | |||
192 | for (int i = 0; i < m_constantsPool.Count; i++) | ||
193 | { | ||
194 | this.m_constantsPool[i].Print(); | ||
195 | } | ||
196 | |||
197 | Console.WriteLine("Access flags: " + m_accessFlags); | ||
198 | Console.WriteLine("This class: " + m_thisClass); | ||
199 | Console.WriteLine("Super class: " + m_supperClass); | ||
200 | |||
201 | for (int count = 0; count < m_fieldCount; count++) | ||
202 | { | ||
203 | Console.WriteLine(); | ||
204 | this.m_fieldList[count].Print(); | ||
205 | } | ||
206 | |||
207 | for (int count = 0; count < m_methodCount; count++) | ||
208 | { | ||
209 | Console.WriteLine(); | ||
210 | this.m_methodsList[count].Print(); | ||
211 | } | ||
212 | |||
213 | Console.WriteLine("class name is " + this.MClass.Name.Value); | ||
214 | } | ||
215 | |||
216 | public static byte[] ReadFully(Stream stream) | ||
217 | { | ||
218 | byte[] buffer = new byte[1024]; | ||
219 | using (MemoryStream ms = new MemoryStream()) | ||
220 | { | ||
221 | while (true) | ||
222 | { | ||
223 | int read = stream.Read(buffer, 0, buffer.Length); | ||
224 | if (read <= 0) | ||
225 | return ms.ToArray(); | ||
226 | ms.Write(buffer, 0, read); | ||
227 | } | ||
228 | } | ||
229 | } | ||
230 | |||
231 | #region nested classes | ||
232 | public class PoolItem | ||
233 | { | ||
234 | public virtual void Print() | ||
235 | { | ||
236 | |||
237 | } | ||
238 | } | ||
239 | |||
240 | public class PoolUtf8 : PoolItem | ||
241 | { | ||
242 | public string Value = ""; | ||
243 | |||
244 | public void readValue(byte[] data, ref int pointer, int length) | ||
245 | { | ||
246 | for (int i = 0; i < length; i++) | ||
247 | { | ||
248 | int a = (int)data[pointer++]; | ||
249 | if ((a & 0x80) == 0) | ||
250 | { | ||
251 | Value = Value + (char)a; | ||
252 | } | ||
253 | else if ((a & 0x20) == 0) | ||
254 | { | ||
255 | int b = (int)data[pointer++]; | ||
256 | Value = Value + (char)(((a & 0x1f) << 6) + (b & 0x3f)); | ||
257 | } | ||
258 | else | ||
259 | { | ||
260 | int b = (int)data[pointer++]; | ||
261 | int c = (int)data[pointer++]; | ||
262 | Value = Value + (char)(((a & 0xf) << 12) + ((b & 0x3f) << 6) + (c & 0x3f)); | ||
263 | } | ||
264 | } | ||
265 | } | ||
266 | |||
267 | public override void Print() | ||
268 | { | ||
269 | Console.WriteLine("Utf8 type: " + Value); | ||
270 | } | ||
271 | } | ||
272 | |||
273 | private class PoolInt : PoolItem | ||
274 | { | ||
275 | |||
276 | } | ||
277 | |||
278 | public class PoolClass : PoolItem | ||
279 | { | ||
280 | //public string name = ""; | ||
281 | public ushort namePointer = 0; | ||
282 | private ClassRecord parent; | ||
283 | public PoolUtf8 Name; | ||
284 | |||
285 | public PoolClass(ClassRecord paren) | ||
286 | { | ||
287 | parent = paren; | ||
288 | } | ||
289 | |||
290 | public void readValue(byte[] data, ref int pointer) | ||
291 | { | ||
292 | namePointer = (ushort)((data[pointer++] << 8) + data[pointer++]); | ||
293 | } | ||
294 | |||
295 | public override void Print() | ||
296 | { | ||
297 | this.Name = ((PoolUtf8)this.parent.m_constantsPool[namePointer - 1]); | ||
298 | Console.Write("Class type: " + namePointer); | ||
299 | Console.WriteLine(" // " + ((PoolUtf8)this.parent.m_constantsPool[namePointer - 1]).Value); | ||
300 | |||
301 | } | ||
302 | } | ||
303 | |||
304 | public class PoolFieldRef : PoolItem | ||
305 | { | ||
306 | public ushort classPointer = 0; | ||
307 | public ushort nameTypePointer = 0; | ||
308 | public PoolNamedType mNameType; | ||
309 | public PoolClass mClass; | ||
310 | private ClassRecord parent; | ||
311 | |||
312 | public PoolFieldRef(ClassRecord paren) | ||
313 | { | ||
314 | parent = paren; | ||
315 | } | ||
316 | |||
317 | public void readValue(byte[] data, ref int pointer) | ||
318 | { | ||
319 | classPointer = (ushort)((data[pointer++] << 8) + data[pointer++]); | ||
320 | nameTypePointer = (ushort)((data[pointer++] << 8) + data[pointer++]); | ||
321 | } | ||
322 | |||
323 | public override void Print() | ||
324 | { | ||
325 | this.mNameType = ((PoolNamedType)this.parent.m_constantsPool[nameTypePointer - 1]); | ||
326 | this.mClass = ((PoolClass)this.parent.m_constantsPool[classPointer - 1]); | ||
327 | Console.WriteLine("FieldRef type: " + classPointer + " , " + nameTypePointer); | ||
328 | } | ||
329 | } | ||
330 | |||
331 | public class PoolMethodRef : PoolItem | ||
332 | { | ||
333 | public ushort classPointer = 0; | ||
334 | public ushort nameTypePointer = 0; | ||
335 | public PoolNamedType mNameType; | ||
336 | public PoolClass mClass; | ||
337 | private ClassRecord parent; | ||
338 | |||
339 | public PoolMethodRef(ClassRecord paren) | ||
340 | { | ||
341 | parent = paren; | ||
342 | } | ||
343 | |||
344 | public void readValue(byte[] data, ref int pointer) | ||
345 | { | ||
346 | classPointer = (ushort)((data[pointer++] << 8) + data[pointer++]); | ||
347 | nameTypePointer = (ushort)((data[pointer++] << 8) + data[pointer++]); | ||
348 | } | ||
349 | |||
350 | public override void Print() | ||
351 | { | ||
352 | this.mNameType = ((PoolNamedType)this.parent.m_constantsPool[nameTypePointer - 1]); | ||
353 | this.mClass = ((PoolClass)this.parent.m_constantsPool[classPointer - 1]); | ||
354 | Console.WriteLine("MethodRef type: " + classPointer + " , " + nameTypePointer); | ||
355 | } | ||
356 | } | ||
357 | |||
358 | public class PoolNamedType : PoolItem | ||
359 | { | ||
360 | public ushort namePointer = 0; | ||
361 | public ushort typePointer = 0; | ||
362 | private ClassRecord parent; | ||
363 | public PoolUtf8 Name; | ||
364 | public PoolUtf8 Type; | ||
365 | |||
366 | public PoolNamedType(ClassRecord paren) | ||
367 | { | ||
368 | parent = paren; | ||
369 | } | ||
370 | |||
371 | public void readValue(byte[] data, ref int pointer) | ||
372 | { | ||
373 | namePointer = (ushort)((data[pointer++] << 8) + data[pointer++]); | ||
374 | typePointer = (ushort)((data[pointer++] << 8) + data[pointer++]); | ||
375 | } | ||
376 | |||
377 | public override void Print() | ||
378 | { | ||
379 | Name = ((PoolUtf8)this.parent.m_constantsPool[namePointer - 1]); | ||
380 | Type = ((PoolUtf8)this.parent.m_constantsPool[typePointer - 1]); | ||
381 | Console.Write("Named type: " + namePointer + " , " + typePointer); | ||
382 | Console.WriteLine(" // " + ((PoolUtf8)this.parent.m_constantsPool[namePointer - 1]).Value); | ||
383 | } | ||
384 | } | ||
385 | |||
386 | //*********************** | ||
387 | public class MethodInfo | ||
388 | { | ||
389 | public ushort AccessFlags = 0; | ||
390 | public ushort NameIndex = 0; | ||
391 | public string Name = ""; | ||
392 | public ushort DescriptorIndex = 0; | ||
393 | public ushort AttributeCount = 0; | ||
394 | public List<MethodAttribute> Attributes = new List<MethodAttribute>(); | ||
395 | private ClassRecord parent; | ||
396 | public int CodePointer = 0; | ||
397 | |||
398 | public MethodInfo(ClassRecord paren) | ||
399 | { | ||
400 | parent = paren; | ||
401 | } | ||
402 | |||
403 | public void AddMethodCode(MethodMemory memory) | ||
404 | { | ||
405 | Array.Copy(this.Attributes[0].Code, 0, memory.MethodBuffer, memory.NextMethodPC, this.Attributes[0].Code.Length); | ||
406 | memory.Methodcount++; | ||
407 | this.CodePointer = memory.NextMethodPC; | ||
408 | memory.NextMethodPC += this.Attributes[0].Code.Length; | ||
409 | } | ||
410 | |||
411 | public void ReadData(byte[] data, ref int pointer) | ||
412 | { | ||
413 | AccessFlags = (ushort)((data[pointer++] << 8) + data[pointer++]); | ||
414 | NameIndex = (ushort)((data[pointer++] << 8) + data[pointer++]); | ||
415 | DescriptorIndex = (ushort)((data[pointer++] << 8) + data[pointer++]); | ||
416 | AttributeCount = (ushort)((data[pointer++] << 8) + data[pointer++]); | ||
417 | for (int i = 0; i < AttributeCount; i++) | ||
418 | { | ||
419 | MethodAttribute attri = new MethodAttribute(this.parent); | ||
420 | attri.ReadData(data, ref pointer); | ||
421 | this.Attributes.Add(attri); | ||
422 | } | ||
423 | } | ||
424 | |||
425 | public void Print() | ||
426 | { | ||
427 | Console.WriteLine("Method Info Struct: "); | ||
428 | Console.WriteLine("AccessFlags: " + AccessFlags); | ||
429 | Console.WriteLine("NameIndex: " + NameIndex + " // " + ((PoolUtf8)this.parent.m_constantsPool[NameIndex - 1]).Value); | ||
430 | Console.WriteLine("DescriptorIndex: " + DescriptorIndex + " // " + ((PoolUtf8)this.parent.m_constantsPool[DescriptorIndex - 1]).Value); | ||
431 | Console.WriteLine("Attribute Count:" + AttributeCount); | ||
432 | for (int i = 0; i < AttributeCount; i++) | ||
433 | { | ||
434 | this.Attributes[i].Print(); | ||
435 | } | ||
436 | } | ||
437 | |||
438 | public class MethodAttribute | ||
439 | { | ||
440 | public ushort NameIndex = 0; | ||
441 | public string Name = ""; | ||
442 | public Int32 Length = 0; | ||
443 | //for now only support code attribute | ||
444 | public ushort MaxStack = 0; | ||
445 | public ushort MaxLocals = 0; | ||
446 | public Int32 CodeLength = 0; | ||
447 | public byte[] Code; | ||
448 | public ushort ExceptionTableLength = 0; | ||
449 | public ushort SubAttributeCount = 0; | ||
450 | public List<SubAttribute> SubAttributes = new List<SubAttribute>(); | ||
451 | private ClassRecord parent; | ||
452 | |||
453 | public MethodAttribute(ClassRecord paren) | ||
454 | { | ||
455 | parent = paren; | ||
456 | } | ||
457 | |||
458 | public void ReadData(byte[] data, ref int pointer) | ||
459 | { | ||
460 | NameIndex = (ushort)((data[pointer++] << 8) + data[pointer++]); | ||
461 | Length = (Int32)((data[pointer++] << 24) + (data[pointer++] << 16) + (data[pointer++] << 8) + data[pointer++]); | ||
462 | MaxStack = (ushort)((data[pointer++] << 8) + data[pointer++]); | ||
463 | MaxLocals = (ushort)((data[pointer++] << 8) + data[pointer++]); | ||
464 | CodeLength = (Int32)((data[pointer++] << 24) + (data[pointer++] << 16) + (data[pointer++] << 8) + data[pointer++]); | ||
465 | Code = new byte[CodeLength]; | ||
466 | for (int i = 0; i < CodeLength; i++) | ||
467 | { | ||
468 | Code[i] = data[pointer++]; | ||
469 | } | ||
470 | ExceptionTableLength = (ushort)((data[pointer++] << 8) + data[pointer++]); | ||
471 | SubAttributeCount = (ushort)((data[pointer++] << 8) + data[pointer++]); | ||
472 | for (int i = 0; i < SubAttributeCount; i++) | ||
473 | { | ||
474 | SubAttribute subAttri = new SubAttribute(this.parent); | ||
475 | subAttri.ReadData(data, ref pointer); | ||
476 | this.SubAttributes.Add(subAttri); | ||
477 | } | ||
478 | } | ||
479 | |||
480 | public void Print() | ||
481 | { | ||
482 | Console.WriteLine("Method Attribute: "); | ||
483 | Console.WriteLine("Name Index: " + NameIndex + " // " + ((PoolUtf8)this.parent.m_constantsPool[NameIndex - 1]).Value); | ||
484 | Console.WriteLine("Length: " + Length); | ||
485 | Console.WriteLine("MaxStack: " + MaxStack); | ||
486 | Console.WriteLine("MaxLocals: " + MaxLocals); | ||
487 | Console.WriteLine("CodeLength: " + CodeLength); | ||
488 | for (int i = 0; i < Code.Length; i++) | ||
489 | { | ||
490 | Console.WriteLine("OpCode #" + i + " is: " + Code[i]); | ||
491 | } | ||
492 | Console.WriteLine("SubAttributes: " + SubAttributeCount); | ||
493 | for (int i = 0; i < SubAttributeCount; i++) | ||
494 | { | ||
495 | this.SubAttributes[i].Print(); | ||
496 | } | ||
497 | } | ||
498 | |||
499 | public class SubAttribute | ||
500 | { | ||
501 | public ushort NameIndex = 0; | ||
502 | public string Name = ""; | ||
503 | public Int32 Length = 0; | ||
504 | public byte[] Data; | ||
505 | private ClassRecord parent; | ||
506 | |||
507 | public SubAttribute(ClassRecord paren) | ||
508 | { | ||
509 | parent = paren; | ||
510 | } | ||
511 | |||
512 | public void ReadData(byte[] data, ref int pointer) | ||
513 | { | ||
514 | NameIndex = (ushort)((data[pointer++] << 8) + data[pointer++]); | ||
515 | Length = (Int32)((data[pointer++] << 24) + (data[pointer++] << 16) + (data[pointer++] << 8) + data[pointer++]); | ||
516 | Data = new byte[Length]; | ||
517 | for (int i = 0; i < Length; i++) | ||
518 | { | ||
519 | Data[i] = data[pointer++]; | ||
520 | } | ||
521 | } | ||
522 | |||
523 | public void Print() | ||
524 | { | ||
525 | Console.WriteLine("SubAttribute: NameIndex: " + NameIndex + " // " + ((PoolUtf8)this.parent.m_constantsPool[NameIndex - 1]).Value); | ||
526 | } | ||
527 | |||
528 | } | ||
529 | } | ||
530 | |||
531 | } | ||
532 | private class InterfaceInfo | ||
533 | { | ||
534 | public void ReadData(byte[] data, ref int i) | ||
535 | { | ||
536 | |||
537 | } | ||
538 | } | ||
539 | |||
540 | public class FieldInfo | ||
541 | { | ||
542 | public ushort AccessFlags = 0; | ||
543 | public ushort NameIndex = 0; | ||
544 | public string Name = ""; | ||
545 | public ushort DescriptorIndex = 0; | ||
546 | public ushort AttributeCount = 0; | ||
547 | public List<FieldAttribute> Attributes = new List<FieldAttribute>(); | ||
548 | private ClassRecord parent; | ||
549 | |||
550 | public FieldInfo(ClassRecord paren) | ||
551 | { | ||
552 | parent = paren; | ||
553 | } | ||
554 | |||
555 | public void ReadData(byte[] data, ref int pointer) | ||
556 | { | ||
557 | AccessFlags = (ushort)((data[pointer++] << 8) + data[pointer++]); | ||
558 | NameIndex = (ushort)((data[pointer++] << 8) + data[pointer++]); | ||
559 | DescriptorIndex = (ushort)((data[pointer++] << 8) + data[pointer++]); | ||
560 | AttributeCount = (ushort)((data[pointer++] << 8) + data[pointer++]); | ||
561 | for (int i = 0; i < AttributeCount; i++) | ||
562 | { | ||
563 | FieldAttribute attri = new FieldAttribute(this.parent); | ||
564 | attri.ReadData(data, ref pointer); | ||
565 | this.Attributes.Add(attri); | ||
566 | } | ||
567 | } | ||
568 | |||
569 | public void Print() | ||
570 | { | ||
571 | Console.WriteLine("Field Info Struct: "); | ||
572 | Console.WriteLine("AccessFlags: " + AccessFlags); | ||
573 | Console.WriteLine("NameIndex: " + NameIndex + " // " + ((PoolUtf8)this.parent.m_constantsPool[NameIndex - 1]).Value); | ||
574 | Console.WriteLine("DescriptorIndex: " + DescriptorIndex + " // " + ((PoolUtf8)this.parent.m_constantsPool[DescriptorIndex - 1]).Value); | ||
575 | Console.WriteLine("Attribute Count:" + AttributeCount); | ||
576 | //if static, add to static field list | ||
577 | // if (this.AccessFlags == 9) //public and static | ||
578 | if ((this.AccessFlags & 0x08) != 0) | ||
579 | { | ||
580 | switch (((PoolUtf8)this.parent.m_constantsPool[DescriptorIndex - 1]).Value) | ||
581 | { | ||
582 | case "I": | ||
583 | Int newin = new Int(); | ||
584 | this.parent.StaticFields.Add(((PoolUtf8)this.parent.m_constantsPool[NameIndex - 1]).Value, newin); | ||
585 | break; | ||
586 | case "F": | ||
587 | Float newfl = new Float(); | ||
588 | this.parent.StaticFields.Add(((PoolUtf8)this.parent.m_constantsPool[NameIndex - 1]).Value, newfl); | ||
589 | break; | ||
590 | } | ||
591 | |||
592 | } | ||
593 | for (int i = 0; i < AttributeCount; i++) | ||
594 | { | ||
595 | this.Attributes[i].Print(); | ||
596 | } | ||
597 | } | ||
598 | |||
599 | public class FieldAttribute | ||
600 | { | ||
601 | public ushort NameIndex = 0; | ||
602 | public string Name = ""; | ||
603 | public Int32 Length = 0; | ||
604 | public byte[] Data; | ||
605 | private ClassRecord parent; | ||
606 | |||
607 | public FieldAttribute(ClassRecord paren) | ||
608 | { | ||
609 | parent = paren; | ||
610 | } | ||
611 | |||
612 | public void ReadData(byte[] data, ref int pointer) | ||
613 | { | ||
614 | NameIndex = (ushort)((data[pointer++] << 8) + data[pointer++]); | ||
615 | Length = (Int32)((data[pointer++] << 24) + (data[pointer++] << 16) + (data[pointer++] << 8) + data[pointer++]); | ||
616 | Data = new byte[Length]; | ||
617 | for (int i = 0; i < Length; i++) | ||
618 | { | ||
619 | Data[i] = data[pointer++]; | ||
620 | } | ||
621 | } | ||
622 | |||
623 | public void Print() | ||
624 | { | ||
625 | Console.WriteLine("FieldAttribute: NameIndex: " + NameIndex + " // " + ((PoolUtf8)this.parent.m_constantsPool[NameIndex - 1]).Value); | ||
626 | } | ||
627 | } | ||
628 | } | ||
629 | |||
630 | private class AttributeInfo | ||
631 | { | ||
632 | public void ReadData(byte[] data, ref int i) | ||
633 | { | ||
634 | |||
635 | } | ||
636 | } | ||
637 | #endregion | ||
638 | |||
639 | } | ||
640 | } \ No newline at end of file | ||