/[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 166 - (show annotations) (download)
Thu Jul 22 14:24:07 2010 UTC (9 years, 6 months ago) by william
File size: 7435 byte(s)
bulk commit

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

  ViewVC Help
Powered by ViewVC 1.1.22