Fix 0858377: [Script] Don't output parameters more than once

pull/678/head
glx22 3 months ago committed by Loïc Guilloux
parent ce3d0097f6
commit ae27ce12a7

@ -166,7 +166,7 @@ std::string ScriptText::GetEncodedText()
std::string result;
auto output = std::back_inserter(result);
this->_FillParamList(params, seen_texts);
this->_GetEncodedText(output, param_count, params);
this->_GetEncodedText(output, param_count, params, true);
if (param_count > SCRIPT_TEXT_MAX_PARAMETERS) throw Script_FatalError(fmt::format("{}: Too many parameters", GetGameStringName(this->string)));
return result;
}
@ -191,16 +191,22 @@ void ScriptText::ParamCheck::Encode(std::back_insert_iterator<std::string> &outp
if (this->used) return;
if (std::holds_alternative<std::string>(*this->param)) fmt::format_to(output, ":\"{}\"", std::get<std::string>(*this->param));
if (std::holds_alternative<SQInteger>(*this->param)) fmt::format_to(output, ":{:X}", std::get<SQInteger>(*this->param));
if (std::holds_alternative<ScriptTextRef>(*this->param)) fmt::format_to(output, ":{:X}", this->owner);
if (std::holds_alternative<ScriptTextRef>(*this->param)) {
fmt::format_to(output, ":");
Utf8Encode(output, SCC_ENCODED);
fmt::format_to(output, "{:X}", std::get<ScriptTextRef>(*this->param)->string);
}
this->used = true;
}
void ScriptText::_GetEncodedText(std::back_insert_iterator<std::string> &output, int &param_count, ParamSpan args)
void ScriptText::_GetEncodedText(std::back_insert_iterator<std::string> &output, int &param_count, ParamSpan args, bool first)
{
const std::string &name = GetGameStringName(this->string);
Utf8Encode(output, SCC_ENCODED);
fmt::format_to(output, "{:X}", this->string);
if (first) {
Utf8Encode(output, SCC_ENCODED);
fmt::format_to(output, "{:X}", this->string);
}
const StringParams &params = GetGameStringParams(this->string);
@ -221,23 +227,21 @@ void ScriptText::_GetEncodedText(std::back_insert_iterator<std::string> &output,
case StringParam::RAW_STRING: {
ParamCheck &p = *get_next_arg();
if (!std::holds_alternative<std::string>(*p.param)) ScriptLog::Error(fmt::format("{}({}): {{{}}} expects a raw string", name, param_count + 1, cur_param.cmd));
p.Encode(output);
if (!std::holds_alternative<std::string>(*p.param)) ScriptLog::Error(fmt::format("{}({}): {{{}}} expects a raw string", name, param_count + 1, cur_param.cmd));
break;
}
case StringParam::STRING: {
ParamCheck &p = *get_next_arg();
p.Encode(output);
if (!std::holds_alternative<ScriptTextRef>(*p.param)){
ScriptLog::Error(fmt::format("{}({}): {{{}}} expects a GSText", name, param_count + 1, cur_param.cmd));
p.Encode(output);
break;
}
int count = 0;
fmt::format_to(output, ":");
ScriptTextRef &ref = std::get<ScriptTextRef>(*p.param);
ref->_GetEncodedText(output, count, args.subspan(idx));
p.used = true;
ref->_GetEncodedText(output, count, args.subspan(idx), false);
if (++count != cur_param.consumes) {
ScriptLog::Error(fmt::format("{}({}): {{{}}} expects {} to be consumed, but {} consumes {}", name, param_count + 1, cur_param.cmd, cur_param.consumes - 1, GetGameStringName(ref->string), count - 1));
/* Fill missing params if needed. */
@ -250,8 +254,8 @@ void ScriptText::_GetEncodedText(std::back_insert_iterator<std::string> &output,
default:
for (int i = 0; i < cur_param.consumes; i++) {
ParamCheck &p = *get_next_arg();
if (!std::holds_alternative<SQInteger>(*p.param)) ScriptLog::Error(fmt::format("{}({}): {{{}}} expects an integer", name, param_count + i + 1, cur_param.cmd));
p.Encode(output);
if (!std::holds_alternative<SQInteger>(*p.param)) ScriptLog::Error(fmt::format("{}({}): {{{}}} expects an integer", name, param_count + i + 1, cur_param.cmd));
}
}

@ -165,8 +165,9 @@ private:
* @param output The output to write the encoded text to.
* @param param_count The number of parameters that are consumed by the string.
* @param args The parameters to be consumed.
* @param first Whether it's the first call in the recursion.
*/
void _GetEncodedText(std::back_insert_iterator<std::string> &output, int &param_count, ParamSpan args);
void _GetEncodedText(std::back_insert_iterator<std::string> &output, int &param_count, ParamSpan args, bool first);
/**
* Set a parameter, where the value is the first item on the stack.

Loading…
Cancel
Save