-
Notifications
You must be signed in to change notification settings - Fork 2
/
HeaderParser.cs
108 lines (79 loc) · 2.97 KB
/
HeaderParser.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
104
105
106
107
108
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ReplayParser.Entities;
using ReplayParser.Interfaces;
namespace ReplayParser.Parser
{
public class HeaderParser : AbstractParser
{
private static int GAME_NAME_SIZE = 28;
private static int CREATOR_NAME_SIZE = 24;
private static int PLAYER_NAME_SIZE = 25;
private static int MAP_NAME_SIZE = 26;
private static int PLAYER_COLOUR_OFFSET = 593;
private static int PLAYER_SPOT_OFFSET = 625;
public HeaderParser(byte[] data)
: base(data)
{
}
public Header ParseHeader()
{
EngineType gameEngine = (EngineType)_input.ReadByte();
int frameCount = _input.ReadInt();
byte fillb = _input.ReadByte();
byte fillc = _input.ReadByte();
byte filld = _input.ReadByte();
long gameTime = _input.ReadInt();
DateTime creation = new DateTime(1970, 1, 1).AddSeconds(gameTime);
byte[] ka2 = new byte[8];
_input.ReadBytes(ref ka2);
int ka3 = _input.ReadInt();
String gameName = parseString(GAME_NAME_SIZE);
int mapWidth = _input.ReadUnsignedShort();
int mapHeight = _input.ReadUnsignedShort();
byte[] fill2 = new byte[16];
_input.ReadBytes(ref fill2);
String gameCreator = parseString(CREATOR_NAME_SIZE);
byte mapTypeId = _input.ReadByte();
String mapName = parseString(MAP_NAME_SIZE);
byte[] fill3 = new byte[6];
_input.ReadBytes(ref fill3);
GameType gameType = (GameType)_input.ReadByte();
byte[] fill4 = new byte[31];
_input.ReadBytes(ref fill4);
IList<IPlayer> players = ParsePlayers();
return new Header(gameEngine, creation, gameType, gameCreator, mapName, frameCount, gameName, mapWidth, mapHeight, players);
}
private IList<IPlayer> ParsePlayers()
{
IList<IPlayer> players = new List<IPlayer>();
for (int i = 0; i < 12; i++) {
IPlayer player = ParsePlayer(i);
if (player.PlayerType != PlayerType.None) {
players.Add(player);
}
}
return players;
}
private IPlayer ParsePlayer(int index)
{
int identifier = _input.ReadInt();
SlotType slotType = (SlotType)_input.ReadInt();
PlayerType playerType = (PlayerType)_input.ReadByte();
RaceType raceType = (RaceType)_input.ReadByte();
byte forceId = _input.ReadByte();
String name = parseString(PLAYER_NAME_SIZE);
ColourType colourType = ColourType.Unknown;
byte spot = 0;
if (index < 8) {
int colourOffset = PLAYER_COLOUR_OFFSET + (index * 4);
colourType = (ColourType)_data[colourOffset];
int spotOffset = PLAYER_SPOT_OFFSET + index;
spot = _data[spotOffset];
}
return new Player(identifier, slotType, playerType, raceType, forceId, name, colourType, spot);
}
}
}