-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathGameManager.cpp
458 lines (388 loc) · 11.2 KB
/
GameManager.cpp
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
#include <SDL.h>
#include <cstring>
#include <cwchar>
#include <chrono>
#include "GameManager.h"
#include "Events/EventHandler.h"
#include "System/System.h"
#include "System/FileUtil.h"
#include "System/Logger.h"
#include "System/ModCampaign.h"
#include "Parsers/TgxFile.h"
#include "Parsers/Gm1File.h"
#include "Parsers/AniFile.h"
#include "Parsers/HlpFile.h"
#include "Parsers/TexFile.h"
#include "Audio/Audio.h"
#include "Rendering/BinkVideo.h"
#include "Rendering/Camera.h"
#include "Rendering/Font.h"
#include "GUI/MenuUtils.h"
#ifdef SOURCEHOLD_ANDROID
#include <jni.h>
#endif
using namespace Sourcehold;
using namespace Game;
using namespace Audio;
using namespace System;
using namespace Rendering;
using namespace GUI;
int _usernameIndex = -1;
GameOptions _opt;
MlbFile _mlb;
CfgFile _cfg;
TexFile _tex;
ghc::filesystem::path _cfgPath;
ghc::filesystem::path _dataFolder;
ghc::filesystem::path _saveFolder;
std::unordered_map<std::string, std::weak_ptr<TgxFile>> _tgxFiles;
std::unordered_map<std::string, std::weak_ptr<Gm1File>> _gm1Files;
std::unordered_map<std::string, std::weak_ptr<AniFile>> _aniFiles;
std::unordered_map<std::string, std::weak_ptr<BinkVideo>> _bikFiles;
StrongholdEdition _edition;
Resolution _resolution;
bool _running = false;
bool IsAssetCached(ghc::filesystem::path path) {
// todo
return false;
}
void DetectEdition() {
/**
* TODO:
* - Maybe give the user the option to manually switch editions
* - How do you detect Collection vs Classic?
*/
if (DoesFileExist(_dataFolder / "gfx/SH1_Back.tgx")) {
_edition = STRONGHOLD_HD;
Logger::message(GAME) << "Detected edition: Stronghold HD" << std::endl;
}
else {
_edition = STRONGHOLD_CLASSIC;
Logger::message(GAME) << "Detected edition: Stronghold (2001)" << std::endl;
}
}
void DetectUsername() {
if (_cfg.username.empty()) {
return;
}
std::vector<std::wstring> words;
// boost::algorithm::split(words, _cfg.username, boost::is_any_of("\t "),
// boost::token_compress_on);
if (words.empty())
return;
int gender = 0;
/* Check if 'Lord', 'Lady' or nothing */
std::wstring username = _cfg.username;
;
if (words.size() >= 2) {
if (words[0] == L"Lord")
gender = 1;
else if (words[0] == L"Lady")
gender = 2;
if (gender) {
username.clear();
for (auto it = words.begin() + 1; it != words.end(); ++it) {
username += *it + (it == words.end() - 1 ? L"" : L" ");
}
}
}
/* Look-up the name and store the index */
/*
for(size_t i = 0; i < lut_names.size(); i++) {
const wchar_t *name = lut_names[i];
if(!wcscmp(name, boost::to_upper_copy<std::wstring>(username).c_str())) {
if((gender == 1 && i < NAME_INDEX_MALE) || (gender == 2 && i >=
NAME_INDEX_MALE)) { return;
}
_usernameIndex = i;
return;
}
}
*/
return;
}
void UpdateGame() {
if (!IsDisplayOpen() || !FetchEvents())
_running = false;
UpdateRenderer();
}
AssetType ExtToType(const std::string &ext) {
AssetType type = UNKNOWN;
if (ext == ".bik") {
type = BIK;
}
else if (ext == ".wav") {
type = WAV;
}
else if (ext == ".raw") {
type = RAW;
}
else if (ext == ".tgx") {
type = TGX;
}
else if (ext == ".gm1") {
type = GM1;
}
else if (ext == ".ani") {
type = ANI;
}
else if (ext == ".map") {
type = MAP;
}
else if (ext == ".mlb") {
type = MLB;
}
else if (ext == ".act") {
type = ACT;
}
else if (ext == ".bmp") {
type = BMP;
}
else if (ext == ".txt") {
type = TXT;
}
return type;
}
bool Game::InitManager(GameOptions &opt, Resolution res) {
#ifdef SOURCEHOLD_ANDROID
// Get JNI env from SDL
JNIEnv *env = (JNIEnv *)SDL_AndroidGetJNIEnv();
jobject activity = (jobject)SDL_AndroidGetActivity();
jclass clazz(env->GetObjectClass(activity));
// Request permissions to read/write
jmethodID method_id = env->GetMethodID(clazz, "permissionCheck", "()V");
env->CallVoidMethod(activity, method_id);
// Get SD card mounting point
method_id = env->GetMethodID(clazz, "getSDCard", "()Ljava/lang/String;");
jstring path =
reinterpret_cast<jstring>(env->CallObjectMethod(activity, method_id));
opt.dataDir = ghc::filesystem::path(env->GetStringUTFChars(path, nullptr)) /
"Stronghold/";
// Cleanup
env->DeleteLocalRef(activity);
env->DeleteLocalRef(clazz);
#endif
_opt = opt;
_resolution = res;
InitDisplay("Sourcehold version " SOURCEHOLD_VERSION_STRING
" - " SOURCEHOLD_BUILD,
opt.width, opt.height, _opt.ndisp, _opt.fullscreen, _opt.noborder,
_opt.nograb, res == RESOLUTION_DYNAMIC);
InitRenderer();
InitOpenAL();
if (_opt.nosound) {
MuteOpenAL();
}
_running = true;
return true;
}
void Game::DestroyManager() {
DestroyOpenAL();
DestroyDisplay();
DestroyRenderer();
}
bool Game::Running() {
UpdateGame();
return _running;
}
void Game::SetDataDirectory(ghc::filesystem::path dir) {
_dataFolder = dir;
}
void Game::SetSaveDirectory(ghc::filesystem::path dir) {
_saveFolder = dir;
}
bool Game::LoadGameData() {
/* Detect game directories */
_dataFolder = _opt.dataDir;
_saveFolder = GetDocumentsPath();
if (_saveFolder.empty()) {
/* Attempt to fall back to local dir */
ghc::filesystem::path np = "../saves/";
if (DoesFileExist(np)) {
_saveFolder = np;
}
else {
Logger::error(GAME) << "Location for save files could not be determined!"
<< std::endl;
return false;
}
}
else
_saveFolder /= "Stronghold/Saves/";
/* Create asset folders. Does nothing if it already exists. */
CreateFolder(_saveFolder);
CreateFolder(_dataFolder / "campaigns/");
/* Create default config */
_cfgPath = _saveFolder / "../stronghold.cfg";
if (!DoesFileExist(_cfgPath)) {
Logger::message(GAME) << "Cfg file not found, creating one!" << std::endl;
_cfg.SetDefaultValues();
_cfg.WriteToDisk(_saveFolder / "../stronghold.cfg");
}
#ifdef SCRIPT_SUPPORT
LoadModCampaigns(_dataFolder / "campaigns/");
#endif
/* Load special files */
if (!_mlb.LoadFromDisk(_dataFolder / "stronghold.mlb") ||
!_tex.LoadFromDisk(_dataFolder / "sh.tex") ||
!_cfg.LoadFromDisk(_cfgPath) || !LoadStrongholdHlp()) {
return false;
}
if (!_cfg.soundEnabled) {
MuteOpenAL();
}
DetectEdition();
DetectUsername();
InitMenuUtils();
LoadFonts();
return true;
}
void Game::ClearFileCache() {
_tgxFiles.clear();
_gm1Files.clear();
_aniFiles.clear();
_bikFiles.clear();
}
void Game::Cache(ghc::filesystem::path filename) {
std::string ext = GetFileExtension(filename);
if (filename.empty() || ext.empty()) {
Logger::warning(GAME) << "Unable to cache asset: '" << filename << "'"
<< std::endl;
return;
}
std::string name = GetFilename(filename);
switch (ExtToType(ext)) {
case TGX: {
_tgxFiles.emplace(name, std::make_shared<TgxFile>(filename));
} break;
case GM1: {
_gm1Files.emplace(name, std::make_shared<Gm1File>(filename));
} break;
case ANI: {
_aniFiles.emplace(name, std::make_shared<AniFile>(filename));
} break;
case BIK: {
_bikFiles.emplace(name, std::make_shared<BinkVideo>(filename));
} break;
case UNKNOWN: {
Logger::warning(GAME)
<< "Unknown asset type cached: '" << name << "'" << std::endl;
} break;
default:
break;
}
}
void Game::DeleteCacheEntry(ghc::filesystem::path filename) {
AssetType t = ExtToType(GetFileExtension(filename));
switch (t) {
case TGX: {
auto iter = _tgxFiles.find(filename.string());
if (iter != _tgxFiles.end())
_tgxFiles.erase(iter);
} break;
case GM1: {
auto iter = _gm1Files.find(filename.string());
if (iter != _gm1Files.end())
_gm1Files.erase(iter);
} break;
case ANI: {
auto iter = _aniFiles.find(filename.string());
if (iter != _aniFiles.end())
_aniFiles.erase(iter);
} break;
case BIK: {
auto iter = _bikFiles.find(filename.string());
if (iter != _bikFiles.end())
_bikFiles.erase(iter);
} break;
default:
break;
}
}
void Game::SaveConfig() {
_cfg.WriteToDisk(_cfgPath);
}
void Game::ExitGame() {
DestroyManager();
_running = false;
}
std::wstring Game::GetDescription(MissionDescription index) {
std::wstring &str = _mlb.GetString(index);
return str;
}
std::wstring Game::GetString(TextSection sec, uint16_t index) {
std::wstring str = _tex.GetString(sec, index);
return str;
}
double Game::GetTime() {
return SDL_GetTicks() / 1000.0f;
}
ghc::filesystem::path Game::GetDirectory() {
return _dataFolder;
}
StrongholdEdition Game::GetEdition() {
return _edition;
}
Resolution Game::GetResolution() {
return _resolution;
}
int Game::GetUsernameIndex() {
return _usernameIndex;
}
CfgFile &Game::GetCfg() {
return _cfg;
}
std::shared_ptr<TgxFile> Game::GetTgx(ghc::filesystem::path filename) {
auto sp = _tgxFiles[filename.string()].lock();
if (!sp)
_tgxFiles[filename.string()] = sp =
std::make_shared<TgxFile>(_dataFolder / filename);
return sp;
}
std::shared_ptr<Gm1File> Game::GetGm1(ghc::filesystem::path filename) {
#if 0
/** TODO
* Certain (large) gm1 files will be cached. The next time this function is called
* and has to load the gm1, the cache will be used instead of parsing the file.
* To save disk space, this behaviour can be disabled via '--nocache'.
*/
const static std::vector<std::string> large_files = { // todo
"tile_workshops.gm1",
"tile_castle.gm1",
"body_catapult.gm1",
"mapedit_buttons.gm1",
"icons_front_end.gm1",
"icons_front_end_builder.gm1",
"icons_front_end_combat.gm1",
"body_trebutchet.gm1",
"tile_buildings2.gm1",
"interface_icons2.gm1",
"tree_oak.gm1",
"face800 - blank.gm1",
"tile_goods.gm1",
"body_farmer.gm1"
};
bool cache = !_opt.nocache && std::find(large_files.begin(), large_files.end(), filename.string()) == large_files.end();
if (cache && IsAssetCached(filename)) {
}
#endif
auto sp = _gm1Files[filename.string()].lock();
if (!sp)
_gm1Files[filename.string()] = sp =
std::make_shared<Gm1File>(_dataFolder / filename);
return sp;
}
std::shared_ptr<AniFile> Game::GetAni(ghc::filesystem::path filename) {
auto sp = _aniFiles[filename.string()].lock();
if (!sp)
_aniFiles[filename.string()] = sp =
std::make_shared<AniFile>(_dataFolder / filename);
return sp;
}
std::shared_ptr<BinkVideo> Game::GetBik(ghc::filesystem::path filename) {
auto sp = _bikFiles[filename.string()].lock();
if (!sp)
_bikFiles[filename.string()] = sp =
std::make_shared<BinkVideo>(_dataFolder / filename);
return sp;
}