1 |
using System; |
2 |
using System.Collections.Generic; |
3 |
using System.Linq; |
4 |
using System.Text; |
5 |
using gr2lib.core.interfaces; |
6 |
using gr2lib.core.exceptions; |
7 |
using gr2lib.core.apiversion; |
8 |
using gr2lib.core.coreclasses.header; |
9 |
using gr2lib.core.coretypes; |
10 |
using gr2lib.core.helpers; |
11 |
using gr2lib.core.coretypes.implementation; |
12 |
|
13 |
namespace gr2lib.core.header |
14 |
{ |
15 |
public class granny_file : igranny_file |
16 |
{ |
17 |
//private const int GrannyGRNExtraTagCount = 4; |
18 |
//private const int GrannyGRNMagixValueCount = 4; |
19 |
//private const int GrannyGRNReservedUnusedCount = 3; |
20 |
//private const int GrannyGRNReservedCount = 2; |
21 |
public granny_file() |
22 |
{ |
23 |
granny2apiloader _loader = new granny2apiloader(); |
24 |
this.IsByteReversed = false; |
25 |
this.Header = new granny_header(); |
26 |
this.SourceMagicValue = new granny_magic(); |
27 |
this.SectionCount = 0; |
28 |
this.Sections = IntPtr.Zero; |
29 |
this.Marshalled = false; |
30 |
this.IsUserMemory = false; |
31 |
this.ConversionBuffer = IntPtr.Zero; |
32 |
|
33 |
this.NativePointer = IntPtr.Zero; |
34 |
this.NativeFilePointer = IntPtr.Zero; |
35 |
|
36 |
this.GrannyFileInfo = new grnfileinfo(); |
37 |
} |
38 |
|
39 |
private void Initialize() |
40 |
{ |
41 |
} |
42 |
|
43 |
public void Dispose() |
44 |
{ |
45 |
if (NativeFilePointer == IntPtr.Zero) return; |
46 |
coreapi.FreeFile(NativeFilePointer); |
47 |
} |
48 |
|
49 |
|
50 |
//internal static granny_file ReadFromMemory(IntPtr pointer) |
51 |
//{ |
52 |
// if (pointer == IntPtr.Zero) return null; |
53 |
// gr2lib.core.coretypes.native.granny_file native = Helpers.ReadFromMemory<gr2lib.core.coretypes.native.granny_file>(pointer); |
54 |
// granny_file managed = new granny_file(); |
55 |
// managed.NativePointer = pointer; |
56 |
|
57 |
// // process |
58 |
|
59 |
|
60 |
// return managed; |
61 |
//} |
62 |
internal static void ReadFromMemory(string filename, ref granny_file grn_file) |
63 |
{ |
64 |
//if (grn_file.NativePointer == IntPtr.Zero) return; |
65 |
gr2lib.core.coretypes.native.granny_file native = Helpers.ReadFromMemory<gr2lib.core.coretypes.native.granny_file>(grn_file.NativePointer); |
66 |
granny_file managed = new granny_file(); |
67 |
managed.NativePointer = grn_file.NativeFilePointer; |
68 |
|
69 |
// process |
70 |
|
71 |
managed.IsByteReversed = native.IsByteReversed; |
72 |
managed.SectionCount = native.SectionCount; |
73 |
managed.Sections = native.Sections; |
74 |
managed.Marshalled = native.Marshalled; |
75 |
managed.IsUserMemory = native.IsUserMemory; |
76 |
managed.ConversionBuffer = native.ConversionBuffer; |
77 |
|
78 |
if (native.Header != IntPtr.Zero) |
79 |
{ |
80 |
managed.Header = granny_header.ReadFromMemory(native.Header); |
81 |
managed.Header.TypeTag = coreapi.GrannyGetFileTypeTag(grn_file.NativePointer); |
82 |
} |
83 |
else |
84 |
{ |
85 |
managed.Header = new granny_header(); |
86 |
} |
87 |
|
88 |
if (native.SourceMagicValue != IntPtr.Zero) |
89 |
{ |
90 |
managed.SourceMagicValue = granny_magic.ReadFromMemory(native.SourceMagicValue); |
91 |
} |
92 |
else |
93 |
{ |
94 |
managed.SourceMagicValue = new granny_magic(); |
95 |
} |
96 |
|
97 |
grn_file.NativeFilePointer = coreapi.GetFileInfo(grn_file.NativePointer); |
98 |
managed.NativeFilePointer = grn_file.NativeFilePointer; |
99 |
managed.GrannyFileInfo = grnfileinfo.ReadFromMemory(managed.NativeFilePointer); |
100 |
|
101 |
grn_file = managed; |
102 |
} |
103 |
|
104 |
//public static void ReadFromFile(string filename, out granny_file grn_file) |
105 |
//{ |
106 |
// grn_file = new granny_file(); |
107 |
// grn_file.NativePointer = coreapi.GrannyReadEntireFile(filename); |
108 |
// if (grn_file.NativePointer == null || grn_file.NativePointer == IntPtr.Zero) |
109 |
// { |
110 |
// Console.WriteLine("Error: unable to load {0} as a Granny file.", filename); |
111 |
// } |
112 |
|
113 |
// grn_file.NativeFilePointer = coreapi.GetFileInfo(grn_file.NativePointer); |
114 |
// ReadFromMemory(out grn_file); |
115 |
// coreapi.FreeFile(grn_file.NativePointer); |
116 |
//} |
117 |
|
118 |
public static granny_file ReadFromFile(string filename) |
119 |
{ |
120 |
granny_file grn_file = new granny_file(); |
121 |
grn_file.NativePointer = coreapi.GrannyReadEntireFile(filename); |
122 |
if (grn_file.NativePointer == null || grn_file.NativePointer == IntPtr.Zero) |
123 |
{ |
124 |
Console.WriteLine("Error: unable to load {0} as a Granny file.", filename); |
125 |
} |
126 |
|
127 |
ReadFromMemory(filename, ref grn_file); |
128 |
coreapi.FreeFile(grn_file.NativePointer); |
129 |
return grn_file; |
130 |
} |
131 |
|
132 |
#region INativePointer Members |
133 |
private IntPtr _NativePointer; |
134 |
public IntPtr NativePointer { get { return _NativePointer; } set { _NativePointer = value; } } |
135 |
private IntPtr _NativeFilePointer; |
136 |
public IntPtr NativeFilePointer { get { return _NativeFilePointer; } set { _NativeFilePointer = value; } } |
137 |
#endregion |
138 |
|
139 |
#region igranny2header members |
140 |
|
141 |
private bool _IsByteReversed; |
142 |
private granny_header _header; |
143 |
private granny_magic _SourceMagicValue; |
144 |
private int _SectionCount; |
145 |
private IntPtr _Sections; |
146 |
private bool _Marshalled; |
147 |
private bool _IsUserMemory; |
148 |
private IntPtr _ConversionBuffer; |
149 |
|
150 |
|
151 |
private grnfileinfo _GrannyFileInfo; |
152 |
|
153 |
public grnfileinfo GrannyFileInfo { get { return _GrannyFileInfo; } set { _GrannyFileInfo = value; } } |
154 |
public bool IsByteReversed { get { return _IsByteReversed; } set { _IsByteReversed = value; } } |
155 |
public granny_header Header { get { return _header; } set { _header = value; } } |
156 |
public granny_magic SourceMagicValue { get { return _SourceMagicValue; } set { _SourceMagicValue = value; } } |
157 |
public int SectionCount { get { return _SectionCount; } set { _SectionCount = value; } } |
158 |
public IntPtr Sections { get { return _Sections; } set { _Sections = value; } } |
159 |
public bool Marshalled { get { return _Marshalled; } set { _Marshalled = value; } } |
160 |
public bool IsUserMemory { get { return _IsUserMemory; } set { _IsUserMemory = value; } } |
161 |
public IntPtr ConversionBuffer { get { return _ConversionBuffer; } set { _ConversionBuffer = value; } } |
162 |
#endregion |
163 |
} |
164 |
} |