Script: Move old-style/lenient text param encode to separate function

This commit is contained in:
Jonathan G Rennison
2024-01-25 17:30:17 +00:00
parent 822ecae85d
commit 78e08fdd18
2 changed files with 109 additions and 94 deletions

View File

@@ -182,16 +182,8 @@ void ScriptText::_TextParamError(std::string msg)
} }
} }
void ScriptText::_GetEncodedText(std::back_insert_iterator<std::string> &output, int &param_count, StringIDList &seen_ids) void ScriptText::_GetEncodedTextParamsTraditional(std::back_insert_iterator<std::string> &output, int &param_count, StringIDList &seen_ids, const std::string &name)
{ {
const std::string &name = GetGameStringName(this->string);
if (std::find(seen_ids.begin(), seen_ids.end(), this->string) != seen_ids.end()) throw Script_FatalError(fmt::format("{}: Circular reference detected", name));
seen_ids.push_back(this->string);
Utf8Encode(output, SCC_ENCODED);
fmt::format_to(output, "{:X}", this->string);
auto write_param_fallback = [&](int idx) { auto write_param_fallback = [&](int idx) {
if (std::holds_alternative<ScriptTextRef>(this->param[idx])) { if (std::holds_alternative<ScriptTextRef>(this->param[idx])) {
int count = 1; // 1 because the string id is included in consumed parameters int count = 1; // 1 because the string id is included in consumed parameters
@@ -210,9 +202,6 @@ void ScriptText::_GetEncodedText(std::back_insert_iterator<std::string> &output,
const StringParams &params = GetGameStringParams(this->string); const StringParams &params = GetGameStringParams(this->string);
int cur_idx = 0; int cur_idx = 0;
int prev_string = -1;
int prev_idx = -1;
int prev_count = -1;
for (const StringParam &cur_param : params) { for (const StringParam &cur_param : params) {
if (cur_idx >= this->paramc) { if (cur_idx >= this->paramc) {
@@ -220,7 +209,6 @@ void ScriptText::_GetEncodedText(std::back_insert_iterator<std::string> &output,
break; break;
} }
if (this->GetActiveInstance()->IsTextParamMismatchAllowed()) {
switch (cur_param.type) { switch (cur_param.type) {
case StringParam::RAW_STRING: case StringParam::RAW_STRING:
if (!std::holds_alternative<std::string>(this->param[cur_idx])) { if (!std::holds_alternative<std::string>(this->param[cur_idx])) {
@@ -263,7 +251,38 @@ void ScriptText::_GetEncodedText(std::back_insert_iterator<std::string> &output,
} }
break; break;
} }
} else { }
for (int i = cur_idx; i < this->paramc; i++) {
write_param_fallback(i);
}
}
void ScriptText::_GetEncodedText(std::back_insert_iterator<std::string> &output, int &param_count, StringIDList &seen_ids)
{
const std::string &name = GetGameStringName(this->string);
if (std::find(seen_ids.begin(), seen_ids.end(), this->string) != seen_ids.end()) throw Script_FatalError(fmt::format("{}: Circular reference detected", name));
seen_ids.push_back(this->string);
Utf8Encode(output, SCC_ENCODED);
fmt::format_to(output, "{:X}", this->string);
if (this->GetActiveInstance()->IsTextParamMismatchAllowed()) {
this->_GetEncodedTextParamsTraditional(output, param_count, seen_ids, name);
seen_ids.pop_back();
return;
}
const StringParams &params = GetGameStringParams(this->string);
int cur_idx = 0;
int prev_string = -1;
int prev_idx = -1;
int prev_count = -1;
for (const StringParam &cur_param : params) {
if (cur_idx >= this->paramc) throw Script_FatalError(fmt::format("{}: Not enough parameters", name));
if (prev_string != -1) { if (prev_string != -1) {
/* The previous substring added more parameters than expected, means we will consume them but can't properly validate them. */ /* The previous substring added more parameters than expected, means we will consume them but can't properly validate them. */
for (int i = 0; i < cur_param.consumes; i++) { for (int i = 0; i < cur_param.consumes; i++) {
@@ -314,13 +333,8 @@ void ScriptText::_GetEncodedText(std::back_insert_iterator<std::string> &output,
} }
} }
} }
}
}
if (this->GetActiveInstance()->IsTextParamMismatchAllowed()) { param_count += cur_param.consumes;
for (int i = cur_idx; i < this->paramc; i++) {
write_param_fallback(i);
}
} }
seen_ids.pop_back(); seen_ids.pop_back();

View File

@@ -136,6 +136,7 @@ private:
int paramc; int paramc;
void _TextParamError(std::string msg); void _TextParamError(std::string msg);
void _GetEncodedTextParamsTraditional(std::back_insert_iterator<std::string> &output, int &param_count, StringIDList &seen_ids, const std::string &name);
/** /**
* Internal function for recursive calling this function over multiple * Internal function for recursive calling this function over multiple