Skip to content

Commit

Permalink
Re-add missing fields on order import
Browse files Browse the repository at this point in the history
  • Loading branch information
lucaFiorini committed Feb 19, 2025
1 parent 009bd5b commit d0780df
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 9 deletions.
64 changes: 55 additions & 9 deletions src/order_cmd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,7 @@ std::string Order::ToJSONString() const

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

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

}

Expand Down Expand Up @@ -492,23 +492,23 @@ Order Order::FromJSONString(std::string jsonSTR)

DestinationID destination = INVALID_STATION;

OrderLabelSubType subtype = OLST_TEXT;
OrderLabelSubType labelSubtype = OLST_TEXT;

//Get basic order data required to build order
switch (type) {
case OT_LABEL:
if (json.contains("label-subtype")) {
try {
subtype = (OrderLabelSubType)json["label-subtype"];
if (subtype == OLST_END) {
labelSubtype = (OrderLabelSubType)json["label-subtype"];
if (labelSubtype == OLST_END) {
return makeJsonErrorOrder("Value of 'label-subtype' is invalid");
}
} catch (...) {
return makeJsonErrorOrder("Data type of 'label-subtype' is invalid");
}
}

if(subtype != OLST_DEPARTURES_REMOVE_VIA && subtype != OLST_DEPARTURES_VIA) break;
if(labelSubtype != OLST_DEPARTURES_REMOVE_VIA && labelSubtype != OLST_DEPARTURES_VIA) break;

//fall through if label has destination
[[fallthrough]];
Expand Down Expand Up @@ -560,7 +560,7 @@ Order Order::FromJSONString(std::string jsonSTR)
case OT_GOTO_DEPOT: new_order.MakeGoToDepot(destination, ODTFB_PART_OF_ORDERS); break;
case OT_IMPLICIT: new_order.MakeImplicit(destination); break;
case OT_LABEL:
new_order.MakeLabel(subtype);
new_order.MakeLabel(labelSubtype);
if (new_order.GetLabelSubType() != OLST_TEXT) {
new_order.SetDestination(destination);
}
Expand Down Expand Up @@ -725,11 +725,57 @@ Order Order::FromJSONString(std::string jsonSTR)
}

if (json.contains("roadstop-travel-dir")) {
if (json["roadstop-trvel-dir"].is_number_integer()) {
new_order.SetRoadVehTravelDirection(json["roadstop-trvel-dir"]);
try {
DiagDirection dir = json["roadstop-trvel-dir"];
if (dir == INVALID_DIAGDIR) {
new_order.SetRoadVehTravelDirection(dir);
} else {
return makeJsonErrorOrder("Value of 'roadstop-travel-dir' is invalid");
}
} catch (...) {
return makeJsonErrorOrder("Type of 'roadstop-travel-dir' is invalid");
}
}

}

if (new_order.IsType(OT_GOTO_WAYPOINT) && json.contains("waypoint-action")) {
try {
new_order.SetWaypointFlags((OrderWaypointFlags)json["waypoint-action"]);
} catch (...) {
return makeJsonErrorOrder("Type of 'waypoint-action' is invalid");
}
}

if (new_order.IsType(OT_LABEL) && new_order.GetLabelSubType() == OLST_TEXT && json.contains("label-text")) {
if(json["label-text"].is_string()){
new_order.SetLabelText(((std::string)json["label-text"]).c_str());
} else {
return makeJsonErrorOrder("Type of 'waypoint-action' is invalid");
}
}

if (json.contains("colour")) {
try {
if ((Colours)json["colour"] != INVALID_COLOUR) {
new_order.SetColour((Colours)json["colour"]);
} else {
return makeJsonErrorOrder("Type of 'refit-cargo' is invalid");
return makeJsonErrorOrder("Value of 'colour' is invalid");
}
} catch (...) {
return makeJsonErrorOrder("Type of 'colour' is invalid");
}
}

if (json.contains("dispatch-index")) {
if(json["dispatch-index"].is_number_integer()){
if (json["dispatch-index"] >= 0) {
new_order.SetDispatchScheduleIndex(json["dispatch-index"]);
} else {
return makeJsonErrorOrder("Value of 'dispatch-index' is invalid");
}
} else {
return makeJsonErrorOrder("Type of 'dispatch-index' is invalid");
}
}

Expand Down
30 changes: 30 additions & 0 deletions src/order_enums_to_json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
#define ORDER_ENUMS_TO_JSON

#include "order_type.h"
#include "direction_type.h"
#include "gfx_type.h"

#include "3rdparty/nlohmann/json.hpp"

Expand Down Expand Up @@ -83,4 +85,32 @@ NLOHMANN_JSON_SERIALIZE_ENUM(OrderUnloadFlags, {
{OUFB_NO_UNLOAD,"no-unload"},
})

NLOHMANN_JSON_SERIALIZE_ENUM(DiagDirection, {
{INVALID_DIAGDIR,nullptr},
{DIAGDIR_NE,"north-east"},
{DIAGDIR_SE,"south-east"},
{DIAGDIR_NW,"north-west"},
{DIAGDIR_SW,"south-west"}
})

NLOHMANN_JSON_SERIALIZE_ENUM( Colours,{
{INVALID_COLOUR, nullptr},
{COLOUR_DARK_BLUE,"dark-blue"},
{COLOUR_PALE_GREEN,"pale-green"},
{COLOUR_PINK,"pink"},
{COLOUR_YELLOW,"yellow"},
{COLOUR_RED,"red"},
{COLOUR_LIGHT_BLUE,"light-blue"},
{COLOUR_GREEN,"green"},
{COLOUR_DARK_GREEN,"dark-green"},
{COLOUR_BLUE,"blue"},
{COLOUR_CREAM,"cream"},
{COLOUR_MAUVE,"mauve"},
{COLOUR_PURPLE,"purple"},
{COLOUR_ORANGE,"orange"},
{COLOUR_BROWN,"brown"},
{COLOUR_GREY,"grey"},
{COLOUR_WHITE,"white"}
})

#endif

0 comments on commit d0780df

Please sign in to comment.