From 2c941cd8b3cb1774f4982b86735e276597a91750 Mon Sep 17 00:00:00 2001 From: glx22 Date: Mon, 7 Jun 2021 23:24:37 +0200 Subject: Codechange: Use ChunkHandlers sub-classes --- src/saveload/order_sl.cpp | 251 +++++++++++++++++++++++++--------------------- 1 file changed, 136 insertions(+), 115 deletions(-) (limited to 'src/saveload/order_sl.cpp') diff --git a/src/saveload/order_sl.cpp b/src/saveload/order_sl.cpp index 797c16d1b..16ee42013 100644 --- a/src/saveload/order_sl.cpp +++ b/src/saveload/order_sl.cpp @@ -118,85 +118,92 @@ SaveLoadTable GetOrderDescription() return _order_desc; } -static void Save_ORDR() -{ - const SaveLoadTable slt = GetOrderDescription(); - SlTableHeader(slt); +struct ORDRChunkHandler : ChunkHandler { + ORDRChunkHandler() : ChunkHandler('ORDR', CH_TABLE) + { + this->fix_pointers = true; + } - for (Order *order : Order::Iterate()) { - SlSetArrayIndex(order->index); - SlObject(order, slt); + void Save() const override + { + const SaveLoadTable slt = GetOrderDescription(); + SlTableHeader(slt); + + for (Order *order : Order::Iterate()) { + SlSetArrayIndex(order->index); + SlObject(order, slt); + } } -} -static void Load_ORDR() -{ - if (IsSavegameVersionBefore(SLV_5, 2)) { - /* Version older than 5.2 did not have a ->next pointer. Convert them - * (in the old days, the orderlist was 5000 items big) */ - size_t len = SlGetFieldLength(); - - if (IsSavegameVersionBefore(SLV_5)) { - /* Pre-version 5 had another layout for orders - * (uint16 instead of uint32) */ - len /= sizeof(uint16); - uint16 *orders = MallocT(len + 1); - - SlCopy(orders, len, SLE_UINT16); - - for (size_t i = 0; i < len; ++i) { - Order *o = new (i) Order(); - o->AssignOrder(UnpackVersion4Order(orders[i])); - } + void Load() const override + { + if (IsSavegameVersionBefore(SLV_5, 2)) { + /* Version older than 5.2 did not have a ->next pointer. Convert them + * (in the old days, the orderlist was 5000 items big) */ + size_t len = SlGetFieldLength(); - free(orders); - } else if (IsSavegameVersionBefore(SLV_5, 2)) { - len /= sizeof(uint32); - uint32 *orders = MallocT(len + 1); + if (IsSavegameVersionBefore(SLV_5)) { + /* Pre-version 5 had another layout for orders + * (uint16 instead of uint32) */ + len /= sizeof(uint16); + uint16 *orders = MallocT(len + 1); - SlCopy(orders, len, SLE_UINT32); + SlCopy(orders, len, SLE_UINT16); - for (size_t i = 0; i < len; ++i) { - new (i) Order(orders[i]); - } + for (size_t i = 0; i < len; ++i) { + Order *o = new (i) Order(); + o->AssignOrder(UnpackVersion4Order(orders[i])); + } - free(orders); - } + free(orders); + } else if (IsSavegameVersionBefore(SLV_5, 2)) { + len /= sizeof(uint32); + uint32 *orders = MallocT(len + 1); - /* Update all the next pointer */ - for (Order *o : Order::Iterate()) { - size_t order_index = o->index; - /* Delete invalid orders */ - if (o->IsType(OT_NOTHING)) { - delete o; - continue; + SlCopy(orders, len, SLE_UINT32); + + for (size_t i = 0; i < len; ++i) { + new (i) Order(orders[i]); + } + + free(orders); } - /* The orders were built like this: - * While the order is valid, set the previous will get its next pointer set */ - Order *prev = Order::GetIfValid(order_index - 1); - if (prev != nullptr) prev->next = o; - } - } else { - const std::vector slt = SlCompatTableHeader(GetOrderDescription(), _order_sl_compat); - int index; + /* Update all the next pointer */ + for (Order *o : Order::Iterate()) { + size_t order_index = o->index; + /* Delete invalid orders */ + if (o->IsType(OT_NOTHING)) { + delete o; + continue; + } + /* The orders were built like this: + * While the order is valid, set the previous will get its next pointer set */ + Order *prev = Order::GetIfValid(order_index - 1); + if (prev != nullptr) prev->next = o; + } + } else { + const std::vector slt = SlCompatTableHeader(GetOrderDescription(), _order_sl_compat); - while ((index = SlIterateArray()) != -1) { - Order *order = new (index) Order(); - SlObject(order, slt); + int index; + + while ((index = SlIterateArray()) != -1) { + Order *order = new (index) Order(); + SlObject(order, slt); + } } } -} -static void Ptrs_ORDR() -{ - /* Orders from old savegames have pointers corrected in Load_ORDR */ - if (IsSavegameVersionBefore(SLV_5, 2)) return; + void FixPointers() const override + { + /* Orders from old savegames have pointers corrected in Load_ORDR */ + if (IsSavegameVersionBefore(SLV_5, 2)) return; - for (Order *o : Order::Iterate()) { - SlObject(o, GetOrderDescription()); + for (Order *o : Order::Iterate()) { + SlObject(o, GetOrderDescription()); + } } -} +}; SaveLoadTable GetOrderListDescription() { @@ -207,37 +214,44 @@ SaveLoadTable GetOrderListDescription() return _orderlist_desc; } -static void Save_ORDL() -{ - const SaveLoadTable slt = GetOrderListDescription(); - SlTableHeader(slt); +struct ORDLChunkHandler : ChunkHandler { + ORDLChunkHandler() : ChunkHandler('ORDL', CH_TABLE) + { + this->fix_pointers = true; + } + + void Save() const override + { + const SaveLoadTable slt = GetOrderListDescription(); + SlTableHeader(slt); - for (OrderList *list : OrderList::Iterate()) { - SlSetArrayIndex(list->index); - SlObject(list, slt); + for (OrderList *list : OrderList::Iterate()) { + SlSetArrayIndex(list->index); + SlObject(list, slt); + } } -} -static void Load_ORDL() -{ - const std::vector slt = SlCompatTableHeader(GetOrderListDescription(), _orderlist_sl_compat); + void Load() const override + { + const std::vector slt = SlCompatTableHeader(GetOrderListDescription(), _orderlist_sl_compat); - int index; + int index; - while ((index = SlIterateArray()) != -1) { - /* set num_orders to 0 so it's a valid OrderList */ - OrderList *list = new (index) OrderList(0); - SlObject(list, slt); - } + while ((index = SlIterateArray()) != -1) { + /* set num_orders to 0 so it's a valid OrderList */ + OrderList *list = new (index) OrderList(0); + SlObject(list, slt); + } -} + } -static void Ptrs_ORDL() -{ - for (OrderList *list : OrderList::Iterate()) { - SlObject(list, GetOrderListDescription()); + void FixPointers() const override + { + for (OrderList *list : OrderList::Iterate()) { + SlObject(list, GetOrderListDescription()); + } } -} +}; SaveLoadTable GetOrderBackupDescription() { @@ -262,45 +276,52 @@ SaveLoadTable GetOrderBackupDescription() return _order_backup_desc; } -static void Save_BKOR() -{ - const SaveLoadTable slt = GetOrderBackupDescription(); - SlTableHeader(slt); +struct BKORChunkHandler : ChunkHandler { + BKORChunkHandler() : ChunkHandler('BKOR', CH_TABLE) + { + this->fix_pointers = true; + } - /* We only save this when we're a network server - * as we want this information on our clients. For - * normal games this information isn't needed. */ - if (!_networking || !_network_server) return; + void Save() const override + { + const SaveLoadTable slt = GetOrderBackupDescription(); + SlTableHeader(slt); - for (OrderBackup *ob : OrderBackup::Iterate()) { - SlSetArrayIndex(ob->index); - SlObject(ob, slt); + /* We only save this when we're a network server + * as we want this information on our clients. For + * normal games this information isn't needed. */ + if (!_networking || !_network_server) return; + + for (OrderBackup *ob : OrderBackup::Iterate()) { + SlSetArrayIndex(ob->index); + SlObject(ob, slt); + } } -} -void Load_BKOR() -{ - const std::vector slt = SlCompatTableHeader(GetOrderBackupDescription(), _order_backup_sl_compat); + void Load() const override + { + const std::vector slt = SlCompatTableHeader(GetOrderBackupDescription(), _order_backup_sl_compat); - int index; + int index; - while ((index = SlIterateArray()) != -1) { - /* set num_orders to 0 so it's a valid OrderList */ - OrderBackup *ob = new (index) OrderBackup(); - SlObject(ob, slt); + while ((index = SlIterateArray()) != -1) { + /* set num_orders to 0 so it's a valid OrderList */ + OrderBackup *ob = new (index) OrderBackup(); + SlObject(ob, slt); + } } -} -static void Ptrs_BKOR() -{ - for (OrderBackup *ob : OrderBackup::Iterate()) { - SlObject(ob, GetOrderBackupDescription()); + void FixPointers() const override + { + for (OrderBackup *ob : OrderBackup::Iterate()) { + SlObject(ob, GetOrderBackupDescription()); + } } -} +}; -static const ChunkHandler BKOR{ 'BKOR', Save_BKOR, Load_BKOR, Ptrs_BKOR, nullptr, CH_TABLE }; -static const ChunkHandler ORDR{ 'ORDR', Save_ORDR, Load_ORDR, Ptrs_ORDR, nullptr, CH_TABLE }; -static const ChunkHandler ORDL{ 'ORDL', Save_ORDL, Load_ORDL, Ptrs_ORDL, nullptr, CH_TABLE }; +static const BKORChunkHandler BKOR; +static const ORDRChunkHandler ORDR; +static const ORDLChunkHandler ORDL; static const ChunkHandlerRef order_chunk_handlers[] = { BKOR, ORDR, -- cgit v1.2.3-54-g00ecf