/[gr2lib]/trunk/gr2lib/core/coreclasses/header/granny_file.cs
ViewVC logotype

Contents of /trunk/gr2lib/core/coreclasses/header/granny_file.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 191 - (show annotations) (download)
Sat Jul 24 06:31:50 2010 UTC (9 years, 1 month ago) by william
File size: 10142 byte(s)
In coreapi:
*Add more texture methods
*Move everthing into related classes, to group related methods,etc
*Update all classes to use these grouped classes

1 //#define HAVE_GRANNY_API_LOGGER // when enabled, enable granny_log_callback_builder
2 using System;
3 using System.Collections.Generic;
4 using System.Linq;
5 using System.Text;
6 using gr2lib.core.interfaces;
7 using gr2lib.core.exceptions;
8 using gr2lib.core.apiversion;
9 using gr2lib.core.coreclasses.header;
10 using gr2lib.core.coretypes;
11 using gr2lib.core.helpers;
12 using gr2lib.core.coretypes.implementation;
13 using gr2lib.core.ui.helpers;
14
15 namespace gr2lib.core.header
16 {
17 /// <summary>
18 /// The master granny_file class -- this is where it all starts
19 /// </summary>
20 public class granny_file : igranny_file
21 {
22 //private const int GrannyGRNExtraTagCount = 4;
23 //private const int GrannyGRNMagixValueCount = 4;
24 //private const int GrannyGRNReservedUnusedCount = 3;
25 //private const int GrannyGRNReservedCount = 2;
26
27 /// <summary>
28 /// default constructor
29 /// </summary>
30 public granny_file()
31 {
32 granny2apiloader _loader = new granny2apiloader(true);
33 this.Initialize();
34 this.GrannyAPILogger = new granny_log_callback_builder();
35 this.GrannyAPILogger.StartLogging();
36 GC.KeepAlive(this.GrannyAPILogger._callback);
37 }
38 /// <summary>
39 /// default constructor, specifying Logging delefate for GrannyAPILogger
40 /// </summary>
41 public granny_file(gr2lib.core.coretypes.implementation.granny_log_callback_builder.OnMessageLogged OnMessageLoggedEventHandler)
42 {
43 granny2apiloader _loader = new granny2apiloader(true);
44 this.Initialize();
45
46 this.GrannyAPILogger = new granny_log_callback_builder(OnMessageLoggedEventHandler);
47 this.GrannyAPILogger.StartLogging();
48 }
49 /// <summary>
50 /// default constructor, specifying whether to check for the granny api, mostly used by designable classes so they won't throw an error about the missing API
51 /// </summary>
52 public granny_file(bool checkforgrannyapi)
53 {
54 granny2apiloader _loader = new granny2apiloader(checkforgrannyapi);
55
56 this.Initialize();
57 }
58
59 private void Initialize()
60 {
61 this.IsByteReversed = false;
62 this.Header = new granny_header();
63 this.SourceMagicValue = new granny_magic();
64 this.SectionCount = 0;
65 this.Sections = IntPtr.Zero;
66 this.Marshalled = false;
67 this.IsUserMemory = false;
68 this.ConversionBuffer = IntPtr.Zero;
69
70 this.NativePointer = IntPtr.Zero;
71 this.NativeFilePointer = IntPtr.Zero;
72
73 this.GrannyFileInfo = new grnfileinfo();
74
75 }
76
77 /// <summary>
78 /// Dispose's and frees this instance
79 /// </summary>
80 public void Dispose()
81 {
82 if (NativeFilePointer == IntPtr.Zero) return;
83 coreapi.FileLoadingSupport.FreeFile(NativeFilePointer);
84 }
85
86 internal static void ReadFromMemory(string filename, ref granny_file grn_file, gr2lib.core.coretypes.implementation.granny_log_callback_builder.OnMessageLogged OnMessageLoggedEventHandler)
87 {
88 //if (grn_file.NativePointer == IntPtr.Zero) return;
89 gr2lib.core.coretypes.native.granny_file native = Helpers.ReadFromMemory<gr2lib.core.coretypes.native.granny_file>(grn_file.NativePointer);
90 granny_file managed = new granny_file(OnMessageLoggedEventHandler);
91 managed.NativePointer = grn_file.NativeFilePointer;
92
93 // process
94
95
96 managed.IsByteReversed = native.IsByteReversed;
97 managed.SectionCount = native.SectionCount;
98 managed.Sections = native.Sections;
99 managed.Marshalled = native.Marshalled;
100 managed.IsUserMemory = native.IsUserMemory;
101 managed.ConversionBuffer = native.ConversionBuffer;
102
103 if (native.Header != IntPtr.Zero)
104 {
105 managed.Header = granny_header.ReadFromMemory(native.Header);
106 managed.Header.TypeTag = coreapi.HeaderSupport.GrannyGetFileTypeTag(grn_file.NativePointer);
107 }
108 else
109 {
110 managed.Header = new granny_header();
111 }
112
113 if (native.SourceMagicValue != IntPtr.Zero)
114 {
115 managed.SourceMagicValue = granny_magic.ReadFromMemory(native.SourceMagicValue);
116 }
117 else
118 {
119 managed.SourceMagicValue = new granny_magic();
120 }
121 #if HAVE_GRANNY_API_LOGGER
122 grn_file.GrannyAPILogger.StartLogging();
123 #endif
124 grn_file.NativeFilePointer = coreapi.FileLoadingSupport.GetFileInfo(grn_file.NativePointer);
125
126 managed.NativeFilePointer = grn_file.NativeFilePointer;
127 managed.GrannyFileInfo = grnfileinfo.ReadFromMemory(managed.NativeFilePointer);
128
129 managed.GrannyFileInfo.FileName = new FilePath(filename);
130
131 grn_file = managed;
132 }
133
134
135 /// <summary>
136 /// Initializes this instance from file data
137 /// </summary>
138 /// <param name="filename">the filename</param>
139 /// <param name="grn_file">the granny_file</param>
140 /// <param name="OnMessageLoggedEventHandler">the granny2 api logging function to use</param>
141 /// <returns></returns>
142 public static granny_file ReadFromFile(string filename, ref granny_file grn_file, gr2lib.core.coretypes.implementation.granny_log_callback_builder.OnMessageLogged OnMessageLoggedEventHandler)
143 {
144
145 grn_file.NativePointer = coreapi.FileLoadingSupport.GrannyReadEntireFile(filename);
146
147 if (grn_file.NativePointer == null || grn_file.NativePointer == IntPtr.Zero)
148 {
149 Console.WriteLine("Error: unable to load {0} as a Granny file.", filename);
150 }
151 ReadFromMemory(filename, ref grn_file, OnMessageLoggedEventHandler);
152
153 coreapi.FileLoadingSupport.FreeFile(grn_file.NativePointer);
154
155 return grn_file;
156 }
157
158 #region INativePointer Members
159 private IntPtr _NativePointer;
160 /// <summary>
161 /// When used in a derived class, gets the native pointer for this instance
162 /// </summary>
163 public IntPtr NativePointer { get { return _NativePointer; } set { _NativePointer = value; } }
164 private IntPtr _NativeFilePointer;
165 /// <summary>
166 /// Represents a native file pointer for this instance
167 /// </summary>
168 public IntPtr NativeFilePointer { get { return _NativeFilePointer; } set { _NativeFilePointer = value; } }
169 #endregion
170
171 #region IGrannyAPILogger Members
172 private granny_log_callback_builder _GrannyAPILogger;
173 /// <summary>
174 /// Gets or Sets a Granny2 API Logger for this instance
175 /// </summary>
176 public granny_log_callback_builder GrannyAPILogger { get { return _GrannyAPILogger; } set { _GrannyAPILogger = value; } }
177 #endregion
178
179 #region igranny2header members
180
181 private bool _IsByteReversed;
182 private granny_header _header;
183 private granny_magic _SourceMagicValue;
184 private int _SectionCount;
185 private IntPtr _Sections;
186 private bool _Marshalled;
187 private bool _IsUserMemory;
188 private IntPtr _ConversionBuffer;
189
190
191 private grnfileinfo _GrannyFileInfo;
192 /// <summary>
193 /// Gets the Granny File Info associated with this instance
194 /// </summary>
195 public grnfileinfo GrannyFileInfo { get { return _GrannyFileInfo; } set { _GrannyFileInfo = value; } }
196 /// <summary>
197 /// Gets a value indicating if this instance Is ByteReversed
198 /// </summary>
199 public bool IsByteReversed { get { return _IsByteReversed; } set { _IsByteReversed = value; } }
200 /// <summary>
201 /// Gets or sets the header associated with this instance
202 /// </summary>
203 public granny_header Header { get { return _header; } set { _header = value; } }
204 /// <summary>
205 /// Gets or sets the magic associated with this instance
206 /// </summary>
207 public granny_magic SourceMagicValue { get { return _SourceMagicValue; } set { _SourceMagicValue = value; } }
208 /// <summary>
209 /// Gets or sets the SectionCount associated with this instance
210 /// </summary>
211 public int SectionCount { get { return _SectionCount; } set { _SectionCount = value; } }
212 /// <summary>
213 /// Gets a pointer to Sections associated with this instance
214 /// </summary>
215 public IntPtr Sections { get { return _Sections; } set { _Sections = value; } }
216 /// <summary>
217 /// Gets a value indicating if this instance is Marhsalled
218 /// </summary>
219 public bool Marshalled { get { return _Marshalled; } set { _Marshalled = value; } }
220 /// <summary>
221 /// Gets a value indicated if this instance is UserMemory
222 /// </summary>
223 public bool IsUserMemory { get { return _IsUserMemory; } set { _IsUserMemory = value; } }
224 /// <summary>
225 /// Gets a pointer to ConversionBuffer associated with this instance
226 /// </summary>
227 public IntPtr ConversionBuffer { get { return _ConversionBuffer; } set { _ConversionBuffer = value; } }
228
229 /// <summary>
230 /// Gets a value indicating if this instance contains a header
231 /// </summary>
232 public bool HaveHeader { get { if (Header == null) return false; return true; } }
233
234 /// <summary>
235 /// Gets a value indicating if this instance contains a magic
236 /// </summary>
237 public bool HaveMagic { get { if (SourceMagicValue == null) return false; return true; } }
238 #endregion
239 }
240 }

  ViewVC Help
Powered by ViewVC 1.1.22