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

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

Parent Directory Parent Directory | Revision Log Revision Log


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

1 using System;
2 using System.Collections.Generic;
3 using System.Text;
4 using gr2lib.core.helpers;
5 using gr2lib.core.interfaces;
6
7 namespace gr2lib.core.coretypes.implementation
8 {
9
10 public class grnfileinfo : Igrnfileinfo
11 {
12 private string fromFileName;
13 private ArtToolInfo _ArtToolInfo;
14 private ExporterInfo _ExporterInfo;
15 private List<Texture> _Textures;
16 private List<Material> _Materials;
17 private List<Skeleton> _Skeletons;
18
19
20 //VertexDatas
21 //TriTopologies
22 //Meshes
23 //Models
24 //TrackGroups
25 //Animations
26 //ExtendedData
27
28
29
30 private string filePath;
31 private string fileName;
32
33 public grnfileinfo(string path)
34 {
35 this.filePath = path;
36 this.fileName = FileNameFromPath(path);
37 }
38
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 try
51 {
52 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
57 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
74 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 {
80
81 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 Texture _tmp = new Texture();
87 _tmp.NativePointer = ptr;
88 managed.Textures.Add(_tmp);
89 }
90 for (int i = 0; i < length; i++)
91 {
92 managed.Textures[i] = Texture.ReadFromMemory(textures[i]);
93 }
94 }
95 else
96 {
97 managed.Textures = new List<Texture>();
98 }
99
100 if (native.Materials.Pointer != IntPtr.Zero)
101 {
102 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 }
114 else
115 {
116 managed.Materials = new List<Material>();
117 }
118
119 if (native.Skeletons.Pointer != IntPtr.Zero)
120 {
121 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 }
133 else
134 {
135 managed.Skeletons = new List<Skeleton>();
136 }
137
138 return managed;
139 }
140 catch (Exception ex)
141 {
142 Console.WriteLine(ex.ToString());
143 return default(grnfileinfo);
144 }
145 }
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 grnfileinfo info = null;
155
156 try
157 {
158 IntPtr pinfo = coreapi.GetFileInfo(file);
159 info = ReadFromMemory(pinfo, fileName);
160 coreapi.FreeFile(file);
161
162 return info;
163 }
164 catch (Exception ex)
165 {
166 Console.WriteLine(ex.ToString());
167 return default(grnfileinfo);
168 }
169 }
170
171
172 #region Igrnfileinfo Members
173
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 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 public List<Skeleton> Skeletons { get { return _Skeletons; } set { _Skeletons = value; } }
188 #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 }
200 }

  ViewVC Help
Powered by ViewVC 1.1.22