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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 96 - (hide annotations) (download)
Sat Jul 17 05:11:13 2010 UTC (9 years, 6 months ago) by william
File size: 7157 byte(s)
bulk commit:

*Update structs to use simple datatypes
*Add new Classes, etc, etc

1 william 70 using System;
2     using System.Collections.Generic;
3     using System.Text;
4    
5     using gr2lib.core.coretypes.native;
6     using System.Runtime.InteropServices;
7 william 96 using System.Diagnostics;
8 william 70
9     namespace gr2lib.core.helpers
10     {
11     internal class Helpers
12     {
13     public static T ReadFromMemory<T>(IntPtr pointer) where T : struct
14     {
15 william 83 try
16 william 70 {
17 william 83 if (pointer == IntPtr.Zero)
18     {
19     throw new InvalidOperationException();
20     }
21    
22 william 96 T _object_implementation_ = (T)Marshal.PtrToStructure(pointer, typeof(T));
23    
24     return _object_implementation_;
25 william 70 }
26 william 83 catch (Exception ex)
27     {
28 william 96 StackTrace st = new StackTrace(true);
29     Console.WriteLine(st.ToString() + "\n\n" + "Stack Trace: \n" + ex.ToString());
30 william 83 return default(T);
31     }
32 william 70 }
33    
34     public static T[] ReadArrayFromMemory<T>(ArrayOfStructures array) where T : struct
35     {
36 william 83 try
37     {
38     return ReadArrayFromMemory<T>(array.Count, array.Pointer);
39     }
40     catch (Exception ex)
41     {
42 william 96 StackTrace st = new StackTrace(true);
43     Console.WriteLine(st.ToString() + "\n\n" + "Stack Trace: \n" + ex.ToString());
44 william 83 return default(T[]);
45     }
46 william 70 }
47    
48     public static T[] ReadArrayFromMemory<T>(int count, IntPtr pointer) where T : struct
49     {
50 william 83 try
51 william 70 {
52 william 83 T[] rez = new T[count];
53     int size = SizeOfStruct<T>();
54     for (int i = 0; i < count; i++)
55     {
56     rez[i] = ReadFromMemory<T>(new IntPtr(pointer.ToInt32() + (i * size)));
57     }
58     return rez;
59 william 70 }
60 william 83 catch (Exception ex)
61     {
62 william 96 StackTrace st = new StackTrace(true);
63     Console.WriteLine(st.ToString() + "\n\n" + "Stack Trace: \n" + ex.ToString());
64 william 83 return default(T[]);
65     }
66 william 70 }
67    
68     public static IntPtr[] ReadPtrArrayFromMemory<T>(ArrayOfStructures array) where T : struct
69     {
70 william 83 try
71     {
72     return ReadPtrArrayFromMemory<T>(array.Count, array.Pointer);
73     }
74     catch (Exception ex)
75     {
76 william 96 StackTrace st = new StackTrace(true);
77     Console.WriteLine(st.ToString() + "\n\n" + "Stack Trace: \n" + ex.ToString());
78 william 83 return default(IntPtr[]);
79     }
80 william 70 }
81    
82     public static IntPtr[] ReadPtrArrayFromMemory<T>(int count, IntPtr pointer) where T : struct
83     {
84 william 83 try
85 william 70 {
86 william 83 IntPtr[] rez = new IntPtr[count];
87     int size = SizeOfStruct<T>();
88     for (int i = 0; i < count; i++)
89     {
90     rez[i] = new IntPtr(pointer.ToInt32() + (i * size));
91     }
92     return rez;
93 william 70 }
94 william 83 catch (Exception ex)
95     {
96 william 96 StackTrace st = new StackTrace(true);
97     Console.WriteLine(st.ToString() + "\n\n" + "Stack Trace: \n" + ex.ToString());
98 william 83 return default(IntPtr[]);
99     }
100 william 70 }
101    
102     public static IntPtr[] ReadArrayFromMemory(ArrayOfReferences array)
103     {
104 william 83 try
105     {
106     return ReadArrayFromMemory(array.Count, array.Pointer);
107     }
108     catch (Exception ex)
109     {
110 william 96 StackTrace st = new StackTrace(true);
111     Console.WriteLine(st.ToString() + "\n\n" + "Stack Trace: \n" + ex.ToString());
112 william 83 return default(IntPtr[]);
113     }
114 william 70 }
115    
116     public static IntPtr[] ReadArrayFromMemory(int count, IntPtr pointer)
117     {
118 william 83 try
119 william 70 {
120 william 83 IntPtr[] rez = new IntPtr[count];
121     for (int i = 0; i < count; i++)
122     {
123     rez[i] = new IntPtr(Marshal.ReadInt32(new IntPtr(pointer.ToInt32() + (i * 4))));
124     }
125     return rez;
126 william 70 }
127 william 83 catch (Exception ex)
128     {
129 william 96 StackTrace st = new StackTrace(true);
130     Console.WriteLine(st.ToString() + "\n\n" + "Stack Trace: \n" + ex.ToString());
131 william 83 return default(IntPtr[]);
132     }
133 william 70 }
134    
135     //public static VariantReference[] ReadArrayFromMemory(ArrayOfVariants array)
136     //{
137     // Native.VariantReference[] rez = new Native.VariantReference[array.Count];
138     // for (int i = 0; i < array.Count; i++)
139     // {
140     // rez[i] = new Native.VariantReference();
141     // rez[i].Unknown1 = new IntPtr(Marshal.ReadInt32(new IntPtr(array.Pointer.Unknown1.ToInt32() + (i * 8))));
142     // rez[i].Unknown2 = new IntPtr(Marshal.ReadInt32(new IntPtr(array.Pointer.Unknown2.ToInt32() + (i * 8))));
143     // }
144     // return rez;
145     //}
146    
147     //public static StringMember[] ReadStrMemsFromMemory(IntPtr pointer)
148     //{
149     // /*ATTENTION!
150     // In original code, a null reference is StringMember[] sm is used to gather the number of string members:
151     // * for(int i = 0; sm[i].Type; ++i)
152     // * ++nVertexComponents
153     // */
154     // List<Native.StringMember> smList = new List<Native.StringMember>();
155     // int count = 0;
156     // int start = pointer.ToInt32();
157     // while (1 == 1)
158     // {
159     // try
160     // {
161     // Native.StringMember sm = ReadFromMemory<Native.StringMember>(new IntPtr(start + (count * 32)));
162     // if (sm.Type == 0) break;
163     // smList.Add(sm);
164     // ++count;
165     // }
166     // catch (Exception)
167     // {
168     // break;
169     // }
170     // }
171     // return smList.ToArray();
172     //}
173    
174     public static int SizeOfStruct<T>() where T : struct
175     {
176 william 96 try
177 william 70 {
178 william 96
179     //object[] attributes = typeof(T).GetCustomAttributes(false);
180     //if (attributes == null || attributes.Length < 1) return 0;
181     int size = 0;
182     //foreach (object attribute in attributes)
183     //{
184     // if (attribute is SizeAttribute)
185     // {
186     // size = (attribute as SizeAttribute).Size;
187     // break;
188     // }
189     //}
190     size = Marshal.SizeOf(typeof(T));
191     return size;
192 william 70 }
193 william 96 catch (Exception ex)
194     {
195     StackTrace st = new StackTrace(true);
196     Console.WriteLine(st.ToString() + "\n\n" + "Stack Trace: \n" + ex.ToString());
197     return 0;
198     }
199 william 70 }
200     }
201     }

  ViewVC Help
Powered by ViewVC 1.1.22