Codechange: separate integer and string usage in StringParameters

This commit is contained in:
Rubidium 2023-06-22 18:47:32 +02:00 committed by rubidium42
parent 8b7c34d7d4
commit 4654b2b0aa
2 changed files with 41 additions and 14 deletions

View File

@ -178,7 +178,12 @@ void CopyOutDParam(std::vector<StringParameterBackup> &backup, size_t num)
{
backup.resize(num);
for (size_t i = 0; i < backup.size(); i++) {
backup[i] = _global_string_params.GetParam(i);
const char *str = _global_string_params.GetParamStr(i);
if (str != nullptr) {
backup[i] = str;
} else {
backup[i] = _global_string_params.GetParam(i);
}
}
}
@ -190,17 +195,7 @@ void CopyOutDParam(std::vector<StringParameterBackup> &backup, size_t num)
*/
void CopyOutDParam(std::vector<StringParameterBackup> &backup, size_t num, StringID string)
{
/* Just get the string to extract the type information. */
GetString(string);
backup.resize(num);
for (size_t i = 0; i < backup.size(); i++) {
if (_global_string_params.GetTypeAtOffset(i) == SCC_RAW_STRING_POINTER) {
backup[i] = (const char *)(size_t)_global_string_params.GetParam(i);
} else {
backup[i] = _global_string_params.GetParam(i);
}
}
CopyOutDParam(backup, num);
}
/**
@ -1124,7 +1119,7 @@ static void FormatString(StringBuilder &builder, const char *str_arg, StringPara
break;
case SCC_RAW_STRING_POINTER: { // {RAW_STRING}
const char *raw_string = (const char *)(size_t)args.GetNextParameter<size_t>();
const char *raw_string = args.GetNextParameterString();
/* raw_string can be(come) nullptr when the parameter is out of range and 0 is returned instead. */
if (raw_string == nullptr ||
(game_script && std::find(_game_script_raw_strings.begin(), _game_script_raw_strings.end(), raw_string) == _game_script_raw_strings.end())) {

View File

@ -17,6 +17,7 @@
/** The data required to format and validate a single parameter of a string. */
struct StringParameter {
uint64_t data; ///< The data of the parameter.
const char *string_view; ///< The string value, if it has any.
WChar type; ///< The #StringControlCode to interpret this data with when it's the first parameter, otherwise '\0'.
};
@ -92,6 +93,18 @@ public:
return static_cast<T>(ptr == nullptr ? 0 : ptr->data);
}
/**
* Get the next string parameter from our parameters.
* This updates the offset, so the next time this is called the next parameter
* will be read.
* @return The next parameter's value.
*/
const char *GetNextParameterString()
{
auto ptr = GetNextParameterPointer();
return ptr == nullptr ? nullptr : ptr->string_view;
}
/**
* Get a new instance of StringParameters that is a "range" into the
* remaining existing parameters. Upon destruction the offset in the parent
@ -134,17 +147,36 @@ public:
{
assert(n < this->parameters.size());
this->parameters[n].data = v;
this->parameters[n].string_view = nullptr;
}
void SetParam(size_t n, const char *str)
{
assert(n < this->parameters.size());
this->parameters[n].data = 0;
this->parameters[n].string_view = str;
}
void SetParam(size_t n, const char *str) { this->SetParam(n, (uint64_t)(size_t)str); }
void SetParam(size_t n, const std::string &str) { this->SetParam(n, str.c_str()); }
void SetParam(size_t n, std::string &&str) = delete; // block passing temporaries to SetDParam
uint64 GetParam(size_t n) const
{
assert(n < this->parameters.size());
assert(this->parameters[n].string_view == nullptr);
return this->parameters[n].data;
}
/**
* Get the stored string of the parameter, or \c nullptr when there is none.
* @param n The index into the parameters.
* @return The stored string.
*/
const char *GetParamStr(size_t n) const
{
assert(n < this->parameters.size());
return this->parameters[n].string_view;
}
};
/**