Skip to content

Commit

Permalink
load/inherit access resolver from json
Browse files Browse the repository at this point in the history
  • Loading branch information
black-sliver committed Dec 31, 2023
1 parent 965c294 commit 2f49f2d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
15 changes: 10 additions & 5 deletions src/core/location.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,14 +113,14 @@ static bool parseRule(const json& v, std::list<std::string>& rule,
}


std::list<Location> Location::FromJSON(json& j, const std::list<Location>& parentLookup, const std::list< std::list<std::string> >& prevAccessRules, const std::list< std::list<std::string> >& prevVisibilityRules, const std::string& parentName, const std::string& closedImgR, const std::string& openedImgR, const std::string& overlayBackgroundR)
std::list<Location> Location::FromJSON(json& j, const std::list<Location>& parentLookup, const std::string& prevAccessResolver, const std::list< std::list<std::string> >& prevAccessRules, const std::list< std::list<std::string> >& prevVisibilityRules, const std::string& parentName, const std::string& closedImgR, const std::string& openedImgR, const std::string& overlayBackgroundR)
{
// TODO: sine we store all intermediate locations now, we could pass a parent to FromJSON instead of all arguments
std::list<Location> locs;

if (j.type() == json::value_t::array) {
for (auto& v : j) {
for (auto& loc : FromJSON(v, parentLookup, prevAccessRules, prevVisibilityRules, parentName, closedImgR, openedImgR, overlayBackgroundR)) {
for (auto& loc : FromJSON(v, parentLookup, prevAccessResolver, prevAccessRules, prevVisibilityRules, parentName, closedImgR, openedImgR, overlayBackgroundR)) {
locs.push_back(std::move(loc)); // TODO: move constructor
}
}
Expand Down Expand Up @@ -159,9 +159,12 @@ std::list<Location> Location::FromJSON(json& j, const std::list<Location>& paren
}
}

const auto& parentAccesResolver = parentLocation ? parentLocation->getAccessResolver() : prevAccessResolver;
const auto& parentAccessRules = parentLocation ? parentLocation->getAccessRules() : prevAccessRules;
const auto& parentVisibilityRules = parentLocation ? parentLocation->getVisibilityRules() : prevVisibilityRules;

std::string accessResolver = to_string(j, "access_resolver", parentAccesResolver);

std::list< std::list<std::string> > accessRules;
if (j["access_rules"].is_array() && !j["access_rules"].empty()) {
// TODO: merge code with Section's access rules
Expand Down Expand Up @@ -220,6 +223,7 @@ std::list<Location> Location::FromJSON(json& j, const std::list<Location>& paren
loc._name = name;
loc._parentName = parentName;
loc._id = loc._parentName.empty() ? loc._name : (loc._parentName + "/" + loc._name);
loc._accessResolver = accessResolver;
loc._accessRules = accessRules;
loc._visibilityRules = visibilityRules;
if (j["map_locations"].is_array()) {
Expand All @@ -239,7 +243,7 @@ std::list<Location> Location::FromJSON(json& j, const std::list<Location>& paren
fprintf(stderr, "Location: bad section\n");
continue;
}
loc._sections.push_back(LocationSection::FromJSON(v, loc._id, accessRules, visibilityRules, closedImg, openedImg, overlayBackground));
loc._sections.push_back(LocationSection::FromJSON(v, loc._id, accessResolver, accessRules, visibilityRules, closedImg, openedImg, overlayBackground));
}
} else if (!j["sections"].is_null()) {
fprintf(stderr, "Location: invalid sections\n");
Expand All @@ -249,7 +253,7 @@ std::list<Location> Location::FromJSON(json& j, const std::list<Location>& paren

if (j["children"].type() == json::value_t::array) {
std::string fullname = parentName.empty() ? name : (parentName + "/" + name);
for (auto& loc : Location::FromJSON(j["children"], parentLookup, accessRules, visibilityRules, fullname, closedImg, openedImg, overlayBackground)) {
for (auto& loc : Location::FromJSON(j["children"], parentLookup, accessResolver, accessRules, visibilityRules, fullname, closedImg, openedImg, overlayBackground)) {
locs.push_back(std::move(loc));
}
} else if (j["children"].type() != json::value_t::null) {
Expand Down Expand Up @@ -300,7 +304,7 @@ Location::MapLocation Location::MapLocation::FromJSON(json& j)
}


LocationSection LocationSection::FromJSON(json& j, const std::string parentId, const std::list< std::list<std::string> >& parentAccessRules, const std::list< std::list<std::string> >& parentVisibilityRules, const std::string& closedImg, const std::string& openedImg, const std::string& overlayBackground)
LocationSection LocationSection::FromJSON(json& j, const std::string parentId, const std::string& parentAccessResolver, const std::list< std::list<std::string> >& parentAccessRules, const std::list< std::list<std::string> >& parentVisibilityRules, const std::string& closedImg, const std::string& openedImg, const std::string& overlayBackground)
{
// TODO: pass inherited values as parent instead
LocationSection sec;
Expand All @@ -316,6 +320,7 @@ LocationSection LocationSection::FromJSON(json& j, const std::string parentId, c
sec._itemCount = sec._hostedItems.empty() && sec._ref.empty() ? 1 : 0;
sec._itemCount = to_int(j["item_count"], sec._itemCount);
bool nonEmpty = sec._itemCount > 0 || !sec._hostedItems.empty();
sec._accessResolver = to_string(j, "access_resolver", parentAccessResolver);

if (j["access_rules"].is_array() && !j["access_rules"].empty()) {
// TODO: merge code with Location's access rules
Expand Down
6 changes: 6 additions & 0 deletions src/core/location.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class LocationSection final : public LuaInterface<LocationSection> {
public:
static LocationSection FromJSON(nlohmann::json& j,
const std::string parentId,
const std::string& parentAccessResolver="",
const std::list< std::list<std::string> >& parentAccessRules={},
const std::list< std::list<std::string> >& parentVisibilityRules={},
const std::string& closedImg="", const std::string& openedImg="",
Expand All @@ -37,13 +38,15 @@ class LocationSection final : public LuaInterface<LocationSection> {
int _itemCount=0;
int _itemCleared=0;
std::list<std::string> _hostedItems;
std::string _accessResolver;
std::list< std::list<std::string> > _accessRules;
std::list< std::list<std::string> > _visibilityRules;
std::string _overlayBackground;
std::string _ref; // path to actual section if it's just a reference
public:
// getters
const std::string& getName() const { return _name; }
const std::string& getAccessResolver() const { return _accessResolver; }
const std::list< std::list<std::string> > getAccessRules() const { return _accessRules; }
const std::list< std::list<std::string> > getVisibilityRules() const { return _visibilityRules; }
int getItemCount() const { return _itemCount; }
Expand Down Expand Up @@ -95,6 +98,7 @@ class Location final : public LuaInterface<Location> {

static std::list<Location> FromJSON(nlohmann::json& j,
const std::list<Location>& parentLookup,
const std::string& parentAccessResolver="",
const std::list< std::list<std::string> >& parentAccessRules={},
const std::list< std::list<std::string> >& parentVisibilityRules={},
const std::string& parentName="", const std::string& closedImg="",
Expand All @@ -106,6 +110,7 @@ class Location final : public LuaInterface<Location> {
std::string _id;
std::list<MapLocation> _mapLocations;
std::list<LocationSection> _sections;
std::string _accessResolver; // this is only used if referenced through @-RUles
std::list< std::list<std::string> > _accessRules; // this is only used if referenced through @-Rules
std::list< std::list<std::string> > _visibilityRules;
public:
Expand All @@ -115,6 +120,7 @@ class Location final : public LuaInterface<Location> {
const std::list<MapLocation>& getMapLocations() const { return _mapLocations; }
std::list<LocationSection>& getSections() { return _sections; }
const std::list<LocationSection>& getSections() const { return _sections; }
const std::string& getAccessResolver() const { return _accessResolver; }
std::list< std::list<std::string> >& getAccessRules() { return _accessRules; }
const std::list< std::list<std::string> >& getAccessRules() const { return _accessRules; }
std::list< std::list<std::string> >& getVisibilityRules() { return _visibilityRules; }
Expand Down

0 comments on commit 2f49f2d

Please sign in to comment.