/[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 96 - (show annotations) (download)
Sat Jul 17 05:11:13 2010 UTC (9 years, 1 month ago) by william
File size: 7157 byte(s)
bulk commit:

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

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 (Exception ex)
27 {
28 StackTrace st = new StackTrace(true);
29 Console.WriteLine(st.ToString() + "\n\n" + "Stack Trace: \n" + ex.ToString());
30 return default(T);
31 }
32 }
33
34 public static T[] ReadArrayFromMemory<T>(ArrayOfStructures array) where T : struct
35 {
36 try
37 {
38 return ReadArrayFromMemory<T>(array.Count, array.Pointer);
39 }
40 catch (Exception ex)
41 {
42 StackTrace st = new StackTrace(true);
43 Console.WriteLine(st.ToString() + "\n\n" + "Stack Trace: \n" + ex.ToString());
44 return default(T[]);
45 }
46 }
47
48 public static T[] ReadArrayFromMemory<T>(int count, IntPtr pointer) where T : struct
49 {
50 try
51 {
52 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 }
60 catch (Exception ex)
61 {
62 StackTrace st = new StackTrace(true);
63 Console.WriteLine(st.ToString() + "\n\n" + "Stack Trace: \n" + ex.ToString());
64 return default(T[]);
65 }
66 }
67
68 public static IntPtr[] ReadPtrArrayFromMemory<T>(ArrayOfStructures array) where T : struct
69 {
70 try
71 {
72 return ReadPtrArrayFromMemory<T>(array.Count, array.Pointer);
73 }
74 catch (Exception ex)
75 {
76 StackTrace st = new StackTrace(true);
77 Console.WriteLine(st.ToString() + "\n\n" + "Stack Trace: \n" + ex.ToString());
78 return default(IntPtr[]);
79 }
80 }
81
82 public static IntPtr[] ReadPtrArrayFromMemory<T>(int count, IntPtr pointer) where T : struct
83 {
84 try
85 {
86 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 }
94 catch (Exception ex)
95 {
96 StackTrace st = new StackTrace(true);
97 Console.WriteLine(st.ToString() + "\n\n" + "Stack Trace: \n" + ex.ToString());
98 return default(IntPtr[]);
99 }
100 }
101
102 public static IntPtr[] ReadArrayFromMemory(ArrayOfReferences array)
103 {
104 try
105 {
106 return ReadArrayFromMemory(array.Count, array.Pointer);
107 }
108 catch (Exception ex)
109 {
110 StackTrace st = new StackTrace(true);
111 Console.WriteLine(st.ToString() + "\n\n" + "Stack Trace: \n" + ex.ToString());
112 return default(IntPtr[]);
113 }
114 }
115
116 public static IntPtr[] ReadArrayFromMemory(int count, IntPtr pointer)
117 {
118 try
119 {
120 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 }
127 catch (Exception ex)
128 {
129 StackTrace st = new StackTrace(true);
130 Console.WriteLine(st.ToString() + "\n\n" + "Stack Trace: \n" + ex.ToString());
131 return default(IntPtr[]);
132 }
133 }
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 try
177 {
178
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 }
193 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 }
200 }
201 }

  ViewVC Help
Powered by ViewVC 1.1.22