Codechange: [Script] Merge the 3 ScriptText param arrays

pull/491/head
glx22 1 year ago committed by Loïc Guilloux
parent af15dca316
commit a1fc4d5c0e

@ -56,8 +56,7 @@ ScriptText::ScriptText(HSQUIRRELVM vm) :
ScriptText::~ScriptText() ScriptText::~ScriptText()
{ {
for (int i = 0; i < SCRIPT_TEXT_MAX_PARAMETERS; i++) { for (int i = 0; i < SCRIPT_TEXT_MAX_PARAMETERS; i++) {
free(this->params[i]); if (std::holds_alternative<ScriptText *>(this->param[i])) std::get<ScriptText *>(this->param[i])->Release();
if (this->paramt[i] != nullptr) this->paramt[i]->Release();
} }
} }
@ -65,20 +64,14 @@ SQInteger ScriptText::_SetParam(int parameter, HSQUIRRELVM vm)
{ {
if (parameter >= SCRIPT_TEXT_MAX_PARAMETERS) return SQ_ERROR; if (parameter >= SCRIPT_TEXT_MAX_PARAMETERS) return SQ_ERROR;
free(this->params[parameter]); if (std::holds_alternative<ScriptText *>(this->param[parameter])) std::get<ScriptText *>(this->param[parameter])->Release();
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)) { switch (sq_gettype(vm, -1)) {
case OT_STRING: { case OT_STRING: {
const SQChar *value; const SQChar *value;
sq_getstring(vm, -1, &value); sq_getstring(vm, -1, &value);
this->params[parameter] = stredup(value); this->param[parameter] = StrMakeValid(value);
StrMakeValidInPlace(this->params[parameter]);
break; break;
} }
@ -86,7 +79,7 @@ SQInteger ScriptText::_SetParam(int parameter, HSQUIRRELVM vm)
SQInteger value; SQInteger value;
sq_getinteger(vm, -1, &value); sq_getinteger(vm, -1, &value);
this->parami[parameter] = value; this->param[parameter] = value;
break; break;
} }
@ -110,7 +103,7 @@ SQInteger ScriptText::_SetParam(int parameter, HSQUIRRELVM vm)
ScriptText *value = static_cast<ScriptText *>(real_instance); ScriptText *value = static_cast<ScriptText *>(real_instance);
value->AddRef(); value->AddRef();
this->paramt[parameter] = value; this->param[parameter] = value;
break; break;
} }
@ -186,17 +179,17 @@ char *ScriptText::_GetEncodedText(char *p, char *lastofp, int &param_count)
p += Utf8Encode(p, SCC_ENCODED); p += Utf8Encode(p, SCC_ENCODED);
p += seprintf(p, lastofp, "%X", this->string); p += seprintf(p, lastofp, "%X", this->string);
for (int i = 0; i < this->paramc; i++) { for (int i = 0; i < this->paramc; i++) {
if (this->params[i] != nullptr) { if (std::holds_alternative<std::string>(this->param[i])) {
p += seprintf(p, lastofp, ":\"%s\"", this->params[i]); p += seprintf(p, lastofp, ":\"%s\"", std::get<std::string>(this->param[i]).c_str());
param_count++; param_count++;
continue; continue;
} }
if (this->paramt[i] != nullptr) { if (std::holds_alternative<ScriptText *>(this->param[i])) {
p += seprintf(p, lastofp, ":"); p += seprintf(p, lastofp, ":");
p = this->paramt[i]->_GetEncodedText(p, lastofp, param_count); p = std::get<ScriptText *>(this->param[i])->_GetEncodedText(p, lastofp, param_count);
continue; continue;
} }
p += seprintf(p, lastofp,":" OTTD_PRINTFHEX64, this->parami[i]); p += seprintf(p, lastofp, ":" OTTD_PRINTFHEX64, std::get<SQInteger>(this->param[i]));
param_count++; param_count++;
} }

@ -13,6 +13,8 @@
#include "script_object.hpp" #include "script_object.hpp"
#include "../../core/alloc_type.hpp" #include "../../core/alloc_type.hpp"
#include <variant>
/** /**
* Internal parent object of all Text-like objects. * Internal parent object of all Text-like objects.
* @api -all * @api -all
@ -128,9 +130,7 @@ public:
private: private:
StringID string; StringID string;
char *params[SCRIPT_TEXT_MAX_PARAMETERS]; std::variant<SQInteger, std::string, ScriptText *> param[SCRIPT_TEXT_MAX_PARAMETERS];
int64 parami[SCRIPT_TEXT_MAX_PARAMETERS];
ScriptText *paramt[SCRIPT_TEXT_MAX_PARAMETERS];
int paramc; int paramc;
/** /**

Loading…
Cancel
Save