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

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 using System.Diagnostics;
8
9 namespace gr2lib.core.helpers
10 {
11 internal class Helpers
12 {
13 public static T ReadFromMemory<T>(IntPtr pointer) where T : struct
14 {
15 try
16 {
17 if (pointer == IntPtr.Zero)
18 {
19 throw new InvalidOperationException();
20 }
21
22 T _object_implementation_ = (T)Marshal.PtrToStructure(pointer, typeof(T));
23
24 return _object_implementation_;
25 }
26 catch
27 {
28 StackTrace st = new StackTrace(true);
29 #if ENABLE_EXCEPTION_OUTPUT_TO_CONSOLE
30 Console.WriteLine(st.ToString());
31 #endif
32 return default(T);
33 }
34 }
35
36 public static T[] ReadArrayFromMemory<T>(ArrayOfStructures array) where T : struct
37 {
38 try
39 {
40 return ReadArrayFromMemory<T>(array.Count, array.Pointer);
41 }
42 catch
43 {
44 StackTrace st = new StackTrace(true);
45 #if ENABLE_EXCEPTION_OUTPUT_TO_CONSOLE
46 Console.WriteLine(st.ToString());
47 #endif
48 return default(T[]);
49 }
50 }
51
52 public static T[] ReadArrayFromMemory<T>(int count, IntPtr pointer) where T : struct
53 {
54 try
55 {
56 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 }
64 catch
65 {
66 StackTrace st = new StackTrace(true);
67 #if ENABLE_EXCEPTION_OUTPUT_TO_CONSOLE
68 Console.WriteLine(st.ToString());
69 #endif
70 return default(T[]);
71 }
72 }
73
74 public static IntPtr[] ReadPtrArrayFromMemory<T>(ArrayOfStructures array) where T : struct
75 {
76 try
77 {
78 return ReadPtrArrayFromMemory<T>(array.Count, array.Pointer);
79 }
80 catch
81 {
82 StackTrace st = new StackTrace(true);
83 #if ENABLE_EXCEPTION_OUTPUT_TO_CONSOLE
84 Console.WriteLine(st.ToString());
85 #endif
86 return default(IntPtr[]);
87 }
88 }
89
90 public static IntPtr[] ReadPtrArrayFromMemory<T>(int count, IntPtr pointer) where T : struct
91 {
92 try
93 {
94 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 }
102 catch
103 {
104 StackTrace st = new StackTrace(true);
105 #if ENABLE_EXCEPTION_OUTPUT_TO_CONSOLE
106 Console.WriteLine(st.ToString());
107 #endif
108 return default(IntPtr[]);
109 }
110 }
111
112 public static IntPtr[] ReadArrayFromMemory(ArrayOfReferences array)
113 {
114 try
115 {
116 return ReadArrayFromMemory(array.Count, array.Pointer);
117 }
118 catch
119 {
120 StackTrace st = new StackTrace(true);
121 #if ENABLE_EXCEPTION_OUTPUT_TO_CONSOLE
122 Console.WriteLine(st.ToString());
123 #endif
124 return default(IntPtr[]);
125 }
126 }
127
128 public static IntPtr[] ReadArrayFromMemory(int count, IntPtr pointer)
129 {
130 try
131 {
132 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 }
139 catch
140 {
141 StackTrace st = new StackTrace(true);
142 #if ENABLE_EXCEPTION_OUTPUT_TO_CONSOLE
143 Console.WriteLine(st.ToString());
144 #endif
145 return default(IntPtr[]);
146 }
147 }
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 try
191 {
192
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 }
207 catch
208 {
209 StackTrace st = new StackTrace(true);
210 #if ENABLE_EXCEPTION_OUTPUT_TO_CONSOLE
211 Console.WriteLine(st.ToString());
212 #endif
213 return 0;
214 }
215 }
216 }
217 }

  ViewVC Help
Powered by ViewVC 1.1.22