* Pointers cannot be loaded from a savegame, so this function * gets the index from the savegame and returns the appropriate * pointer from the already loaded base. * Remember that an index of 0 is a nullptr pointer so all indices * are +1 so vehicle 0 is saved as 1. * @param index The index that is being converted to a pointer * @param rt SLRefType type of the object the pointer is sought o
| 1256 | * @return Return the index converted to a pointer of any type |
| 1257 | */ |
| 1258 | static void *IntToReference(size_t index, SLRefType rt) |
| 1259 | { |
| 1260 | static_assert(sizeof(size_t) <= sizeof(void *)); |
| 1261 | |
| 1262 | assert(_sl.action == SLA_PTRS); |
| 1263 | |
| 1264 | /* After version 4.3 REF_VEHICLE_OLD is saved as REF_VEHICLE, |
| 1265 | * and should be loaded like that */ |
| 1266 | if (rt == REF_VEHICLE_OLD && !IsSavegameVersionBefore(SLV_4, 4)) { |
| 1267 | rt = REF_VEHICLE; |
| 1268 | } |
| 1269 | |
| 1270 | /* No need to look up nullptr pointers, just return immediately */ |
| 1271 | if (index == (rt == REF_VEHICLE_OLD ? 0xFFFF : 0)) return nullptr; |
| 1272 | |
| 1273 | /* Correct index. Old vehicles were saved differently: |
| 1274 | * invalid vehicle was 0xFFFF, now we use 0x0000 for everything invalid. */ |
| 1275 | if (rt != REF_VEHICLE_OLD) index--; |
| 1276 | |
| 1277 | switch (rt) { |
| 1278 | case REF_ORDERLIST: |
| 1279 | if (OrderList::IsValidID(index)) return OrderList::Get(index); |
| 1280 | SlErrorCorrupt("Referencing invalid OrderList"); |
| 1281 | |
| 1282 | case REF_VEHICLE_OLD: |
| 1283 | case REF_VEHICLE: |
| 1284 | if (Vehicle::IsValidID(index)) return Vehicle::Get(index); |
| 1285 | SlErrorCorrupt("Referencing invalid Vehicle"); |
| 1286 | |
| 1287 | case REF_STATION: |
| 1288 | if (Station::IsValidID(index)) return Station::Get(index); |
| 1289 | SlErrorCorrupt("Referencing invalid Station"); |
| 1290 | |
| 1291 | case REF_TOWN: |
| 1292 | if (Town::IsValidID(index)) return Town::Get(index); |
| 1293 | SlErrorCorrupt("Referencing invalid Town"); |
| 1294 | |
| 1295 | case REF_ROADSTOPS: |
| 1296 | if (RoadStop::IsValidID(index)) return RoadStop::Get(index); |
| 1297 | SlErrorCorrupt("Referencing invalid RoadStop"); |
| 1298 | |
| 1299 | case REF_ENGINE_RENEWS: |
| 1300 | if (EngineRenew::IsValidID(index)) return EngineRenew::Get(index); |
| 1301 | SlErrorCorrupt("Referencing invalid EngineRenew"); |
| 1302 | |
| 1303 | case REF_CARGO_PACKET: |
| 1304 | if (CargoPacket::IsValidID(index)) return CargoPacket::Get(index); |
| 1305 | SlErrorCorrupt("Referencing invalid CargoPacket"); |
| 1306 | |
| 1307 | case REF_STORAGE: |
| 1308 | if (PersistentStorage::IsValidID(index)) return PersistentStorage::Get(index); |
| 1309 | SlErrorCorrupt("Referencing invalid PersistentStorage"); |
| 1310 | |
| 1311 | case REF_LINK_GRAPH: |
| 1312 | if (LinkGraph::IsValidID(index)) return LinkGraph::Get(index); |
| 1313 | SlErrorCorrupt("Referencing invalid LinkGraph"); |
| 1314 | |
| 1315 | case REF_LINK_GRAPH_JOB: |
no test coverage detected