Skip to content

Commit

Permalink
Load improvement avoiding bad lexical cast.
Browse files Browse the repository at this point in the history
  • Loading branch information
Mignari committed Jun 7, 2021
1 parent 6022bf7 commit fcbbf5e
Show file tree
Hide file tree
Showing 19 changed files with 125 additions and 155 deletions.
1 change: 0 additions & 1 deletion source/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ ${CMAKE_CURRENT_LIST_DIR}/positionctrl.h
${CMAKE_CURRENT_LIST_DIR}/preferences.h
${CMAKE_CURRENT_LIST_DIR}/process_com.h
${CMAKE_CURRENT_LIST_DIR}/properties_window.h
${CMAKE_CURRENT_LIST_DIR}/pugicast.h
${CMAKE_CURRENT_LIST_DIR}/raw_brush.h
${CMAKE_CURRENT_LIST_DIR}/result_window.h
${CMAKE_CURRENT_LIST_DIR}/rme_forward_declarations.h
Expand Down
3 changes: 1 addition & 2 deletions source/brush.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
#include "map.h"

#include "gui.h"
#include "pugicast.h"

Brushes g_brushes;

Expand Down Expand Up @@ -178,7 +177,7 @@ bool Brushes::unserializeBorder(pugi::xml_node node, wxArrayString& warnings)
return false;
}

