From 9ff00d75dce16a55f7e8851ee5b84f274bf8532f Mon Sep 17 00:00:00 2001 From: Ivan Savenko Date: Sat, 16 Dec 2023 12:47:45 +0200 Subject: [PATCH] Do not crash if Scholar is not a rewardable object --- lib/mapping/MapFormatH3M.cpp | 79 ++++++++++++++++++++---------------- 1 file changed, 43 insertions(+), 36 deletions(-) diff --git a/lib/mapping/MapFormatH3M.cpp b/lib/mapping/MapFormatH3M.cpp index 0e7d35120..605cf52aa 100644 --- a/lib/mapping/MapFormatH3M.cpp +++ b/lib/mapping/MapFormatH3M.cpp @@ -1206,45 +1206,52 @@ CGObjectInstance * CMapLoaderH3M::readScholar(const int3 & position, std::shared auto bonusType = static_cast(bonusTypeRaw); auto bonusID = reader->readUInt8(); - switch (bonusType) + if (rewardable) { - case ScholarBonusType::PRIM_SKILL: + switch (bonusType) { - JsonNode variable; - JsonNode dice; - variable.String() = NPrimarySkill::names[bonusID]; - variable.setMeta(ModScope::scopeGame()); - dice.Integer() = 80; - rewardable->configuration.presetVariable("primarySkill", "gainedStat", variable); - rewardable->configuration.presetVariable("dice", "0", dice); - break; + case ScholarBonusType::PRIM_SKILL: + { + JsonNode variable; + JsonNode dice; + variable.String() = NPrimarySkill::names[bonusID]; + variable.setMeta(ModScope::scopeGame()); + dice.Integer() = 80; + rewardable->configuration.presetVariable("primarySkill", "gainedStat", variable); + rewardable->configuration.presetVariable("dice", "0", dice); + break; + } + case ScholarBonusType::SECONDARY_SKILL: + { + JsonNode variable; + JsonNode dice; + variable.String() = VLC->skills()->getByIndex(bonusID)->getJsonKey(); + variable.setMeta(ModScope::scopeGame()); + dice.Integer() = 50; + rewardable->configuration.presetVariable("secondarySkill", "gainedSkill", variable); + rewardable->configuration.presetVariable("dice", "0", dice); + break; + } + case ScholarBonusType::SPELL: + { + JsonNode variable; + JsonNode dice; + variable.String() = VLC->spells()->getByIndex(bonusID)->getJsonKey(); + variable.setMeta(ModScope::scopeGame()); + dice.Integer() = 20; + rewardable->configuration.presetVariable("spell", "gainedSpell", variable); + rewardable->configuration.presetVariable("dice", "0", dice); + break; + } + case ScholarBonusType::RANDOM: + break;// No-op + default: + logGlobal->warn("Map '%s': Invalid Scholar settings! Ignoring...", mapName); } - case ScholarBonusType::SECONDARY_SKILL: - { - JsonNode variable; - JsonNode dice; - variable.String() = VLC->skills()->getByIndex(bonusID)->getJsonKey(); - variable.setMeta(ModScope::scopeGame()); - dice.Integer() = 50; - rewardable->configuration.presetVariable("secondarySkill", "gainedSkill", variable); - rewardable->configuration.presetVariable("dice", "0", dice); - break; - } - case ScholarBonusType::SPELL: - { - JsonNode variable; - JsonNode dice; - variable.String() = VLC->spells()->getByIndex(bonusID)->getJsonKey(); - variable.setMeta(ModScope::scopeGame()); - dice.Integer() = 20; - rewardable->configuration.presetVariable("spell", "gainedSpell", variable); - rewardable->configuration.presetVariable("dice", "0", dice); - break; - } - case ScholarBonusType::RANDOM: - break;// No-op - default: - logGlobal->warn("Map '%s': Invalid Scholar settings! Ignoring...", mapName); + } + else + { + logGlobal->warn("Failed to set reward parameters for a Scholar! Object is not rewardable!"); } reader->skipZero(6);