Skip to content

Commit

Permalink
Clean up order exporting 1
Browse files Browse the repository at this point in the history
  • Loading branch information
lucaFiorini committed Feb 11, 2025
1 parent a3650bf commit 6b95938
Show file tree
Hide file tree
Showing 3 changed files with 253 additions and 73 deletions.
267 changes: 195 additions & 72 deletions src/order_cmd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
#include "command_aux.h"
#include "rev.h"
#include "schdispatch.h"
#include "order_enums_to_json.hpp"

#include "table/strings.h"

Expand Down Expand Up @@ -288,159 +289,281 @@ void Order::MakeLabel(OrderLabelSubType subtype)
std::string Order::ToJSONString() const
{
std::string out;
nlohmann::json json;
nlohmann::ordered_json json;

//TYPE NOTE TO SELF: the (raw) type is also used in conditional orders to save the operator used in comparison
//in this->GetConditionComparator() other then that, I believe I have fully decoded it

json["type"] = this->GetType();

if (
IsType(OT_GOTO_WAYPOINT) || IsType(OT_GOTO_STATION) ||
(IsType(OT_LABEL) && IsDestinationOrderLabelSubType(this->GetLabelSubType()))
) {

json["destination-id"] = this->GetDestination();

BaseStation *station = BaseStation::GetIfValid(this->GetDestination());

if (station != nullptr) {

json["destination-name"] = station->GetCachedName();

}

} else if (this->IsType(OT_GOTO_DEPOT)) {

json["depot-id"] = this->GetDestination();

json["flags"] = this->GetRawFlags(); //TEMP (need to understand depot flags)

}

json["type"] = this->type;
if (this->IsGotoOrder()) {

json["flags"] = this->GetRawFlags();
json["stopping-pattern"] = this->GetNonStopType();

json["destination-id"] = this->GetDestination();
Station * station = Station::GetIfValid(this->GetDestination());
if (station != nullptr) {
if (this->IsWaitTimetabled()) {

json["destination-name"] = station->GetCachedName();
json["wait-time"] = this->GetWaitTime();

}

if (this->IsWaitTimetabled()) {

json["travel-time"] = this->GetTravelTime();

}

if (this->GetMaxSpeed() != UINT16_MAX) {

json["max-speed"] = this->GetMaxSpeed();

}

}

if(this->extra.get() != nullptr){

auto& extraJson = json["extra"];

auto &cargo_type_flags = this->extra.get()->cargo_type_flags;
if (this->IsType(OT_GOTO_STATION)) {

auto &cargo_type_flags = this->extra.get()->cargo_type_flags;

for (int i = 0; i < NUM_CARGO; i++) {

if (cargo_type_flags[i] != 0) {

extraJson["cargo-type-flags"][std::to_string(i)] = cargo_type_flags[i];
break;

}

for (int i = 0; i < NUM_CARGO; i++) {
if (cargo_type_flags[i] != 0) {
extraJson["cargo-type-flags"][std::to_string(i)] = cargo_type_flags[i];
break;
}

}

if (this->GetColour() != -1) {

extraJson["colour"] = this->GetColour();

}


if (this->GetRoadVehTravelDirection() != INVALID_DIAGDIR) {

extraJson["roadstop-vehicle-travel-dir"] = this->GetRoadVehTravelDirection();

}

extraJson["colour"] = this->extra.get()->colour;
extraJson["dispatch-index"] = this->extra.get()->dispatch_index;
if (this->GetDispatchScheduleIndex() != -1) {

extraJson["dispatch-index"] = this->GetDispatchScheduleIndex();

}

//TMP
if (this->extra.get()->xdata != 0) {

extraJson["xdata"] = this->extra.get()->xdata;

}

//TMP
if (this->extra.get()->xdata2 != 0) {

extraJson["xdata"] = this->extra.get()->xdata2;
}

if (this->extra.get()->xflags != 0) {
extraJson["xdata"] = this->extra.get()->xflags;
}

}

json["refit-cargo"] = this->GetRefitCargo();
if (this->IsType(OT_GOTO_STATION)) {

if (this->IsAutoRefit()) {

json["refit-cargo"] = "auto";

} else if (this->IsRefit()) {

json["refit-cargo"] = this->GetRefitCargo();

}

json["stop-location"] = this->GetStopLocation();

} else if (this->IsType(OT_GOTO_WAYPOINT)) {

if (this->GetWaypointFlags() != OWF_DEFAULT) {

json["reverse"] = this->GetWaypointFlags();

json["wait-time"] = this->GetWaitTime();
}

} else if (this->IsType(OT_LABEL)) {

json["travel-time"] = this->GetTravelTime();
if (this->GetLabelSubType() != OLST_TEXT) {

json["max-speed"] = this->GetMaxSpeed();
json["label-subtype"] = this->GetLabelSubType();

} else {

json["label-text"] = this->GetLabelText();

}

}

out = json.dump();
return out;
}

Order Order::FromJSONString(std::string jsonSTR)
{

nlohmann::json json = nlohmann::json::parse(jsonSTR);

Order new_order;

if(json.contains("type") && json["type"].is_number_integer()) {
try {

if (json.contains("type") && json["type"].is_number_integer()) {

new_order.type = json["type"];
new_order.type = json["type"];

}

}
if (json.contains("flags") && json["flags"].is_number_integer()) {

if (json.contains("flags") && json["flags"].is_number_integer()) {
new_order.flags = json["flags"];

new_order.flags = json["flags"];
}

if (json.contains("destination-id") && json["destination-id"].is_number_integer()) {

}
json["destination-id"].get_to(new_order.dest);

if (json.contains("destination-id") && json["destination-id"].is_number_integer()) {
}

json["destination-id"].get_to(new_order.dest);
if (json.contains("extra") && json["extra"].is_object()) {

}

auto &extraJson = json["extra"];

if (json.contains("extra") && json["extra"].is_object()) {
new_order.AllocExtraInfo();

auto &extraJson = json["extra"];
if (extraJson.contains("cargo-type-flags") && extraJson["cargo-type-flags"].is_object()) {

new_order.AllocExtraInfo();

if (extraJson.contains("cargo-type-flags") && extraJson["cargo-type-flags"].is_object()) {
for (int i = 0; i < NUM_CARGO; i++) {

for (int i = 0; i < NUM_CARGO; i++) {
if (extraJson["cargo-type-flags"].contains(std::to_string(i))) {

if (extraJson["cargo-type-flags"].contains(std::to_string(i))) {
extraJson["cargo-type-flags"][std::to_string(i)].get_to(new_order.extra->cargo_type_flags[i]);

extraJson["cargo-type-flags"][std::to_string(i)].get_to(new_order.extra->cargo_type_flags[i]);
} else {

} else {
new_order.extra->cargo_type_flags[i] = 0;

new_order.extra->cargo_type_flags[i] = 0;
}

}

}

}
if (extraJson.contains("colour")) {

extraJson["colour"].get_to(new_order.extra->colour);

}

if (extraJson.contains("colour")) {
if (extraJson.contains("dispatch-index")) {

extraJson["colour"].get_to(new_order.extra->colour);
extraJson["dispatch-index"].get_to(new_order.extra->dispatch_index);

}

if (extraJson.contains("xdata")) {

extraJson["xdata"].get_to(new_order.extra->xdata);

}

if (extraJson.contains("xdata2")) {

extraJson["xdata2"].get_to(new_order.extra->xdata2);

}

if (extraJson.contains("xflags")) {

extraJson["xflags"].get_to(new_order.extra->xflags);

}

}

if (extraJson.contains("dispatch-index")) {
if (json.contains("refit-cargo")) {

extraJson["dispatch-index"].get_to(new_order.extra->dispatch_index);
new_order.SetRefit(json["refit-cargo"].get<CargoID>());

}

if (extraJson.contains("xdata")) {
if (json.contains("wait-time")) {

extraJson["xdata"].get_to(new_order.extra->xdata);
new_order.SetWaitTime(json["wait-time"].get<TimetableTicks>());
new_order.SetWaitTimetabled(true);

}

if (extraJson.contains("xdata2")) {
if (json.contains("travel-time")) {

extraJson["xdata2"].get_to(new_order.extra->xdata2);
new_order.SetTravelTime(json["travel-time"].get<TimetableTicks>());
new_order.SetTravelTimetabled(true);

}

if (extraJson.contains("xflags")) {
if (json.contains("max-speed")) {

extraJson["xflags"].get_to(new_order.extra->xflags);
new_order.SetMaxSpeed(json["max-speed"].get<uint16_t>());

}

}

if (json.contains("refit-cargo")) {
json["refit-cargo"].get_to(new_order.refit_cargo);
}
} catch (nlohmann::json::exception &e) {

new_order.MakeLabel(OLST_TEXT);
new_order.SetLabelText(std::string(("Error : ") + std::string(e.what())).c_str());
new_order.SetColour(COLOUR_RED);

if (json.contains("wait-time")) {
json["wait-time"].get_to(new_order.wait_time);
}
} catch (std::exception &e) {

if (json.contains("travel-time")) {
json["travel-time"].get_to(new_order.travel_time);
}
new_order.MakeLabel(OLST_TEXT);
new_order.SetLabelText(std::string(("Internal Error") + std::string(e.what())).c_str());
new_order.SetColour(COLOUR_RED);

if (json.contains("max-speed")) {
json["max-speed"].get_to(new_order.max_speed);
}

return new_order;

}

/**
Expand Down Expand Up @@ -917,10 +1040,7 @@ void OrderList::MoveOrder(VehicleOrderID from, VehicleOrderID to)
std::string OrderList::ToJSONString()
{

nlohmann::json json;

json["version"] = ORDERLIST_JSON_OUTPUT_VERSION;
json["source"] = std::string(_openttd_revision);
nlohmann::ordered_json json;

if (this == nullptr) { //order list not intiailised, return an empty result
json["error"] = "Orderlist was not initialised";
Expand All @@ -931,7 +1051,7 @@ std::string OrderList::ToJSONString()
auto& headJson = json["head"];
for (unsigned int i = 0; auto &SD : SD_data) {

headJson["scheduled-dispatch"][i++] = nlohmann::json::parse(SD.ToJSONString());
headJson["scheduled-dispatch"][i++] = nlohmann::ordered_json::parse(SD.ToJSONString());

}

Expand All @@ -940,10 +1060,13 @@ std::string OrderList::ToJSONString()
if (o != nullptr) {
int i = 0;
do {
json["orders"][i++] = nlohmann::json::parse(o->ToJSONString());
json["orders"][i++] = nlohmann::ordered_json::parse(o->ToJSONString());
} while ((o = this->GetNext(o)) != this->GetFirstOrder());
}


json["version"] = ORDERLIST_JSON_OUTPUT_VERSION;
json["source"] = std::string(_openttd_revision);

return json.dump(4);

}
Expand Down
Loading

1 comment on commit 6b95938

@lucaFiorini
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This commit breaks importing.
Conditional orders still need to be parsed

Please sign in to comment.