ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/gr2lib/trunk/gr2lib/core/helpers/Helpers.cs
Revision: 106
Committed: Sat Jul 17 14:52:37 2010 UTC (12 years, 2 months ago) by william
File size: 7316 byte(s)
Log Message:
implement Granny2ExceptionWriter

File Contents

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