mirror of
https://github.com/OpenTTD/OpenTTD.git
synced 2026-07-25 08:22:17 +00:00
Codechange: make SlVarSize, ReadValue and WriteValue use VarMemType
This commit is contained in:
+13
-13
@@ -192,7 +192,7 @@ typedef int32_t CheckButtonClick(int32_t new_value, int32_t change_direction);
|
||||
|
||||
/** Information of a cheat. */
|
||||
struct CheatEntry {
|
||||
VarType type; ///< type of selector
|
||||
VarMemType type; ///< type of selector
|
||||
StringID str; ///< string with descriptive text
|
||||
void *variable; ///< pointer to the variable
|
||||
bool *been_used; ///< has this cheat been used before?
|
||||
@@ -204,15 +204,15 @@ struct CheatEntry {
|
||||
* Order matches with the values of #CheatNumbers
|
||||
*/
|
||||
static const CheatEntry _cheats_ui[] = {
|
||||
{SLE_INT32, STR_CHEAT_MONEY, &_money_cheat_amount, &_cheats.money.been_used, &ClickMoneyCheat },
|
||||
{SLE_UINT8, STR_CHEAT_CHANGE_COMPANY, &_local_company, &_cheats.switch_company.been_used, &ClickChangeCompanyCheat },
|
||||
{SLE_BOOL, STR_CHEAT_EXTRA_DYNAMITE, &_cheats.magic_bulldozer.value, &_cheats.magic_bulldozer.been_used, nullptr },
|
||||
{SLE_BOOL, STR_CHEAT_CROSSINGTUNNELS, &_cheats.crossing_tunnels.value, &_cheats.crossing_tunnels.been_used, nullptr },
|
||||
{SLE_BOOL, STR_CHEAT_NO_JETCRASH, &_cheats.no_jetcrash.value, &_cheats.no_jetcrash.been_used, nullptr },
|
||||
{SLE_BOOL, STR_CHEAT_SETUP_PROD, &_cheats.setup_prod.value, &_cheats.setup_prod.been_used, &ClickSetProdCheat },
|
||||
{SLE_BOOL, STR_CHEAT_STATION_RATING, &_cheats.station_rating.value, &_cheats.station_rating.been_used, nullptr },
|
||||
{SLE_UINT8, STR_CHEAT_EDIT_MAX_HL, &_settings_game.construction.map_height_limit, &_cheats.edit_max_hl.been_used, &ClickChangeMaxHlCheat },
|
||||
{SLE_INT32, STR_CHEAT_CHANGE_DATE, &TimerGameCalendar::year, &_cheats.change_date.been_used, &ClickChangeDateCheat },
|
||||
{SLE_VAR_I32, STR_CHEAT_MONEY, &_money_cheat_amount, &_cheats.money.been_used, &ClickMoneyCheat },
|
||||
{SLE_VAR_U8, STR_CHEAT_CHANGE_COMPANY, &_local_company, &_cheats.switch_company.been_used, &ClickChangeCompanyCheat },
|
||||
{SLE_VAR_BL, STR_CHEAT_EXTRA_DYNAMITE, &_cheats.magic_bulldozer.value, &_cheats.magic_bulldozer.been_used, nullptr },
|
||||
{SLE_VAR_BL, STR_CHEAT_CROSSINGTUNNELS, &_cheats.crossing_tunnels.value, &_cheats.crossing_tunnels.been_used, nullptr },
|
||||
{SLE_VAR_BL, STR_CHEAT_NO_JETCRASH, &_cheats.no_jetcrash.value, &_cheats.no_jetcrash.been_used, nullptr },
|
||||
{SLE_VAR_BL, STR_CHEAT_SETUP_PROD, &_cheats.setup_prod.value, &_cheats.setup_prod.been_used, &ClickSetProdCheat },
|
||||
{SLE_VAR_BL, STR_CHEAT_STATION_RATING, &_cheats.station_rating.value, &_cheats.station_rating.been_used, nullptr },
|
||||
{SLE_VAR_U8, STR_CHEAT_EDIT_MAX_HL, &_settings_game.construction.map_height_limit, &_cheats.edit_max_hl.been_used, &ClickChangeMaxHlCheat },
|
||||
{SLE_VAR_I32, STR_CHEAT_CHANGE_DATE, &TimerGameCalendar::year, &_cheats.change_date.been_used, &ClickChangeDateCheat },
|
||||
};
|
||||
|
||||
static_assert(CHT_NUM_CHEATS == lengthof(_cheats_ui));
|
||||
@@ -282,7 +282,7 @@ struct CheatWindow : Window {
|
||||
const CheatEntry *ce = &_cheats_ui[i];
|
||||
|
||||
std::string str;
|
||||
switch (GetVarMemType(ce->type)) {
|
||||
switch (ce->type) {
|
||||
case SLE_VAR_BL: {
|
||||
bool on = (*(bool*)ce->variable);
|
||||
|
||||
@@ -379,7 +379,7 @@ struct CheatWindow : Window {
|
||||
{
|
||||
uint width = 0;
|
||||
for (const auto &ce : _cheats_ui) {
|
||||
switch (GetVarMemType(ce.type)) {
|
||||
switch (ce.type) {
|
||||
case SLE_VAR_BL:
|
||||
width = std::max(width, GetStringBoundingBox(GetString(ce.str, STR_CONFIG_SETTING_ON)).width);
|
||||
width = std::max(width, GetStringBoundingBox(GetString(ce.str, STR_CONFIG_SETTING_OFF)).width);
|
||||
@@ -465,7 +465,7 @@ struct CheatWindow : Window {
|
||||
this->clicked_setting = nullptr;
|
||||
*ce->been_used = true;
|
||||
|
||||
switch (GetVarMemType(ce->type)) {
|
||||
switch (ce->type) {
|
||||
case SLE_VAR_BL:
|
||||
value ^= 1;
|
||||
if (ce->proc != nullptr) ce->proc(value, 0);
|
||||
|
||||
@@ -869,9 +869,9 @@ size_t SlGetFieldLength()
|
||||
* type, eg one with other flags because it is parsed
|
||||
* @return returns the value of the pointer-setting
|
||||
*/
|
||||
int64_t ReadValue(const void *ptr, VarType conv)
|
||||
int64_t ReadValue(const void *ptr, VarMemType conv)
|
||||
{
|
||||
switch (GetVarMemType(conv)) {
|
||||
switch (conv) {
|
||||
case SLE_VAR_BL: return (*static_cast<const bool *>(ptr) != 0);
|
||||
case SLE_VAR_I8: return *static_cast<const int8_t *>(ptr);
|
||||
case SLE_VAR_U8: return *static_cast<const uint8_t *>(ptr);
|
||||
@@ -893,9 +893,9 @@ int64_t ReadValue(const void *ptr, VarType conv)
|
||||
* with other flags. It is parsed upon read
|
||||
* @param val the new value being given to the variable
|
||||
*/
|
||||
void WriteValue(void *ptr, VarType conv, int64_t val)
|
||||
void WriteValue(void *ptr, VarMemType conv, int64_t val)
|
||||
{
|
||||
switch (GetVarMemType(conv)) {
|
||||
switch (conv) {
|
||||
case SLE_VAR_BL: *static_cast<bool *>(ptr) = (val != 0); break;
|
||||
case SLE_VAR_I8: *static_cast<int8_t *>(ptr) = val; break;
|
||||
case SLE_VAR_U8: *static_cast<uint8_t *>(ptr) = val; break;
|
||||
@@ -923,7 +923,7 @@ static void SlSaveLoadConv(void *ptr, VarType conv)
|
||||
{
|
||||
switch (_sl.action) {
|
||||
case SaveLoadAction::Save: {
|
||||
int64_t x = ReadValue(ptr, conv);
|
||||
int64_t x = ReadValue(ptr, conv.mem);
|
||||
|
||||
/* Write the value to the file and check if its value is in the desired range */
|
||||
switch (GetVarFileType(conv)) {
|
||||
@@ -980,7 +980,7 @@ static void SlSaveLoadConv(void *ptr, VarType conv)
|
||||
}
|
||||
|
||||
/* Write The value to the struct. These ARE endian safe. */
|
||||
WriteValue(ptr, conv, x);
|
||||
WriteValue(ptr, conv.mem, x);
|
||||
break;
|
||||
}
|
||||
case SaveLoadAction::Ptrs: break;
|
||||
|
||||
@@ -853,9 +853,9 @@ inline constexpr bool IsNumericType(VarType conv)
|
||||
* @param type VarType to get size of.
|
||||
* @return size of type in bytes.
|
||||
*/
|
||||
inline constexpr size_t SlVarSize(VarType type)
|
||||
inline constexpr size_t SlVarSize(VarMemType type)
|
||||
{
|
||||
switch (GetVarMemType(type)) {
|
||||
switch (type) {
|
||||
case SLE_VAR_BL: return sizeof(bool);
|
||||
case SLE_VAR_I8: return sizeof(int8_t);
|
||||
case SLE_VAR_U8: return sizeof(uint8_t);
|
||||
@@ -884,10 +884,10 @@ inline constexpr size_t SlVarSize(VarType type)
|
||||
inline constexpr bool SlCheckVarSize(SaveLoadType cmd, VarType type, size_t length, size_t size)
|
||||
{
|
||||
switch (cmd) {
|
||||
case SaveLoadType::Variable: return SlVarSize(type) == size;
|
||||
case SaveLoadType::Variable: return SlVarSize(type.mem) == size;
|
||||
case SaveLoadType::Reference: return sizeof(void *) == size;
|
||||
case SaveLoadType::String: return SlVarSize(type) == size;
|
||||
case SaveLoadType::Array: return SlVarSize(type) * length <= size; // Partial load of array is permitted.
|
||||
case SaveLoadType::String: return SlVarSize(type.mem) == size;
|
||||
case SaveLoadType::Array: return SlVarSize(type.mem) * length <= size; // Partial load of array is permitted.
|
||||
case SaveLoadType::Vector: return sizeof(std::vector<void *>) == size;
|
||||
case SaveLoadType::ReferenceList: return sizeof(std::list<void *>) == size;
|
||||
case SaveLoadType::ReferenceVector: return sizeof(std::vector<void *>) == size;
|
||||
@@ -1366,8 +1366,8 @@ inline void *GetVariableAddress(const void *object, const SaveLoad &sld)
|
||||
return sld.address_proc(const_cast<void *>(object), sld.extra_data);
|
||||
}
|
||||
|
||||
int64_t ReadValue(const void *ptr, VarType conv);
|
||||
void WriteValue(void *ptr, VarType conv, int64_t val);
|
||||
int64_t ReadValue(const void *ptr, VarMemType conv);
|
||||
void WriteValue(void *ptr, VarMemType conv, int64_t val);
|
||||
|
||||
void SlSetArrayIndex(uint index);
|
||||
static void SlSetArrayIndex(const ConvertibleThroughBase auto &index) { SlSetArrayIndex(index.base()); }
|
||||
|
||||
+5
-5
@@ -307,7 +307,7 @@ static std::optional<std::vector<uint32_t>> ParseIntList(std::string_view str)
|
||||
* @param type the type of elements the array holds (eg INT8, UINT16, etc.)
|
||||
* @return return true on success and false on error
|
||||
*/
|
||||
static bool LoadIntList(std::optional<std::string_view> str, void *array, int nelems, VarType type)
|
||||
static bool LoadIntList(std::optional<std::string_view> str, void *array, int nelems, VarMemType type)
|
||||
{
|
||||
size_t elem_size = SlVarSize(type);
|
||||
std::byte *p = static_cast<std::byte *>(array);
|
||||
@@ -582,7 +582,7 @@ void IntSettingDesc::MakeValueValid(int32_t &val) const
|
||||
void IntSettingDesc::Write(const void *object, int32_t val) const
|
||||
{
|
||||
void *ptr = GetVariableAddress(object, this->save);
|
||||
WriteValue(ptr, this->save.conv, (int64_t)val);
|
||||
WriteValue(ptr, this->save.conv.mem, (int64_t)val);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -593,7 +593,7 @@ void IntSettingDesc::Write(const void *object, int32_t val) const
|
||||
int32_t IntSettingDesc::Read(const void *object) const
|
||||
{
|
||||
void *ptr = GetVariableAddress(object, this->save);
|
||||
return (int32_t)ReadValue(ptr, this->save.conv);
|
||||
return (int32_t)ReadValue(ptr, this->save.conv.mem);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -706,13 +706,13 @@ void ListSettingDesc::ParseValue(const IniItem *item, void *object) const
|
||||
str = this->def;
|
||||
}
|
||||
void *ptr = GetVariableAddress(object, this->save);
|
||||
if (!LoadIntList(str, ptr, this->save.length, this->save.conv)) {
|
||||
if (!LoadIntList(str, ptr, this->save.length, this->save.conv.mem)) {
|
||||
_settings_error_list.emplace_back(
|
||||
GetEncodedString(STR_CONFIG_ERROR),
|
||||
GetEncodedString(STR_CONFIG_ERROR_ARRAY, this->GetName()));
|
||||
|
||||
/* Use default */
|
||||
LoadIntList(this->def, ptr, this->save.length, this->save.conv);
|
||||
LoadIntList(this->def, ptr, this->save.length, this->save.conv.mem);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user