From 26fff27e506828d24365c4049d4bb5920b65f048 Mon Sep 17 00:00:00 2001 From: lucaFiorini Date: Thu, 28 Mar 2024 20:50:19 +0100 Subject: [PATCH] full import working --- src/order_base.h | 4 +++ src/order_cmd.cpp | 47 ++----------------------- src/schdispatch_cmd.cpp | 77 +++++++++++++++++++++++++++++++++++++---- 3 files changed, 77 insertions(+), 51 deletions(-) diff --git a/src/order_base.h b/src/order_base.h index a811d29147..79c5e96e1f 100644 --- a/src/order_base.h +++ b/src/order_base.h @@ -886,12 +886,16 @@ public: inline std::string &ScheduleName() { return this->name; } inline const std::string &ScheduleName() const { return this->name; } + + static DispatchSchedule FromJSONString(std::string jsonString); + std::string ToJSONString(); }; /** * Shared order list linking together the linked list of orders and the list * of vehicles sharing this order list. */ + struct OrderList : OrderListPool::PoolItem<&_orderlist_pool> { private: friend void AfterLoadVehicles(bool part_of_load); ///< For instantiating the shared vehicle chain diff --git a/src/order_cmd.cpp b/src/order_cmd.cpp index f3c7f60516..4d3509e863 100644 --- a/src/order_cmd.cpp +++ b/src/order_cmd.cpp @@ -931,19 +931,7 @@ std::string OrderList::ToJSONString() auto& headJson = json["head"]; for (unsigned int i = 0; auto &SD : SD_data) { - auto &SDJson = headJson["scheduled-dispatch"][i++]; - - for (unsigned int i = 0; auto &SD_slot : SD.GetScheduledDispatch()) { - auto& slotsJson = SDJson["slots"][i++]; - slotsJson["offset"] = SD_slot.offset; - slotsJson["flags"] = SD_slot.flags; - } - - SDJson["name"] = SD.ScheduleName(); - SDJson["start-tick"] = SD.GetScheduledDispatchStartTick().value; - SDJson["duration"] = SD.GetScheduledDispatchDuration(); - SDJson["max-delay"] = SD.GetScheduledDispatchDelay(); - SDJson["flags"] = SD.GetScheduledDispatchFlags(); + headJson["scheduled-dispatch"][i++] = nlohmann::json::parse(SD.ToJSONString()); } @@ -985,52 +973,23 @@ void OrderList::FromJSONString(const Vehicle * v,std::string json_str) if (ordersJson.is_array()) { for (nlohmann::json::iterator it = ordersJson.begin(); it != ordersJson.end(); ++it) { auto &orderJson = it.value(); - Order new_order = Order::FromJSONString(orderJson.dump()); OrderID new_orderID = v->GetNumOrders(); DoCommandPEx(v->tile, v->index, new_orderID, 0, CMD_INSERT_ORDER | CMD_MSG(STR_ERROR_CAN_T_INSERT_NEW_ORDER), nullptr, orderJson.dump().c_str(), nullptr, 0); } } } - if (json.contains("orders") && json.contains("head")){ + if (json.contains("head")){ auto &headJson = json["head"]; if (headJson.contains("scheduled-dispatch")) { auto &SDJson = headJson["scheduled-dispatch"]; if (SDJson.is_array()) { for (nlohmann::json::iterator it = SDJson.begin(); it != SDJson.end(); ++it) { - if (it.value().contains("slots")) { - - auto &slotsJson = it.value().at("slots");; - DispatchSchedule dispatchSchedule; - - if (slotsJson.is_array()) { - for (nlohmann::json::iterator it = slotsJson.begin(); it != slotsJson.end(); ++it) { - - DispatchSlot newDispatchSlot; - - newDispatchSlot.offset = it.value().at("offset").template get(); - newDispatchSlot.flags = it.value().at("flags").template get(); - - dispatchSchedule.GetScheduledDispatchMutable().push_back(newDispatchSlot); - - } - } - - dispatchSchedule.ScheduleName() = it.value().at("name").template get(); - dispatchSchedule.SetScheduledDispatchStartTick(it.value().at("start-tick").template get()); - dispatchSchedule.SetScheduledDispatchDuration(it.value().at("duration").template get()); - dispatchSchedule.SetScheduledDispatchDelay(it.value().at("max-delay").template get()); - dispatchSchedule.SetScheduledDispatchFlags(it.value().at("flags").template get()); - - } + DoCommandPEx(0, v->index, 0, 0, CMD_SCHEDULED_DISPATCH_ADD_NEW_SCHEDULE | CMD_MSG(STR_ERROR_CAN_T_TIMETABLE_VEHICLE), nullptr, it.value().dump().c_str() , 0); } } } - } - - - } /** diff --git a/src/schdispatch_cmd.cpp b/src/schdispatch_cmd.cpp index da5a88907e..8dd82c99f1 100644 --- a/src/schdispatch_cmd.cpp +++ b/src/schdispatch_cmd.cpp @@ -20,6 +20,7 @@ #include "settings_type.h" #include "schdispatch.h" #include "vehicle_gui.h" +#include <3rdparty/nlohmann/json.hpp> #include @@ -357,15 +358,15 @@ CommandCost CmdScheduledDispatchClear(TileIndex tile, DoCommandFlag flags, uint3 * @param p1 Vehicle index * @param p2 Duration, in scaled tick * @param p3 Start tick - * @param text unused + * @param text optional JSON string * @return the cost of this operation or an error */ -CommandCost CmdScheduledDispatchAddNewSchedule(TileIndex tile, DoCommandFlag flags, uint32_t p1, uint32_t p2, uint64_t p3, const char *text, const CommandAuxiliaryBase *aux_data) +CommandCost CmdScheduledDispatchAddNewSchedule(TileIndex tile, DoCommandFlag flags, uint32_t p1, uint32_t p2, uint64_t p3, const char *scheduleJson, const CommandAuxiliaryBase *aux_data) { VehicleID veh = GB(p1, 0, 20); Vehicle *v = Vehicle::GetIfValid(veh); - if (v == nullptr || !v->IsPrimaryVehicle() || p2 == 0) return CMD_ERROR; + if (v == nullptr || !v->IsPrimaryVehicle()) return CMD_ERROR; CommandCost ret = CheckOwnership(v->owner); if (ret.Failed()) return ret; @@ -376,10 +377,19 @@ CommandCost CmdScheduledDispatchAddNewSchedule(TileIndex tile, DoCommandFlag fla if (flags & DC_EXEC) { v->orders->GetScheduledDispatchScheduleSet().emplace_back(); DispatchSchedule &ds = v->orders->GetScheduledDispatchScheduleSet().back(); - ds.SetScheduledDispatchDuration(p2); - ds.SetScheduledDispatchStartTick((StateTicks)p3); - ds.UpdateScheduledDispatch(nullptr); - SetTimetableWindowsDirty(v, STWDF_SCHEDULED_DISPATCH); + if (scheduleJson == nullptr) { + if (p2 == 0) return CMD_ERROR; + ds.SetScheduledDispatchDuration(p2); + ds.SetScheduledDispatchStartTick((StateTicks)p3); + ds.UpdateScheduledDispatch(nullptr); + SetTimetableWindowsDirty(v, STWDF_SCHEDULED_DISPATCH); + } else { + + DispatchSchedule new_ds = DispatchSchedule::FromJSONString(scheduleJson); + ds.BorrowSchedule(new_ds); + + } + } return CommandCost(); @@ -801,3 +811,56 @@ void DispatchSchedule::UpdateScheduledDispatch(const Vehicle *v) SetTimetableWindowsDirty(v, STWDF_SCHEDULED_DISPATCH); } } + +DispatchSchedule DispatchSchedule::FromJSONString(std::string jsonString) +{ + nlohmann::json json = nlohmann::json::parse(jsonString); + + DispatchSchedule new_schedule; + + if (json.contains("slots")) { + + auto &slotsJson = json.at("slots"); + + if (slotsJson.is_array()) { + for (nlohmann::json::iterator it = slotsJson.begin(); it != slotsJson.end(); ++it) { + + DispatchSlot newDispatchSlot; + + newDispatchSlot.offset = it.value().at("offset").template get(); + newDispatchSlot.flags = it.value().at("flags").template get(); + + new_schedule.GetScheduledDispatchMutable().push_back(newDispatchSlot); + + } + } + } + + new_schedule.ScheduleName() = json.at("name").get(); + new_schedule.SetScheduledDispatchStartTick(json.at("start-tick").get()); + new_schedule.SetScheduledDispatchDuration(json.at("duration").get()); + new_schedule.SetScheduledDispatchDelay(json.at("max-delay").get()); + new_schedule.SetScheduledDispatchFlags(json.at("flags").get()); + + return new_schedule; +} + +std::string DispatchSchedule::ToJSONString() +{ + + nlohmann::json json; + + for (unsigned int i = 0; auto & SD_slot : this->GetScheduledDispatch()) { + auto &slotsJson = json["slots"][i++]; + slotsJson["offset"] = SD_slot.offset; + slotsJson["flags"] = SD_slot.flags; + } + + json["name"] = this->ScheduleName(); + json["start-tick"] = this->GetScheduledDispatchStartTick().value; + json["duration"] = this->GetScheduledDispatchDuration(); + json["max-delay"] = this->GetScheduledDispatchDelay(); + json["flags"] = this->GetScheduledDispatchFlags(); + + return json.dump(); +}