void Config::loadFile(const string& path) { XMLDocument document; if (document.LoadFile(path.c_str()) == XML_NO_ERROR) { XMLElement* file = document.FirstChildElement("file"); if (file) { XMLElement* config = file->FirstChildElement("config"); while (config) { const char* name = config->Attribute("name"); if (name) { if (strcmp(name, "DefaultGravity") == 0) config->QueryDoubleAttribute("value", &Config::DefaultGravity); else if (strcmp(name, "PlayerMoveSpeed") == 0) config->QueryDoubleAttribute("value", &Config::PlayerMoveSpeed); else if (strcmp(name, "PlayerJumpSpeed") == 0) config->QueryDoubleAttribute("value", &Config::PlayerJumpSpeed); else if (strcmp(name, "MaxCrosshairDistance") == 0) config->QueryDoubleAttribute("value", &Config::MaxCrosshairDistance); else if (strcmp(name, "MaxGibsOnScreen") == 0) config->QueryUnsignedAttribute("value", &Config::MaxGibsOnScreen); else if (strcmp(name, "GibsMin") == 0) config->QueryIntAttribute("value", &Config::GibsMin); else if (strcmp(name, "GibsMax") == 0) config->QueryIntAttribute("value", &Config::GibsMax); else if (strcmp(name, "BloodParticles") == 0) config->QueryIntAttribute("value", &Config::BloodParticles); else if (strcmp(name, "GibMaxSpeedX") == 0) config->QueryIntAttribute("value", &Config::GibMaxSpeedX); else if (strcmp(name, "GibMaxSpeedY") == 0) config->QueryIntAttribute("value", &Config::GibMaxSpeedY); else if (strcmp(name, "BloodMaxSpeedX") == 0) config->QueryIntAttribute("value", &Config::BloodMaxSpeedX); else if (strcmp(name, "BloodMaxSpeedY") == 0) config->QueryIntAttribute("value", &Config::BloodMaxSpeedY); } config = config->NextSiblingElement("config"); } } } else { cout << document.GetErrorStr1() << endl; cout << document.GetErrorStr2() << endl; } }
bool FractalConfiguration::readFractalID(string filename, string *id, string *error) { XMLDocument doc; // Parse file doc.LoadFile(filename.c_str()); if(doc.Error()) { *error = doc.GetErrorStr1(); return true; } // Get ID XMLElement *root = doc.RootElement(); string name = string(root->Name()); if(name != "fractal") { *error = "Configuration file is invalid!"; return true; } const char* id_tmp = root->Attribute("id"); if(id_tmp == NULL) { *error = "Configuration file is invalid!"; return true; } *id = string(id_tmp); return false; }
// Loads the campaign data int _Campaign::Init() { Campaigns.clear(); Log.Write("_Campaign::Init - Loading file irrlamb.xml"); // Open the XML file std::string LevelFile = std::string("levels/main.xml"); XMLDocument Document; if(Document.LoadFile(LevelFile.c_str()) != XML_NO_ERROR) { Log.Write("Error loading level file with error id = %d", Document.ErrorID()); Log.Write("Error string 1: %s", Document.GetErrorStr1()); Log.Write("Error string 2: %s", Document.GetErrorStr2()); Close(); return 0; } // Check for level tag XMLElement *CampaignsElement = Document.FirstChildElement("campaigns"); if(!CampaignsElement) { Log.Write("Could not find campaigns tag"); return 0; } // Load campaigns XMLElement *CampaignElement = CampaignsElement->FirstChildElement("campaign"); for(; CampaignElement != 0; CampaignElement = CampaignElement->NextSiblingElement("campaign")) { CampaignStruct Campaign; Campaign.Name = CampaignElement->Attribute("name"); // Get levels XMLElement *LevelElement = CampaignElement->FirstChildElement("level"); for(; LevelElement != 0; LevelElement = LevelElement->NextSiblingElement("level")) { LevelStruct Level; Level.File = LevelElement->GetText(); Level.DataPath = Game.GetWorkingPath() + "levels/" + Level.File + "/"; Level.Unlocked = 0; LevelElement->QueryIntAttribute("unlocked", &Level.Unlocked); ::Level.Init(Level.File, true); Level.NiceName = ::Level.GetLevelNiceName(); Campaign.Levels.push_back(Level); } Campaigns.push_back(Campaign); } return 1; }
void ImageMapWorldCreator::readXML(MapDirectory* mapDirectory, Engine* engine, SDL_Surface* surface, World* world) { XMLDocument document; if (document.LoadFile(mapDirectory->getXMLPath().c_str()) == XML_NO_ERROR) { XMLElement* mapElement = document.FirstChildElement("map"); if (mapElement) { XMLElement* backgroundsElement = mapElement->FirstChildElement("backgrounds"); XMLElement* texturesElement = mapElement->FirstChildElement("textures"); if (texturesElement) { readTexturesElement(texturesElement, surface); } if (backgroundsElement) { readBackgroundsElement(backgroundsElement, engine, mapDirectory, world); } } } else { cout << document.GetErrorStr1() << endl; cout << document.GetErrorStr2() << endl; } }
void load_items() { static const std::string itemDatabase = config::res_path("Items.xml"); XMLDocument doc; if (doc.LoadFile(itemDatabase.c_str()) != 0) { TRACE("Unable to open item database %s (%s)!", itemDatabase.c_str(), doc.GetErrorStr1()); throw std::runtime_error("Unable to open item database " + itemDatabase); } const XMLElement* root = doc.FirstChildElement("items"); for (const XMLElement* element = root->FirstChildElement(); element; element = element->NextSiblingElement()) { Item item = parse_item_element(element); TRACE("Loaded new item %s", item.name.c_str()); itemDefinitions.push_back(item); } }
void load_status_effects() { static const std::string database = config::res_path("StatusEffects.xml"); XMLDocument doc; if (doc.LoadFile(database.c_str()) != 0) { TRACE("Unable to open status database %s (%s)!", database.c_str(), doc.GetErrorStr1()); throw std::runtime_error("Unable to open status database " + database); } const XMLElement* root = doc.FirstChildElement("statusEffects"); for (const XMLElement* element = root->FirstChildElement(); element; element = element->NextSiblingElement()) { StatusEffect status = parse_status_effect_element(element); TRACE("New statusEffect %s loaded.", status.name.c_str()); statusEffects.push_back(status); } }
void Game::LoadMap( const std::string & filename ) { XMLDocument doc; doc.LoadFile(filename.c_str()); if ( doc.ErrorID() != XML_NO_ERROR ) { ostringstream ss; ss << "Loadfile:" << g_TinyXmlErrors[doc.ErrorID()] << " " << doc.GetErrorStr1() << " " << doc.GetErrorStr2(); throw XmlParsingException(ss.str()); } // Load rooms XMLElement *pElem = doc.FirstChildElement( "Room"); while( pElem != NULL ) { g_Log << "loading..." << pElem->Attribute("id"); Room *pRoom = NULL; // Custom rooms if ( pElem->Attribute("class")) { std::string classID = pElem->Attribute("class"); pRoom = RoomFactory::Create(classID); } else { pRoom = new Room(); } pRoom->Load( *pElem ); if ( m_Rooms.find(pRoom->GetId()) != m_Rooms.end()) { throw DuplicateRoomIdException(pRoom->GetId()); } m_Rooms[pRoom->GetId()] = pRoom; pElem = pElem->NextSiblingElement("Room"); } // Load transitions pElem = doc.FirstChildElement( "Transitions"); if ( pElem != NULL ) { pElem = pElem->FirstChildElement("Transition"); while( pElem != NULL ) { const char * from = pElem->Attribute("from"); const char * to = pElem->Attribute("to"); const char * direction = pElem->Attribute("direction"); const char * oneway = pElem->Attribute("oneway"); if ( direction == NULL ) throw AttributeMissingException("Direction is missing from transition"); if ( from == NULL ) throw AttributeMissingException("From attribute is missing from transition!"); if ( to == NULL ) throw AttributeMissingException("To attribute is missing from transition!"); // check is from a proper id if ( m_Rooms.find(from) == m_Rooms.end() ) { ostringstream ss; ss << from << " is not a proper room id"; throw InvalidAttributeException(ss.str()); } // check is to a proper id if ( m_Rooms.find(to) == m_Rooms.end() ) { ostringstream ss; ss << to << " is not a proper room id"; throw InvalidAttributeException(ss.str()); } string tmp = direction; transform(tmp.begin(),tmp.end(), tmp.begin(), [] (char c){ return tolower(c);}); Direction dir = kNumDirs; if ( tmp == "east" ) dir = East; else if ( tmp == "north" ) dir = North; else if ( tmp == "south" ) dir = South; else if ( tmp == "west" ) dir = West; else throw InvalidAttributeException("Direction is not properly set"); m_Rooms[from]->SetNextRoom(dir, m_Rooms[to]); if ( !oneway ) { m_Rooms[to]->SetNextRoom(g_Opposite[dir], m_Rooms[from]); } pElem = pElem->NextSiblingElement("Transition"); } } else { throw ElementMissingException("Transitions is missing!"); } // set current room pElem = doc.FirstChildElement("OnStart"); if ( pElem == NULL ) throw ElementMissingException("No OnStart defined"); pElem = pElem->FirstChildElement("CurrentRoom"); if ( pElem == NULL ) { cout << "No current room set, problems might arise\n"; } else { const char *szRoomId = pElem->Attribute("id"); if ( szRoomId == NULL) throw InvalidAttributeException("Bad format CurrentRoom"); string tmp = szRoomId; if ( m_Rooms.find(tmp) == m_Rooms.end() ) { ostringstream ss; ss << "No room " << tmp << " found!"; throw ElementMissingException(ss.str()); } SetCurrentRoom(m_Rooms[tmp]); } pElem = doc.FirstChildElement("OnStart"); pElem = pElem->FirstChildElement("Story"); if ( pElem == NULL ) throw ElementMissingException("Story"); m_Story = (pElem->GetText() == NULL) ? "" : pElem->GetText(); pElem = doc.FirstChildElement("Player"); if ( !pElem ) throw new ElementMissingException("Player"); m_Player.Load(*pElem); }
// Loads a level file int _Level::Init(const std::string &LevelName, bool HeaderOnly) { if(!HeaderOnly) Log.Write("_Level::Init - Loading level: %s", LevelName.c_str()); // Get paths this->LevelName = LevelName; LevelNiceName = ""; std::string LevelFile = LevelName + "/" + LevelName + ".xml"; std::string FilePath = Game.GetWorkingPath() + std::string("levels/") + LevelFile; std::string CustomFilePath = Save.GetCustomLevelsPath() + LevelFile; std::string DataPath = Game.GetWorkingPath() + std::string("levels/") + LevelName + "/"; // See if custom level exists first IsCustomLevel = false; std::ifstream CustomLevelExists(CustomFilePath.c_str()); if(CustomLevelExists) { IsCustomLevel = true; FilePath = CustomFilePath; DataPath = Save.GetCustomLevelsPath() + LevelName + "/"; } CustomLevelExists.close(); // Open the XML file XMLDocument Document; if(Document.LoadFile(FilePath.c_str()) != XML_NO_ERROR) { Log.Write("Error loading level file with error id = %d", Document.ErrorID()); Log.Write("Error string 1: %s", Document.GetErrorStr1()); Log.Write("Error string 2: %s", Document.GetErrorStr2()); Close(); return 0; } // Check for level tag XMLElement *LevelElement = Document.FirstChildElement("level"); if(!LevelElement) { Log.Write("Could not find level tag"); Close(); return 0; } // Level version if(LevelElement->QueryIntAttribute("version", &LevelVersion) == XML_NO_ATTRIBUTE) { Log.Write("Could not find level version"); Close(); return 0; } // Check required game version GameVersion = LevelElement->Attribute("gameversion"); if(GameVersion == "") { Log.Write("Could not find game version attribute"); Close(); return 0; } // Load level info XMLElement *InfoElement = LevelElement->FirstChildElement("info"); if(InfoElement) { XMLElement *NiceNameElement = InfoElement->FirstChildElement("name"); if(NiceNameElement) { LevelNiceName = NiceNameElement->GetText(); } } // Return after header is read if(HeaderOnly) { Close(); return true; } // Load default lua script Scripts.clear(); Scripts.push_back(Game.GetWorkingPath() + "scripts/default.lua"); // Options bool Fog = false; bool EmitLight = false; Level.ClearColor.set(255, 0, 0, 0); // Load options XMLElement *OptionsElement = LevelElement->FirstChildElement("options"); if(OptionsElement) { // Does the player emit light? XMLElement *EmitLightElement = OptionsElement->FirstChildElement("emitlight"); if(EmitLightElement) { EmitLightElement->QueryBoolAttribute("enabled", &EmitLight); } } // Load world XMLElement *ResourcesElement = LevelElement->FirstChildElement("resources"); if(ResourcesElement) { // Load scenes for(XMLElement *SceneElement = ResourcesElement->FirstChildElement("scene"); SceneElement != 0; SceneElement = SceneElement->NextSiblingElement("scene")) { // Get file std::string File = SceneElement->Attribute("file"); if(File == "") { Log.Write("Could not find file attribute on scene"); Close(); return 0; } // Reset fog irrDriver->setFog(video::SColor(0, 0, 0, 0), video::EFT_FOG_EXP, 0, 0, 0.0f); // Load scene if(IsCustomLevel) { irrFile->changeWorkingDirectoryTo(DataPath.c_str()); irrScene->loadScene(File.c_str(), &UserDataLoader); irrFile->changeWorkingDirectoryTo(Game.GetWorkingPath().c_str()); } else { irrScene->loadScene((DataPath + File).c_str(), &UserDataLoader); } // Set texture filters on meshes in the scene core::array<irr::scene::ISceneNode *> MeshNodes; irrScene->getSceneNodesFromType(scene::ESNT_MESH, MeshNodes); for(u32 i = 0; i < MeshNodes.size(); i++) { if(EmitLight && Config.Shaders) { video::SMaterial &Material = MeshNodes[i]->getMaterial(0); int ShaderType = 0; if(Material.MaterialType == video::EMT_TRANSPARENT_ALPHA_CHANNEL) { ShaderType = 1; } MeshNodes[i]->setMaterialType((video::E_MATERIAL_TYPE)Graphics.GetCustomMaterial(ShaderType)); } MeshNodes[i]->setMaterialFlag(video::EMF_TRILINEAR_FILTER, Config.TrilinearFiltering); for(u32 j = 0; j < MeshNodes[i]->getMaterialCount(); j++) { for(int k = 0; k < 4; k++) { MeshNodes[i]->getMaterial(j).TextureLayer[k].AnisotropicFilter = Config.AnisotropicFiltering; if(MeshNodes[i]->getMaterial(j).FogEnable) Fog = true; } } } } // Load collision for(XMLElement *CollisionElement = ResourcesElement->FirstChildElement("collision"); CollisionElement != 0; CollisionElement = CollisionElement->NextSiblingElement("collision")) { // Get file std::string File = CollisionElement->Attribute("file"); if(File == "") { Log.Write("Could not find file attribute on collision"); Close(); return 0; } // Create template TemplateStruct *Template = new TemplateStruct; Template->CollisionFile = DataPath + File; Template->Type = _Object::COLLISION; Template->Mass = 0.0f; Templates.push_back(Template); // Create spawn SpawnStruct *ObjectSpawn = new SpawnStruct; ObjectSpawn->Template = Template; ObjectSpawns.push_back(ObjectSpawn); } // Load scripts for(XMLElement *ScriptElement = ResourcesElement->FirstChildElement("script"); ScriptElement != 0; ScriptElement = ScriptElement->NextSiblingElement("script")) { // Get file std::string File = ScriptElement->Attribute("file"); if(File == "") { Log.Write("Could not find file attribute on script"); Close(); return 0; } Scripts.push_back(DataPath + File); } // Load sounds Sounds.clear(); for(XMLElement *SoundElement = ResourcesElement->FirstChildElement("sound"); SoundElement != 0; SoundElement = SoundElement->NextSiblingElement("sound")) { // Get file std::string File = SoundElement->Attribute("file"); if(File == "") { Log.Write("Could not find file attribute on sound"); Close(); return 0; } // Attempt to load sound if(Audio.LoadBuffer(File)) Sounds.push_back(File); } } // Load templates XMLElement *TemplatesElement = LevelElement->FirstChildElement("templates"); if(TemplatesElement) { int TemplateID = 0; for(XMLElement *TemplateElement = TemplatesElement->FirstChildElement(); TemplateElement != 0; TemplateElement = TemplateElement->NextSiblingElement()) { // Create a template TemplateStruct *Template = new TemplateStruct; Template->Fog = Fog; // Get the template properties if(!GetTemplateProperties(TemplateElement, *Template)) return 0; // Assign options Template->TemplateID = TemplateID; if(EmitLight) { // Use shaders on materials that receive light if(Config.Shaders) Template->CustomMaterial = Graphics.GetCustomMaterial(0); // Set the player to emit light if(Template->Type == _Object::PLAYER || Template->Type == _Object::ORB) Template->EmitLight = true; } TemplateID++; // Store for later Templates.push_back(Template); } } // Load object spawns XMLElement *ObjectsElement = LevelElement->FirstChildElement("objects"); if(ObjectsElement) { for(XMLElement *ObjectElement = ObjectsElement->FirstChildElement(); ObjectElement != 0; ObjectElement = ObjectElement->NextSiblingElement()) { // Create an object spawn SpawnStruct *ObjectSpawn = new SpawnStruct; // Get the object properties if(!GetObjectSpawnProperties(ObjectElement, *ObjectSpawn)) return 0; // Store for later ObjectSpawns.push_back(ObjectSpawn); } } return 1; }
bool FractalConfiguration::save(string filename) { if(invalidID()) return true; // Check filename and append .xml if(!endsWith(filename, ".xml")) filename += ".xml"; // Create document XMLDocument doc; // Root element XMLElement *root = doc.NewElement("fractal"); root->SetAttribute("id", m_id.c_str()); doc.InsertEndChild(root); // Add properties iterate(); while(true) { if(!next()) break; XMLElement *prop = doc.NewElement("property"); prop->SetAttribute("name", getName().c_str()); XMLText *text = doc.NewText(""); if(isString()) { prop->SetAttribute("type", "string"); text->SetValue(getString().c_str()); } else if(isInt()) { prop->SetAttribute("type", "int"); stringstream ss; ss << getInt(); text->SetValue(ss.str().c_str()); } else if(isDouble()) { prop->SetAttribute("type", "double"); stringstream ss; ss << getDouble(); text->SetValue(ss.str().c_str()); } else if(isBool()) { prop->SetAttribute("type", "bool"); if(getBool()) text->SetValue("1"); else text->SetValue("0"); } prop->InsertEndChild(text); root->InsertEndChild(prop); } // Save to file if(doc.SaveFile(filename.c_str()) != XML_NO_ERROR) { m_last_error = doc.GetErrorStr1(); return true; } resetDirty(); return false; }
bool FractalConfiguration::open(string filename) { if(invalidID()) return true; m_dirty = false; XMLDocument doc; // Parse file doc.LoadFile(filename.c_str()); if(doc.Error()) { m_last_error = doc.GetErrorStr1(); return true; } // Get data XMLElement *root = doc.RootElement(); string name = string(root->Name()); if(name != "fractal") { m_last_error = "Configuration file is invalid!"; return true; } if(root->Attribute("id", m_id.c_str())) { XMLNode *node = root->FirstChild(); // Loop over all properties while(node != NULL) { XMLElement *element = node->ToElement(); if(element == NULL) { m_last_error = "Configuration file is invalid!"; return true; } // Get name and type (attributes) const char* tmp_name = element->Attribute("name"); const char* tmp_type = element->Attribute("type"); if(tmp_name == NULL || tmp_type == NULL) { m_last_error = "Configuration file is invalid!"; return true; } string name(tmp_name); string type(tmp_type); // Get text const char* tmp_text = element->GetText(); string text = ""; if(tmp_text != NULL) text = tmp_text; // Set property if(type == "string") { if(setStringProperty(name, text)) return true; } else if(type == "int") { int value; if(stringToInt(text, &value)) { m_last_error = "Error in configuration: " + text + " should be an integer"; return true; } if(setIntProperty(name, value)) return true; } else if(type == "double") { double value; if(stringToDouble(text, &value)) { m_last_error = "Error in configuration: " + text + " should be a double"; return true; } if(setDoubleProperty(name, value)) return true; } else if(type == "bool") { bool value; if(text == "1") value = true; else if(text == "0") value = false; else { m_last_error = "Error in configuration: " + text + " should be boolean (0 or 1)"; return true; } if(setBoolProperty(name, value)) return true; } else { m_last_error = "Error in configuration: " + type + " is not a valid type"; return true; } // Next node node = node->NextSibling(); } } else { m_last_error = "File contains not the configuration of a fractal with ID " + m_id; return true; } resetDirty(); return false; }
bool TilemapLoader::LoadTilemap(const string _path, Tilemap& _tilemap) { map_path = _path; // Temporary map object Tilemap temp_map; // Load TMX file in TinyXML XMLDocument doc; string temp = map_path + "/map.tmx"; doc.LoadFile(temp.c_str()); // Check for error in parsing if (doc.Error()) { cout << "XML Parsing Error: " << doc.ErrorID() << endl; cout << "\tXML Desc: " << doc.GetErrorStr1() << endl; return false; } // Find the map element const XMLElement* map_element = doc.FirstChildElement("map"); if (!CheckPointer(map_element, "Couldn't locate map element in map file")) return false; // Load all of the basic map data map_element->QueryFloatAttribute("version", &temp_map.version); map_element->QueryIntAttribute("width", &temp_map.map_dimensions.x); map_element->QueryIntAttribute("height", &temp_map.map_dimensions.y); map_element->QueryIntAttribute("tilewidth", &temp_map.tile_dimensions.x); map_element->QueryIntAttribute("tileheight", &temp_map.tile_dimensions.y); // Load the orientation of the tile map. Only orthogonal // is supported currently. string orient_str = map_element->Attribute("orientation"); if (orient_str == "orthogonal") temp_map.orientation = SFTILE_ORIENT_ORTHOGONAL; else { cout << "SfTileEngine currently only supports orthogonal tile maps." << endl; temp_map.orientation = SFTILE_ORIENT_UNSUPPORTED; return false; } // Parse the tilesets in the tile map. const XMLElement* tileset_element = map_element->FirstChildElement("tileset"); while (tileset_element) { if (!ParseTileset(tileset_element, temp_map.tileset)) { cout << "Failed to parse tileset" << endl; return false; } tileset_element = tileset_element->NextSiblingElement("tileset"); } // Don't let your pointer dangle... That's gross. tileset_element = nullptr; // Parse all layers: tile, object, image const XMLElement* layer_element = map_element->FirstChildElement(); while (layer_element) { string name = layer_element->Name(); if (name == "layer") { TileLayer temp_tile_layer; if (!ParseTileLayer(layer_element, temp_tile_layer)) { cout << "Failed to parse tile layer" << endl; return false; } else temp_map.layers.push_back(unique_ptr<TileLayer>(new TileLayer(temp_tile_layer))); } else if (name == "objectgroup") { ObjectLayer temp_object_layer; if (!ParseObjectLayer(layer_element, temp_object_layer)) { cout << "Failed to parse object layer" << endl; return false; } else temp_map.layers.push_back(unique_ptr<ObjectLayer>(new ObjectLayer(temp_object_layer))); } /*else if (layer_element->Name() == "imagelayer") { ImageLayer temp_image_layer; if (!ParseImageLayer(layer_element, temp_image_layer)) { cout << "Failed to parse image layer" << endl; return false; } else temp_map.layers.push_back(unique_ptr<ImageLayer>(new ImageLayer(temp_image_layer))); }*/ layer_element = layer_element->NextSiblingElement(); } layer_element = nullptr; /* // Parse the tile layers const XMLElement* tile_layer_element = map_element->FirstChildElement("layer"); while (tile_layer_element) { TileLayer temp_tile_layer; if (!ParseTileLayer(tile_layer_element, temp_tile_layer)) { cout << "Failed to parse tile layer" << endl; return false; } else { temp_map.layers.push_back(unique_ptr<TileLayer>(new TileLayer(temp_tile_layer))); } tile_layer_element = tile_layer_element->NextSiblingElement("layer"); } tile_layer_element = nullptr;*/ /// Parse the object layers (object groups) /*const XMLElement* object_layer_element = map_element->FirstChildElement("objectgroup"); while (object_layer_element) { ObjectLayer temp_object_layer; if (!ParseObjectLayer(object_layer_element, temp_object_layer)) { cout << "Failed to parse object layer" << endl; return false; } else { temp_map.layers.push_back(temp_object_layer); } object_layer_element = object_layer_element->NextSiblingElement("objectgroup"); } object_layer_element = nullptr;*/ _tilemap = std::move(temp_map); return true; }