Use StringBuilder for GetString/GetStringWithArgs, as per upstream
Update dependent code as required
This commit is contained in:
@@ -136,7 +136,7 @@ SQInteger ScriptText::AddParam(HSQUIRRELVM vm)
|
||||
|
||||
SQInteger ScriptText::_set(HSQUIRRELVM vm)
|
||||
{
|
||||
int32 k;
|
||||
int32_t k;
|
||||
|
||||
if (sq_gettype(vm, 2) == OT_STRING) {
|
||||
const SQChar *key_string;
|
||||
@@ -149,7 +149,7 @@ SQInteger ScriptText::_set(HSQUIRRELVM vm)
|
||||
} else if (sq_gettype(vm, 2) == OT_INTEGER) {
|
||||
SQInteger key;
|
||||
sq_getinteger(vm, 2, &key);
|
||||
k = (int32)key;
|
||||
k = (int32_t)key;
|
||||
} else {
|
||||
return SQ_ERROR;
|
||||
}
|
||||
@@ -161,15 +161,16 @@ SQInteger ScriptText::_set(HSQUIRRELVM vm)
|
||||
return this->_SetParam(k, vm);
|
||||
}
|
||||
|
||||
const std::string ScriptText::GetEncodedText()
|
||||
std::string ScriptText::GetEncodedText()
|
||||
{
|
||||
static char buf[1024];
|
||||
static StringIDList seen_ids;
|
||||
int param_count = 0;
|
||||
seen_ids.clear();
|
||||
this->_GetEncodedText(buf, lastof(buf), param_count, seen_ids);
|
||||
std::string result;
|
||||
auto output = std::back_inserter(result);
|
||||
this->_GetEncodedText(output, param_count, seen_ids);
|
||||
if (param_count > SCRIPT_TEXT_MAX_PARAMETERS) throw Script_FatalError(fmt::format("{}: Too many parameters", GetGameStringName(this->string)));
|
||||
return buf;
|
||||
return result;
|
||||
}
|
||||
|
||||
void ScriptText::_TextParamError(std::string msg)
|
||||
@@ -181,28 +182,28 @@ void ScriptText::_TextParamError(std::string msg)
|
||||
}
|
||||
}
|
||||
|
||||
char *ScriptText::_GetEncodedText(char *p, char *lastofp, int ¶m_count, StringIDList &seen_ids)
|
||||
void ScriptText::_GetEncodedText(std::back_insert_iterator<std::string> &output, int ¶m_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);
|
||||
|
||||
p += Utf8Encode(p, SCC_ENCODED);
|
||||
p += seprintf(p, lastofp, "%X", this->string);
|
||||
Utf8Encode(output, SCC_ENCODED);
|
||||
fmt::format_to(output, "{:X}", this->string);
|
||||
|
||||
auto write_param_fallback = [&](int idx) {
|
||||
if (std::holds_alternative<ScriptTextRef>(this->param[idx])) {
|
||||
int count = 1; // 1 because the string id is included in consumed parameters
|
||||
p += seprintf(p, lastofp, ":");
|
||||
p = std::get<ScriptTextRef>(this->param[idx])->_GetEncodedText(p, lastofp, count, seen_ids);
|
||||
fmt::format_to(output, ":");
|
||||
std::get<ScriptTextRef>(this->param[idx])->_GetEncodedText(output, count, seen_ids);
|
||||
param_count += count;
|
||||
} else if (std::holds_alternative<SQInteger>(this->param[idx])) {
|
||||
p += seprintf(p, lastofp, ":" OTTD_PRINTFHEX64, std::get<SQInteger>(this->param[idx]));
|
||||
fmt::format_to(output, ":{:X}", std::get<SQInteger>(this->param[idx]));
|
||||
param_count++;
|
||||
} else {
|
||||
/* Fallback value */
|
||||
p += seprintf(p, lastofp, ":0");
|
||||
fmt::format_to(output, ":0");
|
||||
param_count++;
|
||||
}
|
||||
};
|
||||
@@ -227,7 +228,7 @@ char *ScriptText::_GetEncodedText(char *p, char *lastofp, int ¶m_count, Stri
|
||||
write_param_fallback(cur_idx++);
|
||||
break;
|
||||
}
|
||||
p += seprintf(p, lastofp, ":\"%s\"", std::get<std::string>(this->param[cur_idx++]).c_str());
|
||||
fmt::format_to(output, ":\"{}\"", std::get<std::string>(this->param[cur_idx++]));
|
||||
param_count++;
|
||||
break;
|
||||
|
||||
@@ -238,8 +239,8 @@ char *ScriptText::_GetEncodedText(char *p, char *lastofp, int ¶m_count, Stri
|
||||
break;
|
||||
}
|
||||
int count = 1; // 1 because the string id is included in consumed parameters
|
||||
p += seprintf(p, lastofp, ":");
|
||||
p = std::get<ScriptTextRef>(this->param[cur_idx++])->_GetEncodedText(p, lastofp, count, seen_ids);
|
||||
fmt::format_to(output, ":");
|
||||
std::get<ScriptTextRef>(this->param[cur_idx++])->_GetEncodedText(output, count, seen_ids);
|
||||
if (count != cur_param.consumes) {
|
||||
this->_TextParamError(fmt::format("{}: Parameter {} substring consumes {}, but expected {} to be consumed", name, cur_idx, count - 1, cur_param.consumes - 1));
|
||||
}
|
||||
@@ -257,7 +258,7 @@ char *ScriptText::_GetEncodedText(char *p, char *lastofp, int ¶m_count, Stri
|
||||
write_param_fallback(cur_idx++);
|
||||
continue;
|
||||
}
|
||||
p += seprintf(p, lastofp, ":" OTTD_PRINTFHEX64, std::get<SQInteger>(this->param[cur_idx++]));
|
||||
fmt::format_to(output, ":{:X}", std::get<SQInteger>(this->param[cur_idx++]));
|
||||
param_count++;
|
||||
}
|
||||
break;
|
||||
@@ -267,12 +268,12 @@ char *ScriptText::_GetEncodedText(char *p, char *lastofp, int ¶m_count, Stri
|
||||
/* 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++) {
|
||||
if (prev_idx < prev_count) {
|
||||
ScriptLog::Warning(fmt::format("{}: Parameter {} uses parameter {} from substring {} and cannot be validated", name, param_count + i, prev_idx++, prev_string).c_str());
|
||||
ScriptLog::Warning(fmt::format("{}: Parameter {} uses parameter {} from substring {} and cannot be validated", name, param_count + i, prev_idx++, prev_string));
|
||||
} else {
|
||||
/* No more extra parameters, assume SQInteger are expected. */
|
||||
if (cur_idx >= this->paramc) throw Script_FatalError(fmt::format("{}: Not enough parameters", name));
|
||||
if (!std::holds_alternative<SQInteger>(this->param[cur_idx])) throw Script_FatalError(fmt::format("{}: Parameter {} expects an integer", name, param_count + i));
|
||||
p += seprintf(p, lastofp, ":" OTTD_PRINTFHEX64, std::get<SQInteger>(this->param[cur_idx++]));
|
||||
fmt::format_to(output, ":{:X}", std::get<SQInteger>(this->param[cur_idx++]));
|
||||
}
|
||||
}
|
||||
if (prev_idx == prev_count) {
|
||||
@@ -283,18 +284,18 @@ char *ScriptText::_GetEncodedText(char *p, char *lastofp, int ¶m_count, Stri
|
||||
switch (cur_param.type) {
|
||||
case StringParam::RAW_STRING:
|
||||
if (!std::holds_alternative<std::string>(this->param[cur_idx])) throw Script_FatalError(fmt::format("{}: Parameter {} expects a raw string", name, param_count));
|
||||
p += seprintf(p, lastofp, ":\"%s\"", std::get<std::string>(this->param[cur_idx++]).c_str());
|
||||
fmt::format_to(output, ":\"{}\"", std::get<std::string>(this->param[cur_idx++]));
|
||||
break;
|
||||
|
||||
case StringParam::STRING: {
|
||||
if (!std::holds_alternative<ScriptTextRef>(this->param[cur_idx])) throw Script_FatalError(fmt::format("{}: Parameter {} expects a substring", name, param_count));
|
||||
int count = 0;
|
||||
p += seprintf(p, lastofp, ":");
|
||||
p = std::get<ScriptTextRef>(this->param[cur_idx++])->_GetEncodedText(p, lastofp, count, seen_ids);
|
||||
fmt::format_to(output, ":");
|
||||
std::get<ScriptTextRef>(this->param[cur_idx++])->_GetEncodedText(output, count, seen_ids);
|
||||
if (++count != cur_param.consumes) {
|
||||
ScriptLog::Error(fmt::format("{}: Parameter {} substring consumes {}, but expected {} to be consumed", name, param_count, count - 1, cur_param.consumes - 1).c_str());
|
||||
ScriptLog::Error(fmt::format("{}: Parameter {} substring consumes {}, but expected {} to be consumed", name, param_count, count - 1, cur_param.consumes - 1));
|
||||
/* Fill missing params if needed. */
|
||||
for (int i = count; i < cur_param.consumes; i++) p += seprintf(p, lastofp, ":0");
|
||||
for (int i = count; i < cur_param.consumes; i++) fmt::format_to(output, ":0");
|
||||
/* Disable validation for the extra params if any. */
|
||||
if (count > cur_param.consumes) {
|
||||
prev_string = param_count;
|
||||
@@ -309,7 +310,7 @@ char *ScriptText::_GetEncodedText(char *p, char *lastofp, int ¶m_count, Stri
|
||||
if (cur_idx + cur_param.consumes > this->paramc) throw Script_FatalError(fmt::format("{}: Not enough parameters", name));
|
||||
for (int i = 0; i < cur_param.consumes; i++) {
|
||||
if (!std::holds_alternative<SQInteger>(this->param[cur_idx])) throw Script_FatalError(fmt::format("{}: Parameter {} expects an integer", name, param_count + i));
|
||||
p += seprintf(p, lastofp, ":" OTTD_PRINTFHEX64, std::get<SQInteger>(this->param[cur_idx++]));
|
||||
fmt::format_to(output, ":{:X}", std::get<SQInteger>(this->param[cur_idx++]));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -323,14 +324,10 @@ char *ScriptText::_GetEncodedText(char *p, char *lastofp, int ¶m_count, Stri
|
||||
}
|
||||
|
||||
seen_ids.pop_back();
|
||||
|
||||
return p;
|
||||
}
|
||||
|
||||
const std::string Text::GetDecodedText()
|
||||
{
|
||||
static char buf[1024];
|
||||
::SetDParamStr(0, this->GetEncodedText());
|
||||
::GetString(buf, STR_JUST_RAW_STRING, lastof(buf));
|
||||
return buf;
|
||||
return ::GetString(STR_JUST_RAW_STRING);
|
||||
}
|
||||
|
Reference in New Issue
Block a user