mirror of
https://github.com/OpenTTD/OpenTTD
synced 2026-01-28 06:34:33 +01:00
Codechange: Replace SaveLoad var length arrays with switch block and sizeof. (#12570)
SlCalcConvMemLen(), SlCalcConfFileLen() and CalcOldVarLen() follow a pattern of looking up part of a value in an array. These function returns the size of bytes of a variable type, but is not very clear. Replace with a switch block instead. Removes lengthof, array indices, and magic numbers.
This commit is contained in:
@@ -599,17 +599,25 @@ static uint8_t GetSavegameFileType(const SaveLoad &sld)
|
||||
*/
|
||||
static inline uint SlCalcConvMemLen(VarType conv)
|
||||
{
|
||||
static const uint8_t conv_mem_size[] = {1, 1, 1, 2, 2, 4, 4, 8, 8, 0};
|
||||
|
||||
switch (GetVarMemType(conv)) {
|
||||
case SLE_VAR_BL: return sizeof(bool);
|
||||
case SLE_VAR_I8: return sizeof(int8_t);
|
||||
case SLE_VAR_U8: return sizeof(uint8_t);
|
||||
case SLE_VAR_I16: return sizeof(int16_t);
|
||||
case SLE_VAR_U16: return sizeof(uint16_t);
|
||||
case SLE_VAR_I32: return sizeof(int32_t);
|
||||
case SLE_VAR_U32: return sizeof(uint32_t);
|
||||
case SLE_VAR_I64: return sizeof(int64_t);
|
||||
case SLE_VAR_U64: return sizeof(uint64_t);
|
||||
case SLE_VAR_NULL: return 0;
|
||||
|
||||
case SLE_VAR_STR:
|
||||
case SLE_VAR_STRQ:
|
||||
return SlReadArrayLength();
|
||||
|
||||
case SLE_VAR_NAME:
|
||||
default:
|
||||
uint8_t type = GetVarMemType(conv) >> 4;
|
||||
assert(type < lengthof(conv_mem_size));
|
||||
return conv_mem_size[type];
|
||||
NOT_REACHED();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -621,17 +629,24 @@ static inline uint SlCalcConvMemLen(VarType conv)
|
||||
*/
|
||||
static inline uint8_t SlCalcConvFileLen(VarType conv)
|
||||
{
|
||||
static const uint8_t conv_file_size[] = {0, 1, 1, 2, 2, 4, 4, 8, 8, 2};
|
||||
|
||||
switch (GetVarFileType(conv)) {
|
||||
case SLE_FILE_END: return 0;
|
||||
case SLE_FILE_I8: return sizeof(int8_t);
|
||||
case SLE_FILE_U8: return sizeof(uint8_t);
|
||||
case SLE_FILE_I16: return sizeof(int16_t);
|
||||
case SLE_FILE_U16: return sizeof(uint16_t);
|
||||
case SLE_FILE_I32: return sizeof(int32_t);
|
||||
case SLE_FILE_U32: return sizeof(uint32_t);
|
||||
case SLE_FILE_I64: return sizeof(int64_t);
|
||||
case SLE_FILE_U64: return sizeof(uint64_t);
|
||||
case SLE_FILE_STRINGID: return sizeof(uint16_t);
|
||||
|
||||
case SLE_FILE_STRING:
|
||||
return SlReadArrayLength();
|
||||
|
||||
case SLE_FILE_STRUCT:
|
||||
default:
|
||||
uint8_t type = GetVarFileType(conv);
|
||||
if (type >= lengthof(conv_file_size)) fmt::println("{}", type);
|
||||
assert(type < lengthof(conv_file_size));
|
||||
return conv_file_size[type];
|
||||
NOT_REACHED();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user