forked from EasyRPG/Player
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame_strings.cpp
361 lines (292 loc) · 10.1 KB
/
game_strings.cpp
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
/*
* This file is part of EasyRPG Player.
*
* EasyRPG Player 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 3 of the License, or
* (at your option) any later version.
*
* EasyRPG Player 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 EasyRPG Player. If not, see <http://www.gnu.org/licenses/>.
*/
// Headers
#include <regex>
#include <lcf/encoder.h>
#include "async_handler.h"
#include "game_map.h"
#include "game_message.h"
#include "game_strings.h"
#include "game_switches.h"
#include "game_variables.h"
#include "output.h"
#include "player.h"
#include "utils.h"
void Game_Strings::WarnGet(int id) const {
Output::Debug("Invalid read strvar[{}]!", id);
--_warnings;
}
StringView Game_Strings::Asg(Str_Params params, StringView string) {
Set(params, string);
return Get(params.string_id);
}
StringView Game_Strings::Cat(Str_Params params, StringView string) {
if (params.string_id <= 0) {
return {};
}
auto it = _strings.find(params.string_id);
if (it == _strings.end()) {
Set(params, string);
return Get(params.string_id);
}
it->second += ToString(string);
return it->second;
}
int Game_Strings::ToNum(Str_Params params, int var_id, Game_Variables& variables) {
if (params.string_id <= 0) {
return -1;
}
auto it = _strings.find(params.string_id);
if (it == _strings.end()) {
return 0;
}
int num;
if (params.hex)
num = static_cast<int>(std::strtol(it->second.c_str(), nullptr, 16));
else
num = static_cast<int>(std::strtol(it->second.c_str(), nullptr, 0));
variables.Set(var_id, num);
Game_Map::SetNeedRefresh(true);
return num;
}
int Game_Strings::GetLen(Str_Params params, int var_id, Game_Variables& variables) const {
// Note: The length differs between Maniac and EasyRPG due to different internal encoding (utf-8 vs. ansi)
if (params.string_id <= 0) {
return -1;
}
int len = Get(params.string_id).length();
variables.Set(var_id, len);
Game_Map::SetNeedRefresh(true);
return len;
}
int Game_Strings::InStr(Str_Params params, std::string search, int var_id, int begin, Game_Variables& variables) const {
if (params.string_id <= 0) {
return -1;
}
if (params.extract) {
search = Extract(search, params.hex);
}
int index = Get(params.string_id).find(search, begin);
variables.Set(var_id, index);
Game_Map::SetNeedRefresh(true);
return index;
}
int Game_Strings::Split(Str_Params params, const std::string& delimiter, int string_out_id, int var_id, Game_Variables& variables) {
if (params.string_id <= 0) {
return -1;
}
int splits = 0;
std::string str = ToString(Get(params.string_id));
params.string_id = string_out_id;
if (delimiter.empty()) {
const char* iter = str.data();
const auto end = str.data() + str.size();
while (iter != end) {
const char* start_copy = iter;
auto ret = Utils::UTF8Next(iter, end);
iter = ret.next;
if (iter == end) {
break;
}
Set(params, std::string(start_copy, iter - start_copy));
params.string_id++;
splits++;
}
} else {
if (str.find(delimiter) == std::string::npos) {
// token not found -> 1 split
splits = 1;
} else {
std::string token;
for (auto index = str.find(delimiter); index != std::string::npos; index = str.find(delimiter)) {
token = str.substr(0, index);
Set(params, token);
params.string_id++;
splits++;
str.erase(0, index + delimiter.length());
}
}
}
// set the remaining string
Set(params, str);
variables.Set(var_id, splits);
return splits;
}
std::string Game_Strings::FromFile(StringView filename, int encoding, bool& do_yield) {
do_yield = false;
Filesystem_Stream::InputStream is = FileFinder::OpenText(filename);
if (!is) {
// Emscripten: Try to async fetch the file
auto* request = AsyncHandler::RequestFile("Text", filename);
request->SetImportantFile(true);
request->Start();
do_yield = !request->IsReady();
return {};
}
auto vec = Utils::ReadStream(is);
std::string file_content(vec.begin(), vec.end());
if (encoding == 0) {
lcf::Encoder enc(Player::encoding);
enc.Encode(file_content);
}
return file_content;
}
StringView Game_Strings::ToFile(Str_Params params, std::string filename, int encoding) {
std::string str = ToString(Get(params.string_id));
if (params.extract) {
filename = Extract(filename, params.hex);
}
// Maniacs forces the File in Text/ folder with .txt extension
filename = "Text/" + filename;
// EasyRPG Extension: When "*" is at the end of filename, ".txt" is not appended
if (Player::HasEasyRpgExtensions() && filename.back() == '*') {
filename.pop_back();
} else {
filename += ".txt";
}
auto txt_out = FileFinder::Save().OpenOutputStream(filename);
auto txt_dir = FileFinder::GetPathAndFilename(filename).first;
if (!txt_out) {
if (!FileFinder::Save().MakeDirectory(txt_dir, false)) {
Output::Warning("Maniac String Op ToFile failed. Cannot create directory {}", txt_dir);
return {};
}
txt_out = FileFinder::Save().OpenOutputStream(filename);
if (!txt_out) {
Output::Warning("Maniac String Op ToFile failed. Cannot write to {}", filename);
return {};
}
}
if (encoding == 0) {
lcf::Encoder enc(Player::encoding);
enc.Decode(str);
}
txt_out << str;
txt_out.Close();
AsyncHandler::SaveFilesystem();
return str;
}
StringView Game_Strings::PopLine(Str_Params params, int offset, int string_out_id) {
// FIXME: consideration needed around encoding -- what mode are files read in?
if (params.string_id <= 0) {
return {};
}
std::string result;
StringView str = Get(params.string_id);
std::stringstream ss(ToString(str));
while (offset >= 0 && Utils::ReadLine(ss, result)) { offset--; }
offset = ss.rdbuf()->in_avail();
Set(params, ss.str().substr(str.length() - offset));
params.string_id = string_out_id;
Set(params, result);
return Get(params.string_id);
}
StringView Game_Strings::ExMatch(Str_Params params, std::string expr, int var_id, int begin, int string_out_id, Game_Variables& variables) {
int var_result;
std::string str_result;
std::smatch match;
if (params.extract) {
expr = Extract(expr, params.hex);
}
std::string base = ToString(Get(params.string_id)).erase(0, begin);
std::regex r(expr);
std::regex_search(base, match, r);
var_result = match.position() + begin;
variables.Set(var_id, var_result);
Game_Map::SetNeedRefresh(true);
str_result = match.str();
if (string_out_id > 0) {
params.string_id = string_out_id;
Set(params, str_result);
}
return str_result;
}
const Game_Strings::Strings_t& Game_Strings::RangeOp(Str_Params params, int string_id_1, std::string string, int op, int args[], Game_Variables& variables) {
if (EP_UNLIKELY(ShouldWarn(params.string_id))) {
WarnGet(params.string_id);
}
if (EP_UNLIKELY(ShouldWarn(string_id_1))) {
WarnGet(string_id_1);
}
if (params.string_id <= 0 && string_id_1 <= 0) { return GetData(); }
// maniacs just ignores if only one of the params is <= 0
if (params.string_id <= 0) { params.string_id = 1; }
if (string_id_1 <= 0) { string_id_1 = 1; }
// swap so that id_0 is < id_1
if (params.string_id > string_id_1) {
std::swap(params.string_id, string_id_1);
}
for (int start = params.string_id; params.string_id <= string_id_1; params.string_id++) {
switch (op) {
case 0: Asg(params, string); break;
case 1: Cat(params, string); break;
case 2: ToNum(params, args[0] + (params.string_id - start), variables); break;
case 3: GetLen(params, args[0] + (params.string_id - start), variables); break;
case 4: InStr(params, string, args[1], args[2], variables); break;
case 5: params.string_id += Split(params, string, args[1], args[2], variables); break;
case 8: break; // range case not applicable for popLine; see case in game_interpreter.cpp
case 9: ExMatch(params, string, args[1] + (params.string_id - start), args[2], -1, variables); break;
case 10: ExMatch(params, string, args[1] + (params.string_id - start), args[2], args[3], variables); break;
}
}
return GetData();
}
std::string Game_Strings::PrependMin(StringView string, int min_size, char c) {
if (static_cast<int>(string.size()) < min_size) {
int s = min_size - string.size();
return std::string(s, c) + ToString(string);
}
return ToString(string);
}
std::string Game_Strings::Extract(StringView string, bool as_hex) {
PendingMessage::CommandInserter cmd_fn;
if (as_hex) {
cmd_fn = ManiacsCommandInserterHex;
} else {
cmd_fn = ManiacsCommandInserter;
}
return PendingMessage::ApplyTextInsertingCommands(ToString(string), Player::escape_char, cmd_fn);
}
std::optional<std::string> Game_Strings::ManiacsCommandInserter(char ch, const char** iter, const char* end, uint32_t escape_char) {
if (ch == 'S' || ch == 's') {
// \s in a normal message is the speed modifier
// parsing a switch within an extracted string var command will parse \s[N] as a switch (ON/OFF)
auto parse_ret = Game_Message::ParseSpeed(*iter, end, escape_char, true);
*iter = parse_ret.next;
int value = parse_ret.value;
return Main_Data::game_switches->Get(value) ? "ON" : "OFF";
} else if (ch == 'T' || ch == 't') {
auto parse_ret = Game_Message::ParseString(*iter, end, escape_char, true);
*iter = parse_ret.next;
int value = parse_ret.value;
// Contrary to Messages, the content of \t[]-strings is not evaluated
return ToString(Main_Data::game_strings->Get(value));
}
return Game_Message::CommandCodeInserter(ch, iter, end, escape_char);
};
std::optional<std::string> Game_Strings::ManiacsCommandInserterHex(char ch, const char** iter, const char* end, uint32_t escape_char) {
if (ch == 'V' || ch == 'v') {
auto parse_ret = Game_Message::ParseVariable(*iter, end, escape_char, true);
*iter = parse_ret.next;
int value = parse_ret.value;
int variable_value = Main_Data::game_variables->Get(value);
std::ostringstream ss;
ss << std::hex << variable_value;
return ss.str();
}
return ManiacsCommandInserter(ch, iter, end, escape_char);
};