/[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 102 - (hide annotations) (download)
Sat Jul 17 13:54:18 2010 UTC (9 years, 11 months ago) by william
File size: 7069 byte(s)
add compiler directive to write exceptions to console or not

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 102 catch
27 william 83 {
28 william 96 StackTrace st = new StackTrace(true);
29 william 102 #if ENABLE_EXCEPTION_OUTPUT_TO_CONSOLE
30     Console.WriteLine(st.ToString());
31     #endif
32 william 83 return default(T);
33     }
34 william 70 }
35    
36     public static T[] ReadArrayFromMemory<T>(ArrayOfStructures array) where T : struct
37     {
38 william 83 try
39     {
40     return ReadArrayFromMemory<T>(array.Count, array.Pointer);
41     }
42 william 102 catch
43 william 83 {
44 william 96 StackTrace st = new StackTrace(true);
45 william 102 #if ENABLE_EXCEPTION_OUTPUT_TO_CONSOLE
46     Console.WriteLine(st.ToString());
47     #endif
48 william 83 return default(T[]);
49     }
50 william 70 }
51    
52     public static T[] ReadArrayFromMemory<T>(int count, IntPtr pointer) where T : struct
53     {
54 william 83 try
55 william 70 {
56 william 83 T[] rez = new T[count];
57     int size = SizeOfStruct<T>();
58     for (int i = 0; i < count; i++)
59     {
60     rez[i] = ReadFromMemory<T>(new IntPtr(pointer.ToInt32() + (i * size)));
61     }
62     return rez;
63 william 70 }
64 william 102 catch
65 william 83 {
66 william 96 StackTrace st = new StackTrace(true);
67 william 102 #if ENABLE_EXCEPTION_OUTPUT_TO_CONSOLE
68     Console.WriteLine(st.ToString());
69     #endif
70 william 83 return default(T[]);
71     }
72 william 70 }
73    
74     public static IntPtr[] ReadPtrArrayFromMemory<T>(ArrayOfStructures array) where T : struct
75     {
76 william 83 try
77     {
78     return ReadPtrArrayFromMemory<T>(array.Count, array.Pointer);
79     }
80 william 102 catch
81 william 83 {
82 william 96 StackTrace st = new StackTrace(true);
83 william 102 #if ENABLE_EXCEPTION_OUTPUT_TO_CONSOLE
84     Console.WriteLine(st.ToString());
85     #endif
86 william 83 return default(IntPtr[]);
87     }
88 william 70 }
89    
90     public static IntPtr[] ReadPtrArrayFromMemory<T>(int count, IntPtr pointer) where T : struct
91     {
92 william 83 try
93 william 70 {
94 william 83 IntPtr[] rez = new IntPtr[count];
95     int size = SizeOfStruct<T>();
96     for (int i = 0; i < count; i++)
97     {
98     rez[i] = new IntPtr(pointer.ToInt32() + (i * size));
99     }
100     return rez;
101 william 70 }
102 william 102 catch
103 william 83 {
104 william 96 StackTrace st = new StackTrace(true);
105 william 102 #if ENABLE_EXCEPTION_OUTPUT_TO_CONSOLE
106     Console.WriteLine(st.ToString());
107     #endif
108 william 83 return default(IntPtr[]);
109     }
110 william 70 }
111    
112     public static IntPtr[] ReadArrayFromMemory(ArrayOfReferences array)
113     {
114 william 83 try
115     {
116     return ReadArrayFromMemory(array.Count, array.Pointer);
117     }
118 william 102 catch
119 william 83 {
120 william 96 StackTrace st = new StackTrace(true);
121 william 102 #if ENABLE_EXCEPTION_OUTPUT_TO_CONSOLE
122     Console.WriteLine(st.ToString());
123     #endif
124 william 83 return default(IntPtr[]);
125     }
126 william 70 }
127    
128     public static IntPtr[] ReadArrayFromMemory(int count, IntPtr pointer)
129     {
130 william 83 try
131 william 70 {
132 william 83 IntPtr[] rez = new IntPtr[count];
133     for (int i = 0; i < count; i++)
134     {
135     rez[i] = new IntPtr(Marshal.ReadInt32(new IntPtr(pointer.ToInt32() + (i * 4))));
136     }
137     return rez;
138 william 70 }
139 william 102 catch
140 william 83 {
141 william 96 StackTrace st = new StackTrace(true);
142 william 102 #if ENABLE_EXCEPTION_OUTPUT_TO_CONSOLE
143     Console.WriteLine(st.ToString());
144     #endif
145 william 83 return default(IntPtr[]);
146     }
147 william 70 }
148    
149     //public static VariantReference[] ReadArrayFromMemory(ArrayOfVariants array)
150     //{
151     // Native.VariantReference[] rez = new Native.VariantReference[array.Count];
152     // for (int i = 0; i < array.Count; i++)
153     // {
154     // rez[i] = new Native.VariantReference();
155     // rez[i].Unknown1 = new IntPtr(Marshal.ReadInt32(new IntPtr(array.Pointer.Unknown1.ToInt32() + (i * 8))));
156     // rez[i].Unknown2 = new IntPtr(Marshal.ReadInt32(new IntPtr(array.Pointer.Unknown2.ToInt32() + (i * 8))));
157     // }
158     // return rez;
159     //}
160    
161     //public static StringMember[] ReadStrMemsFromMemory(IntPtr pointer)
162     //{
163     // /*ATTENTION!
164     // In original code, a null reference is StringMember[] sm is used to gather the number of string members:
165     // * for(int i = 0; sm[i].Type; ++i)
166     // * ++nVertexComponents
167     // */
168     // List<Native.StringMember> smList = new List<Native.StringMember>();
169     // int count = 0;
170     // int start = pointer.ToInt32();
171     // while (1 == 1)
172     // {
173     // try
174     // {
175     // Native.StringMember sm = ReadFromMemory<Native.StringMember>(new IntPtr(start + (count * 32)));
176     // if (sm.Type == 0) break;
177     // smList.Add(sm);
178     // ++count;
179     // }
180     // catch (Exception)
181     // {
182     // break;
183     // }
184     // }
185     // return smList.ToArray();
186     //}
187    
188     public static int SizeOfStruct<T>() where T : struct
189     {
190 william 96 try
191 william 70 {
192 william 96
193     //object[] attributes = typeof(T).GetCustomAttributes(false);
194     //if (attributes == null || attributes.Length < 1) return 0;
195     int size = 0;
196     //foreach (object attribute in attributes)
197     //{
198     // if (attribute is SizeAttribute)
199     // {
200     // size = (attribute as SizeAttribute).Size;
201     // break;
202     // }
203     //}
204     size = Marshal.SizeOf(typeof(T));
205     return size;
206 william 70 }
207 william 102 catch
208 william 96 {
209     StackTrace st = new StackTrace(true);
210 william 102 #if ENABLE_EXCEPTION_OUTPUT_TO_CONSOLE
211     Console.WriteLine(st.ToString());
212     #endif
213 william 96 return 0;
214     }
215 william 70 }
216     }
217     }

  ViewVC Help
Powered by ViewVC 1.1.22