diff --git a/src/framework/stdext/cast.h b/src/framework/stdext/cast.h index 025d0275..8ee136d5 100644 --- a/src/framework/stdext/cast.h +++ b/src/framework/stdext/cast.h @@ -120,7 +120,6 @@ R unsafe_cast(const T& t, R def) { return def; } } - } #endif diff --git a/src/framework/stdext/string.h b/src/framework/stdext/string.h index 5e467bca..785724e4 100644 --- a/src/framework/stdext/string.h +++ b/src/framework/stdext/string.h @@ -214,10 +214,20 @@ inline std::string utf8StringToLatin1(uchar *utf8) { } // Convert string to lower case -inline std::string tolower(std::string& str) { return boost::algorithm::to_lower_copy(str); } +inline std::string tolower(const std::string& str) +{ + std::string _str = str; + boost::algorithm::to_lower(_str); + return _str; +} // Convert string to upper case -inline std::string toupper(std::string& str) { return boost::algorithm::to_upper_copy(str); } +inline std::string toupper(const std::string& str) +{ + std::string _str = str; + boost::algorithm::to_upper(_str); + return _str; +} // utility for printing messages into stdout template diff --git a/src/framework/xml/tinyxml.cpp b/src/framework/xml/tinyxml.cpp index 309f8df2..ec7c3e49 100644 --- a/src/framework/xml/tinyxml.cpp +++ b/src/framework/xml/tinyxml.cpp @@ -596,16 +596,10 @@ const std::string* TiXmlElement::Attribute( const std::string& name ) const const char* TiXmlElement::Attribute( const char* name, int* i ) const { - const TiXmlAttribute* attrib = attributeSet.Find( name ); - const char* result = 0; - - if ( attrib ) { - result = attrib->Value(); - if ( i ) { - attrib->QueryIntValue( i ); - } - } - return result; + int p = readType(name); + if(i) + *i = p; + return Attribute(name); } @@ -625,19 +619,12 @@ const std::string* TiXmlElement::Attribute( const std::string& name, int* i ) co } #endif - -const char* TiXmlElement::Attribute( const char* name, double* d ) const +const char* TiXmlElement::Attribute(const char *name, double *d) const { - const TiXmlAttribute* attrib = attributeSet.Find( name ); - const char* result = 0; - - if ( attrib ) { - result = attrib->Value(); - if ( d ) { - attrib->QueryDoubleValue( d ); - } - } - return result; + double p = readType(name); + if(d) + *d = p; + return Attribute(name); } diff --git a/src/framework/xml/tinyxml.h b/src/framework/xml/tinyxml.h index 1fea277e..34281a5f 100644 --- a/src/framework/xml/tinyxml.h +++ b/src/framework/xml/tinyxml.h @@ -40,6 +40,10 @@ distribution. #include #include +#include +#include +#include + // Help out windows: #if defined( _DEBUG ) && !defined( DEBUG ) #define DEBUG @@ -200,6 +204,9 @@ class TiXmlBase friend class TiXmlDocument; public: + TiXmlBase( const TiXmlBase& ) = delete; + void operator=( const TiXmlBase& base ) = delete; + TiXmlBase() : userData(0) {} virtual ~TiXmlBase() {} @@ -396,9 +403,6 @@ protected: static void ConvertUTF32ToUTF8( unsigned long input, char* output, int* length ); private: - TiXmlBase( const TiXmlBase& ); // not implemented. - void operator=( const TiXmlBase& base ); // not allowed. - struct Entity { const char* str; @@ -428,6 +432,9 @@ class TiXmlNode : public TiXmlBase friend class TiXmlElement; public: + TiXmlNode( const TiXmlNode& ) = delete; + void operator=( const TiXmlNode& base ) = delete; + #ifdef TIXML_USE_STL /** An input stream operator, for every class. Tolerant of newlines and @@ -764,10 +771,6 @@ protected: TiXmlNode* prev; TiXmlNode* next; - -private: - TiXmlNode( const TiXmlNode& ); // not implemented. - void operator=( const TiXmlNode& base ); // not allowed. }; @@ -977,6 +980,18 @@ public: */ const char* Attribute( const char* name, double* d ) const; + template + inline T readType(const std::string& str) const { return stdext::unsafe_cast(Attribute(str)); } + + Position readPos(const std::string& base = std::string()) const + { + return Position(readType(base + "x"), readType(base + "y"), readType(base + "z")); + } + + Point readPoint() const + { + return Point(readType("x"), readType("y")); + } /** QueryIntAttribute examines the attribute - it is an alternative to the Attribute() method with richer error checking. If the attribute is an integer, it is stored in 'value' and diff --git a/src/otclient/creature.h b/src/otclient/creature.h index e779b87c..98e816d6 100644 --- a/src/otclient/creature.h +++ b/src/otclient/creature.h @@ -160,11 +160,6 @@ class Monster : public Creature public: MonsterPtr asMonster() { return std::static_pointer_cast(shared_from_this()); } bool isMonster() { return true; } - - Position getPos() { return m_pos; } - void setPos(const Position& pos) { m_pos = pos; } -private: - Position m_pos; }; #endif diff --git a/src/otclient/houses.cpp b/src/otclient/houses.cpp index 08dbc78b..b7226e5b 100644 --- a/src/otclient/houses.cpp +++ b/src/otclient/houses.cpp @@ -42,29 +42,21 @@ void House::setTile(const TilePtr& tile) m_tiles.push_back(tile); } -#define fugly_get(attrib, type) stdext::unsafe_cast(elem->Attribute((attrib))) - void House::load(const TiXmlElement *elem) { std::string name = elem->Attribute("name"); if(name.empty()) name = stdext::format("UnNamed house #%u", getId()); - uint32 rent = fugly_get("rent", uint32); - m_rent = rent; + m_rent = elem->readType("rent"); + m_size = elem->readType("size"); - uint32 townId = fugly_get("townid", uint32); + uint32 townId = elem->readType("townid"); if(!g_map.getTown(townId)) stdext::throw_exception(stdext::format("invalid town id for house %d", townId)); - uint32 size = fugly_get("size", uint32); - if(size == 0) - size = 1; - - m_size = size; - m_isGuildHall = fugly_get("guildhall", bool); - addDoor(0, Position(fugly_get("entryx", uint16), fugly_get("entryy", uint16), - fugly_get("entryz", uint8))); + m_isGuildHall = elem->readType("rent"); + addDoor(0, elem->readPos()); } void Houses::addHouse(const HousePtr& house) @@ -103,7 +95,7 @@ void Houses::load(const std::string& fileName) if(elem->ValueTStr() != "house") stdext::throw_exception("invalid house tag."); - uint32 houseId = fugly_get("houseid", uint32); + uint32 houseId = elem->readType("houseid"); HousePtr house = getHouse(houseId); if(!house) house = HousePtr(new House(houseId)), addHouse(house); diff --git a/src/otclient/map.cpp b/src/otclient/map.cpp index a623cdb5..5ca3bd56 100644 --- a/src/otclient/map.cpp +++ b/src/otclient/map.cpp @@ -378,7 +378,6 @@ void Map::saveOtbm(const std::string &fileName) void Map::loadSpawns(const std::string &fileName) { -#define cast(NAME, TYPE, NODE) stdext::unsafe_cast(NODE->Attribute((NAME))) if(!m_monsters.isLoaded()) stdext::throw_exception("cannot load spawns; monsters aren't loaded."); @@ -394,7 +393,7 @@ void Map::loadSpawns(const std::string &fileName) if (node->ValueTStr() != "spawn") stdext::throw_exception("invalid spawn node"); - Position centerPos(cast("x", uint16, node), cast("y", uint16, node), cast("z", uint8, node)); + Position centerPos = node->readPos("center"); for(TiXmlElement* mType = node->FirstChildElement(); mType; mType = mType->NextSiblingElement()) { if (mType->ValueStr() != "monster") stdext::throw_exception("invalid spawn-subnode"); @@ -404,7 +403,7 @@ void Map::loadSpawns(const std::string &fileName) if (!m) stdext::throw_exception(stdext::format("unkown monster %s", mName)); - Point off(cast("x", int, mType), cast("y", int, mType)); + Point off = mType->readPoint(); Position mPos(centerPos.x + off.x, centerPos.y + off.y, centerPos.z); addThing(m, mPos, -1); } diff --git a/src/otclient/map.h b/src/otclient/map.h index 3b4c80e1..6ae287d2 100644 --- a/src/otclient/map.h +++ b/src/otclient/map.h @@ -151,7 +151,7 @@ public: // town/house/monster related TownPtr getTown(uint32 tid) { return m_towns.getTown(tid); } HousePtr getHouse(uint32 hid) { return m_houses.getHouse(hid); } - MonsterPtr getMonster(const std::string& name); + MonsterPtr getMonster(const std::string &name); void setLight(const Light& light) { m_light = light; } void setCentralPosition(const Position& centralPosition); diff --git a/src/otclient/monsters.cpp b/src/otclient/monsters.cpp index cc2436b3..ba1de071 100644 --- a/src/otclient/monsters.cpp +++ b/src/otclient/monsters.cpp @@ -48,7 +48,6 @@ void Monsters::loadMonsters(const std::string& file) void Monsters::loadSingleMonster(const std::string& file, const MonsterPtr& m) { -#define read(str) stdext::unsafe_cast(attrib->Attribute((str))) if (!m || std::find(m_monsters.begin(), m_monsters.end(), m) != m_monsters.end()) stdext::throw_exception("reloading monsters is not supported yet."); @@ -64,18 +63,18 @@ void Monsters::loadSingleMonster(const std::string& file, const MonsterPtr& m) if(attrib->ValueStr() == "look") { Outfit out; - int type = read("type"); + int type = attrib->readType("type"); if (type <= 0) - type = read("typeex"); + type = attrib->readType("typeex"); if(type) { out.setId(type); { - out.setHead(read("head")); - out.setBody(read("body")); - out.setLegs(read("legs")); - out.setFeet(read("feet")); - out.setAddons(read("addons")); - out.setMount(read("mount")); + out.setHead(attrib->readType(("head"))); + out.setBody(attrib->readType(("body"))); + out.setLegs(attrib->readType(("legs"))); + out.setFeet(attrib->readType(("feet"))); + out.setAddons(attrib->readType(("addons"))); + out.setMount(attrib->readType(("mount"))); } } else stdext::throw_exception(stdext::format("invalid look type/typeex for monster %s", m->getName())); @@ -90,14 +89,12 @@ MonsterPtr Monsters::getMonster(const std::string& name) { auto it = std::find_if(m_monsters.begin(), m_monsters.end(), [=] (const MonsterPtr& m) -> bool { return m->getName() == name; }); - if (it != m_monsters.end()) - return *it; - return nullptr; + return it != m_monsters.end() ? *it : nullptr; } MonsterPtr Monsters::getMonsterByPos(const Position& pos) { auto it = std::find_if(m_monsters.begin(), m_monsters.end(), - [=] (const MonsterPtr& m) -> bool { return m->getPos() == pos; }); + [=] (const MonsterPtr& m) -> bool { return m->getPosition() == pos; }); return it != m_monsters.end() ? *it : nullptr; }