1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-11-25 22:42:04 +02:00

Use bonus only as shared_ptr to avoid memory corruption

This commit is contained in:
Ivan Savenko
2025-06-16 16:22:21 +03:00
parent 4044e03c0a
commit 463c404a83
19 changed files with 62 additions and 67 deletions

View File

@@ -109,10 +109,10 @@ void CGPandoraBox::grantRewardWithMessage(IGameEventCallback & gameEvents, const
for(auto b : vi.reward.heroBonuses)
{
if(b.val && b.type == BonusType::MORALE)
txt = setText(b.val > 0, 179, 178, h);
if(b.val && b.type == BonusType::LUCK)
txt = setText(b.val > 0, 181, 180, h);
if(b->val && b->type == BonusType::MORALE)
txt = setText(b->val > 0, 179, 178, h);
if(b->val && b->type == BonusType::LUCK)
txt = setText(b->val > 0, 181, 180, h);
}
sendInfoWindow(txt, temp);
@@ -229,11 +229,11 @@ void CGPandoraBox::serializeJsonOptions(JsonSerializeFormat & handler)
int val = 0;
handler.serializeInt("morale", val, 0);
if(val)
vinfo.reward.heroBonuses.emplace_back(BonusDuration::ONE_BATTLE, BonusType::MORALE, BonusSource::OBJECT_INSTANCE, val, BonusSourceID(id));
vinfo.reward.heroBonuses.push_back(std::make_shared<Bonus>(BonusDuration::ONE_BATTLE, BonusType::MORALE, BonusSource::OBJECT_INSTANCE, val, BonusSourceID(id)));
handler.serializeInt("luck", val, 0);
if(val)
vinfo.reward.heroBonuses.emplace_back(BonusDuration::ONE_BATTLE, BonusType::LUCK, BonusSource::OBJECT_INSTANCE, val, BonusSourceID(id));
vinfo.reward.heroBonuses.push_back(std::make_shared<Bonus>(BonusDuration::ONE_BATTLE, BonusType::LUCK, BonusSource::OBJECT_INSTANCE, val, BonusSourceID(id)));
vinfo.reward.resources.serializeJson(handler, "resources");
{

View File

@@ -703,9 +703,9 @@ void CGSeerHut::serializeJsonOptions(JsonSerializeFormat & handler)
if(metaTypeName == "mana")
reward.manaDiff = val;
if(metaTypeName == "morale")
reward.heroBonuses.emplace_back(BonusDuration::ONE_BATTLE, BonusType::MORALE, BonusSource::OBJECT_INSTANCE, val, BonusSourceID(id));
reward.heroBonuses.push_back(std::make_shared<Bonus>(BonusDuration::ONE_BATTLE, BonusType::MORALE, BonusSource::OBJECT_INSTANCE, val, BonusSourceID(id)));
if(metaTypeName == "luck")
reward.heroBonuses.emplace_back(BonusDuration::ONE_BATTLE, BonusType::LUCK, BonusSource::OBJECT_INSTANCE, val, BonusSourceID(id));
reward.heroBonuses.push_back(std::make_shared<Bonus>(BonusDuration::ONE_BATTLE, BonusType::LUCK, BonusSource::OBJECT_INSTANCE, val, BonusSourceID(id)));
if(metaTypeName == "resource")
{
auto rawId = *LIBRARY->identifiers()->getIdentifier(ModScope::scopeMap(), fullIdentifier, false);

View File

@@ -73,7 +73,7 @@ TownRewardableBuildingInstance::TownRewardableBuildingInstance(CGTownInstance *
configuration = generateConfiguration(gameRandomizer);
}
void TownRewardableBuildingInstance::assignBonuses(std::vector<Bonus> & bonuses) const
void TownRewardableBuildingInstance::assignBonuses(std::vector<std::shared_ptr<Bonus>> & bonuses) const
{
const auto & building = town->getTown()->buildings.at(getBuildingType());
@@ -81,13 +81,13 @@ void TownRewardableBuildingInstance::assignBonuses(std::vector<Bonus> & bonuses)
{
if (building->mapObjectLikeBonuses.hasValue())
{
bonus.source = BonusSource::OBJECT_TYPE;
bonus.sid = BonusSourceID(building->mapObjectLikeBonuses);
bonus->source = BonusSource::OBJECT_TYPE;
bonus->sid = BonusSourceID(building->mapObjectLikeBonuses);
}
else
{
bonus.source = BonusSource::TOWN_STRUCTURE;
bonus.sid = BonusSourceID(building->getUniqueTypeID());
bonus->source = BonusSource::TOWN_STRUCTURE;
bonus->sid = BonusSourceID(building->getUniqueTypeID());
}
}
}

View File

@@ -58,7 +58,7 @@ class DLL_LINKAGE TownRewardableBuildingInstance : public TownBuildingInstance,
bool wasVisitedBefore(const CGHeroInstance * contextHero) const override;
void grantReward(IGameEventCallback & gameEvents, ui32 rewardID, const CGHeroInstance * hero) const override;
Rewardable::Configuration generateConfiguration(IGameRandomizer & gameRandomizer) const;
void assignBonuses(std::vector<Bonus> & bonuses) const;
void assignBonuses(std::vector<std::shared_ptr<Bonus>> & bonuses) const;
const IObjectInterface * getObject() const override;
bool wasVisited(PlayerColor player) const override;