mirror of https://github.com/OpenTTD/OpenTTD.git
Codechange: Replace SaveLoad var length arrays with switch block and sizeof.
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:
parent
9121770582
commit
60dc628e48
|
@ -33,12 +33,24 @@ static inline OldChunkType GetOldChunkType(OldChunkType type) {return (OldCh
|
|||
static inline OldChunkType GetOldChunkVarType(OldChunkType type) {return (OldChunkType)(GB(type, 8, 8) << 8);}
|
||||
static inline OldChunkType GetOldChunkFileType(OldChunkType type) {return (OldChunkType)(GB(type, 16, 8) << 16);}
|
||||
|
||||
/**
|
||||
* Return expected size in bytes of a OldChunkType
|
||||
* @param type OldChunkType to get size of.
|
||||
* @return size of type in bytes.
|
||||
*/
|
||||
static inline uint8_t CalcOldVarLen(OldChunkType type)
|
||||
{
|
||||
static const uint8_t type_mem_size[] = {0, 1, 1, 2, 2, 4, 4, 8};
|
||||
uint8_t length = GB(type, 8, 8);
|
||||
assert(length != 0 && length < lengthof(type_mem_size));
|
||||
return type_mem_size[length];
|
||||
switch (GetOldChunkVarType(type)) {
|
||||
case OC_VAR_I8: return sizeof(int8_t);
|
||||
case OC_VAR_U8: return sizeof(uint8_t);
|
||||
case OC_VAR_I16: return sizeof(int16_t);
|
||||
case OC_VAR_U16: return sizeof(uint16_t);
|
||||
case OC_VAR_I32: return sizeof(int32_t);
|
||||
case OC_VAR_U32: return sizeof(uint32_t);
|
||||
case OC_VAR_I64: return sizeof(int64_t);
|
||||
case OC_VAR_U64: return sizeof(uint64_t);
|
||||
default: NOT_REACHED();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue