/[gr2lib]/trunk/gr2lib/core/helpers/Helpers.cs
ViewVC logotype

Contents of /trunk/gr2lib/core/helpers/Helpers.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 70 - (show annotations) (download)
Thu Jul 15 12:55:21 2010 UTC (9 years, 6 months ago) by william
File size: 4418 byte(s)
initial commit of support to read ArtToolInfo

1 using System;
2 using System.Collections.Generic;
3 using System.Text;
4
5 using gr2lib.core.coretypes.native;
6 using System.Runtime.InteropServices;
7
8 namespace gr2lib.core.helpers
9 {
10 internal class Helpers
11 {
12 public static T ReadFromMemory<T>(IntPtr pointer) where T : struct
13 {
14 if (pointer == IntPtr.Zero)
15 {
16 throw new InvalidOperationException();
17 }
18
19 return (T)Marshal.PtrToStructure(pointer, typeof(T));
20 }
21
22 public static T[] ReadArrayFromMemory<T>(ArrayOfStructures array) where T : struct
23 {
24 return ReadArrayFromMemory<T>(array.Count, array.Pointer);
25 }
26
27 public static T[] ReadArrayFromMemory<T>(int count, IntPtr pointer) where T : struct
28 {
29 T[] rez = new T[count];
30 int size = SizeOfStruct<T>();
31 for (int i = 0; i < count; i++)
32 {
33 rez[i] = ReadFromMemory<T>(new IntPtr(pointer.ToInt32() + (i * size)));
34 }
35 return rez;
36 }
37
38 public static IntPtr[] ReadPtrArrayFromMemory<T>(ArrayOfStructures array) where T : struct
39 {
40 return ReadPtrArrayFromMemory<T>(array.Count, array.Pointer);
41 }
42
43 public static IntPtr[] ReadPtrArrayFromMemory<T>(int count, IntPtr pointer) where T : struct
44 {
45 IntPtr[] rez = new IntPtr[count];
46 int size = SizeOfStruct<T>();
47 for (int i = 0; i < count; i++)
48 {
49 rez[i] = new IntPtr(pointer.ToInt32() + (i * size));
50 }
51 return rez;
52 }
53
54 public static IntPtr[] ReadArrayFromMemory(ArrayOfReferences array)
55 {
56 return ReadArrayFromMemory(array.Count, array.Pointer);
57 }
58
59 public static IntPtr[] ReadArrayFromMemory(int count, IntPtr pointer)
60 {
61 IntPtr[] rez = new IntPtr[count];
62 for (int i = 0; i < count; i++)
63 {
64 rez[i] = new IntPtr(Marshal.ReadInt32(new IntPtr(pointer.ToInt32() + (i * 4))));
65 }
66 return rez;
67 }
68
69 //public static VariantReference[] ReadArrayFromMemory(ArrayOfVariants array)
70 //{
71 // Native.VariantReference[] rez = new Native.VariantReference[array.Count];
72 // for (int i = 0; i < array.Count; i++)
73 // {
74 // rez[i] = new Native.VariantReference();
75 // rez[i].Unknown1 = new IntPtr(Marshal.ReadInt32(new IntPtr(array.Pointer.Unknown1.ToInt32() + (i * 8))));
76 // rez[i].Unknown2 = new IntPtr(Marshal.ReadInt32(new IntPtr(array.Pointer.Unknown2.ToInt32() + (i * 8))));
77 // }
78 // return rez;
79 //}
80
81 //public static StringMember[] ReadStrMemsFromMemory(IntPtr pointer)
82 //{
83 // /*ATTENTION!
84 // In original code, a null reference is StringMember[] sm is used to gather the number of string members:
85 // * for(int i = 0; sm[i].Type; ++i)
86 // * ++nVertexComponents
87 // */
88 // List<Native.StringMember> smList = new List<Native.StringMember>();
89 // int count = 0;
90 // int start = pointer.ToInt32();
91 // while (1 == 1)
92 // {
93 // try
94 // {
95 // Native.StringMember sm = ReadFromMemory<Native.StringMember>(new IntPtr(start + (count * 32)));
96 // if (sm.Type == 0) break;
97 // smList.Add(sm);
98 // ++count;
99 // }
100 // catch (Exception)
101 // {
102 // break;
103 // }
104 // }
105 // return smList.ToArray();
106 //}
107
108 public static int SizeOfStruct<T>() where T : struct
109 {
110 object[] attributes = typeof(T).GetCustomAttributes(false);
111 if (attributes == null || attributes.Length < 1) return 0;
112 int size = 0;
113 foreach (object attribute in attributes)
114 {
115 if (attribute is SizeAttribute)
116 {
117 size = (attribute as SizeAttribute).Size;
118 break;
119 }
120 }
121 return size;
122 }
123 }
124 }

  ViewVC Help
Powered by ViewVC 1.1.22