Update to 14.0-beta1
This commit is contained in:
@@ -10,22 +10,20 @@
|
||||
#include "../../stdafx.h"
|
||||
#include "../../string_func.h"
|
||||
#include "../../strings_func.h"
|
||||
#include "../../game/game_text.hpp"
|
||||
#include "script_text.hpp"
|
||||
#include "script_log.hpp"
|
||||
#include "../script_fatalerror.hpp"
|
||||
#include "../../table/control_codes.h"
|
||||
|
||||
#include "table/strings.h"
|
||||
|
||||
#include "../../safeguards.h"
|
||||
|
||||
RawText::RawText(const char *text) : text(stredup(text))
|
||||
RawText::RawText(const std::string &text) : text(text)
|
||||
{
|
||||
}
|
||||
|
||||
RawText::~RawText()
|
||||
{
|
||||
free(this->text);
|
||||
}
|
||||
|
||||
|
||||
ScriptText::ScriptText(HSQUIRRELVM vm) :
|
||||
ZeroedMemoryAllocator()
|
||||
@@ -57,32 +55,16 @@ ScriptText::ScriptText(HSQUIRRELVM vm) :
|
||||
}
|
||||
}
|
||||
|
||||
ScriptText::~ScriptText()
|
||||
{
|
||||
for (int i = 0; i < SCRIPT_TEXT_MAX_PARAMETERS; i++) {
|
||||
free(this->params[i]);
|
||||
if (this->paramt[i] != nullptr) this->paramt[i]->Release();
|
||||
}
|
||||
}
|
||||
|
||||
SQInteger ScriptText::_SetParam(int parameter, HSQUIRRELVM vm)
|
||||
{
|
||||
if (parameter >= SCRIPT_TEXT_MAX_PARAMETERS) return SQ_ERROR;
|
||||
|
||||
free(this->params[parameter]);
|
||||
if (this->paramt[parameter] != nullptr) this->paramt[parameter]->Release();
|
||||
|
||||
this->parami[parameter] = 0;
|
||||
this->params[parameter] = nullptr;
|
||||
this->paramt[parameter] = nullptr;
|
||||
|
||||
switch (sq_gettype(vm, -1)) {
|
||||
case OT_STRING: {
|
||||
const SQChar *value;
|
||||
sq_getstring(vm, -1, &value);
|
||||
|
||||
this->params[parameter] = stredup(value);
|
||||
StrMakeValidInPlace(this->params[parameter]);
|
||||
this->param[parameter] = StrMakeValid(value);
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -90,7 +72,7 @@ SQInteger ScriptText::_SetParam(int parameter, HSQUIRRELVM vm)
|
||||
SQInteger value;
|
||||
sq_getinteger(vm, -1, &value);
|
||||
|
||||
this->parami[parameter] = value;
|
||||
this->param[parameter] = value;
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -113,8 +95,7 @@ SQInteger ScriptText::_SetParam(int parameter, HSQUIRRELVM vm)
|
||||
if (real_instance == nullptr) return SQ_ERROR;
|
||||
|
||||
ScriptText *value = static_cast<ScriptText *>(real_instance);
|
||||
value->AddRef();
|
||||
this->paramt[parameter] = value;
|
||||
this->param[parameter] = ScriptTextRef(value);
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -152,19 +133,20 @@ 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;
|
||||
sq_getstring(vm, 2, &key_string);
|
||||
StrMakeValidInPlace(const_cast<char *>(key_string));
|
||||
|
||||
if (strncmp(key_string, "param_", 6) != 0 || strlen(key_string) > 8) return SQ_ERROR;
|
||||
k = atoi(key_string + 6);
|
||||
std::string str = StrMakeValid(key_string);
|
||||
if (!str.starts_with("param_") || str.size() > 8) return SQ_ERROR;
|
||||
|
||||
k = stoi(str.substr(6));
|
||||
} 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;
|
||||
}
|
||||
@@ -176,43 +158,95 @@ SQInteger ScriptText::_set(HSQUIRRELVM vm)
|
||||
return this->_SetParam(k, vm);
|
||||
}
|
||||
|
||||
const char *ScriptText::GetEncodedText()
|
||||
std::string ScriptText::GetEncodedText()
|
||||
{
|
||||
static char buf[1024];
|
||||
StringIDList seen_ids;
|
||||
ParamList params;
|
||||
int param_count = 0;
|
||||
this->_GetEncodedText(buf, lastof(buf), param_count);
|
||||
return (param_count > SCRIPT_TEXT_MAX_PARAMETERS) ? nullptr : buf;
|
||||
std::string result;
|
||||
auto output = std::back_inserter(result);
|
||||
this->_FillParamList(params);
|
||||
this->_GetEncodedText(output, param_count, seen_ids, params);
|
||||
if (param_count > SCRIPT_TEXT_MAX_PARAMETERS) throw Script_FatalError(fmt::format("{}: Too many parameters", GetGameStringName(this->string)));
|
||||
return result;
|
||||
}
|
||||
|
||||
char *ScriptText::_GetEncodedText(char *p, char *lastofp, int ¶m_count)
|
||||
void ScriptText::_FillParamList(ParamList ¶ms)
|
||||
{
|
||||
p += Utf8Encode(p, SCC_ENCODED);
|
||||
p += seprintf(p, lastofp, "%X", this->string);
|
||||
for (int i = 0; i < this->paramc; i++) {
|
||||
if (this->params[i] != nullptr) {
|
||||
p += seprintf(p, lastofp, ":\"%s\"", this->params[i]);
|
||||
param_count++;
|
||||
continue;
|
||||
Param *p = &this->param[i];
|
||||
params.emplace_back(this->string, i, p);
|
||||
if (!std::holds_alternative<ScriptTextRef>(*p)) continue;
|
||||
std::get<ScriptTextRef>(*p)->_FillParamList(params);
|
||||
}
|
||||
}
|
||||
|
||||
void ScriptText::_GetEncodedText(std::back_insert_iterator<std::string> &output, int ¶m_count, StringIDList &seen_ids, ParamSpan args)
|
||||
{
|
||||
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);
|
||||
|
||||
const StringParams ¶ms = GetGameStringParams(this->string);
|
||||
|
||||
size_t idx = 0;
|
||||
auto get_next_arg = [&]() {
|
||||
if (idx >= args.size()) throw Script_FatalError(fmt::format("{}({}): Not enough parameters", name, param_count + 1));
|
||||
ParamCheck &pc = args[idx++];
|
||||
if (pc.owner != this->string) ScriptLog::Warning(fmt::format("{}({}): Consumes {}({})", name, param_count + 1, GetGameStringName(pc.owner), pc.idx + 1));
|
||||
return pc.param;
|
||||
};
|
||||
auto skip_args = [&](size_t nb) { idx += nb; };
|
||||
|
||||
for (const StringParam &cur_param : params) {
|
||||
switch (cur_param.type) {
|
||||
case StringParam::UNUSED:
|
||||
skip_args(cur_param.consumes);
|
||||
break;
|
||||
|
||||
case StringParam::RAW_STRING: {
|
||||
Param *p = get_next_arg();
|
||||
if (!std::holds_alternative<std::string>(*p)) throw Script_FatalError(fmt::format("{}({}): {{{}}} expects a raw string", name, param_count + 1, cur_param.cmd));
|
||||
fmt::format_to(output, ":\"{}\"", std::get<std::string>(*p));
|
||||
break;
|
||||
}
|
||||
|
||||
case StringParam::STRING: {
|
||||
Param *p = get_next_arg();
|
||||
if (!std::holds_alternative<ScriptTextRef>(*p)) throw Script_FatalError(fmt::format("{}({}): {{{}}} expects a GSText", name, param_count + 1, cur_param.cmd));
|
||||
int count = 0;
|
||||
fmt::format_to(output, ":");
|
||||
ScriptTextRef &ref = std::get<ScriptTextRef>(*p);
|
||||
ref->_GetEncodedText(output, count, seen_ids, args.subspan(idx));
|
||||
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. */
|
||||
for (int i = count; i < cur_param.consumes; i++) fmt::format_to(output, ":0");
|
||||
}
|
||||
skip_args(cur_param.consumes - 1);
|
||||
break;
|
||||
}
|
||||
|
||||
default:
|
||||
for (int i = 0; i < cur_param.consumes; i++) {
|
||||
Param *p = get_next_arg();
|
||||
if (!std::holds_alternative<SQInteger>(*p)) throw Script_FatalError(fmt::format("{}({}): {{{}}} expects an integer", name, param_count + i + 1, cur_param.cmd));
|
||||
fmt::format_to(output, ":{:X}", std::get<SQInteger>(*p));
|
||||
}
|
||||
}
|
||||
if (this->paramt[i] != nullptr) {
|
||||
p += seprintf(p, lastofp, ":");
|
||||
p = this->paramt[i]->_GetEncodedText(p, lastofp, param_count);
|
||||
continue;
|
||||
}
|
||||
p += seprintf(p, lastofp,":" OTTD_PRINTFHEX64, this->parami[i]);
|
||||
param_count++;
|
||||
|
||||
param_count += cur_param.consumes;
|
||||
}
|
||||
|
||||
return p;
|
||||
seen_ids.pop_back();
|
||||
}
|
||||
|
||||
const char *Text::GetDecodedText()
|
||||
const std::string Text::GetDecodedText()
|
||||
{
|
||||
const char *encoded_text = this->GetEncodedText();
|
||||
if (encoded_text == nullptr) return nullptr;
|
||||
|
||||
static char buf[1024];
|
||||
::SetDParamStr(0, encoded_text);
|
||||
::GetString(buf, STR_JUST_RAW_STRING, lastof(buf));
|
||||
return buf;
|
||||
::SetDParamStr(0, this->GetEncodedText());
|
||||
return ::GetString(STR_JUST_RAW_STRING);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user