MCPcopy Create free account
hub / github.com/OpenTTD/OpenTTD / SlCopyInternal

Function SlCopyInternal

src/saveload/saveload.cpp:1101–1140  ·  view source on GitHub ↗

* Internal function to save/Load a list of SL_VARs. * SlCopy() and SlArray() are very similar, with the exception of the header. * This function represents the common part. * @param object The object being manipulated. * @param length The length of the object in elements * @param conv VarType type of the items. */

Source from the content-addressed store, hash-verified

1099 * @param conv VarType type of the items.
1100 */
1101static void SlCopyInternal(void *object, size_t length, VarType conv)
1102{
1103 if (GetVarMemType(conv) == SLE_VAR_NULL) {
1104 assert(_sl.action != SLA_SAVE); // Use SL_NULL if you want to write null-bytes
1105 SlSkipBytes(length * SlCalcConvFileLen(conv));
1106 return;
1107 }
1108
1109 /* NOTICE - handle some buggy stuff, in really old versions everything was saved
1110 * as a byte-type. So detect this, and adjust object size accordingly */
1111 if (_sl.action != SLA_SAVE && _sl_version == 0) {
1112 /* all objects except difficulty settings */
1113 if (conv == SLE_INT16 || conv == SLE_UINT16 || conv == SLE_STRINGID ||
1114 conv == SLE_INT32 || conv == SLE_UINT32) {
1115 SlCopyBytes(object, length * SlCalcConvFileLen(conv));
1116 return;
1117 }
1118 /* used for conversion of Money 32bit->64bit */
1119 if (conv == (SLE_FILE_I32 | SLE_VAR_I64)) {
1120 for (uint i = 0; i < length; i++) {
1121 ((int64_t*)object)[i] = (int32_t)std::byteswap(SlReadUint32());
1122 }
1123 return;
1124 }
1125 }
1126
1127 /* If the size of elements is 1 byte both in file and memory, no special
1128 * conversion is needed, use specialized copy-copy function to speed up things */
1129 if (conv == SLE_INT8 || conv == SLE_UINT8) {
1130 SlCopyBytes(object, length);
1131 } else {
1132 uint8_t *a = (uint8_t*)object;
1133 uint8_t mem_size = SlCalcConvMemLen(conv);
1134
1135 for (; length != 0; length --) {
1136 SlSaveLoadConv(a, conv);
1137 a += mem_size; // get size
1138 }
1139 }
1140}
1141
1142/**
1143 * Copy a list of SL_VARs to/from a savegame.

Callers 2

SlCopyFunction · 0.85
SlArrayFunction · 0.85

Calls 8

GetVarMemTypeFunction · 0.85
SlSkipBytesFunction · 0.85
SlCalcConvFileLenFunction · 0.85
SlCopyBytesFunction · 0.85
byteswapFunction · 0.85
SlReadUint32Function · 0.85
SlCalcConvMemLenFunction · 0.85
SlSaveLoadConvFunction · 0.85

Tested by

no test coverage detected