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 | #include "fsCRunningOrderGrammar.h"
#include <fsCore/src/debug/fsAssert.h>
// Port of gui-grammar-tool/src/running_order.rs. Kept in lockstep with that
// module and the golden corpus (tests/gui_grammar_golden_corpus/running_order);
// its standalone C++ twin tooling/running_order_resolver.h verifies the same
// contract outside the engine. The fsStr primitives used here (chunk/chomp/
// leadingWhitespaceTrim) are the same ones the GUI grammar parses with.
namespace
{
// --- small fsStr helpers -------------------------------------------------
fsStr subStr(const fsStr& pStr, fsStr::sizeT pStart, fsStr::sizeT pLen)
{
if (pStart >= pStr.dataSizeGet())
{
return fsStr();
}
const fsStr::sizeT avail = pStr.dataSizeGet() - pStart;
return fsStr(pStr.utf8Get() + pStart, pLen < avail ? pLen : avail);
}
fsStr trimmed(const fsStr& pStr)
{
fsStr out = pStr;
out.leadingWhitespaceTrim();
out.trailingWhitespaceTrim();
return out;
}
fsBool isAsciiAlpha(fsChar pC)
{
return (pC >= 'a' && pC <= 'z') || (pC >= 'A' && pC <= 'Z');
}
fsBool isAsciiDigit(fsChar pC)
{
return pC >= '0' && pC <= '9';
}
// --- model ---------------------------------------------------------------
struct Tag
{
enum Kind { eFlavour, eLanguage };
Kind mKind;<--- Member variable 'Tag::mKind' has no initializer. [+]Member variable 'Tag::mKind' has no initializer. Member variables of native types, pointers, or references are left uninitialized when the class is instantiated. That may cause bugs or undefined behavior.
fsStr mValue;
};
struct DefaultBlock
{
fsStr mPrefix; // `*` matches every scene
VarTableMap mConfig;
};
struct Entry
{
fsStr mName;
VarTableMap mConfig;
std::vector<Tag> mExclude;
};
struct RunningOrder
{
std::vector<DefaultBlock> mDefaults; // definition order
std::vector<Entry> mEntries;
};
// The scene a `map->X` entry refers to. `.gui` never authors `map->` (it's
// derived on resolve), but parity with the tooling keeps grouping identical.
fsStr sceneNameStripped(const fsStr& pName)
{
static const fsStr prefix("map->");
if (pName.find(prefix) == 0)
{
return subStr(pName, prefix.dataSizeGet(), pName.dataSizeGet());
}
return pName;
}
// The `default <group>` a scene matches, keyed off its name:
// jig_trees -> jig (text before the first '_')
// link1/link5 -> link (a <word><number> series shares a group)
// end -> end (the terminator, its own group)
// mainGatesRoadUp -> normal (hidden-object catch-all)
// link screens and `end` are deliberately NOT normal, so they neither inherit
// the hidden-object GUI nor get treated as map-first.
fsStr sceneGroup(const fsStr& pName)
{
const fsStr stripped = sceneNameStripped(pName);
const fsStr::sizeT underscore = stripped.find('_');
if (underscore != fsStr::npos)
{
// Only an alphabetic prefix is a minigame type (`jig_`, `hog_`…); a
// numeric/mixed prefix (`01_`) is a sequence number on a hidden-object
// room, so fall through to the `normal` group.
const fsStr prefix = subStr(stripped, 0, underscore);
fsBool allAlpha = !prefix.emptyGet();
for (fsStr::sizeT i = 0; i < prefix.dataSizeGet(); ++i)
{
allAlpha = allAlpha && isAsciiAlpha(prefix[i]);
}
if (allAlpha)
{
return prefix;
}
}
if (stripped == "end")
{
return "end";
}
fsStr::sizeT digitPos = fsStr::npos;
for (fsStr::sizeT i = 0; i < stripped.dataSizeGet(); ++i)
{
if (isAsciiDigit(stripped[i]))
{
digitPos = i;
break;
}
}
if (digitPos != fsStr::npos && digitPos > 0)
{
const fsStr stem = subStr(stripped, 0, digitPos);
const fsStr digits = subStr(stripped, digitPos, stripped.dataSizeGet());
fsBool ok = true;
for (fsStr::sizeT i = 0; i < stem.dataSizeGet(); ++i)
{
ok = ok && isAsciiAlpha(stem[i]);
}
for (fsStr::sizeT i = 0; i < digits.dataSizeGet(); ++i)
{
ok = ok && isAsciiDigit(digits[i]);
}
if (ok)
{
return stem;
}
}
return "normal";
}
// --- parsing -------------------------------------------------------------
void skipCommentsAndWhitespace(fsStr& pCursor)
{
pCursor.leadingWhitespaceTrim();
while (!pCursor.emptyGet() && '#' == pCursor[0])
{
pCursor.chomp("\n");
pCursor.leadingWhitespaceTrim();
}
}
// Consume a balanced `{ ... }` block and return its inner text. Precondition
// (after comment/ws skip): the cursor is on '{'. Brace-counts so nested braces
// are respected (mirrors fsIGuiComponent's include-contiguity scan).
fsStr readBlockContents(fsStr& pCursor)
{
skipCommentsAndWhitespace(pCursor);
if (pCursor.emptyGet() || '{' != pCursor[0])
{
return fsStr();
}
fsS32 depth = 1;
fsStr::sizeT i = 1;
while (depth > 0 && i < pCursor.dataSizeGet())
{
const fsChar c = pCursor[i];
if ('{' == c)
{
++depth;
}
else if ('}' == c)
{
--depth;
}
++i;
}
// inner spans [1, i-1): between the opening '{' and the matching '}'.
const fsStr body = subStr(pCursor, 1, i >= 2 ? i - 2 : 0);
pCursor.erase(0, i);
return body;
}
fsBool parseTag(const fsStr& pRaw, Tag& pOut)
{
static const fsStr flavour("flavour:");
static const fsStr lang("lang:");
if (pRaw.find(flavour) == 0)
{
pOut.mKind = Tag::eFlavour;
pOut.mValue = subStr(pRaw, flavour.dataSizeGet(), pRaw.dataSizeGet());
return true;
}
if (pRaw.find(lang) == 0)
{
pOut.mKind = Tag::eLanguage;
pOut.mValue = subStr(pRaw, lang.dataSizeGet(), pRaw.dataSizeGet());
return true;
}
fsAssert(false, "running order exclude tag has unknown namespace (expected flavour:*/lang:*): %s",
pRaw.utf8Get());
return false;
}
// Parse a `{ ... }` body into (config, exclude): `key: value` lines, `[...]`
// list literals (only `exclude` is lifted out as tags; any other list kept as
// a bracketed string), quoted or bare values, last-set-wins on dup keys.
void parseBlockBody(const fsStr& pBodyText, VarTableMap& pConfig, std::vector<Tag>& pExclude)
{
fsStr cursor = pBodyText;
for (;;)
{
skipCommentsAndWhitespace(cursor);
if (cursor.emptyGet())
{
break;
}
const fsStr key = trimmed(cursor.chomp(":"));
if (key.emptyGet())
{
break;
}
cursor.leadingWhitespaceTrim();
if (!cursor.emptyGet() && '[' == cursor[0])
{
cursor.erase(0, 1);
fsStr inner = cursor.chomp("]");
if (key == "exclude")
{
while (!inner.emptyGet())
{
const fsStr part = trimmed(inner.chomp(","));
Tag tag;
if (!part.emptyGet() && parseTag(part, tag))
{
pExclude.push_back(tag);
}
}
}
else
{
pConfig[key] = fsStr("[") + trimmed(inner) + fsStr("]");
}
}
else
{
// chunk() reads a quoted "..." verbatim (backslash paths survive)
// or a bare leaf up to whitespace.
pConfig[key] = cursor.chunk();
}
}
}
RunningOrder parseGui(const fsStr& pText)
{
fsStr cursor = pText;
RunningOrder order;
for (;;)
{
skipCommentsAndWhitespace(cursor);
if (cursor.emptyGet())
{
break;
}
const fsStr token = cursor.chunk();
if (token.emptyGet())
{
break;
}
if (token == "default")
{
DefaultBlock block;
block.mPrefix = cursor.chunk();
const fsStr body = readBlockContents(cursor);
std::vector<Tag> exclude;
parseBlockBody(body, block.mConfig, exclude);
fsAssert(exclude.empty(), "running order `default` block may not carry an exclude tag");
order.mDefaults.push_back(block);
continue;
}
// Entry. Names are plain - `map->` is derived at resolve time, never
// authored - so a bare scene name is all we read. A block is optional.
Entry entry;
entry.mName = token;
skipCommentsAndWhitespace(cursor);
if (!cursor.emptyGet() && '{' == cursor[0])
{
const fsStr body = readBlockContents(cursor);
parseBlockBody(body, entry.mConfig, entry.mExclude);
}
order.mEntries.push_back(entry);
}
return order;
}
// --- resolving -----------------------------------------------------------
fsBool isExcluded(const Entry& pEntry, const fsStr& pFlavour, const fsStr& pLanguage)
{
for (const Tag& tag : pEntry.mExclude)<--- Consider using std::any_of algorithm instead of a raw loop.
{
if (Tag::eFlavour == tag.mKind && tag.mValue == pFlavour)
{
return true;
}
if (Tag::eLanguage == tag.mKind && tag.mValue == pLanguage)
{
return true;
}
}
return false;
}
} // namespace
std::vector<fsCRunningOrderGrammar::ResolvedScene> fsCRunningOrderGrammar::resolve(const fsStr& pText,
const fsStr& pFlavour,
const fsStr& pLanguage)
{
const RunningOrder order = parseGui(pText);
std::vector<ResolvedScene> resolved;
fsBool seenFirstHog = false;
for (const Entry& entry : order.mEntries)
{
if (isExcluded(entry, pFlavour, pLanguage))
{
continue;
}
ResolvedScene scene;
scene.mName = entry.mName;
const fsStr group = sceneGroup(entry.mName);
for (const DefaultBlock& def : order.mDefaults)
{
if (def.mPrefix == "*" || def.mPrefix == group)
{
for (const auto& kv : def.mConfig)
{
scene.mConfig[kv.first] = kv.second;
}
}
}
for (const auto& kv : entry.mConfig)
{
scene.mConfig[kv.first] = kv.second;
}
// A map-routed HOG room is a hidden-object scene (declares numHogItems)
// in the `normal` group - prefix-less or sequence-numbered (`01_`). Map-
// first for every such room except the first in the sequence. Excluded:
// minigames/`barPuzzles`/link/end (no numHogItems) and alphabetically-
// prefixed direct-access HOGs like `hog_rangerBedroom` (not in `normal`).
const fsBool isHogRoom =
group == "normal" && scene.mConfig.find("numHogItems") != scene.mConfig.end();
scene.mGoesToMap = isHogRoom && seenFirstHog;
if (isHogRoom)
{
seenFirstHog = true;
}
resolved.push_back(scene);
}
return resolved;
}
|