/[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 79 - (show annotations) (download)
Thu Jul 15 21:50:56 2010 UTC (11 years, 6 months ago) by william
File size: 4219 byte(s)
create and implement interfaces for all core classes (for type casting)

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
11 {
12 private string fromFileName;
13 private ArtToolInfo _ArtToolInfo;
14 private ExporterInfo _ExporterInfo;
15 private List<Texture> _Textures;
16 private List<Material> _Materials;
17
18 #region Igrnfileinfo Members
19 public ArtToolInfo ArtToolInfo { get { return _ArtToolInfo; } set { _ArtToolInfo = value; } }
20 public ExporterInfo ExporterInfo { get { return _ExporterInfo; } set { _ExporterInfo = value; } }
21 public string FromFileName { get { return fromFileName; } set { fromFileName = value; } }
22 public List<Texture> Textures { get { return _Textures; } set { _Textures = value; } }
23 public List<Material> Materials { get { return _Materials; } set { _Materials = value; } }
24 #endregion
25
26 public IntPtr NativePointer;
27
28 internal string filePath;
29 internal string fileName;
30
31 public grnfileinfo(string path)
32 {
33 this.filePath = path;
34 this.fileName = FileNameFromPath(path);
35 }
36
37 private static string FileNameFromPath(string path)
38 {
39 int lastSlash = path.LastIndexOf('\\');
40 if (lastSlash > 0) return path.Substring(lastSlash + 1);
41 lastSlash = path.LastIndexOf('/');
42 if (lastSlash > 0) return path.Substring(lastSlash + 1);
43 return path;
44 }
45
46 internal static grnfileinfo ReadFromMemory(IntPtr pointer, string path)
47 {
48 if (pointer == IntPtr.Zero) return null;
49 native.FileInfo native = Helpers.ReadFromMemory<native.FileInfo>(pointer);
50 grnfileinfo managed = new grnfileinfo(path);
51 managed.NativePointer = pointer;
52
53 if (native.ArtToolInfo != IntPtr.Zero)
54 {
55 managed.ArtToolInfo = ArtToolInfo.ReadFromMemory(native.ArtToolInfo);
56 }
57 if (native.ExporterInfo != IntPtr.Zero)
58 {
59 managed.ExporterInfo = ExporterInfo.ReadFromMemory(native.ExporterInfo);
60 }
61
62 managed.fromFileName = string.IsNullOrEmpty(native.FromFileName) ? "{null}" : native.FromFileName;
63
64 int length = 0;
65 if (native.Textures.Pointer != IntPtr.Zero)
66 {
67 IntPtr[] textures = Helpers.ReadArrayFromMemory(native.Textures);
68 length = textures.Length;
69 managed.Textures = new List<Texture>(length);
70 foreach (IntPtr ptr in textures)
71 {
72 managed.Textures.Add(new Texture());
73 }
74 for (int i = 0; i < length; i++)
75 {
76 managed.Textures[i] = Texture.ReadFromMemory(textures[i]);
77 }
78 }
79
80 if (native.Materials.Pointer != IntPtr.Zero)
81 {
82 IntPtr[] materials = Helpers.ReadArrayFromMemory(native.Materials);
83 length = materials.Length;
84 managed.Materials = new List<Material>(length);
85 foreach (IntPtr ptr in materials)
86 {
87 managed.Materials.Add(new Material());
88 }
89 for (int i = 0; i < length; i++)
90 {
91 managed.Materials[i] = Material.ReadFromMemory(materials[i]);
92 }
93 }
94 return managed;
95 }
96 public static grnfileinfo ReadFromFile(string fileName)
97 {
98 IntPtr file = coreapi.GrannyReadEntireFile(fileName);
99
100 if (file == null || file == IntPtr.Zero)
101 {
102 Console.WriteLine("Error: unable to load {0} as a Granny file.", fileName);
103 }
104
105 IntPtr pinfo = coreapi.GetFileInfo(file);
106 grnfileinfo info = ReadFromMemory(pinfo, fileName);
107 coreapi.FreeFile(file);
108
109 return info;
110 }
111 }
112 }

  ViewVC Help
Powered by ViewVC 1.1.22