make dat reader more compatible with malformed dats
This commit is contained in:
parent
ae2a476872
commit
469e0bbbca
|
@ -47,8 +47,12 @@ bool ThingsType::load(const std::string& file)
|
||||||
|
|
||||||
for(int i = 0; i < LastCategory; ++i) {
|
for(int i = 0; i < LastCategory; ++i) {
|
||||||
m_things[i].resize(numThings[i]);
|
m_things[i].resize(numThings[i]);
|
||||||
for(int id = 0; id < numThings[i]; ++id)
|
for(int id = 0; id < numThings[i]; ++id) {
|
||||||
parseThingType(fin, m_things[i][id]);
|
if(!parseThingType(fin, m_things[i][id])) {
|
||||||
|
logError("corrupt or dat file");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
m_loaded = true;
|
m_loaded = true;
|
||||||
|
@ -61,18 +65,24 @@ void ThingsType::unload()
|
||||||
m_things[i].clear();
|
m_things[i].clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
void ThingsType::parseThingType(const FileStreamPtr& fin, ThingType& thingType)
|
bool ThingsType::parseThingType(const FileStreamPtr& fin, ThingType& thingType)
|
||||||
{
|
{
|
||||||
while(true) {
|
bool done = false;
|
||||||
|
for(int i=0;i<ThingType::LastProperty;++i) {
|
||||||
int property = fin->getU8();
|
int property = fin->getU8();
|
||||||
if(property == ThingType::LastPropertyValue) {
|
if(property == ThingType::LastPropertyValue) {
|
||||||
|
done = true;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
thingType.m_properties[property] = true;
|
thingType.m_properties[property] = true;
|
||||||
|
|
||||||
if(property == ThingType::IsGround)
|
if(property == ThingType::IsGround) {
|
||||||
thingType.m_parameters[ThingType::GroundSpeed] = fin->getU16();
|
int speed = fin->getU16();
|
||||||
|
if(speed == 0)
|
||||||
|
speed = 100;
|
||||||
|
thingType.m_parameters[ThingType::GroundSpeed] = speed;
|
||||||
|
}
|
||||||
else if(property == ThingType::IsWritable || property == ThingType::IsWritableOnce)
|
else if(property == ThingType::IsWritable || property == ThingType::IsWritableOnce)
|
||||||
thingType.m_parameters[ThingType::MaxTextLenght] = fin->getU16();
|
thingType.m_parameters[ThingType::MaxTextLenght] = fin->getU16();
|
||||||
else if(property == ThingType::HasLight) {
|
else if(property == ThingType::HasLight) {
|
||||||
|
@ -107,23 +117,36 @@ void ThingsType::parseThingType(const FileStreamPtr& fin, ThingType& thingType)
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(!done)
|
||||||
|
return false;
|
||||||
|
|
||||||
int totalSprites = 1;
|
int totalSprites = 1;
|
||||||
for(int i = 0; i < ThingType::LastDimension; ++i) {
|
for(int i = 0; i < ThingType::LastDimension; ++i) {
|
||||||
if(i == ThingType::ExactSize && thingType.m_dimensions[ThingType::Width] <= 1 && thingType.m_dimensions[ThingType::Height] <= 1) {
|
int value;
|
||||||
thingType.m_dimensions[i] = 32;
|
if(i == ThingType::ExactSize) {
|
||||||
continue;
|
if(thingType.m_dimensions[ThingType::Width] <= 1 && thingType.m_dimensions[ThingType::Height] <= 1)
|
||||||
|
value = 32;
|
||||||
|
else
|
||||||
|
value = std::min((int)fin->getU8(), std::max(thingType.m_dimensions[ThingType::Width] * 32, thingType.m_dimensions[ThingType::Height] * 32));
|
||||||
|
} else {
|
||||||
|
value = fin->getU8();
|
||||||
|
if(value == 0)
|
||||||
|
return false;
|
||||||
|
totalSprites *= value;
|
||||||
}
|
}
|
||||||
|
|
||||||
thingType.m_dimensions[i] = fin->getU8();
|
thingType.m_dimensions[i] = value;
|
||||||
|
|
||||||
if(i != ThingType::ExactSize)
|
|
||||||
totalSprites *= thingType.m_dimensions[i];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(totalSprites > 4096)
|
||||||
|
return false;
|
||||||
|
|
||||||
thingType.m_spritesIndex.resize(totalSprites);
|
thingType.m_spritesIndex.resize(totalSprites);
|
||||||
thingType.m_sprites.resize(totalSprites);
|
thingType.m_sprites.resize(totalSprites);
|
||||||
for(int i = 0; i < totalSprites; i++)
|
for(int i = 0; i < totalSprites; i++)
|
||||||
thingType.m_spritesIndex[i] = fin->getU16();
|
thingType.m_spritesIndex[i] = fin->getU16();
|
||||||
|
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
ThingType *ThingsType::getThingType(uint16 id, Categories category)
|
ThingType *ThingsType::getThingType(uint16 id, Categories category)
|
||||||
|
|
|
@ -42,7 +42,7 @@ public:
|
||||||
bool load(const std::string& file);
|
bool load(const std::string& file);
|
||||||
void unload();
|
void unload();
|
||||||
|
|
||||||
void parseThingType(const FileStreamPtr& fin, ThingType& thingType);
|
bool parseThingType(const FileStreamPtr& fin, ThingType& thingType);
|
||||||
|
|
||||||
ThingType *getEmptyThingType() { return &m_emptyThingType; }
|
ThingType *getEmptyThingType() { return &m_emptyThingType; }
|
||||||
ThingType *getThingType(uint16 id, Categories category);
|
ThingType *getThingType(uint16 id, Categories category);
|
||||||
|
|
|
@ -37,7 +37,12 @@ void UIItem::drawSelf()
|
||||||
if(m_item) {
|
if(m_item) {
|
||||||
Rect drawRect = getClippingRect();
|
Rect drawRect = getClippingRect();
|
||||||
Point dest = drawRect.topLeft();
|
Point dest = drawRect.topLeft();
|
||||||
float scaleFactor = std::min(drawRect.width() / (float)m_item->getExactSize(), drawRect.height() / (float)m_item->getExactSize());
|
|
||||||
|
int exactSize = m_item->getExactSize();
|
||||||
|
if(exactSize == 0)
|
||||||
|
return;
|
||||||
|
|
||||||
|
float scaleFactor = std::min(drawRect.width() / (float)exactSize, drawRect.height() / (float)exactSize);
|
||||||
dest += m_item->getDisplacement() * scaleFactor;
|
dest += m_item->getDisplacement() * scaleFactor;
|
||||||
|
|
||||||
g_painter->setColor(Color::white);
|
g_painter->setColor(Color::white);
|
||||||
|
|
Loading…
Reference in New Issue