Esempio n. 1
0
void EntityCollection::handleEntityStateChange(json11::Json entity) {
    if (!entity["id"].is_number()) {
        return;
    }
    if (!entity["component"].is_string()) {
        return;
    }
    if (!entity["field"].is_string()) {
        return;
    }

    unsigned int id = entity["id"].int_value();
    std::string component = entity["component"].string_value();
    std::string field = entity["field"].string_value();
    json11::Json value = entity["value"];
    auto ent_it =
        std::find_if(m_entities.begin(), m_entities.end(),
                     [=](Entity & entity) { return entity.getID() == id; });
    Entity * ent = nullptr;
    if (ent_it == m_entities.end()) {
        ent = &createEntity(id);
    } else {
        ent = &(*ent_it);
    }
    if ((*ent)[component] == nullptr || !(*ent).hasComponent(component)) {
        if (m_component_types.count(component) == 0) {
            // We don't know what type of component this is, just give up
            return;
        }
        ent->addComponent(m_component_types[component]());
    }
    (*ent)[component]->setField(field, entity["value"]);
}
Esempio n. 2
0
Scene Scene::load(const std::string &filename, const json11::Json &settings) {
    std::ifstream is(filename);
    std::stringstream ss;
    ss << is.rdbuf();

    std::string err;
    Json jsonRoot = Json::parse(ss.str(), err);
    if (jsonRoot.is_null()) {
        throw Exception("Failed to load scene from '%s' (error: %s)", filename, err);
    }

    _resolver = filesystem::resolver();
    _resolver.prepend(filesystem::path(filename).parent_path());

    Scene scene;

    // Patch settings
    auto settingsValues = jsonRoot["settings"].object_items();
    for (auto kv : settings.object_items()) {
        settingsValues[kv.first] = kv.second;
    }
    scene.settings = Properties(json11::Json(settingsValues));

    // Parse scene objects
    Json jsonScene = jsonRoot["scene"];
    if (jsonScene.is_object()) {
        auto jsonCamera = jsonScene["camera"];
        if (jsonCamera.is_object()) {
            Properties props(jsonCamera);
            scene.camera = Camera(jsonCamera);
        }
        auto jsonWorld = jsonScene["world"];
        if (jsonWorld.is_object()) {
            Properties props(jsonWorld);
            scene.world = World(jsonWorld);
        }
        for (auto jsonBox : jsonScene["boxes"].array_items()) {
            scene.boxes.emplace_back(Box(Properties(jsonBox)));
        }
        for (auto jsonSphere : jsonScene["spheres"].array_items()) {
            scene.spheres.emplace_back(Sphere(Properties(jsonSphere)));
        }
        for (auto jsonMesh : jsonScene["meshes"].array_items()) {
            scene.meshes.emplace_back(Mesh(Properties(jsonMesh)));
        }
        for (auto jsonCameraKeyframe : jsonScene["cameraKeyframes"].array_items()) {
            scene.cameraKeyframes.emplace_back(Camera(Properties(jsonCameraKeyframe)));
        }
        // Set default camera
        if (!jsonCamera.is_object()) {
            Vector3f center = scene.world.bounds.center();
            scene.camera.position += center;
            scene.camera.target += center;
        }
    }

    return scene;
}
Esempio n. 3
0
 void fromJsonSub(const json11::Json& json) {
     comment = json["comment"].string_value();
     content = json["*"].string_value();
     contentformat = json["contentformat"].string_value();
     contentmodel = json["contentmodel"].string_value();
     if(json.object_items().find("minor") != json.object_items().end()) minor = 1;
     parentid = json["parentid"].int_value();
     parsedcomment = json["parsedcomment"].string_value();
     parsetree = json["parsetree"].string_value();
     revid = json["revid"].int_value();
     size = json["size"].int_value();
     sha1 = json["sha1"].string_value();
     timestamp = json["timestamp"].string_value();
     auto tagsJson = json["tags"].array_items();
     for(auto it : tagsJson) tags.push_back(it.string_value());
     user = json["user"].string_value();
     userid = json["userid"].int_value();
     if(json.object_items().find("anon") != json.object_items().end()) anon = 1;
 }
Esempio n. 4
0
  static bool InitFromJson(Peer& peer, const json11::Json& json, std::string& err) {
    if (!json.is_string()) {
      err = "Expected a JSON string object as input";
      return false;
    }

    // WTF do you do when the server returns an empty string?  The cluster is
    // broken or split-brained.  Quorum isn't available for the agent.  Set
    // the peer's leader state to false and move on, but return true.  We
    // successfully parsed nothing, meaning no leader, but no error in the
    // API call.
    if (json.string_value().size() == 0) {
      peer.leader = false;
      return true;
    }

    std::vector<std::string> toks;
    boost::split(toks, json.string_value(), boost::is_any_of(":"), boost::token_compress_on);
    if (toks.size() != 2) {
      std::ostringstream errMsg;
      errMsg << "Expected a host:port pattern from string \"" << json.string_value() << "\"";
      err = errMsg.str();
      return false;
    }

    if (!peer.setHost(toks[0])) {
      std::ostringstream errMsg;
      errMsg << "Failed to set host: " << toks[0];
      err = errMsg.str();
      return false;
    }

    if (!peer.setPort(toks[1])) {
      std::ostringstream errMsg;
      errMsg << "Failed to set port: " << toks[1];
      err = errMsg.str();
      return false;
    }

    return true;
  }