int32_t id = pugi::cast<int32_t>(attribute.value());
uint32_t id = attribute.as_uint();
if(borders[id]) {
warnings.push_back("Border ID " + std::to_string(id) + " already exists");
return false;
Expand Down
11 changes: 5 additions & 6 deletions source/carpet_brush.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@

#include "basemap.h"
#include "items.h"
#include "pugicast.h"

//=============================================================================
// Carpet brush
Expand All @@ -43,11 +42,11 @@ bool CarpetBrush::load(pugi::xml_node node, wxArrayString& warnings)
{
pugi::xml_attribute attribute;
if((attribute = node.attribute("lookid"))) {
look_id = pugi::cast<uint16_t>(attribute.value());
look_id = attribute.as_ushort();
}

if((attribute = node.attribute("server_lookid"))) {
look_id = g_items[pugi::cast<uint16_t>(attribute.value())].clientID;
look_id = g_items[attribute.as_ushort()].clientID;
}

for(pugi::xml_node childNode = node.first_child(); childNode; childNode = childNode.next_sibling()) {
Expand Down Expand Up @@ -84,13 +83,13 @@ bool CarpetBrush::load(pugi::xml_node node, wxArrayString& warnings)
continue;
}

int32_t id = pugi::cast<int32_t>(attribute.value());
int32_t id = attribute.as_int();
if(!(attribute = subChildNode.attribute("chance"))) {
warnings.push_back("Could not read chance tag of item node\n");
continue;
}

int32_t chance = pugi::cast<int32_t>(attribute.value());
int32_t chance = attribute.as_int();

ItemType& it = g_items[id];
if(it.id == 0) {
Expand Down Expand Up @@ -120,7 +119,7 @@ bool CarpetBrush::load(pugi::xml_node node, wxArrayString& warnings)
continue;
}

int32_t id = pugi::cast<int32_t>(attribute.value());
uint16_t id = attribute.as_ushort();

ItemType& it = g_items[id];
if(it.id == 0) {
Expand Down
7 changes: 3 additions & 4 deletions source/client_version.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
#include "gui.h"

#include "client_version.h"
#include "pugicast.h"
#include "otml.h"
#include <wx/dir.h>

Expand Down Expand Up @@ -170,13 +169,13 @@ void ClientVersion::loadOTBInfo(pugi::xml_node otbNode)
return;
}

otb.id = pugi::cast<int32_t>(attribute.value());
otb.id = static_cast<ClientVersionID>(attribute.as_int());
if(!(attribute = otbNode.attribute("version"))) {
wxLogError("Node 'otb' must contain 'version' tag.");
return;
}

OtbFormatVersion versionId = static_cast<OtbFormatVersion>(pugi::cast<uint32_t>(attribute.value()));
OtbFormatVersion versionId = static_cast<OtbFormatVersion>(attribute.as_uint());
if(versionId < OTB_VERSION_1 || versionId > OTB_VERSION_3) {
wxLogError("Node 'otb' unrecognized format version (version 1..3 supported).");
return;
Expand Down Expand Up @@ -225,7 +224,7 @@ void ClientVersion::loadVersion(pugi::xml_node versionNode)
continue;
}

int32_t otbmVersion = pugi::cast<int32_t>(attribute.value()) - 1;
int32_t otbmVersion = attribute.as_int() - 1;
if(otbmVersion < MAP_OTBM_1 || otbmVersion > MAP_OTBM_4) {
wxLogError("Node 'otbm' unsupported version.");
continue;
Expand Down
33 changes: 16 additions & 17 deletions source/creatures.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
#include "brush.h"
#include "creatures.h"
#include "creature_brush.h"
#include "pugicast.h"

CreatureDatabase g_creatures;

Expand Down Expand Up @@ -90,38 +89,38 @@ CreatureType* CreatureType::loadFromXML(pugi::xml_node node, wxArrayString& warn
ct->isNpc = tmpType == "npc";

if((attribute = node.attribute("looktype"))) {
ct->outfit.lookType = pugi::cast<int32_t>(attribute.value());
ct->outfit.lookType = attribute.as_int();
if(g_gui.gfx.getCreatureSprite(ct->outfit.lookType) == nullptr) {
warnings.push_back("Invalid creature \"" + wxstr(ct->name) + "\" look type #" + std::to_string(ct->outfit.lookType));
}
}

if((attribute = node.attribute("lookitem"))) {
ct->outfit.lookItem = pugi::cast<int32_t>(attribute.value());
ct->outfit.lookItem = attribute.as_int();
}

if ((attribute = node.attribute("lookmount"))) {
ct->outfit.lookMount = pugi::cast<int32_t>(attribute.value());
ct->outfit.lookMount = attribute.as_int();
}

if((attribute = node.attribute("lookaddon"))) {
ct->outfit.lookAddon = pugi::cast<int32_t>(attribute.value());
ct->outfit.lookAddon = attribute.as_int();
}

if((attribute = node.attribute("lookhead"))) {
ct->outfit.lookHead = pugi::cast<int32_t>(attribute.value());
ct->outfit.lookHead = attribute.as_int();
}

if((attribute = node.attribute("lookbody"))) {
ct->outfit.lookBody = pugi::cast<int32_t>(attribute.value());
ct->outfit.lookBody = attribute.as_int();
}

if((attribute = node.attribute("looklegs"))) {
ct->outfit.lookLegs = pugi::cast<int32_t>(attribute.value());
ct->outfit.lookLegs = attribute.as_int();
}

if((attribute = node.attribute("lookfeet"))) {
ct->outfit.lookFeet = pugi::cast<int32_t>(attribute.value());
ct->outfit.lookFeet = attribute.as_int();
}
return ct;
}
Expand Down Expand Up @@ -161,35 +160,35 @@ CreatureType* CreatureType::loadFromOTXML(const FileName& filename, pugi::xml_do
}

if((attribute = optionNode.attribute("type"))) {
ct->outfit.lookType = pugi::cast<int32_t>(attribute.value());
ct->outfit.lookType = attribute.as_int();
}

if((attribute = optionNode.attribute("item")) || (attribute = optionNode.attribute("lookex")) || (attribute = optionNode.attribute("typeex"))) {
ct->outfit.lookItem = pugi::cast<int32_t>(attribute.value());
ct->outfit.lookItem = attribute.as_int();
}

if ((attribute = optionNode.attribute("mount"))) {
ct->outfit.lookMount = pugi::cast<int32_t>(attribute.value());
ct->outfit.lookMount = attribute.as_int();
}

if((attribute = optionNode.attribute("addon"))) {
ct->outfit.lookAddon = pugi::cast<int32_t>(attribute.value());
ct->outfit.lookAddon = attribute.as_int();
}

if((attribute = optionNode.attribute("head"))) {
ct->outfit.lookHead = pugi::cast<int32_t>(attribute.value());
ct->outfit.lookHead = attribute.as_int();
}

if((attribute = optionNode.attribute("body"))) {
ct->outfit.lookBody = pugi::cast<int32_t>(attribute.value());
ct->outfit.lookBody = attribute.as_int();
}

if((attribute = optionNode.attribute("legs"))) {
ct->outfit.lookLegs = pugi::cast<int32_t>(attribute.value());
ct->outfit.lookLegs = attribute.as_int();
}

if((attribute = optionNode.attribute("feet"))) {
ct->outfit.lookFeet = pugi::cast<int32_t>(attribute.value());
ct->outfit.lookFeet = attribute.as_int();
}
}
return ct;
Expand Down
17 changes: 9 additions & 8 deletions source/doodad_brush.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@

#include "doodad_brush.h"
#include "basemap.h"
#include "pugicast.h"

#include <boost/lexical_cast.hpp>

//=============================================================================
// Doodad brush
Expand Down Expand Up @@ -101,7 +102,7 @@ bool DoodadBrush::loadAlternative(pugi::xml_node node, wxArrayString& warnings,

SingleBlock sb;
sb.item = item;
sb.chance = pugi::cast<int32_t>(attribute.value());
sb.chance = attribute.as_int();

alternativeBlock->single_items.push_back(sb);
alternativeBlock->single_chance += sb.chance;
Expand All @@ -111,7 +112,7 @@ bool DoodadBrush::loadAlternative(pugi::xml_node node, wxArrayString& warnings,
continue;
}

alternativeBlock->composite_chance += pugi::cast<int32_t>(attribute.value());
alternativeBlock->composite_chance += attribute.as_int();

CompositeBlock cb;
cb.chance = alternativeBlock->composite_chance;
Expand All @@ -126,14 +127,14 @@ bool DoodadBrush::loadAlternative(pugi::xml_node node, wxArrayString& warnings,
continue;
}

int32_t x = pugi::cast<int32_t>(attribute.value());
int32_t x = attribute.as_int();
if(!(attribute = compositeNode.attribute("y"))) {
warnings.push_back("Couldn't read positionY values of composite tile node.");
continue;
}

int32_t y = pugi::cast<int32_t>(attribute.value());
int32_t z = pugi::cast<int32_t>(compositeNode.attribute("z").value());
int32_t y = attribute.as_int();
int32_t z = compositeNode.attribute("z").as_int();
if(x < -0x7FFF || x > 0x7FFF) {
warnings.push_back("Invalid range of x value on composite tile node.");
continue;
Expand Down Expand Up @@ -180,11 +181,11 @@ bool DoodadBrush::load(pugi::xml_node node, wxArrayString& warnings)
{
pugi::xml_attribute attribute;
if((attribute = node.attribute("lookid"))) {
look_id = pugi::cast<uint16_t>(attribute.value());
look_id = attribute.as_ushort();
}

if((attribute = node.attribute("server_lookid"))) {
look_id = g_items[pugi::cast<uint16_t>(attribute.value())].clientID;
look_id = g_items[attribute.as_ushort()].clientID;
}

if((attribute = node.attribute("on_blocking"))) {
Expand Down
16 changes: 16 additions & 0 deletions source/ext/pugixml.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3288,6 +3288,17 @@ PUGI__NS_BEGIN
}

// get value with conversion functions
PUGI__FN unsigned short get_value_ushort(const char_t* value, unsigned short def)
{
if (!value) return def;

#ifdef PUGIXML_WCHAR_MODE
return static_cast<unsigned short>(wcstoul(value, 0, 10));
#else
return static_cast<unsigned short>(strtoul(value, 0, 10));
#endif
}

PUGI__FN int get_value_int(const char_t* value, int def)
{
if (!value) return def;
Expand Down Expand Up @@ -3780,6 +3791,11 @@ namespace pugi
return (_attr && _attr->value) ? _attr->value : def;
}

PUGI__FN unsigned short xml_attribute::as_ushort(unsigned short def) const
{
return impl::get_value_ushort(_attr ? _attr->value : 0, def);
}

PUGI__FN int xml_attribute::as_int(int def) const
{
return impl::get_value_int(_attr ? _attr->value : 0, def);
Expand Down
1 change: 1 addition & 0 deletions source/ext/pugixml.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,7 @@ namespace pugi
const char_t* as_string(const char_t* def = PUGIXML_TEXT("")) const;

// Get attribute value as a number, or the default value if conversion did not succeed or attribute is empty
unsigned short as_ushort(unsigned short def = 0) const;
int as_int(int def = 0) const;
unsigned int as_uint(unsigned int def = 0) const;
double as_double(double def = 0) const;
Expand Down
Loading

0 comments on commit fcbbf5e

Please sign in to comment.