-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWordCache.cpp
More file actions
405 lines (359 loc) · 13.6 KB
/
WordCache.cpp
File metadata and controls
405 lines (359 loc) · 13.6 KB
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
#include "PhyreCorePCH.h"
#include "HumaNature/Doki/WordCache.h"
#include "HumaNature/String/Utf8.h"
#ifdef PHYRE_TOOL_BUILD
#include <../../rapidjson/reader.h>
#endif // PHYRE_TOOL_BUILD
namespace core {
using namespace Phyre;
PHYRE_BIND_START(WordCache)
PHYRE_ADD_MEMBER_FLAGS(DataMember, Phyre::PE_CLASS_DATA_MEMBER_READ_ONLY)
PHYRE_BIND_CLASS_DATA_MEMBER(mPhraseCount)
PHYRE_BIND_CLASS_DATA_MEMBER_PHYRE_ARRAY(mPhraseBuffer, PChar)
PHYRE_BIND_CLASS_DATA_MEMBER_PHYRE_ARRAY(mPhrasesStart, PUInt32)
PHYRE_BIND_CLASS_DATA_MEMBER_PHYRE_ARRAY(mPhrasesLength, PUInt8)
PHYRE_BIND_CLASS_DATA_MEMBER_PHYRE_ARRAY(mDekoBuffer, PUInt16)
PHYRE_BIND_CLASS_DATA_MEMBER_PHYRE_ARRAY(mDekosStart, PUInt32)
PHYRE_BIND_CLASS_DATA_MEMBER_PHYRE_ARRAY(mDekosStarred, PUInt8)
PHYRE_BIND_CLASS_DATA_MEMBER_PHYRE_ARRAY(mDekosUnstarred, PUInt8)
PHYRE_BIND_END
const U32 WordCache::kPhraseNotFound = 0xFFFFFFFF;
// implementation taken from WordCacheDataLookupImpl.as
void WordCache::findMatches(const char* originalText, std::vector<Match>& matches) const
{
matches.clear();
size_t textLen = strlen(originalText);
// make local modifiable copy to work with
char* text = new char[textLen + 1];
if (!text)
{
logError("WordCache", "findMatches failed to make local copy");
return;
}
memcpy(text, originalText, textLen + 1);
const char* textEnd = text + textLen;
// cache unicode-index at start of each unicode character
U32* textUnicodeIdx = new U32[textLen];
if (!textUnicodeIdx)
{
assertThat(textUnicodeIdx, isNotNull);
delete[] text;
return;
}
U32 unichrCount = 0;
for (const char* unichr = text; unichr < textEnd; unichr = Utf8::NextChar(unichr))
{
textUnicodeIdx[unichr - text] = unichrCount++;
}
// go through all the words in the word cache
for (U32 phraseIdx = 0; phraseIdx < mPhraseCount; ++phraseIdx)
{
const char* phrase = getPhrase(phraseIdx);
U32 phraseLen = mPhrasesLength[phraseIdx];
// for each instance of phrase in the text
const char* phraseInText = Utf8::StrStr(text, textLen, phrase, phraseLen);
while (phraseInText)
{
// look up adjacent characters
const char* nextCharAddr = phraseInText + phraseLen;
U32 nextChar = Utf8::Decode(nextCharAddr);
const char* prevCharAddr;
U32 prevChar;
if (phraseInText == text)
{
prevCharAddr = 0;
prevChar = 0;
}
else
{
prevCharAddr = Utf8::PrevChar(phraseInText);
prevChar = Utf8::Decode(prevCharAddr);
}
// accept or reject match based on further criteria:
// TODO: what about non-english languages?
bool isMatch;
// 1) Always accept phrases of single-character-punctuation.
// - ex: "!"
if (*Utf8::NextChar(phrase) == 0 &&
iswpunct((wint_t)Utf8::Decode(phrase)))
{
isMatch = true;
}
// 2) Reject phrase that occurs within a larger word.
// - ex: reject "hen" when it's part of "then"
else if (iswalpha((wint_t)prevChar) ||
iswalpha((wint_t)nextChar))
{
isMatch = false;
}
// 3) Reject contractions.
// - ex: reject "can" when it's part of "can't"
else if (prevChar == '\'' &&
prevCharAddr != text &&
iswalpha((wint_t)Utf8::Decode(Utf8::PrevChar(prevCharAddr)))) // check left
{
isMatch = false;
}
else if (nextChar == '\'' &&
nextCharAddr != textEnd &&
iswalpha((wint_t)Utf8::Decode(Utf8::NextChar(nextCharAddr)))) // check right
{
isMatch = false;
}
// 4) Otherwise, accept
else
{
isMatch = true;
}
// handle match, increment and search for next instance of phrase
if (isMatch)
{
// replace phrase in text with *'s so it isn't flagged for further matches
memset(const_cast<char*>(phraseInText), '*', phraseLen);
// find unicode index of phrase in original string
U32 phraseInTextUnicodeIdx = textUnicodeIdx[phraseInText - text];
const Match m = {phraseIdx, phraseInTextUnicodeIdx};
matches.push_back(m);
phraseInText = nextCharAddr;
}
else
{
phraseInText = Utf8::NextChar(phraseInText);
}
phraseInText = Utf8::StrStr(phraseInText, textEnd - phraseInText, phrase, phraseLen);
}
}
delete[] text;
delete[] textUnicodeIdx;
}
#ifdef PHYRE_TOOL_BUILD
typedef rapidjson::UTF8<> WordCacheEncoding;
struct WordCacheJsonHandler
{
typedef WordCacheEncoding::Ch Ch;
struct Entry
{
std::string phrase;
std::vector<int> starredDekos;
std::vector<int> unstarredDekos;
};
typedef std::vector<Entry*> EntryVec;
// For sorting Entries. Returns true if a goes before b.
// Entries are sorted from from longest to shortest phrase,
// phrases of same length are further sorted by memcmp.
static bool CompareEntryPtrs(const Entry* a, const Entry* b)
{
return WordCache::CompareStrings(a->phrase.c_str(), a->phrase.size(), b->phrase.c_str(), b->phrase.size()) < 0;
}
Entry* mCurEntry;
EntryVec mAllEntries;
enum State
{
kReadyForBigList,
kReadyForPhrase,
kReadyForDekoLists,
kReadyForStarredList,
kReadyForStarredListValues,
kReadyForUnstarredList,
kReadyForUnstarredListValues,
kReadyForDekoListsClose,
kDone,
kError,
} mState;
WordCacheJsonHandler()
{
mState = kReadyForBigList;
mCurEntry = nullptr;
mAllEntries.clear();
}
~WordCacheJsonHandler()
{
delete mCurEntry;
for (size_t i = 0; i < mAllEntries.size(); ++i)
{
delete mAllEntries[i];
}
}
void SetError(const char* msg)
{
if (mState != kError)
{
PHYRE_PRINTF("WordCache JSON Error (state %d): %s\n", mState, msg);
mState = kError;
}
}
void StartObject()
{
switch (mState)
{
case kReadyForBigList:
mState = kReadyForPhrase;
break;
default:
SetError("unexpected {");
}
}
void EndObject(rapidjson::SizeType /*memberCount*/)
{
switch (mState)
{
case kReadyForPhrase:
mState = kDone;
break;
default:
SetError("unexpected }");
}
}
void StartArray()
{
switch (mState)
{
case kReadyForDekoLists:
mState = kReadyForStarredList;
break;
case kReadyForStarredList:
mState = kReadyForStarredListValues;
break;
case kReadyForUnstarredList:
mState = kReadyForUnstarredListValues;
break;
default:
SetError("unexpected [");
}
}
void EndArray(rapidjson::SizeType /*elementCount*/)
{
switch (mState)
{
case kReadyForStarredListValues:
mState = kReadyForUnstarredList;
break;
case kReadyForUnstarredListValues:
mState = kReadyForDekoListsClose;
break;
case kReadyForDekoListsClose:
mAllEntries.push_back(mCurEntry);
mCurEntry = nullptr;
mState = kReadyForPhrase;
break;
default:
SetError("unexpected ]");
}
}
void String(const Ch* str, rapidjson::SizeType /*length*/, bool /*copy*/)
{
if (mState == kReadyForPhrase)
{
mCurEntry = new Entry();
mCurEntry->phrase = (char*)str;
mState = kReadyForDekoLists;
}
else
{
SetError("unexpected string");
}
}
void Int(int i)
{
switch (mState)
{
case kReadyForStarredListValues:
mCurEntry->starredDekos.push_back(i);
break;
case kReadyForUnstarredListValues:
mCurEntry->unstarredDekos.push_back(i);
break;
default:
SetError("unexpected int");
}
}
void Null() { PHYRE_ASSERT(false); }
void Bool(bool /*b*/) { PHYRE_ASSERT(false); }
void Uint(unsigned i) { Int((int)i); }
void Int64(int64_t i) { Int((int)i); }
void Uint64(uint64_t i) { Int((int)i); }
void Double(double d) { Int((int)d); }
};
bool WordCache::ReadJson(char* jsonStr)
{
WordCacheJsonHandler handler;
rapidjson::InsituStringStream strStream(jsonStr);
rapidjson::GenericReader<WordCacheEncoding> reader(nullptr);
reader.Parse<rapidjson::kParseInsituFlag>(strStream, handler);
if (handler.mState != WordCacheJsonHandler::kDone)
{
PHYRE_PRINTF("WordCache JSON parsing failed\n");
return false;
}
WordCacheJsonHandler::EntryVec& entries = handler.mAllEntries;
// Ensure entries are sorted the way the game expects them to be sorted
std::sort(entries.begin(), entries.end(), WordCacheJsonHandler::CompareEntryPtrs);
// set lengths
mPhraseCount = (U32)entries.size();
mPhrasesStart.resize(mPhraseCount);
mPhrasesLength.resize(mPhraseCount);
mDekosStart.resize(mPhraseCount);
mDekosStarred.resize(mPhraseCount);
mDekosUnstarred.resize(mPhraseCount);
// figure out total space needed for buffers
U32 phraseBufferLength = 0;
U32 dekoBufferLength = 0;
for (U32 i = 0; i < mPhraseCount; ++i)
{
const WordCacheJsonHandler::Entry& entry = *entries[i];
mPhrasesStart[i] = phraseBufferLength;
const char* phrase = entry.phrase.c_str();
U32 phraseLen = (U32)strlen(phrase);
if (phraseLen > 0xFF)
{
PHYRE_PRINTF("WordCache phrase is too long: \"%s\"\n", phrase);
return false;
}
mPhrasesLength[i] = (U8)phraseLen;
phraseBufferLength += phraseLen + 1; // space for '\0' after each word
if (entry.starredDekos.size() > 0xFF || entry.unstarredDekos.size() > 0xFF)
{
PHYRE_PRINTF("WordCache phrase has too many dekos: \"%s\"\n", phrase);
return false;
}
mDekosStart[i] = dekoBufferLength;
mDekosStarred[i] = (U8)entry.starredDekos.size();
mDekosUnstarred[i] = (U8)entry.unstarredDekos.size();
dekoBufferLength += mDekosStarred[i] + mDekosUnstarred[i];
}
// fill buffers
mPhraseBuffer.resize(phraseBufferLength);
mDekoBuffer.resize(dekoBufferLength);
for (U32 i = 0; i < mPhraseCount; ++i)
{
const WordCacheJsonHandler::Entry& entry = *entries[i];
const char* phrase = entry.phrase.c_str();
// copy phrase into mPhraseBuffer
memcpy(&mPhraseBuffer[mPhrasesStart[i]], phrase, mPhrasesLength[i] + 1);
// copy starred dekos into mDekoBuffer
for (U8 dekoI = 0; dekoI < entry.starredDekos.size(); ++dekoI)
{
int dekoVal = entry.starredDekos[dekoI];
if (dekoVal < 0 || dekoVal > 0xFFFF)
{
PHYRE_PRINTF("Wordcache has invalid deko value '%d' in phrase: \"%s\"\n", dekoVal, phrase);
return false;
}
mDekoBuffer[getStarredDekoIndex(i, dekoI)] = (U16)dekoVal;
}
// copy unstarred dekos into mDekoBuffer
for (U8 dekoI = 0; dekoI < entry.unstarredDekos.size(); ++dekoI)
{
int dekoVal = entry.unstarredDekos[dekoI];
if (dekoVal < 0 || dekoVal > 0xFFFF)
{
PHYRE_PRINTF("Wordcache has invalid deko value '%d' in phrase: \"%s\"\n", dekoVal, phrase);
return false;
}
mDekoBuffer[getUnstarredDekoIndex(i, dekoI)] = (U16)dekoVal;
}
}
return true;
}
#endif // PHYRE_TOOL_BUILD
}