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
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
|
/*
* The ManaPlus Client
* Copyright (C) 2004-2009 The Mana World Development Team
* Copyright (C) 2009-2010 The Mana Developers
* Copyright (C) 2011-2016 The ManaPlus Developers
*
* This file is part of The ManaPlus Client.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "client.h"
#include "configmanager.h"
#include "dirs.h"
#include "eventsmanager.h"
#include "graphicsmanager.h"
#include "main.h"
#include "settings.h"
#include "soundmanager.h"
#include "input/inputmanager.h"
#include "input/joystick.h"
#include "input/keyboardconfig.h"
#include "input/touch/touchmanager.h"
#include "gui/dialogsmanager.h"
#include "gui/gui.h"
#include "gui/skin.h"
#include "gui/popupmanager.h"
#include "gui/windowmanager.h"
#include "gui/widgets/button.h"
#include "gui/widgets/createwidget.h"
#include "gui/widgets/desktop.h"
#include "gui/widgets/windowcontainer.h"
#include "gui/widgets/window.h"
#include "resources/imagehelper.h"
#include "resources/resourcemanager/resourcemanager.h"
#include "utils/cpu.h"
#include "utils/delete2.h"
#include "utils/env.h"
#include "utils/fuzzer.h"
#include "utils/gettext.h"
#include "utils/gettexthelper.h"
#include "utils/mrand.h"
#ifdef ANDROID
#include "utils/paths.h"
#endif // ANDROID
#include "utils/physfstools.h"
#include "utils/sdlcheckutils.h"
#include "utils/timer.h"
#include "utils/translation/translationmanager.h"
#include "configuration.h"
#include "net/ipc.h"
#ifdef WIN32
#include <SDL_syswm.h>
#include "utils/specialfolder.h"
#undef ERROR
#endif // WIN32
#ifdef ANDROID
#ifndef USE_SDL2
#include <SDL_screenkeyboard.h>
#include <fstream>
#endif // USE_SDL2
#endif // ANDROID
#ifdef USE_SDL2
#include <SDL2_framerate.h>
#else // USE_SDL2
#include <SDL_framerate.h>
#endif // USE_SDL2
#include "debug.h"
std::string errorMessage;
Client *client = nullptr;
extern FPSmanager fpsManager;
volatile bool runCounters;
bool isSafeMode = false;
int serverVersion = 0;
int packetVersion = 0;
unsigned int tmwServerVersion = 0;
time_t start_time;
unsigned int mLastHost = 0;
unsigned long mSearchHash = 0;
int textures_count = 0;
Client::Client() :
ActionListener(),
mCurrentDialog(nullptr),
mSetupButton(nullptr),
mVideoButton(nullptr),
mHelpButton(nullptr),
mAboutButton(nullptr),
mThemesButton(nullptr),
mPerfomanceButton(nullptr),
#ifdef ANDROID
mCloseButton(nullptr),
#endif // ANDROID
mState(State::GAME),
mOldState(State::START),
mSkin(nullptr),
mButtonPadding(1),
mButtonSpacing(3)
{
WindowManager::init();
}
void Client::testsInit()
{
}
void Client::testsClear()
{
}
// +++ need remove duplicate from here
void Client::gameInit()
{
logger = new Logger;
initRand();
// Load branding information
if (!settings.options.brandingPath.empty())
branding.init(settings.options.brandingPath);
branding.setDefaultValues(getBrandingDefaults());
Dirs::initRootDir();
Dirs::initHomeDir();
// Configure logger
if (!settings.options.logFileName.empty())
settings.logFileName = settings.options.logFileName;
else
settings.logFileName = settings.localDataDir + "/manaplus.log";
logger->setLogFile(settings.logFileName);
#ifdef USE_FUZZER
Fuzzer::init();
#endif // USE_FUZZER
if (settings.options.test.empty())
ConfigManager::backupConfig("config.xml");
ConfigManager::initConfiguration();
paths.setDefaultValues(getPathsDefaults());
initPaths();
logger->log("init 4");
logger->setDebugLog(config.getBoolValue("debugLog"));
logger->setReportUnimplimented(config.getBoolValue("unimplimentedLog"));
config.incValue("runcount");
#ifndef ANDROID
if (settings.options.test.empty())
ConfigManager::storeSafeParameters();
#endif // ANDROID
ResourceManager::init();
if (!resourceManager->setWriteDir(settings.localDataDir))
{
logger->error(strprintf("%s couldn't be set as home directory! "
"Exiting.", settings.localDataDir.c_str()));
}
GettextHelper::initLang();
/*
chatLogger = new ChatLogger;
if (settings.options.chatLogDir.empty())
{
chatLogger->setBaseLogDir(settings.localDataDir
+ std::string("/logs/"));
}
else
{
chatLogger->setBaseLogDir(settings.options.chatLogDir);
}
*/
logger->setLogToStandardOut(config.getBoolValue("logToStandardOut"));
// Log the client version
logger->log1(FULL_VERSION);
logger->log("Start configPath: " + config.getConfigPath());
Dirs::initScreenshotDir();
// Initialize SDL
logger->log1("Initializing SDL...");
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) < 0)
{
logger->safeError(strprintf("Could not initialize SDL: %s",
SDL_GetError()));
}
atexit(SDL_Quit);
#ifndef USE_SDL2
SDL_EnableUNICODE(1);
#endif // USE_SDL2
WindowManager::applyKeyRepeat();
// disable unused SDL events
#ifndef USE_SDL2
SDL_EventState(SDL_VIDEOEXPOSE, SDL_IGNORE);
#endif // USE_SDL2
SDL_EventState(SDL_SYSWMEVENT, SDL_IGNORE);
SDL_EventState(SDL_USEREVENT, SDL_IGNORE);
#ifdef WIN32
Dirs::extractDataDir();
Dirs::mountDataDir();
#endif // WIN32
WindowManager::setIcon();
ConfigManager::checkConfigVersion();
logVars();
Cpu::detect();
#if defined(USE_OPENGL)
#if !defined(ANDROID) && !defined(__APPLE__) && !defined(__native_client__)
if (!settings.options.safeMode && settings.options.test.empty()
&& !config.getBoolValue("videodetected"))
{
graphicsManager.detectVideoSettings();
}
#endif // !defined(ANDROID) && !defined(__APPLE__) &&
// !defined(__native_client__)
#endif // defined(USE_OPENGL)
updateEnv();
initGraphics();
#ifndef WIN32
Dirs::extractDataDir();
Dirs::mountDataDir();
#endif // WIN32
Dirs::updateDataPath();
// Add the main data directories to our PhysicsFS search path
if (!settings.options.dataPath.empty())
{
resourceManager->addToSearchPath(settings.options.dataPath,
Append_false);
}
// Add the local data directory to PhysicsFS search path
resourceManager->addToSearchPath(settings.localDataDir, Append_false);
TranslationManager::loadCurrentLang();
#ifdef ENABLE_CUSTOMNLS
TranslationManager::loadGettextLang();
#endif // ENABLE_CUSTOMNLS
WindowManager::initTitle();
mainGraphics->postInit();
theme = new Theme;
Theme::selectSkin();
touchManager.init();
// Initialize the item and emote shortcuts.
// for (unsigned f = 0; f < SHORTCUT_TABS; f ++)
// itemShortcut[f] = new ItemShortcut(f);
// emoteShortcut = new EmoteShortcut;
// dropShortcut = new DropShortcut;
gui = new Gui();
gui->postInit(mainGraphics);
dialogsManager = new DialogsManager;
popupManager = new PopupManager;
initSoundManager();
eventsManager.init();
// Initialize keyboard
keyboard.init();
inputManager.init();
// Initialise player relations
Joystick::init();
WindowManager::createWindows();
keyboard.update();
if (joystick)
joystick->update();
// if (mState != State::ERROR)
// mState = State::CHOOSE_SERVER;
startTimers();
const int fpsLimit = config.getIntValue("fpslimit");
settings.limitFps = fpsLimit > 0;
SDL_initFramerate(&fpsManager);
WindowManager::setFramerate(fpsLimit);
start_time = CAST_S32(time(nullptr));
// PlayerInfo::init();
#ifdef ANDROID
#ifndef USE_SDL2
WindowManager::updateScreenKeyboard(SDL_GetScreenKeyboardHeight(nullptr));
#endif // USE_SDL2
#endif // ANDROID
mSkin = theme->load("windowmenu.xml", "");
if (mSkin)
{
mButtonPadding = mSkin->getPadding();
mButtonSpacing = mSkin->getOption("spacing", 3);
}
}
Client::~Client()
{
if (!settings.options.testMode)
gameClear();
else
testsClear();
}
void Client::initSoundManager()
{
// Initialize sound engine
try
{
if (config.getBoolValue("sound"))
soundManager.init();
soundManager.setSfxVolume(config.getIntValue("sfxVolume"));
soundManager.setMusicVolume(config.getIntValue("musicVolume"));
}
catch (const char *const err)
{
mState = State::ERROR;
errorMessage = err;
logger->log("Warning: %s", err);
}
soundManager.playMusic(branding.getValue(
"loginMusic",
"Magick - Real.ogg"),
SkipError_true);
}
void Client::initGraphics()
{
WindowManager::applyVSync();
runCounters = config.getBoolValue("packetcounters");
graphicsManager.initGraphics();
imageHelper->postInit();
getConfigDefaults2(config.getDefaultValues());
WindowManager::applyGrabMode();
WindowManager::applyGamma();
mainGraphics->beginDraw();
}
void Client::gameClear()
{
if (logger)
logger->log1("Quitting1");
eventsManager.shutdown();
WindowManager::deleteWindows();
if (windowContainer)
windowContainer->slowLogic();
stopTimers();
delete2(ipc);
if (logger)
logger->log1("Quitting2");
delete2(mCurrentDialog);
delete2(popupManager);
delete2(dialogsManager);
delete2(gui);
if (logger)
logger->log1("Quitting3");
graphicsManager.deleteRenderers();
if (logger)
logger->log1("Quitting4");
XML::cleanupXML();
if (logger)
logger->log1("Quitting5");
// Shutdown sound
soundManager.close();
if (logger)
logger->log1("Quitting6");
touchManager.clear();
ResourceManager::deleteInstance();
if (logger)
logger->log1("Quitting8");
WindowManager::deleteIcon();
if (logger)
logger->log1("Quitting9");
delete2(joystick);
keyboard.deinit();
if (logger)
logger->log1("Quitting10");
soundManager.shutdown();
touchManager.shutdown();
#ifdef DEBUG_CONFIG
config.enableKeyLogging();
#endif // DEBUG_CONFIG
config.removeOldKeys();
config.write();
serverConfig.write();
config.clear();
serverConfig.clear();
if (logger)
logger->log1("Quitting11");
#ifdef USE_PROFILER
Perfomance::clear();
#endif // USE_PROFILER
#ifdef DEBUG_OPENGL_LEAKS
if (logger)
logger->log("textures left: %d", textures_count);
#endif // DEBUG_OPENGL_LEAKS
// delete2(chatLogger);
TranslationManager::close();
}
#define ADDBUTTON(var, object) var = object; \
x -= var->getWidth() + mButtonSpacing; \
var->setPosition(x, mButtonPadding); \
top->add(var);
void Client::stateGame()
{
if (!gui)
return;
BasicContainer2 *const top = static_cast<BasicContainer2*>(
gui->getTop());
if (!top)
return;
CREATEWIDGETV(desktop, Desktop, nullptr);
top->add(desktop);
int x = top->getWidth() - mButtonPadding;
ADDBUTTON(mSetupButton, new Button(desktop,
// TRANSLATORS: setup tab quick button
_("Setup"), "Setup", this))
ADDBUTTON(mPerfomanceButton, new Button(desktop,
// TRANSLATORS: perfoamance tab quick button
_("Performance"), "Perfomance", this))
ADDBUTTON(mVideoButton, new Button(desktop,
// TRANSLATORS: video tab quick button
_("Video"), "Video", this))
ADDBUTTON(mThemesButton, new Button(desktop,
// TRANSLATORS: theme tab quick button
_("Theme"), "Themes", this))
ADDBUTTON(mAboutButton, new Button(desktop,
// TRANSLATORS: theme tab quick button
_("About"), "about", this))
ADDBUTTON(mHelpButton, new Button(desktop,
// TRANSLATORS: theme tab quick button
_("Help"), "help", this))
#ifdef ANDROID
ADDBUTTON(mCloseButton, new Button(desktop,
// TRANSLATORS: close quick button
_("Close"), "close", this))
#endif // ANDROID
desktop->setSize(mainGraphics->getWidth(),
mainGraphics->getHeight());
}
int Client::gameExec()
{
int lastTickTime = tick_time;
while (mState != State::EXIT)
{
PROFILER_START();
if (eventsManager.handleEvents())
continue;
BLOCK_START("Client::gameExec 4")
if (gui)
gui->logic();
cur_time = time(nullptr);
int k = 0;
while (lastTickTime != tick_time && k < 40)
{
// if (mGame)
// mGame->logic();
// else
if (gui)
gui->handleInput();
++lastTickTime;
k ++;
}
soundManager.logic();
logic_count += k;
if (gui)
gui->slowLogic();
// if (mGame)
// mGame->slowLogic();
slowLogic();
BLOCK_END("Client::gameExec 4")
// This is done because at some point tick_time will wrap.
lastTickTime = tick_time;
// Update the screen when application is visible, delay otherwise.
if (!WindowManager::getIsMinimized())
{
frame_count++;
if (gui)
gui->draw();
mainGraphics->updateScreen();
}
else
{
SDL_Delay(100);
}
BLOCK_START("~Client::SDL_framerateDelay")
if (settings.limitFps)
SDL_framerateDelay(&fpsManager);
BLOCK_END("~Client::SDL_framerateDelay")
BLOCK_START("Client::gameExec 6")
/*
if (mState == State::CONNECT_GAME)
{
stateConnectGame1();
}
*/
BLOCK_END("Client::gameExec 6")
if (mState != mOldState)
{
BLOCK_START("Client::gameExec 7")
mOldState = mState;
// Get rid of the dialog of the previous state
delete2(mCurrentDialog);
// State has changed, while the quitDialog was active, it might
// not be correct anymore
/*
if (mQuitDialog)
{
mQuitDialog->scheduleDelete();
mQuitDialog = nullptr;
}
*/
BLOCK_END("Client::gameExec 7")
BLOCK_START("Client::gameExec 8")
PRAGMA45(GCC diagnostic push)
PRAGMA45(GCC diagnostic ignored "-Wswitch-enum")
switch (mState)
{
case State::GAME:
stateGame();
break;
case State::LOAD_DATA:
{
BLOCK_START("Client::gameExec State::LOAD_DATA")
logger->log1("State: LOAD DATA");
// If another data path has been set,
// we don't load any other files...
if (settings.options.dataPath.empty())
{
// Add customdata directory
resourceManager->searchAndAddArchives(
"customdata/",
"zip",
Append_false);
}
if (!settings.options.skipUpdate)
{
resourceManager->searchAndAddArchives(
settings.updatesDir + "/local/",
"zip",
Append_false);
resourceManager->addToSearchPath(settings.localDataDir
+ dirSeparator + settings.updatesDir + "/local/",
Append_false);
}
logger->log("Init paths");
paths.init("paths.xml", UseResman_true);
paths.setDefaultValues(getPathsDefaults());
initPaths();
TranslationManager::loadCurrentLang();
if (desktop)
desktop->reloadWallpaper();
mState = State::GET_CHARACTERS;
BLOCK_END("Client::gameExec State::LOAD_DATA")
break;
}
case State::START:
default:
mState = State::FORCE_QUIT;
break;
}
PRAGMA45(GCC diagnostic pop)
BLOCK_END("Client::gameExec 8")
}
PROFILER_END();
}
return 0;
}
void Client::action(const ActionEvent &event)
{
std::string tab;
const std::string &eventId = event.getId();
if (eventId == "close")
{
setState(State::FORCE_QUIT);
return;
}
if (eventId == "Setup")
{
tab.clear();
}
else if (eventId == "help")
{
inputManager.executeAction(InputAction::WINDOW_HELP);
return;
}
else if (eventId == "about")
{
inputManager.executeAction(InputAction::WINDOW_ABOUT);
return;
}
else if (eventId == "Video")
{
tab = "Video";
}
else if (eventId == "Themes")
{
tab = "Theme";
}
else if (eventId == "Perfomance")
{
tab = "Perfomance";
}
else
{
return;
}
}
void Client::moveButtons(const int width)
{
if (mSetupButton)
{
int x = width - mSetupButton->getWidth() - mButtonPadding;
mSetupButton->setPosition(x, mButtonPadding);
#ifndef WIN32
x -= mPerfomanceButton->getWidth() + mButtonSpacing;
mPerfomanceButton->setPosition(x, mButtonPadding);
x -= mVideoButton->getWidth() + mButtonSpacing;
mVideoButton->setPosition(x, mButtonPadding);
x -= mThemesButton->getWidth() + mButtonSpacing;
mThemesButton->setPosition(x, mButtonPadding);
x -= mAboutButton->getWidth() + mButtonSpacing;
mAboutButton->setPosition(x, mButtonPadding);
x -= mHelpButton->getWidth() + mButtonSpacing;
mHelpButton->setPosition(x, mButtonPadding);
#ifdef ANDROID
x -= mCloseButton->getWidth() + mButtonSpacing;
mCloseButton->setPosition(x, mButtonPadding);
#endif // ANDROID
#endif // WIN32
}
}
void Client::windowRemoved(const Window *const window)
{
if (mCurrentDialog == window)
mCurrentDialog = nullptr;
}
void Client::logVars()
{
#ifdef ANDROID
logger->log("APPDIR: %s", getenv("APPDIR"));
logger->log("DATADIR2: %s", getSdStoragePath().c_str());
#endif // ANDROID
}
void Client::slowLogic()
{
}
int Client::testsExec()
{
return 0;
}
bool Client::isTmw()
{
const std::string &name = settings.serverName;
if (name == "server.themanaworld.org"
|| name == "themanaworld.org"
|| name == "167.114.185.71")
{
return true;
}
return false;
}
void Client::initPaths()
{
}
|