mirror of
https://github.com/OpenTTD/OpenTTD.git
synced 2025-03-13 02:52:37 +00:00
Codechange: use std::string to build ScriptText's encoded text
This commit is contained in:
parent
e32f0aa20b
commit
ca1e34c121
@ -159,24 +159,25 @@ SQInteger ScriptText::_set(HSQUIRRELVM vm)
|
|||||||
|
|
||||||
const std::string ScriptText::GetEncodedText()
|
const std::string ScriptText::GetEncodedText()
|
||||||
{
|
{
|
||||||
static char buf[1024];
|
|
||||||
static StringIDList seen_ids;
|
static StringIDList seen_ids;
|
||||||
int param_count = 0;
|
int param_count = 0;
|
||||||
seen_ids.clear();
|
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)));
|
if (param_count > SCRIPT_TEXT_MAX_PARAMETERS) throw Script_FatalError(fmt::format("{}: Too many parameters", GetGameStringName(this->string)));
|
||||||
return buf;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
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);
|
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));
|
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);
|
seen_ids.push_back(this->string);
|
||||||
|
|
||||||
p += Utf8Encode(p, SCC_ENCODED);
|
Utf8Encode(output, SCC_ENCODED);
|
||||||
p += seprintf(p, lastofp, "%X", this->string);
|
fmt::format_to(output, "{:X}", this->string);
|
||||||
|
|
||||||
const StringParams ¶ms = GetGameStringParams(this->string);
|
const StringParams ¶ms = GetGameStringParams(this->string);
|
||||||
int cur_idx = 0;
|
int cur_idx = 0;
|
||||||
@ -196,7 +197,7 @@ char *ScriptText::_GetEncodedText(char *p, char *lastofp, int ¶m_count, Stri
|
|||||||
/* No more extra parameters, assume SQInteger are expected. */
|
/* No more extra parameters, assume SQInteger are expected. */
|
||||||
if (cur_idx >= this->paramc) throw Script_FatalError(fmt::format("{}: Not enough parameters", name));
|
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));
|
if (!std::holds_alternative<SQInteger>(this->param[cur_idx])) throw Script_FatalError(fmt::format("{}: Parameter {} expects an integer", name, param_count + i));
|
||||||
p = strecpy(p, fmt::format(":{:X}", std::get<SQInteger>(this->param[cur_idx++])).c_str(), lastofp);
|
fmt::format_to(output, ":{:X}", std::get<SQInteger>(this->param[cur_idx++]));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (prev_idx == prev_count) {
|
if (prev_idx == prev_count) {
|
||||||
@ -207,18 +208,18 @@ char *ScriptText::_GetEncodedText(char *p, char *lastofp, int ¶m_count, Stri
|
|||||||
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])) throw Script_FatalError(fmt::format("{}: Parameter {} expects a raw string", name, param_count));
|
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, ":\"%s\"", std::get<std::string>(this->param[cur_idx++]));
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case StringParam::STRING: {
|
case StringParam::STRING: {
|
||||||
if (!std::holds_alternative<ScriptTextRef>(this->param[cur_idx])) throw Script_FatalError(fmt::format("{}: Parameter {} expects a substring", name, param_count));
|
if (!std::holds_alternative<ScriptTextRef>(this->param[cur_idx])) throw Script_FatalError(fmt::format("{}: Parameter {} expects a substring", name, param_count));
|
||||||
int count = 0;
|
int count = 0;
|
||||||
p = strecpy(p, ":", lastofp);
|
fmt::format_to(output, ":");
|
||||||
p = std::get<ScriptTextRef>(this->param[cur_idx++])->_GetEncodedText(p, lastofp, count, seen_ids);
|
std::get<ScriptTextRef>(this->param[cur_idx++])->_GetEncodedText(output, count, seen_ids);
|
||||||
if (++count != cur_param.consumes) {
|
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).c_str());
|
||||||
/* Fill missing params if needed. */
|
/* 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. */
|
/* Disable validation for the extra params if any. */
|
||||||
if (count > cur_param.consumes) {
|
if (count > cur_param.consumes) {
|
||||||
prev_string = param_count;
|
prev_string = param_count;
|
||||||
@ -233,7 +234,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));
|
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++) {
|
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));
|
if (!std::holds_alternative<SQInteger>(this->param[cur_idx])) throw Script_FatalError(fmt::format("{}: Parameter {} expects an integer", name, param_count + i));
|
||||||
p = strecpy(p, fmt::format(":{:X}", std::get<SQInteger>(this->param[cur_idx++])).c_str(), lastofp);
|
fmt::format_to(output, ":{:X}", std::get<SQInteger>(this->param[cur_idx++]));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -242,8 +243,6 @@ char *ScriptText::_GetEncodedText(char *p, char *lastofp, int ¶m_count, Stri
|
|||||||
}
|
}
|
||||||
|
|
||||||
seen_ids.pop_back();
|
seen_ids.pop_back();
|
||||||
|
|
||||||
return p;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const std::string Text::GetDecodedText()
|
const std::string Text::GetDecodedText()
|
||||||
|
@ -138,13 +138,11 @@ private:
|
|||||||
/**
|
/**
|
||||||
* Internal function for recursive calling this function over multiple
|
* Internal function for recursive calling this function over multiple
|
||||||
* instances, while writing in the same buffer.
|
* instances, while writing in the same buffer.
|
||||||
* @param p The current position in the buffer.
|
* @param output The output to write the encoded text to.
|
||||||
* @param lastofp The last position valid in the buffer.
|
|
||||||
* @param param_count The number of parameters that are in the string.
|
* @param param_count The number of parameters that are in the string.
|
||||||
* @param seen_ids The list of seen StringID.
|
* @param seen_ids The list of seen StringID.
|
||||||
* @return The new current position in the buffer.
|
|
||||||
*/
|
*/
|
||||||
char *_GetEncodedText(char *p, char *lastofp, int ¶m_count, StringIDList &seen_ids);
|
void _GetEncodedText(std::back_insert_iterator<std::string> &output, int ¶m_count, StringIDList &seen_ids);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set a parameter, where the value is the first item on the stack.
|
* Set a parameter, where the value is the first item on the stack.
|
||||||
|
Loading…
Reference in New Issue
Block a user