1 |
|
|
/* |
2 |
|
|
* The ManaPlus Client |
3 |
|
|
* Copyright (C) 2011-2019 The ManaPlus Developers |
4 |
|
|
* Copyright (C) 2019-2021 Andrei Karas |
5 |
|
|
* |
6 |
|
|
* This file is part of The ManaPlus Client. |
7 |
|
|
* |
8 |
|
|
* This program is free software; you can redistribute it and/or modify |
9 |
|
|
* it under the terms of the GNU General Public License as published by |
10 |
|
|
* the Free Software Foundation; either version 2 of the License, or |
11 |
|
|
* any later version. |
12 |
|
|
* |
13 |
|
|
* This program is distributed in the hope that it will be useful, |
14 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 |
|
|
* GNU General Public License for more details. |
17 |
|
|
* |
18 |
|
|
* You should have received a copy of the GNU General Public License |
19 |
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
20 |
|
|
*/ |
21 |
|
|
|
22 |
|
|
#include "resources/db/deaddb.h" |
23 |
|
|
|
24 |
|
|
#include "configuration.h" |
25 |
|
|
#include "logger.h" |
26 |
|
|
|
27 |
|
|
#include "utils/translation/podict.h" |
28 |
|
|
|
29 |
|
|
#include "resources/beingcommon.h" |
30 |
|
|
|
31 |
|
|
#include "debug.h" |
32 |
|
|
|
33 |
|
|
namespace |
34 |
|
|
{ |
35 |
|
|
bool mLoaded = false; |
36 |
|
1 |
STD_VECTOR<std::string> mMessages; |
37 |
|
|
} // namespace |
38 |
|
|
|
39 |
|
|
void DeadDB::load() |
40 |
|
|
{ |
41 |
|
|
if (mLoaded) |
42 |
|
|
unload(); |
43 |
|
|
|
44 |
|
|
logger->log1("Initializing dead database..."); |
45 |
|
|
|
46 |
|
|
loadXmlFile(paths.getStringValue("deadMessagesFile"), SkipError_false); |
47 |
|
|
loadXmlFile(paths.getStringValue("deadMessagesPatchFile"), SkipError_true); |
48 |
|
|
loadXmlDir("deadMessagesPatchDir", loadXmlFile) |
49 |
|
|
mLoaded = true; |
50 |
|
|
} |
51 |
|
|
|
52 |
|
|
void DeadDB::loadXmlFile(const std::string &fileName, |
53 |
|
|
const SkipError skipError) |
54 |
|
|
{ |
55 |
|
|
XML::Document *doc = new XML::Document(fileName, |
56 |
|
|
UseVirtFs_true, |
57 |
|
|
skipError); |
58 |
|
|
XmlNodeConstPtrConst root = doc->rootNode(); |
59 |
|
|
|
60 |
|
|
if ((root == nullptr) || !xmlNameEqual(root, "messages")) |
61 |
|
|
{ |
62 |
|
|
logger->log("DeadDB: Failed to parse %s.", |
63 |
|
|
paths.getStringValue("deadMessagesFile").c_str()); |
64 |
|
|
delete doc; |
65 |
|
|
return; |
66 |
|
|
} |
67 |
|
|
|
68 |
|
|
for_each_xml_child_node(node, root) |
69 |
|
|
{ |
70 |
|
|
if (xmlNameEqual(node, "include")) |
71 |
|
|
{ |
72 |
|
|
const std::string name = XML::getProperty(node, "name", ""); |
73 |
|
|
if (!name.empty()) |
74 |
|
|
loadXmlFile(name, skipError); |
75 |
|
|
continue; |
76 |
|
|
} |
77 |
|
|
else if (xmlNameEqual(node, "message")) |
78 |
|
|
{ |
79 |
|
|
XmlChar *const data = reinterpret_cast<XmlChar*>( |
80 |
|
|
XmlNodeGetContent(node)); |
81 |
|
|
if (data == nullptr) |
82 |
|
|
continue; |
83 |
|
|
if (*data == 0) |
84 |
|
|
{ |
85 |
|
|
XmlFree(data); |
86 |
|
|
continue; |
87 |
|
|
} |
88 |
|
|
mMessages.push_back(data); |
89 |
|
|
XmlFree(data); |
90 |
|
|
} |
91 |
|
|
} |
92 |
|
|
|
93 |
|
|
delete doc; |
94 |
|
|
} |
95 |
|
|
|
96 |
|
215 |
void DeadDB::unload() |
97 |
|
|
{ |
98 |
|
215 |
logger->log1("Unloading dead database..."); |
99 |
|
215 |
mMessages.clear(); |
100 |
|
215 |
mLoaded = false; |
101 |
|
215 |
} |
102 |
|
|
|
103 |
|
|
std::string DeadDB::getRandomString() |
104 |
|
|
{ |
105 |
|
|
const size_t sz = mMessages.size(); |
106 |
|
|
if (sz == 0U) |
107 |
|
|
return std::string(); |
108 |
|
|
return translator->getStr(mMessages[rand() % sz]); |
109 |
|
2 |
} |