Exemplo n.º 1
0
int CTransportConfig::Initialize(const char* confname)
{
    if (m_transports.size() <= 0)
    {
        XMLDocument doc;
        if (doc.LoadFile(confname) != XML_NO_ERROR)
        {
            throw CMudException("load xml transports config file failed");
        }
        
        XMLHandle docHandle(&doc);
        XMLHandle current_handle = docHandle.FirstChildElement("config").FirstChildElement("transports").FirstChild();
        XMLElement* elem = current_handle.ToElement();
        while (elem)
        {
            int id = elem->IntAttribute("id");
            m_transports[id].id = id;
            m_transports[id].name = elem->Attribute("name");
            m_transports[id].x = elem->IntAttribute("x");
            m_transports[id].y = elem->IntAttribute("y");
            m_transports[id].enabled = elem->BoolAttribute("enabled");

            STDOUT_DEBUG("id=%d, name=%s, x=%d, y=%d, enabled=%d", 
                    id, m_transports[id].name.c_str(), m_transports[id].x, m_transports[id].y, m_transports[id].enabled);
            elem = current_handle.NextSibling().ToElement();
        }
    }

    return 0;
}
Exemplo n.º 2
0
void VisualComponent::init(XMLElement *xmlData)
{
	XMLElement *visibleElt = xmlData->FirstChildElement("Visible");
	// if no visibility if specified, default to true
	if (visibleElt == nullptr)
		visible = true;
	else
	{
		visible = visibleElt->BoolAttribute("value");
	}

	XMLElement *textureElt = xmlData->FirstChildElement("Texture");
	// if no texture is specified, set it to null
	if (textureElt == nullptr)
		texture = nullptr;
	else
	{
		string texID = textureElt->Attribute("id");
		texture = TextureManager::get()->getTexture(texID);
	}

	XMLElement *dimElt = xmlData->FirstChildElement("Dimensions");
	// if both are null, we have no dimension data
	if (dimElt == nullptr && textureElt == nullptr)
		throw BAD_DIMENSION_ERR;
	// if no dimensions are specified, take them from the texture
	else if (dimElt == nullptr)
	{
		width = texture->width;
		height = texture->height;
	}
	else
	{
		width = dimElt->DoubleAttribute("w");
		height = dimElt->DoubleAttribute("h");
	}


	XMLElement *colorElt = xmlData->FirstChildElement("Color");
	// if no color is specified, default to white
	if (colorElt == nullptr)
		colorData = bm::Color(255, 255, 255, 255);
	else
	{
		int r = colorElt->IntAttribute("r");
		int g = colorElt->IntAttribute("g");
		int b = colorElt->IntAttribute("b");
		int a = colorElt->IntAttribute("a");
		colorData = bm::Color(r, g, b, a);
	}
}
Exemplo n.º 3
0
void 
GameObject::LoadProperties( XMLElement & e )
{
  XMLElement * prop = e.FirstChildElement("Property");

  while ( prop ){
    g_Log << "Property id is: " << prop->Attribute("id") << "\n";
    if ( !prop->Attribute("id") ) throw AttributeMissingException("Property - No id attribute");
    if ( !prop->Attribute("type") ) throw AttributeMissingException("Property - No type attribute");
    if ( !prop->Attribute("value") ) throw AttributeMissingException("Property - No value attribute");
    string type = prop->Attribute("type");
    
    if ( type == "bool" )		AddProperty( prop->Attribute("id"), prop->BoolAttribute("value"));
    else if ( type == "int" )	AddProperty( prop->Attribute("id"), prop->IntAttribute("value"));
    else if ( type == "float" )	AddProperty( prop->Attribute("id"), prop->FloatAttribute("value"));
    else if ( type == "string" )	AddProperty( prop->Attribute("id"), prop->Attribute("value"));
    else throw InvalidAttributeException("Invalid attribute type defined!");
    prop = prop->NextSiblingElement("Property");
  }
}
Exemplo n.º 4
0
int Configure::load(const string& xmlpath)
{
    if (m_doc.LoadFile(xmlpath.c_str()) != XML_NO_ERROR) {
        g_sysLog.e("{Configure} can't load xml %s\n", xmlpath.c_str());
        return ERR_LDCFG;
    }

    XMLElement* rootElement = m_doc.RootElement();
    if (rootElement == NULL) {
        g_sysLog.e("{Configure} can't get root element %s\n", xmlpath.c_str());
        return ERR_LDCFG;    
    } else {
        bool reset = rootElement->IntAttribute("reset");
        if (reset)
            return ERR_LDCFG;

        int ver = rootElement->IntAttribute("version");
        if (ver != CONF_VERSION)
            return ERR_LDCFG; // Recreate configure.xml
    }

    XMLElement* tempElement = rootElement->FirstChildElement("srclan");
    if (tempElement && tempElement->GetText()) {
        m_srcLan = tempElement->GetText();
    }

    tempElement = rootElement->FirstChildElement("detlan");
    if (tempElement && tempElement->GetText()) {
        m_detLan = tempElement->GetText();
    }

    // Loading dictionary 
    vector<string> dictFiles;
    scanDictDir(dictFiles);

    XMLElement* dictsElement = rootElement->FirstChildElement("dict");
    XMLElement* dictElement = dictsElement->FirstChildElement();
    while (dictElement) {
        struct DictNode dict;
        const char* textE;
        int pos;

        if (dictElement->FirstChildElement("path")) {
            if ((textE = dictElement->FirstChildElement("path")->GetText()) != NULL)
                dict.path = textE;
        }

        if ((pos = findDict(dict.path, dictFiles)) == -1 ) {
            dictsElement->DeleteChild(dictElement);
            dictElement = dictElement->NextSiblingElement();
            continue;
        }
        dictFiles[pos] = "";

        dict.en = dictElement->BoolAttribute("en");
        if (dictElement->Attribute("open"))
            dict.open   = string(dictElement->Attribute("open"));

        if (dictElement->FirstChildElement("srclan")) {
            if ((textE = dictElement->FirstChildElement("srclan")->GetText()) != NULL)
                dict.srclan = textE;
        }
        if (dictElement->FirstChildElement("detlan")) {
	    if ((textE = dictElement->FirstChildElement("detlan")->GetText()) != NULL)
                dict.detlan = textE;
        }

        if (dictElement->FirstChildElement("name")) {
            if ((textE = dictElement->FirstChildElement("name")->GetText()) != NULL)
                dict.name = textE;
        }

        if (dictElement->FirstChildElement("summary")) {
            if ((textE = dictElement->FirstChildElement("summary")->GetText()) != NULL)
	        dict.summary = textE;
        }
        m_dictNodes.push_back(dict);
        dictElement = dictElement->NextSiblingElement();
    }

    vector<string>::iterator iter = dictFiles.begin();
    for (; iter != dictFiles.end(); iter++) {
        if (*iter != "") {
            struct DictNode d;
            d.en = true;
            d.path = *iter;
            d.name = boost::filesystem::path(d.path).filename().string();
            //printf("%s\n",d.name.c_str());
            m_dictNodes.push_back(d);
        }
    }
    // Loading dictionary -- end

    tempElement = rootElement->FirstChildElement(XML_TAG_CWS);
    if (tempElement) {
        m_cws.bselection  = tempElement->BoolAttribute("selection");
        m_cws.bclipboard  = tempElement->BoolAttribute("clipboard");
        m_cws.bmouse      = tempElement->BoolAttribute("mouse");
        m_cws.benable     = tempElement->BoolAttribute("enable"); 
        m_cws.shortcutKey = tempElement->IntAttribute("shortcutkey");
        m_cws.autoCloseEn = tempElement->BoolAttribute("AutoCloseEnable");
        m_cws.autoCloseInv= tempElement->IntAttribute("AutoCloseInterval");
    } else {
        m_cws.benable    = false;
        m_cws.bselection = true;
        m_cws.bmouse     = false;
        m_cws.bclipboard = false;
        m_cws.shortcutKey = 'g'-'a';
        m_cws.autoCloseEn  = true;
        m_cws.autoCloseInv = 1000*10;

        XMLElement* e = m_doc.NewElement(XML_TAG_CWS);
        e->SetAttribute("selection",        m_cws.bselection);
        e->SetAttribute("clipboard",        m_cws.bclipboard);
        e->SetAttribute("mouse",            m_cws.bmouse);
        e->SetAttribute("enable",           m_cws.benable);
        e->SetAttribute("shortcutkey",      m_cws.shortcutKey);
        e->SetAttribute("AutoCloseEnable",  m_cws.autoCloseEn);
        e->SetAttribute("AutoCloseInterval",m_cws.autoCloseInv);

        rootElement->InsertEndChild(e);
    }

    tempElement = rootElement->FirstChildElement(XML_TAG_SETTING);
    if (tempElement) {
        m_setting.uilanID     = tempElement->IntAttribute("uilan");
        m_setting.fontsize    = tempElement->IntAttribute("fontsize");
        m_setting.font        = util::XMLUtil::Attribute(tempElement, "font", "");
        m_setting.bsystemTray = tempElement->BoolAttribute("systemtray");
    } else {
        m_setting.uilanID = UILAN_NONE;
        m_setting.fontsize = 10;
        m_setting.font = "";
        m_setting.bsystemTray = true;

        XMLElement* e = m_doc.NewElement(XML_TAG_SETTING);
        e->SetAttribute("uilan", m_setting.uilanID);
        e->SetAttribute("fontsize", m_setting.fontsize);

        rootElement->InsertEndChild(e);
    }

    m_doc.SaveFile(xmlpath.c_str());
    return 0;
}
Exemplo n.º 5
0
std::unique_ptr<ensoft::tmx::Map> ensoft::tmx::TmxLoader::loadMap(std::string filename)
{
	Logger logger("TMX Loader");
	logger.log("Loading " + filename);
    XMLDocument doc;
    XMLError error = doc.LoadFile(filename.c_str());
    std::unique_ptr<ensoft::tmx::Map> map;

    if (error != XMLError::XML_NO_ERROR)
    {
		logger.error("Error code: " + std::to_string(error));
    }
    else
    {
        // start parsing the TMX map
        XMLElement *xmap = doc.FirstChildElement("map");
        
        map = std::make_unique<ensoft::tmx::Map>(loadProperties(xmap));
        map->version = xmap->Attribute("version");
        map->orientation = xmap->Attribute("orientation");
        map->width = xmap->IntAttribute("width");
        map->height = xmap->IntAttribute("height");
        map->tilewidth = xmap->IntAttribute("tilewidth");
        map->tileheight = xmap->IntAttribute("tileheight");
        map->renderorder = xmap->Attribute("renderorder");
        map->properties = loadProperties(xmap);

        // load all the tilesets
        XMLElement *xtileset = xmap->FirstChildElement("tileset");
        while (xtileset)
        {
            auto tileSet = std::make_unique<ensoft::tmx::TileSet>(loadProperties(xtileset));
            tileSet->firstgid = xtileset->IntAttribute("firstgid");
            tileSet->source = getAttributeString(xtileset, "source");
            tileSet->name = getAttributeString(xtileset, "name");
            tileSet->tilewidth = xtileset->IntAttribute("tilewidth");
            tileSet->tileheight = xtileset->IntAttribute("tileheight");
            tileSet->spacing = xtileset->IntAttribute("spacing");
            tileSet->margin = xtileset->IntAttribute("margin");

            // get the tiles for this tileset
            XMLElement *xtile = xtileset->FirstChildElement("tile");
            while (xtile)
            {
                auto tile = make_unique<ensoft::tmx::Tile>(loadProperties(xtile));
                tile->id = xtile->IntAttribute("id");
                tile->terrain = getAttributeString(xtile, "terrain");
                tile->probability = xtile->FloatAttribute("probability");
                tile->image = loadImage(xtile);

                // keep track of all tiles and their global IDs
                int tileGid = tileSet->firstgid + tile->id;
                map->allTiles[tileGid] = tile.get();
                std::cout << "[TMX Loader] Added: " << tile->image.source << " GID: " << tileGid << " Tileset: " << tileSet->name << std::endl;

                tileSet->tiles.push_back(std::move(tile));
                xtile = xtile->NextSiblingElement("tile");
            }

            map->tilesets.push_back(std::move(tileSet));
            // try to find another tileset
            xtileset = xtileset->NextSiblingElement("tileset");
        }

        XMLElement *xlayer = xmap->FirstChildElement("layer");
        while (xlayer)
        {
            auto layer = std::make_shared<ensoft::tmx::Layer>(loadProperties(xlayer));
            layer->name = xlayer->Attribute("name");
            layer->x = xlayer->IntAttribute("x");
            layer->y = xlayer->IntAttribute("y");
            layer->width = xlayer->IntAttribute("width");
            layer->height = xlayer->IntAttribute("height");
            layer->opacity = xlayer->FloatAttribute("opacity");
            layer->visible = xlayer->BoolAttribute("visible");

            // load the data element
            XMLElement *xdata = xlayer->FirstChildElement("data");
            if (xdata)
            {
                string data = trim_copy(xdata->GetText());
                loadData(*layer, map.get(), data);
            }
            
            map->layers.push_back(layer);
            xlayer = xlayer->NextSiblingElement("layer");
        }

        XMLElement *ximagelayer = xmap->FirstChildElement("imagelayer");
        while (ximagelayer)
        {
            auto imageLayer = std::make_unique<ensoft::tmx::ImageLayer>(loadProperties(ximagelayer));
            imageLayer->name = ximagelayer->Attribute("name");
            imageLayer->x = ximagelayer->IntAttribute("x");
            imageLayer->y = ximagelayer->IntAttribute("y");
            imageLayer->width = ximagelayer->IntAttribute("width");
            imageLayer->height = ximagelayer->IntAttribute("height");
            imageLayer->opacity = ximagelayer->FloatAttribute("opacity");
            imageLayer->visible = ximagelayer->BoolAttribute("visible");

            imageLayer->image = loadImage(ximagelayer);
            map->imageLayers.push_back(std::move(imageLayer));

            ximagelayer = ximagelayer->NextSiblingElement("imagelayer");
        }

        XMLElement *xobjectgroup = xmap->FirstChildElement("objectgroup");
        while (xobjectgroup)
        {
            auto objectGroup = std::make_unique<ensoft::tmx::ObjectGroup>(loadProperties(xobjectgroup));
            objectGroup->name = xobjectgroup->Attribute("name");
            objectGroup->x = xobjectgroup->IntAttribute("x");
            objectGroup->y = xobjectgroup->IntAttribute("y");
            objectGroup->width = xobjectgroup->IntAttribute("width");
            objectGroup->height = xobjectgroup->IntAttribute("height");
            objectGroup->opacity = xobjectgroup->FloatAttribute("opacity");
            objectGroup->visible = xobjectgroup->BoolAttribute("visible");

            //grab the objects
            XMLElement *xobject = xobjectgroup->FirstChildElement("object");
            while (xobject)
            {
                auto object = std::make_unique<ensoft::tmx::Object>(loadProperties(xobject));
                object->id = xobject->Attribute("id");
                object->name = getAttributeString(xobject, "name");
                object->type = getAttributeString(xobject, "type");
                object->x = xobject->IntAttribute("x");
                object->y = xobject->IntAttribute("y");
                object->width = xobject->IntAttribute("witdth");
                object->height = xobject->IntAttribute("height");
                object->gid = xobject->IntAttribute("gid");
                object->visible = xobject->BoolAttribute("visible");
                objectGroup->objects.push_back(std::move(object));

                xobject = xobject->NextSiblingElement("object");
            }
            map->objectGroups.push_back(std::move(objectGroup));
            xobjectgroup = xobjectgroup->NextSiblingElement("objectgroup");
        }
    }

    return std::move(map); 
}