Esempio n. 5
0
  void fromJsonSub(const json11::Json& json) {
   //cout << "[[Revisions::fromJson]]..." << endl;
   batchcomplete = json["batchcomplete"].string_value();
   continue_res.clear();
   continue_2_res.clear();
   if(json.object_items().find("continue") != json.object_items().end()) {
    auto continueJson = json["continue"].object_items();
    if(continueJson.find("rvcontinue") != continueJson.end()) continue_res = continueJson["rvcontinue"].string_value();
    else if(continueJson.find("arvcontinue") != continueJson.end()) continue_res = continueJson["arvcontinue"].string_value();
    continue_2_res = continueJson["continue"].string_value();
   }
   //cout << "[[Revisions::fromJson]] continue_res: " << continue_res << endl;
   auto queryJson = json["query"].object_items();
   if(queryJson.find("normalized") != queryJson.end()) {
    auto normalizedJson = queryJson["normalized"].array_items();
    for(auto inor : normalizedJson) {
     string from = inor["from"].string_value();
     if(pagesNormalizedTitles.find(from) != pagesNormalizedTitles.end()) continue;
     string to = inor["to"].string_value();
     pagesNormalizedTitles[from] = to;
    }
   }

   map<string, json11::Json> pagesJsonObjects = queryJson["pages"].object_items();
   //cout << "[[Revisions::fromJson]] pagesJsonObjects.size(): " << pagesJsonObjects.size() << endl;
   for(auto ipro : pagesJsonObjects) {
    if(pagesById.find(stol(ipro.first)) == pagesById.end()) {
     //Page page(ipro.second);
     Page page;
     page.fromJson(ipro.second);
     pages.push_back(page);
     //cout << "[[Revisions::fromJson]] pages.size(): " << pages.size() << endl;
     //Page* pagePointer = &pages[pages.size()-1];
     //cout << "[[Revisions::fromJson]] page.pageid: " << page.pageid << endl;
     //pagesById[page.pageid] = pagePointer;
     //cout << "[[Revisions::fromJson]] page.title: " << page.title << endl;
     //pagesByTitle[page.title] = pagePointer;
     pagesById[page.pageid] = pages.size()-1;
     pagesByTitle[page.title] = pages.size()-1;
     /*
     pagesById[page.pageid] = page;
     pagesByTitle[page.title] = page;
     */
    } else {
     //cout << "[[Revisions::fromJson]] stol(ipro.first): " << stol(ipro.first) << endl;
     cout << "[[Revisions::fromJson]] pagesById[stol(ipro.first)]: " << pagesById[stol(ipro.first)] << endl;
     //pagesById[stol(ipro.first)]->fromJson(ipro.second);
     //pagesById[stol(ipro.first)].fromJson(ipro.second);
     pages[pagesById[stol(ipro.first)]].fromJson(ipro.second);
    }
   }

   vector<json11::Json> pagesJsonArray = queryJson["allrevisions"].array_items();;
   //cout << "[[Revisions::fromJson]] pagesJsonArray.size(): " << pagesJsonArray.size() << endl;
   for(auto ipra : pagesJsonArray) {
    //Page page(ipra);
    Page page;
    page.fromJson(ipra);
    //cout << "[[Revisions::fromJson]] page.pageid: " << page.pageid << endl;
    if(pagesById.find(page.pageid) == pagesById.end()) {
     pages.push_back(page);
     //cout << "[[Revisions::fromJson]] pages.size(): " << pages.size() << endl;
     //Page* pagePointer = &pages[pages.size()-1];
     //cout << "[[Revisions::fromJson]] pagePointer: " << pagePointer << endl;
     //cout << "[[Revisions::fromJson]] page.pageid (new): " << page.pageid << endl;
     //pagesById[page.pageid] = pagePointer;
     //cout << "[[Revisions::fromJson]] page.title: " << page.title << endl;
     //pagesByTitle[page.title] = pagePointer;
     pagesById[page.pageid] = pages.size()-1;
     pagesByTitle[page.title] = pages.size()-1;
     /*
     pagesById[page.pageid] = page;
     pagesByTitle[page.title] = page;
     */
    } else {
     //cout << "[[Revisions::fromJson]] page.pageid (old): " << page.pageid << endl;
     //Page* pagePointer = pagesById[page.pageid];
     //cout << "[[Revisions::fromJson]] pagesById[page.pageid]: pagePointer" << pagePointer << endl;
     //pagePointer->fromJson(ipra);
     pages[pagesById[page.pageid]].fromJson(ipra);
     //pagesById[page.pageid]->fromJson(ipra);
     //pagesById[pageRevisions.pageid].fromJson(ipra);
    }
   }

   //cout << "[[Revisions::fromJson]] pages.size(): " << pages.size() << endl;
   revisions.clear();
   for(Page prs : pages) {
    for(Revision r : prs.revisions) {
     revisions.push_back(r);
    }
   }

   //cout << "[[Revisions::fromJson]] revisions.size(): " << revisions.size() << endl;
   if(dir.compare("older") == 0) sort(revisions.begin( ), revisions.end( ), [] (const Revision& lhs, const Revision& rhs) {return lhs.revid < rhs.revid;});
   else sort(revisions.begin( ), revisions.end( ), [] (const Revision& lhs, const Revision& rhs) {return lhs.revid > rhs.revid;});   
  }
Esempio n. 6
0
void cd_user::send2(json11::Json payload) {
  send(payload.dump());
}