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 |
internal static void ReadFromMemory(string filename, ref granny_file grn_file) |
50 |
{ |
51 |
//if (grn_file.NativePointer == IntPtr.Zero) return; |
52 |
gr2lib.core.coretypes.native.granny_file native = Helpers.ReadFromMemory<gr2lib.core.coretypes.native.granny_file>(grn_file.NativePointer); |
53 |
granny_file managed = new granny_file(); |
54 |
managed.NativePointer = grn_file.NativeFilePointer; |
55 |
|
56 |
// process |
57 |
|
58 |
|
59 |
managed.IsByteReversed = native.IsByteReversed; |
60 |
managed.SectionCount = native.SectionCount; |
61 |
managed.Sections = native.Sections; |
62 |
managed.Marshalled = native.Marshalled; |
63 |
managed.IsUserMemory = native.IsUserMemory; |
64 |
managed.ConversionBuffer = native.ConversionBuffer; |
65 |
|
66 |
if (native.Header != IntPtr.Zero) |
67 |
{ |
68 |
managed.Header = granny_header.ReadFromMemory(native.Header); |
69 |
managed.Header.TypeTag = coreapi.GrannyGetFileTypeTag(grn_file.NativePointer); |
70 |
} |
71 |
else |
72 |
{ |
73 |
managed.Header = new granny_header(); |
74 |
} |
75 |
|
76 |
if (native.SourceMagicValue != IntPtr.Zero) |
77 |
{ |
78 |
managed.SourceMagicValue = granny_magic.ReadFromMemory(native.SourceMagicValue); |
79 |
} |
80 |
else |
81 |
{ |
82 |
managed.SourceMagicValue = new granny_magic(); |
83 |
} |
84 |
|
85 |
grn_file.NativeFilePointer = coreapi.GetFileInfo(grn_file.NativePointer); |
86 |
managed.NativeFilePointer = grn_file.NativeFilePointer; |
87 |
managed.GrannyFileInfo = grnfileinfo.ReadFromMemory(managed.NativeFilePointer); |
88 |
|
89 |
grn_file = managed; |
90 |
} |
91 |
|
92 |
|
93 |
|
94 |
public static granny_file ReadFromFile(string filename) |
95 |
{ |
96 |
granny_file grn_file = new granny_file(); |
97 |
grn_file.NativePointer = coreapi.GrannyReadEntireFile(filename); |
98 |
if (grn_file.NativePointer == null || grn_file.NativePointer == IntPtr.Zero) |
99 |
{ |
100 |
Console.WriteLine("Error: unable to load {0} as a Granny file.", filename); |
101 |
} |
102 |
|
103 |
ReadFromMemory(filename, ref grn_file); |
104 |
coreapi.FreeFile(grn_file.NativePointer); |
105 |
return grn_file; |
106 |
} |
107 |
|
108 |
#region INativePointer Members |
109 |
private IntPtr _NativePointer; |
110 |
public IntPtr NativePointer { get { return _NativePointer; } set { _NativePointer = value; } } |
111 |
private IntPtr _NativeFilePointer; |
112 |
public IntPtr NativeFilePointer { get { return _NativeFilePointer; } set { _NativeFilePointer = value; } } |
113 |
#endregion |
114 |
|
115 |
#region igranny2header members |
116 |
|
117 |
private bool _IsByteReversed; |
118 |
private granny_header _header; |
119 |
private granny_magic _SourceMagicValue; |
120 |
private int _SectionCount; |
121 |
private IntPtr _Sections; |
122 |
private bool _Marshalled; |
123 |
private bool _IsUserMemory; |
124 |
private IntPtr _ConversionBuffer; |
125 |
|
126 |
|
127 |
private grnfileinfo _GrannyFileInfo; |
128 |
|
129 |
public grnfileinfo GrannyFileInfo { get { return _GrannyFileInfo; } set { _GrannyFileInfo = value; } } |
130 |
public bool IsByteReversed { get { return _IsByteReversed; } set { _IsByteReversed = value; } } |
131 |
public granny_header Header { get { return _header; } set { _header = value; } } |
132 |
public granny_magic SourceMagicValue { get { return _SourceMagicValue; } set { _SourceMagicValue = value; } } |
133 |
public int SectionCount { get { return _SectionCount; } set { _SectionCount = value; } } |
134 |
public IntPtr Sections { get { return _Sections; } set { _Sections = value; } } |
135 |
public bool Marshalled { get { return _Marshalled; } set { _Marshalled = value; } } |
136 |
public bool IsUserMemory { get { return _IsUserMemory; } set { _IsUserMemory = value; } } |
137 |
public IntPtr ConversionBuffer { get { return _ConversionBuffer; } set { _ConversionBuffer = value; } } |
138 |
|
139 |
|
140 |
public bool HaveHeader { get { if (Header == null) return false; return true; } } |
141 |
public bool HaveMagic { get { if (SourceMagicValue == null) return false; return true; } } |
142 |
#endregion |
143 |
} |
144 |
} |