Browse Source

Cleanup
* simplify JSON values getting
* ensure initialization of Bonus fields

alexvins 12 years ago
parent
commit
f686edc1b8
4 changed files with 14 additions and 23 deletions
  1. 2 6
      lib/CBonusTypeHandler.cpp
  2. 1 3
      lib/CSpellHandler.cpp
  3. 7 2
      lib/HeroBonus.cpp
  4. 4 12
      lib/JsonNode.cpp

+ 2 - 6
lib/CBonusTypeHandler.cpp

@@ -23,17 +23,13 @@
 
 static inline void jsonSetString(const JsonNode& source, const std::string& name, std::string& dest)
 {
-	const JsonNode& val = source[name];
-	if(!val.isNull())
-	{
-		dest = val.String();
-	}
+	dest = source[name].String();//null->empty string
 }
 
 static inline void jsonSetBool(const JsonNode& source, const std::string& name, bool& dest)
 {
 	const JsonNode& val = source[name];
-	if(!val.isNull())
+	if(!val.isNull()) //do not rely on default value
 	{
 		dest = val.Bool();
 	}

+ 1 - 3
lib/CSpellHandler.cpp

@@ -532,9 +532,7 @@ void CSpellHandler::load()
 		const JsonNode & graphicsNode = spell.second["graphics"];
 		if (!graphicsNode.isNull())
 		{
-			const JsonNode& iconImmune = graphicsNode["iconImmune"];
-			if (!iconImmune.isNull())
-				s->iconImmune = iconImmune.String();
+			s->iconImmune = graphicsNode["iconImmune"].String();
 		}
 	}
 }

+ 7 - 2
lib/HeroBonus.cpp

@@ -1146,13 +1146,18 @@ Bonus::Bonus(ui16 Dur, BonusType Type, BonusSource Src, si32 Val, ui32 ID, si32
 	effectRange = NO_LIMIT;
 }
 
-Bonus::Bonus()
+Bonus::Bonus()   
 {
+	duration = PERMANENT;
+	turnsRemain = 0;
+	type = NONE;
 	subtype = -1;
 	additionalInfo = -1;
-	turnsRemain = 0;
+	
 	valType = ADDITIVE_VALUE;
 	effectRange = NO_LIMIT;
+	val = 0;
+	source = OTHER;	
 }
 
 Bonus::~Bonus()

+ 4 - 12
lib/JsonNode.cpp

@@ -965,9 +965,7 @@ Bonus * JsonUtils::parseBonus (const JsonNode &ability)
 
 	resolveIdentifier (b->subtype, ability, "subtype");
 
-	value = &ability["val"];
-	if (!value->isNull())
-		b->val = value->Float();
+	b->val = ability["val"].Float();
 
 	value = &ability["valueType"];
 	if (!value->isNull())
@@ -975,17 +973,11 @@ Bonus * JsonUtils::parseBonus (const JsonNode &ability)
 
 	resolveIdentifier (b->additionalInfo, ability, "addInfo");
 
-	value = &ability["turns"];
-	if (!value->isNull())
-		b->turnsRemain = value->Float();
+	b->turnsRemain = ability["turns"].Float();
 
-	value = &ability["sourceID"];
-	if (!value->isNull())
-		b->sid = value->Float();
+	b->sid = ability["sourceID"].Float();
 
-	value = &ability["description"];
-	if (!value->isNull())
-		b->description = value->String();
+	b->description = ability["description"].String();
 
 	value = &ability["effectRange"];
 	if (!value->isNull())