full import working

pull/678/head
lucaFiorini 2 months ago
parent 3f678ade03
commit 26fff27e50

@ -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

@ -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<uint32_t>();
newDispatchSlot.flags = it.value().at("flags").template get<uint16_t>();
dispatchSchedule.GetScheduledDispatchMutable().push_back(newDispatchSlot);
}
}
dispatchSchedule.ScheduleName() = it.value().at("name").template get<std::string>();
dispatchSchedule.SetScheduledDispatchStartTick(it.value().at("start-tick").template get<uint32_t>());
dispatchSchedule.SetScheduledDispatchDuration(it.value().at("duration").template get<uint32_t>());
dispatchSchedule.SetScheduledDispatchDelay(it.value().at("max-delay").template get<uint32_t>());
dispatchSchedule.SetScheduledDispatchFlags(it.value().at("flags").template get<uint8_t>());
}
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);
}
}
}
}
}
/**

@ -20,6 +20,7 @@
#include "settings_type.h"
#include "schdispatch.h"
#include "vehicle_gui.h"
#include <3rdparty/nlohmann/json.hpp>
#include <algorithm>
@ -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<uint32_t>();
newDispatchSlot.flags = it.value().at("flags").template get<uint16_t>();
new_schedule.GetScheduledDispatchMutable().push_back(newDispatchSlot);
}
}
}
new_schedule.ScheduleName() = json.at("name").get<std::string>();
new_schedule.SetScheduledDispatchStartTick(json.at("start-tick").get<uint32_t>());
new_schedule.SetScheduledDispatchDuration(json.at("duration").get<uint32_t>());
new_schedule.SetScheduledDispatchDelay(json.at("max-delay").get<uint32_t>());
new_schedule.SetScheduledDispatchFlags(json.at("flags").get<uint8_t>());
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();
}

Loading…
Cancel
Save