Beispiel #1
0
 bool etcd_packer::parse_object(rapidjson::Document &doc, const char *data) {
     try {
         doc.Parse(data);
         return doc.IsObject();
     } catch (...) {
         return false;
     }
 }
bool FileSystem::ReadJSON( const char* filePath, rapidjson::Document& json )
{
	using namespace rapidjson;

	int fileSize;
	char* jsonString = ReadFile( filePath, fileSize );
	if (jsonString == NULL)	return false;

	json.Parse<0>( jsonString );
	if (!json.IsObject())	return false;

	return true;
}
int SettingRegistry::loadJSONsettingsFromDoc(rapidjson::Document& json_document, SettingsBase* settings_base, bool warn_duplicates)
{
    
    if (!json_document.IsObject())
    {
        cura::logError("JSON file is not an object.\n");
        return 3;
    }

    { // handle machine name
        std::string machine_name = "Unknown";
        if (json_document.HasMember("name"))
        {
            const rapidjson::Value& machine_name_field = json_document["name"];
            if (machine_name_field.IsString())
            {
                machine_name = machine_name_field.GetString();
            }
        }
        SettingConfig& machine_name_setting = addSetting("machine_name", "Machine Name");
        machine_name_setting.setDefault(machine_name);
        machine_name_setting.setType("string");
    }

    if (json_document.HasMember("settings"))
    {
        std::list<std::string> path;
        handleChildren(json_document["settings"], path, settings_base, warn_duplicates);
    }
    
    if (json_document.HasMember("overrides"))
    {
        const rapidjson::Value& json_object_container = json_document["overrides"];
        for (rapidjson::Value::ConstMemberIterator override_iterator = json_object_container.MemberBegin(); override_iterator != json_object_container.MemberEnd(); ++override_iterator)
        {
            std::string setting = override_iterator->name.GetString();
            SettingConfig* conf = getSettingConfig(setting);
            if (!conf) //Setting could not be found.
            {
                logWarning("Trying to override unknown setting %s.\n", setting.c_str());
                continue;
            }
            _loadSettingValues(conf, override_iterator, settings_base);
        }
    }
    
    return 0;
}
Beispiel #4
0
JSON_API void read_json(std::string const &fname, rapidjson::Document &d)
{
    FILE* fp = fopen(fname.c_str(), "rb"); // non-Windows use "r"
    if(!fp)
    {
        std::stringstream str; str << "ERROR [read_json]: file " << fname << " not found!";
        throw std::runtime_error(str.str());
    }

    fseek(fp, 0, SEEK_END);
    size_t length = static_cast<size_t>(ftell(fp));
    //std::cout << "file size = " << length << std::endl;
    fseek(fp, 0, SEEK_SET);
    char* readBuffer = static_cast<char*>(malloc(length + 1));
    size_t readLength = fread(readBuffer, 1, length, fp);
    readBuffer[readLength] = '\0';
    fclose(fp);

    d.Parse<rapidjson::kParseCommentsFlag>(readBuffer);

    if(d.HasParseError())
    {
        std::stringstream str;
        str << "ERROR [read_json]: json document cannot be parsed!" << std::endl;
        str << "\t Parse Error " << d.GetParseError() << " (" << GetParseError_En(d.GetParseError()) << ")" << std::endl;
        str << "\t Error offset = " << d.GetErrorOffset() << '\n';
        throw std::runtime_error(str.str());
    }    

    if(!d.IsObject())
    {
        std::stringstream str;
        str << "ERROR [read_json]: json document is not an object!";
        throw std::runtime_error(str.str());
    }
}
Beispiel #5
0
void MapInfo::load(const std::string& json_file)
{
  std::ifstream file(json_file);
  if (!file) {
    logger << Log::WARN << "Did not find minimap: " << json_file << Log::ENDL;
    map_json.Parse("{}");
    // disable map entirely
    this->tex = nullptr;
    this->current_map = "";
    return;
  }

  const std::string str((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
  const auto& res = map_json.Parse(str.c_str());
  DM_ASSERT(map_json.IsObject(), "Could not parse map JSON file properly!");
  if (res.HasParseError()) {
    DM_ASSERT(false, "Could not parse map JSON file properly!");
  }

  if (map_json.HasMember("filename")) {
    this->set_map(map_json["filename"].GetString());
  }

  this->mask.enabled = map_json.HasMember("mask");
  if (this->mask.enabled)
  {
    auto mask_json = map_json["mask"].GetObject();
    if (mask_json.HasMember("active")) {
      this->mask.enabled = mask_json["active"].GetBool();
    }
    if (this->mask.enabled)
    {
      this->mask.radius = mask_json["radius"].GetInt();
      this->mask.border = mask_json["border"].GetInt();
      if (this->map_changed) {
        // create new mask
        this->mask.reset(tex->getWidth(), tex->getHeight());
      }
    }
  }

  // play area is used to restrict the player icon
  // the player location is transformed from the world into this area
  if (map_json.HasMember("play_area"))
  {
    auto area = map_json["play_area"].GetArray();
    this->play_area = glm::ivec4(area[0].GetInt(), area[1].GetInt(),
                                 area[2].GetInt(), area[3].GetInt());
  }
  else {
    // use whole image if play area is not specified
    this->play_area = glm::ivec4(0, 0, tex->getWidth(), tex->getHeight());
  }

  this->gridmode = map_json.HasMember("grid");
  if (this->gridmode)
  {
    auto& grid = map_json["grid"];
    this->grid_base_coord = glm::ivec2(grid["base_x"].GetInt(),
                                       grid["base_y"].GetInt());
    this->grid_dimensions = glm::ivec2(grid["size_x"].GetInt(),
                                       grid["size_y"].GetInt());
    // grid to pixel multiplier
    const int w = this->grid_pixel_multiplier().x;
    const int h = this->grid_pixel_multiplier().y;
    logger << Log::INFO << "Map screen is a grid of " << w << "x" << h << " pixels" << Log::ENDL;
  }
  else
  {
    const int w = this->tex->getWidth();
    const int h = this->tex->getHeight();
    // not a grid
    logger << Log::INFO << "Map screen is a bitmap of size " << w << "x" << h << " pixels" << Log::ENDL;
  }
  
  this->icons.clear();
  if (map_json.HasMember("icons") == false) return;
  auto& icon_json = map_json["icons"];
  const std::string icon_file = icon_json["filename"].GetString();
  this->sprite_texid = textures.add(icon_file);
  
  for (auto& m : icon_json.GetObject())
  {
    if (std::string("icon") == m.name.GetString())
    {
      const auto icon = m.value.GetObject();
      int px = icon["x"].GetInt();
      int py = icon["y"].GetInt();
      assert(icon.HasMember("sprite") && "Icons must have a sprite object");
      const auto sprite = icon["sprite"].GetObject();
      short x = sprite["x"].GetInt();
      short y = sprite["y"].GetInt();
      short w = sprite["w"].GetInt();
      short h = sprite["h"].GetInt();
      
      const Sprite spr {x, y, w, h, sprite_texid};
      this->icons.push_back(Icon{spr, glm::ivec2(px, py)});
    }
  }
  
  /// border clouds & shadows ///
  this->border.clear();
  if (map_json.HasMember("clouds"))
  {
    assert(map_json.HasMember("shadows") && "Clouds must have a shadow array");
    auto clouds = map_json["clouds"].GetArray();
    auto shadows = map_json["shadows"].GetArray();
    
    const size_t count = std::max(clouds.Size(), shadows.Size());
    for (size_t i = 0; i < count; i++)
    {
      // load clouds & shadows separately
      std::unique_ptr<Texture> cldtex = nullptr;
      if (clouds.Size() > i) {
        cldtex = load_texture(clouds[i].GetString());
        if (cldtex == nullptr) {
          logger << Log::WARN << "Missing cloud layer: " << clouds[i].GetString() << Log::ENDL;
        }
      }
      std::unique_ptr<Texture> shdtex = nullptr;
      if (shadows.Size() > i) {
        shdtex = load_texture(shadows[i].GetString());
        if (shdtex == nullptr) {
          logger << Log::WARN << "Missing shadow layer: " << shadows[i].GetString() << Log::ENDL;
        }
      }
      border.push_back(BorderLayer{
            std::move(cldtex), std::move(shdtex), glm::mat4{}
        });
    }
    logger << Log::INFO << "Found " << count << " cloud & shadow layers" << Log::ENDL;
  }
}
int SettingRegistry::loadJSONsettingsFromDoc(rapidjson::Document& json_document, bool warn_duplicates)
{
    
    if (!json_document.IsObject())
    {
        cura::logError("JSON file is not an object.\n");
        return 3;
    }

    if (json_document.HasMember("machine_extruder_trains"))
    {
        categories.emplace_back("machine_extruder_trains", "Extruder Trains Settings Objects");
        SettingContainer* category_trains = &categories.back();
        const rapidjson::Value& trains = json_document["machine_extruder_trains"];
        if (trains.IsArray()) 
        {
            if (trains.Size() > 0 && trains[0].IsObject())
            {
                unsigned int idx = 0;
                for (auto it = trains.Begin(); it != trains.End(); ++it)
                {
                    SettingConfig* child = category_trains->addChild(std::to_string(idx), std::to_string(idx));
                    
                    for (rapidjson::Value::ConstMemberIterator setting_iterator = it->MemberBegin(); setting_iterator != it->MemberEnd(); ++setting_iterator)
                    {
                        _addSettingToContainer(child, setting_iterator, warn_duplicates, false);
                    }
                    
                    idx++;
                }
            }
        }
        else 
        {
            logError("Error: JSON machine_extruder_trains is not an array!\n");
        }
    }
    if (json_document.HasMember("machine_settings"))
    {
        categories.emplace_back("machine_settings", "Machine Settings");
        SettingContainer* category_machine_settings = &categories.back();
        
        const rapidjson::Value& json_object_container = json_document["machine_settings"];
        for (rapidjson::Value::ConstMemberIterator setting_iterator = json_object_container.MemberBegin(); setting_iterator != json_object_container.MemberEnd(); ++setting_iterator)
        {
            _addSettingToContainer(category_machine_settings, setting_iterator, warn_duplicates);
        }
    }
    
    if (json_document.HasMember("categories"))
    {
        for (rapidjson::Value::ConstMemberIterator category_iterator = json_document["categories"].MemberBegin(); category_iterator != json_document["categories"].MemberEnd(); ++category_iterator)
        {
            if (!category_iterator->value.IsObject())
            {
                continue;
            }
            if (!category_iterator->value.HasMember("label") || !category_iterator->value["label"].IsString())
            {
                continue;
            }
            if (!category_iterator->value.HasMember("settings") || !category_iterator->value["settings"].IsObject())
            {
                continue;
            }
            
            categories.emplace_back(category_iterator->name.GetString(), category_iterator->value["label"].GetString());
            SettingContainer* category = &categories.back();
            
            const rapidjson::Value& json_object_container = category_iterator->value["settings"];
            for (rapidjson::Value::ConstMemberIterator setting_iterator = json_object_container.MemberBegin(); setting_iterator != json_object_container.MemberEnd(); ++setting_iterator)
            {
                _addSettingToContainer(category, setting_iterator, warn_duplicates);
            }
        }
    }
    
    if (false && json_document.HasMember("overrides"))
    {
        const rapidjson::Value& json_object_container = json_document["overrides"];
        for (rapidjson::Value::ConstMemberIterator override_iterator = json_object_container.MemberBegin(); override_iterator != json_object_container.MemberEnd(); ++override_iterator)
        {
            SettingConfig* conf = getSettingConfig(override_iterator->name.GetString());
            _addSettingToContainer(conf, override_iterator, false);
        }
    }
    
    return 0;
}