diff --git a/src/fios.cpp b/src/fios.cpp index 4e116d79ce..beee4c4fe3 100644 --- a/src/fios.cpp +++ b/src/fios.cpp @@ -518,13 +518,13 @@ void FiosGetSavegameList(SaveLoadOperation fop, bool show_dirs, FileList &file_l FiosType FiosGetOrderlistListCallback(SaveLoadOperation fop, const std::string &file, const char *ext, char *title, const char *last) { /* Show orderlist files - * .XML orderlist files + * .json orderlist files */ /* Don't crash if we supply no extension */ if (ext == nullptr) ext = ""; - if (StrEqualsIgnoreCase(ext, ".xml")) { + if (StrEqualsIgnoreCase(ext, ".json")) { GetFileTitle(file, title, last, SAVE_DIR); return FIOS_TYPE_ORDERLIST; } diff --git a/src/order_base.h b/src/order_base.h index c317b33d05..e554bf752b 100644 --- a/src/order_base.h +++ b/src/order_base.h @@ -234,8 +234,7 @@ public: void MakeLabel(OrderLabelSubType subtype); std::string ToJSONString() const; - static Order FromJSONString(std::string); - + static void FromJSONString(const Vehicle * vehicle,std::string jsonSTR); /** * Is this a 'goto' order with a real destination? @@ -978,7 +977,7 @@ public: void MoveOrder(int from, int to); std::string ToJSONString(); - static OrderList FromJSONString(std::string); + static void FromJSONString(const Vehicle* v,std::string str); /** * Is this a shared order list? diff --git a/src/order_cmd.cpp b/src/order_cmd.cpp index 31c692c26c..2f1004421f 100644 --- a/src/order_cmd.cpp +++ b/src/order_cmd.cpp @@ -277,6 +277,9 @@ std::string Order::ToJSONString() const json["packed-data"] = this->Pack(); json["destination-id"] = this->GetDestination(); + Station * station = Station::GetIfValid(this->GetDestination()); + if(station != nullptr) + json["destination-name"] = station->cached_name; if(this->extra.get() != nullptr){ auto& extraJson = json["extra"]; @@ -298,11 +301,29 @@ std::string Order::ToJSONString() const return out; } -Order Order::FromJSONString(std::string) +void Order::FromJSONString(const Vehicle * v,std::string jsonSTR) { + + /* + this->type = (OrderType)GB(packed, 0, 8); //done + this->flags = GB(packed, 8, 16); // done + this->dest = GB(packed, 24, 16); // done + this->extra = nullptr; //masive pain ?????? + this->next = nullptr; + this->refit_cargo = CARGO_NO_REFIT; + this->occupancy = 0; + this->wait_time = 0; + this->travel_time = 0; + this->max_speed = UINT16_MAX; + */ + //hell + nlohmann::json json = nlohmann::json::parse(jsonSTR); + + uint64_t order_pack = json.at("packed-data").get(); + uint64_t extra = json.at("packed-data").get(); + + DoCommandPEx(v->tile, v->index, 0, order_pack, CMD_INSERT_ORDER | CMD_MSG(STR_ERROR_CAN_T_INSERT_NEW_ORDER), nullptr, nullptr, 0); - Order newOrder; - return newOrder; } /** @@ -893,21 +914,41 @@ std::string OrderList::ToJSONString() json["orders"][i++] = nlohmann::json::parse(o->ToJSONString()); } while ((o = this->GetNext(o)) != this->GetFirstOrder()); } - - std::cout << json.dump(); + + std::cout << std::setw(4)<< json.dump() << "\n"; return json.dump(); } -OrderList OrderList::FromJSONString(std::string json_str) +void OrderList::FromJSONString(const Vehicle * v,std::string json_str) { - OrderList list; nlohmann::json json = nlohmann::json::parse(json_str); - if (json.contains("head")){ - auto &headJson = json.at("head"); + //plan... painful plan + CMD_DELETE_ORDER; //for all existing orders + CMD_INSERT_ORDER; //for each order new + + CMD_SCHEDULED_DISPATCH_ADD_NEW_SCHEDULE; + CMD_SCHEDULED_DISPATCH_ADD; + CMD_SCHEDULED_DISPATCH_RENAME_SCHEDULE; + + if (json.contains("orders")) { + auto &ordersJson = json["orders"]; + if (ordersJson.is_array()) { + for (nlohmann::json::iterator it = ordersJson.begin(); it != ordersJson.end(); ++it) { + auto &orderJson = it.value(); + Order::FromJSONString(v,orderJson.dump()); + + + } + + } + } + + if (json.contains("orders") && json.contains("head")){ + auto &headJson = json["head"]; if (headJson.contains("scheduled-dispatch")) { - auto &SDJson = headJson.at("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")) { @@ -934,8 +975,6 @@ OrderList OrderList::FromJSONString(std::string json_str) dispatchSchedule.SetScheduledDispatchDelay(it.value().at("max-delay").template get()); dispatchSchedule.SetScheduledDispatchFlags(it.value().at("flags").template get()); - list.dispatch_schedules.push_back(dispatchSchedule); - } } } @@ -943,12 +982,8 @@ OrderList OrderList::FromJSONString(std::string json_str) } - if (json.contains("orders")) { - /*Where do orders get allocated?*/ - } - return list; } /** diff --git a/src/order_gui.cpp b/src/order_gui.cpp index a06643a8b3..a0ac9c36bb 100644 --- a/src/order_gui.cpp +++ b/src/order_gui.cpp @@ -1631,7 +1631,7 @@ private: bool InsertNewOrder(uint64_t order_pack) { - return DoCommandPEx(this->vehicle->tile, this->vehicle->index, this->OrderGetSel(), order_pack, CMD_INSERT_ORDER | CMD_MSG(STR_ERROR_CAN_T_INSERT_NEW_ORDER), nullptr, nullptr, 0); + return DoCommandPEx(this->vehicle->tile, this->vehicle->index, 0, order_pack, CMD_INSERT_ORDER | CMD_MSG(STR_ERROR_CAN_T_INSERT_NEW_ORDER), nullptr, nullptr, 0); } bool ModifyOrder(VehicleOrderID sel_ord, uint32_t p2, bool error_msg = true, const char *text = nullptr) @@ -3492,73 +3492,85 @@ public: case 0: this->OrderClick_ReverseOrderList(0); break; case 1: this->OrderClick_ReverseOrderList(1); break; case 2: this->vehicle->orders->ToJSONString(); break; - case 3: this->vehicle->orders->FromJSONString(R"( - { - "head": { - "scheduled-dispatch": [ - { - "duration": 106560, - "flags": 0, - "max-delay": 0, - "name": "alfredo", - "slots": [ - { - "flags": 0, - "offset": 4514 - }, - { - "flags": 0, - "offset": 90280 - } - ], - "start-tick": 16729920 - } - ] - }, - "orders": [ - { - "destination-id": 0, - "max-speed": 65535, - "packed-data": 578, - "refit-cargo": 254, - "travel-time": 0, - "wait-time": 0 - }, - { - "destination-id": 0, - "max-speed": 65535, - "packed-data": 578, - "refit-cargo": 254, - "travel-time": 0, - "wait-time": 0 - }, - { - "destination-id": 0, - "max-speed": 65535, - "packed-data": 578, - "refit-cargo": 254, - "travel-time": 0, - "wait-time": 0 - }, - { - "destination-id": 0, - "max-speed": 65535, - "packed-data": 578, - "refit-cargo": 254, - "travel-time": 0, - "wait-time": 0 - }, - { - "destination-id": 0, - "max-speed": 65535, - "packed-data": 578, - "refit-cargo": 254, - "travel-time": 0, - "wait-time": 0 - } - ] - } - )"); break; + case 3: this->vehicle->orders->FromJSONString(this->vehicle, R"({ + "head": { + "scheduled-dispatch": [ + { + "duration": 106560, + "flags": 0, + "max-delay": 0, + "name": "alfredo", + "slots": [ + { + "flags": 0, + "offset": 4514 + }, + { + "flags": 0, + "offset": 90280 + } + ], + "start-tick": 16729920 + } + ] + }, + "orders": [ + { + "destination-id": 2, + "destination-name": "Grateley Halt", + "max-speed": 65535, + "packed-data": 33554513, + "refit-cargo": 254, + "travel-time": 0, + "wait-time": 0 + }, + { + "destination-id": 3, + "destination-name": "Grateley Exchange", + "max-speed": 65535, + "packed-data": 50331729, + "refit-cargo": 254, + "travel-time": 0, + "wait-time": 0 + }, + { + "destination-id": 4, + "destination-name": "Grateley Annexe", + "max-speed": 65535, + "packed-data": 67108945, + "refit-cargo": 254, + "travel-time": 0, + "wait-time": 0 + }, + { + "destination-id": 3, + "destination-name": "Grateley Exchange", + "max-speed": 65535, + "packed-data": 50331729, + "refit-cargo": 254, + "travel-time": 0, + "wait-time": 0 + }, + { + "destination-id": 2, + "destination-name": "Grateley Halt", + "max-speed": 65535, + "packed-data": 33554513, + "refit-cargo": 254, + "travel-time": 0, + "wait-time": 0 + }, + { + "destination-id": 1, + "destination-name": "Grateley Transfer", + "max-speed": 65535, + "packed-data": 16777297, + "refit-cargo": 254, + "travel-time": 0, + "wait-time": 0 + } + ] +})"); break; //case 2: ShowSaveLoadDialog(FT_ORDERLIST, SLO_SAVE); break; //case 3: ShowSaveLoadDialog(FT_ORDERLIST, SLO_LOAD); break;