/[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 83 - (hide annotations) (download)
Fri Jul 16 21:26:08 2010 UTC (9 years, 6 months ago) by william
File size: 5948 byte(s)
bulk commit --
** Enable Support for Skeletons & Bones

** A bug has found its way into the code
 -get an unhandled EHAccessViolation
 - I think the problem is in the Textures Class (or one of the sub-classes)

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

  ViewVC Help
Powered by ViewVC 1.1.22