/[gr2lib]/trunk/gr2lib/core/coretypes/implementation/grnfileinfo.cs
ViewVC logotype

Annotation of /trunk/gr2lib/core/coretypes/implementation/grnfileinfo.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 90 - (hide annotations) (download)
Sat Jul 17 00:00:16 2010 UTC (9 years, 6 months ago) by william
File size: 7716 byte(s)
Add boolean indicators, that indicate if each type is present

1 william 70 using System;
2     using System.Collections.Generic;
3     using System.Text;
4     using gr2lib.core.helpers;
5 william 79 using gr2lib.core.interfaces;
6 william 70
7     namespace gr2lib.core.coretypes.implementation
8     {
9 william 87
10 william 82 public class grnfileinfo : Igrnfileinfo
11 william 70 {
12 william 72 private string fromFileName;
13     private ArtToolInfo _ArtToolInfo;
14     private ExporterInfo _ExporterInfo;
15     private List<Texture> _Textures;
16 william 79 private List<Material> _Materials;
17 william 83 private List<Skeleton> _Skeletons;
18 william 70
19 william 72
20 william 82 //VertexDatas
21     //TriTopologies
22     //Meshes
23     //Models
24     //TrackGroups
25     //Animations
26     //ExtendedData
27 william 72
28 william 70
29 william 82
30     private string filePath;
31     private string fileName;
32    
33 william 87 public grnfileinfo(string path)
34     {
35     this.filePath = path;
36     this.fileName = FileNameFromPath(path);
37     }
38 william 70
39     private static string FileNameFromPath(string path)
40     {
41     int lastSlash = path.LastIndexOf('\\');
42     if (lastSlash > 0) return path.Substring(lastSlash + 1);
43     lastSlash = path.LastIndexOf('/');
44     if (lastSlash > 0) return path.Substring(lastSlash + 1);
45     return path;
46     }
47    
48     internal static grnfileinfo ReadFromMemory(IntPtr pointer, string path)
49     {
50 william 87 try
51 william 70 {
52 william 87 if (pointer == IntPtr.Zero) return null;
53     native.grnfileinfo native = Helpers.ReadFromMemory<native.grnfileinfo>(pointer);
54     grnfileinfo managed = new grnfileinfo(path);
55     managed.NativePointer = pointer;
56 william 72
57 william 87 if (native.ArtToolInfo != IntPtr.Zero)
58     {
59     managed.ArtToolInfo = ArtToolInfo.ReadFromMemory(native.ArtToolInfo);
60     }
61     else
62     {
63     managed.ArtToolInfo = new ArtToolInfo();
64     }
65     if (native.ExporterInfo != IntPtr.Zero)
66     {
67     managed.ExporterInfo = ExporterInfo.ReadFromMemory(native.ExporterInfo);
68     }
69     else
70     {
71     managed.ExporterInfo = new ExporterInfo();
72     }
73 william 72
74 william 87 managed.fromFileName = string.IsNullOrEmpty(native.FromFileName) ? "{null}" : native.FromFileName;
75     managed.ExtendedData = native.ExtendedData;
76    
77     int length = 0;
78     if (native.Textures.Pointer != IntPtr.Zero)
79 william 72 {
80 william 87
81 william 83 IntPtr[] textures = Helpers.ReadArrayFromMemory(native.Textures);
82     length = textures.Length;
83     managed.Textures = new List<Texture>(length);
84     foreach (IntPtr ptr in textures)
85     {
86 william 86 Texture _tmp = new Texture();
87     _tmp.NativePointer = ptr;
88     managed.Textures.Add(_tmp);
89 william 83 }
90     for (int i = 0; i < length; i++)
91     {
92     managed.Textures[i] = Texture.ReadFromMemory(textures[i]);
93     }
94 william 72 }
95 william 87 else
96 william 72 {
97 william 87 managed.Textures = new List<Texture>();
98 william 72 }
99    
100 william 87 if (native.Materials.Pointer != IntPtr.Zero)
101 william 79 {
102 william 87 IntPtr[] materials = Helpers.ReadArrayFromMemory(native.Materials);
103     length = materials.Length;
104     managed.Materials = new List<Material>(length);
105     foreach (IntPtr ptr in materials)
106     {
107     managed.Materials.Add(new Material());
108     }
109     for (int i = 0; i < length; i++)
110     {
111     managed.Materials[i] = Material.ReadFromMemory(materials[i]);
112     }
113 william 79 }
114 william 87 else
115 william 79 {
116 william 87 managed.Materials = new List<Material>();
117 william 79 }
118 william 83
119 william 87 if (native.Skeletons.Pointer != IntPtr.Zero)
120 william 83 {
121 william 87 IntPtr[] skeletons = Helpers.ReadArrayFromMemory(native.Skeletons);
122     length = skeletons.Length;
123     managed.Skeletons = new List<Skeleton>(length);
124     foreach (IntPtr ptr in skeletons)
125     {
126     managed.Skeletons.Add(new Skeleton());
127     }
128     for (int i = 0; i < length; i++)
129     {
130     managed.Skeletons[i] = (Skeleton.ReadFromMemory(skeletons[i]));
131     }
132 william 83 }
133 william 87 else
134 william 83 {
135 william 87 managed.Skeletons = new List<Skeleton>();
136 william 83 }
137 william 87
138     return managed;
139 william 83 }
140 william 87 catch (Exception ex)
141 william 83 {
142 william 87 Console.WriteLine(ex.ToString());
143     return default(grnfileinfo);
144 william 83 }
145 william 70 }
146     public static grnfileinfo ReadFromFile(string fileName)
147     {
148     IntPtr file = coreapi.GrannyReadEntireFile(fileName);
149    
150     if (file == null || file == IntPtr.Zero)
151     {
152     Console.WriteLine("Error: unable to load {0} as a Granny file.", fileName);
153     }
154 william 83 grnfileinfo info = null;
155 william 70
156 william 83 try
157     {
158     IntPtr pinfo = coreapi.GetFileInfo(file);
159     info = ReadFromMemory(pinfo, fileName);
160     coreapi.FreeFile(file);
161 william 70
162 william 83 return info;
163     }
164     catch (Exception ex)
165     {
166 william 86 Console.WriteLine(ex.ToString());
167 william 87 return default(grnfileinfo);
168 william 83 }
169 william 70 }
170 william 82
171    
172     #region Igrnfileinfo Members
173 william 90
174    
175     public bool HaveArtToolInfo { get { if (this.ArtToolInfo == null) return false; return true; } }
176     public bool HaveExporterInfo { get { if (this.ExporterInfo == null) return false; return true; } }
177     public bool HaveFromFileName { get { if (this.FromFileName == null || this.FromFileName == "") return false; return true; } }
178     public bool HaveTextures { get { if (this.Textures == null || this.Textures.Count <= 0) return false; return true; } }
179     public bool HaveMaterials { get { if (this.Materials == null || this.Materials.Count <= 0) return false; return true; } }
180     public bool HaveSkeletons { get { if (this.Skeletons == null || this.Skeletons.Count <= 0) return false; return true; } }
181    
182 william 82 public ArtToolInfo ArtToolInfo { get { return _ArtToolInfo; } set { _ArtToolInfo = value; } }
183     public ExporterInfo ExporterInfo { get { return _ExporterInfo; } set { _ExporterInfo = value; } }
184     public string FromFileName { get { return fromFileName; } set { fromFileName = value; } }
185     public List<Texture> Textures { get { return _Textures; } set { _Textures = value; } }
186     public List<Material> Materials { get { return _Materials; } set { _Materials = value; } }
187 william 83 public List<Skeleton> Skeletons { get { return _Skeletons; } set { _Skeletons = value; } }
188 william 82 #endregion
189    
190     #region INativePointer Members
191     private IntPtr _NativePointer;
192     public IntPtr NativePointer { get { return _NativePointer; } set { _NativePointer = value; } }
193     #endregion
194    
195     #region IExtendedData Members
196     private IntPtr _ExtendedData;
197     public IntPtr ExtendedData { get { return _ExtendedData; } set { _ExtendedData = value; } }
198     #endregion
199 william 70 }
200     }

  ViewVC Help
Powered by ViewVC 1.1.22