-
Notifications
You must be signed in to change notification settings - Fork 0
/
Load.cs
201 lines (169 loc) · 6.66 KB
/
Load.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/* Load */
namespace SharpDune;
static class Load
{
/*
* In case the current house is Mercenary, another palette is loaded.
*/
internal static void Load_Palette_Mercenaries()
{
if (g_playerHouseID == HouseType.HOUSE_MERCENARY)
{
File_ReadBlockFile("IBM.PAL", g_palette1, 256 * 3);
}
}
internal static bool SaveGame_LoadFile(string filename)
{
FileStream fp;
bool res;
Sound_Output_Feedback(0xFFFE);
Game_Init();
fp = FOpenDataDir(SearchDirectory.SEARCHDIR_PERSONAL_DATA_DIR, filename, "rb");
if (fp == null)
{
Trace.WriteLine($"ERROR: Failed to open file '{filename}' for reading.");
return false;
}
Sprites_LoadTiles();
g_validateStrictIfZero++;
res = Load_Main(fp);
g_validateStrictIfZero--;
fp.Close();
if (!res)
{
Trace.WriteLine($"ERROR: Error while loading savegame '{filename}'.");
return false;
}
if (g_gameMode != GameMode.GM_RESTART) Game_Prepare();
return true;
}
static bool Load_Main(FileStream fp)
{
uint position;
uint length;
uint header;
ushort version;
try
{
using var br = new BinaryReader(fp);
/* All SharpDUNE / Dune2 savegames should start with 'FORM' */
header = br.ReadUInt32(); //if (fread(&header, sizeof(uint32), 1, fp) != 1) return false;
if (BEToH32(header) != (uint)SharpDune.MultiChar[FourCC.FORM])
{
Trace.WriteLine("ERROR: Invalid magic header in savegame. Not a SharpDUNE / Dune2 savegame.");
return false;
}
/* The total length field, which is ignored */
length = br.ReadUInt32(); //if (fread(&length, sizeof(uint32), 1, fp) != 1) return false;
/* The next 'chunk' is fake, and has no length field */
header = br.ReadUInt32(); //if (fread(&header, sizeof(uint32), 1, fp) != 1) return false;
if (BEToH32(header) != (uint)SharpDune.MultiChar[FourCC.SCEN]) return false;
position = (uint)fp.Position; //ftell(fp);
/* Find the 'INFO' chunk, as it contains the savegame version */
version = 0;
length = Load_FindChunk(br, (uint)SharpDune.MultiChar[FourCC.INFO]);
if (length == 0) return false;
/* Read the savegame version */
if (!FRead_LE_UInt16(ref version, fp)) return false;
length -= 2;
if (version == 0) return false;
if (version != 0x0290)
{
/* Get the scenarioID / campaignID */
if (!Info_LoadOld(br)) return false;
g_gameMode = GameMode.GM_RESTART;
/* Find the 'PLYR' chunk */
fp.Seek(position, SeekOrigin.Begin); //fseek(fp, position, SEEK_SET);
length = Load_FindChunk(br, (uint)SharpDune.MultiChar[FourCC.PLYR]);
if (length == 0) return false;
/* Find the human player */
if (!House_LoadOld(br, length)) return false;
GUI_DisplayModalMessage(String_Get_ByIndex(Text.STR_WARNING_ORIGINAL_SAVED_GAMES_ARE_INCOMPATABLE_WITH_THE_NEW_VERSION_THE_BATTLE_WILL_BE_RESTARTED), 0xFFFF);
return true;
}
/* Load the 'INFO' chunk'. It has to be the first chunk loaded */
if (!Info_Load(br, length)) return false;
/* Rewind, and read other chunks */
fp.Seek(position, SeekOrigin.Begin); //fseek(fp, position, SEEK_SET);
while (true) //(fread(&header, sizeof(uint32), 1, fp) == 1)
{
header = br.ReadUInt32();
length = br.ReadUInt32(); //(fread(&length, sizeof(uint32), 1, fp) != 1)
length = BEToH32(length);
var headerValue = BEToH32(header);
if (headerValue == SharpDune.MultiChar[FourCC.NAME])
{
/* 'NAME' chunk is of no interest to us */
}
else if (headerValue == SharpDune.MultiChar[FourCC.INFO])
{
/* 'INFO' chunk is already read */
}
else if (headerValue == SharpDune.MultiChar[FourCC.MAP])
{
if (!Map_Load(fp, length)) return false;
}
else if (headerValue == SharpDune.MultiChar[FourCC.PLYR])
{
if (!House_Load(br, length)) return false;
}
else if (headerValue == SharpDune.MultiChar[FourCC.UNIT])
{
if (!Unit_Load(br, length)) return false;
}
else if (headerValue == SharpDune.MultiChar[FourCC.BLDG])
{
if (!Structure_Load(br, length)) return false;
}
else if (headerValue == SharpDune.MultiChar[FourCC.TEAM])
{
if (!Team_Load(br, length)) return false;
}
else if (headerValue == SharpDune.MultiChar[FourCC.ODUN])
{
if (!UnitNew_Load(br, length)) return false;
}
else
{
Trace.WriteLine($"ERROR: Unknown chunk in savegame: {header}{header >> 8}{header >> 16}{header >> 24} (length: {length}). Skipped.");
}
/* Savegames are word aligned */
position += length + 8 + (length & 1);
fp.Seek(position, SeekOrigin.Begin); //fseek(fp, position, SEEK_SET);
}
}
catch (EndOfStreamException)
{
return true;
}
catch (IOException e)
{
Trace.WriteLine($"ERROR: {e.Message}");
return false;
}
}
static uint Load_FindChunk(BinaryReader br, uint chunk)
{
uint header;
uint length;
try
{
while (true) //(fread(&header, sizeof(uint32), 1, fp) == 1)
{
header = br.ReadUInt32();
length = br.ReadUInt32(); //(fread(&length, sizeof(uint32), 1, fp) != 1)
length = BEToH32(length);
if (BEToH32(header) != chunk)
{
br.BaseStream.Seek(length + (length & 1), SeekOrigin.Current); //fseek(fp, length + (length & 1), SEEK_CUR);
continue;
}
return length;
}
}
catch (IOException)
{
return 0;
}
}
}