Codechange: introduce new type and functions for StringParameter backups

pull/603/merge
Rubidium 12 months ago committed by rubidium42
parent 26f3efb419
commit 2687704afc

@ -197,6 +197,56 @@ void CopyOutDParam(uint64 *dst, const char **strings, StringID string, int num)
}
}
/**
* Copy the parameters from the backup into the global string parameter array.
* @param backup The backup to copy from.
*/
void CopyInDParam(const span<const StringParameterBackup> backup)
{
for (size_t i = 0; i < backup.size(); i++) {
auto &value = backup[i];
if (value.string.has_value()) {
_global_string_params.SetParam(i, value.string.value());
} else {
_global_string_params.SetParam(i, value.data);
}
}
}
/**
* Copy \a num string parameters from the global string parameter array to the \a backup.
* @param backup The backup to write to.
* @param num Number of string parameters to copy.
*/
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);
}
}
/**
* Copy \a num string parameters from the global string parameter array to the \a backup.
* @param backup The backup to write to.
* @param num Number of string parameters to copy.
* @param string The string used to determine where raw strings are and where there are no raw strings.
*/
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);
}
}
}
static void StationGetSpecialString(StringBuilder &builder, StationFacility x);
static void GetSpecialTownNameString(StringBuilder &builder, int ind, uint32 seed);
static void GetSpecialNameString(StringBuilder &builder, int ind, StringParameters &args);

@ -14,6 +14,7 @@
#include "string_type.h"
#include "gfx_type.h"
#include "core/bitmath_func.hpp"
#include "core/span_type.hpp"
#include "vehicle_type.h"
/**
@ -89,6 +90,10 @@ void CopyInDParam(const uint64 *src, int num);
void CopyOutDParam(uint64 *dst, int num);
void CopyOutDParam(uint64 *dst, const char **strings, StringID string, int num);
void CopyInDParam(const span<const StringParameterBackup> backup);
void CopyOutDParam(std::vector<StringParameterBackup> &backup, size_t num);
void CopyOutDParam(std::vector<StringParameterBackup> &backup, size_t num, StringID string);
uint64_t GetDParam(size_t n);
extern TextDirection _current_text_dir; ///< Text direction of the currently selected language

@ -88,4 +88,34 @@ enum SpecialStrings {
SPECSTR_PRESIDENT_NAME = 0x70E7,
};
/** Data that is to be stored when backing up StringParameters. */
struct StringParameterBackup {
uint64_t data; ///< The data field; valid *when* string has no value.
std::optional<std::string> string; ///< The string value.
/**
* Assign the numeric data with the given value, while clearing the stored string.
* @param data The new value of the data field.
* @return This object.
*/
StringParameterBackup &operator=(uint64_t data)
{
this->string.reset();
this->data = data;
return *this;
}
/**
* Assign a copy of the given string to the string field, while clearing the data field.
* @param string The new value of the string.
* @return This object.
*/
StringParameterBackup &operator=(const std::string_view string)
{
this->data = 0;
this->string.emplace(string);
return *this;
}
};
#endif /* STRINGS_TYPE_H */

Loading…
Cancel
Save