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 |
#include "gfCCatalogueParserYaml.h"
#include <yaml-cpp/yaml.h>
#include <fsCore/fsIFile.h>
#include <fsCore/src/fsCVariableTable.h>
#include <fsCore/src/debug/fsAssert.h>
#include <fsCore/src/debug/fsLog.h>
#include "gfCCatalogueCatalogue.h"
std::map<std::string, std::string> gfCCatalogueParserYaml::flatten(const YAML::Node& node) {
std::map<std::string, std::string> result;
std::function<void(const YAML::Node&, const std::string&)> process =
[&](const YAML::Node& n, const std::string& prefix) {
if (n.IsScalar()) {
result[prefix] = n.as<std::string>();
} else if (n.IsMap()) {
for (const auto& pair : n) {
std::string key = pair.first.as<std::string>();
std::string newPrefix = prefix.empty() ? key : prefix + "." + key;
process(pair.second, newPrefix);
}
} else if (n.IsSequence()) {
for (size_t i = 0; i < n.size(); ++i) {
if (prefix.size() >= 6 && prefix.substr(prefix.size() - 6) == "params") {
// Special case: convert params array to param1, param2, etc.
std::string basePrefix = prefix.substr(0, prefix.length() - 6); // Remove "params"
std::string paramKey = basePrefix + "param" + std::to_string(i + 1);
result[paramKey] = n[i].as<std::string>();
} else {
std::string newPrefix = prefix + "[" + std::to_string(i) + "]";
process(n[i], newPrefix);
}
}
}
};
process(node, "");
return result;
}
gfCCatalogueCatalogue* const gfCCatalogueParserYaml::catalogueLoad(const fsStr& pFileName)
{
auto catalogue = new gfCCatalogueCatalogue();
auto resourceName = fsCResourceName(pFileName, fsCResourceName::eRES_LOCATION_ASSETS);
// yaml-cpp throws YAML::BadFile for a missing/unreadable file, and nothing
// here catches it - an unresolvable path would abort the whole process with
// only "bad file:" to go on. Fail loudly but survivably instead.
if (!fsIFile::fileExistsGet(resourceName))
{
fsLogError("Catalogue file '%s' not found - returning an empty catalogue.", pFileName.utf8Get());
return catalogue;
}
auto fd = fsIFile::fullPathGet(resourceName);
YAML::Node config = YAML::LoadFile(fd.utf8Get());
auto catalogueSettings = flatten(config);
auto varTable = new fsCVariableTable();
for (const auto& [key, value] : catalogueSettings) {
varTable->strSet(key.c_str(), value.c_str());
}
SSettings settings;
graphicFileBasesGet(varTable, settings);
entriesCreate(catalogue, "", varTable, settings);
delete varTable;
return catalogue;
}
void gfCCatalogueParserYaml::graphicFileBasesGet(fsCVariableTable* const pVarTable, SSettings& pSettings)
{
pSettings.mIconFileBase = pVarTable->strGetWithDefault("iconFilesBase", "");
pSettings.mImageFileBase = pVarTable->strGetWithDefault("largeImageFilesBase", "");
pSettings.mDefaultAction = pVarTable->strGetWithDefault("defaultAction", "");
pSettings.mDefaultType = pVarTable->strGetWithDefault("defaultType", "");
}
void gfCCatalogueParserYaml::entriesCreate(gfCCatalogueCatalogue* pCatalogue, const fsStr& pParentCategory,
fsCVariableTable* pVarTable, const SSettings& pSettings)
{
fsS32 index = 0;
fsStr entryType;
do
{
fsStr key = "entry" + fsStr(index) + "type";
entryType = pVarTable->strGetWithDefault(key, pSettings.mDefaultType);
if (entryType == "category") {
key = "entry" + fsStr(index) + "categoryFile";
const fsStr categoryFile = pVarTable->strGet(key);
if (categoryFile.emptyGet())
{
// Mirrors the "item" branch's itemCreate() guard below: no entry at
// this index means we've run off the end of the list. Without this
// the loop can never terminate when defaultType is "category" -
// strGetWithDefault() keeps handing back that non-empty default for
// keys that don't exist - and categoryLoad() is then handed an empty
// filename, which yaml-cpp throws YAML::BadFile on (uncaught: abort).
entryType = "";
}
else
{
categoryLoad(pCatalogue, pParentCategory, categoryFile);
}
}
else if (entryType == "item") {
if (!itemCreate(pCatalogue, pParentCategory, pVarTable, pSettings, index))
{
entryType = "";
}
}
else {
fsAssert(entryType.emptyGet(), "Unknown entry type: ", entryType.utf8Get());
}
++index;
}
while (!entryType.emptyGet());
}
void gfCCatalogueParserYaml::categoryLoad(gfCCatalogueCatalogue* pCatalogue, const fsStr& pParentCategory,
const fsStr& pFileName)
{
auto resourceName = fsCResourceName(pFileName, fsCResourceName::eRES_LOCATION_ASSETS);
// Same guard as catalogueLoad() - a bad categoryFile path would otherwise
// abort the process via an uncaught YAML::BadFile.
if (!fsIFile::fileExistsGet(resourceName))
{
fsLogError("Catalogue category file '%s' not found - skipping this category.", pFileName.utf8Get());
return;
}
auto fd = fsIFile::fullPathGet(resourceName);
YAML::Node config = YAML::LoadFile(fd.utf8Get());
auto catalogueSettings = flatten(config);
auto varTable = new fsCVariableTable();
for (const auto& [key, value] : catalogueSettings) {
varTable->strSet(key.c_str(), value.c_str());
}
const fsStr categoryName = varTable->strGet("categoryName");
SSettings settings;
graphicFileBasesGet(varTable, settings);
fsStr textKey("shopCat_");
textKey += categoryName;
fsStr iconFileName;
iconFileName = extensionAdd(settings.mIconFileBase + categoryName);
pCatalogue->categoryAdd(pParentCategory, categoryName, textKey, iconFileName);
entriesCreate(pCatalogue, categoryName, varTable, settings);
delete varTable;
}
fsStr gfCCatalogueParserYaml::extensionAdd(const fsStr& pFilename)
{
if (fsIFile::fileExistsGet(fsCResourceName(pFilename + ".png", fsCResourceName::eRES_LOCATION_ASSETS)))
{
return pFilename + ".png";
}
return pFilename + ".jpg";
}
fsBool gfCCatalogueParserYaml::itemCreate(gfCCatalogueCatalogue* pCatalogue, const fsStr& pParentCategory,<--- Parameter 'pCatalogue' can be declared as pointer to const
fsCVariableTable* pVarTable, const SSettings& pSettings, fsS32 pIndex)
{
fsStr keyBase = "entry" + fsStr(pIndex);
const fsStr name = pVarTable->strGet(keyBase + "name");
if (name.emptyGet()) {
return false;
}
std::map<fsStr, fsStr> params = {
{"itemName", name},
{"itemCost", pVarTable->strGet(keyBase + "cost")},
{"purchaseAction", pVarTable->strGetWithDefault(keyBase + "action", pSettings.mDefaultAction)},
// See gfCCatalogueParserVarTable::itemCreate() - keys are looked up case-sensitively on
// builds with fsDisableDowncase active, and strings.xml keys are authored lowercase.
{"itemNameKey", fsStr(pParentCategory + "item_" + name)},
{"descKey", fsStr(pParentCategory + "desc_" + name)},
{"iconFileName", extensionAdd(pSettings.mIconFileBase + name)},
{"imageFileName",extensionAdd(pSettings.mImageFileBase + name)},
};
for (fsS32 paramIndex = 1; ; ++paramIndex) {
const fsStr paramKey = keyBase + "param" + fsStr(paramIndex);
const fsStr paramValue = pVarTable->strGet(paramKey);
if (paramValue.emptyGet()) break; // No more parameters
params["purchaseParam" + fsStr(paramIndex)] = paramValue;
}
// Pass through any other entryN* keys as generic named params, so
// per-category data (e.g. a building's serviceTime/capacity, a staff
// member's walkSpeed) is readable via gfCCatalogueItem::paramGet()
// without needing a category-specific subclass or parser change.
for (const fsStr& key : pVarTable->partKeyCollectionGet(keyBase)) {
fsStr suffix = key;
suffix.erase(0, keyBase.dataSizeGet());
if (suffix.emptyGet() || fsStr::isDigit(suffix.utf8Get()[0])) {
// Belongs to a different, longer entry index - e.g. keyBase
// "entry1" is also a string-prefix of "entry10"/"entry15".
continue;
}
if (suffix == "name" || suffix == "cost" || suffix == "action"
|| suffix == "type" || suffix.find("param") == 0) {
continue;
}
params[suffix] = pVarTable->strGet(key);
}
pCatalogue->itemAddToCategory(pParentCategory, params);
return true;
}
gfCCatalogueParserYaml::gfCCatalogueParserYaml()
{
}
gfCCatalogueParserYaml::~gfCCatalogueParserYaml()
{
}
|