-
Notifications
You must be signed in to change notification settings - Fork 1
/
MZTFileReader.cs
103 lines (84 loc) · 3.81 KB
/
MZTFileReader.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Security.RightsManagement;
using System.Text;
using System.Threading.Tasks;
using static QDTool.Utility;
namespace QDTool
{
internal class MZTFileReader
{
public long currentPosition = 0;
public long totalLength = 0;
public long bytesRemaining = 0;
public List<(MZQFileHeader, MZQFileBody)> ReadMztFile(string filePath)
{
List<(MZQFileHeader, MZQFileBody)> mzfBlocks = new List<(MZQFileHeader, MZQFileBody)>();
using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read))
using (BinaryReader reader = new BinaryReader(fs))
{
// Read each MZF File
while ((reader.BaseStream.Position + 128 < reader.BaseStream.Length))
{
var mzfBlock = ReadMzfFile(reader);
mzfBlocks.Add(mzfBlock);
}
currentPosition = fs.Position;
totalLength = fs.Length;
bytesRemaining = totalLength - currentPosition;
}
return mzfBlocks;
}
public (MZQFileHeader, MZQFileBody) ReadMzfFile(BinaryReader reader)
{
MZQFileHeader header = new MZQFileHeader();
// Načtení jednotlivých členů struktury
header.StartSign = ExpectedStartSign; // zadny tu neni, ale vyplnime
header.MzfHeaderSign = 0x00;
header.DataSize = 0x0040; // (ushort)(header.MzfSize + 128); // ma vyznam asi jen u MZQ - ??? - aaa
header.MzfFtype = reader.ReadByte();
header.MzfFname = reader.ReadBytes(16);
header.MzfFnameEnd = reader.ReadByte();
header.Unused1 = ExpectedUnused; // zadny tu neni, ale vyplnime
header.MzfSize = reader.ReadUInt16();
header.MzfStart = reader.ReadUInt16();
header.MzfExec = reader.ReadUInt16();
// Načtení celych 104 bajtů pro MzfHeaderDescription
header.MzfHeaderDescription = new byte[104]; // Inicializace pole 104 bajty
byte[] descriptionBytes = reader.ReadBytes(104); // Načtení tady 104 bajtů
Array.Copy(descriptionBytes, header.MzfHeaderDescription, descriptionBytes.Length);
// Načtení CRC
header.Crc = ExpectedCrc; // zadny tu neni, ale vyplnime
// Načtení zbytku MZF Těla
MZQFileBody body = new MZQFileBody
{
StartSign = ExpectedStartSign,
MzfBodySign = 0x05,
DataSize = header.MzfSize, // ??? - aaa
MzfBody = reader.ReadBytes(header.MzfSize),
Crc = ExpectedCrc
};
return (header, body);
}
public void WriteMZFFileHeaderToFile(FileStream fileStream, MZQFileHeader mzfHeader)
{
fileStream.WriteByte(mzfHeader.MzfFtype);
fileStream.Write(mzfHeader.MzfFname, 0, mzfHeader.MzfFname.Length);
fileStream.WriteByte(mzfHeader.MzfFnameEnd);
var mzfSizeBytes = BitConverter.GetBytes(mzfHeader.MzfSize);
fileStream.Write(mzfSizeBytes, 0, mzfSizeBytes.Length);
var mzfStartBytes = BitConverter.GetBytes(mzfHeader.MzfStart);
fileStream.Write(mzfStartBytes, 0, mzfStartBytes.Length);
var mzfExecBytes = BitConverter.GetBytes(mzfHeader.MzfExec);
fileStream.Write(mzfExecBytes, 0, mzfExecBytes.Length);
fileStream.Write(mzfHeader.MzfHeaderDescription, 0, 104);
}
public void WriteMZFFileBodyToFile(FileStream fileStream, MZQFileBody mzfBody)
{
fileStream.Write(mzfBody.MzfBody, 0, mzfBody.DataSize);
}
}
}