Skip to content

Commit

Permalink
Merge pull request #1183 from DarkflameUniverse/fastcrashfixes
Browse files Browse the repository at this point in the history
fix: dangling pointer in mission when saving character data
  • Loading branch information
DarwinAnim8or authored Aug 12, 2023
2 parents 5eca25e + 598f4e1 commit 0337449
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 54 deletions.
107 changes: 54 additions & 53 deletions dGame/dMission/Mission.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,10 @@ Mission::Mission(MissionComponent* missionComponent, const uint32_t missionId) {

auto* missionsTable = CDClientManager::Instance().GetTable<CDMissionsTable>();

info = missionsTable->GetPtrByMissionID(missionId);
auto* mis = missionsTable->GetPtrByMissionID(missionId);
info = *mis;

if (info == &CDMissionsTable::Default) {
if (mis == &CDMissionsTable::Default) {
Game::logger->Log("Missions", "Failed to find mission (%i)!", missionId);

return;
Expand Down Expand Up @@ -137,7 +138,7 @@ void Mission::UpdateXml(tinyxml2::XMLElement* element) {

element->DeleteChildren();

element->SetAttribute("id", static_cast<unsigned int>(info->id));
element->SetAttribute("id", static_cast<unsigned int>(info.id));

if (m_Completions > 0) {
element->SetAttribute("cct", static_cast<unsigned int>(m_Completions));
Expand Down Expand Up @@ -212,11 +213,11 @@ User* Mission::GetUser() const {
}

uint32_t Mission::GetMissionId() const {
return info->id;
return info.id;
}

const CDMissions& Mission::GetClientInfo() const {
return *info;
return info;
}

uint32_t Mission::GetCompletions() const {
Expand All @@ -240,15 +241,15 @@ eMissionState Mission::GetMissionState() const {
}

bool Mission::IsAchievement() const {
return !info->isMission;
return !info.isMission;
}

bool Mission::IsMission() const {
return info->isMission;
return info.isMission;
}

bool Mission::IsRepeatable() const {
return info->repeatable;
return info.repeatable;
}

bool Mission::IsComplete() const {
Expand Down Expand Up @@ -284,7 +285,7 @@ void Mission::MakeAvalible() {
}

void Mission::Accept() {
SetMissionTypeState(eMissionLockState::NEW, info->defined_type, info->defined_subtype);
SetMissionTypeState(eMissionLockState::NEW, info.defined_type, info.defined_subtype);

SetMissionState(m_Completions > 0 ? eMissionState::COMPLETE_ACTIVE : eMissionState::ACTIVE);

Expand Down Expand Up @@ -321,16 +322,16 @@ void Mission::Complete(const bool yieldRewards) {

auto* characterComponent = entity->GetComponent<CharacterComponent>();
if (characterComponent != nullptr) {
characterComponent->TrackMissionCompletion(!info->isMission);
characterComponent->TrackMissionCompletion(!info.isMission);
}

auto* missionComponent = entity->GetComponent<MissionComponent>();

missionComponent->Progress(eMissionTaskType::META, info->id);
missionComponent->Progress(eMissionTaskType::META, info.id);

missionComponent->Progress(eMissionTaskType::RACING, info->id, (LWOOBJID)eRacingTaskParam::COMPLETE_ANY_RACING_TASK);
missionComponent->Progress(eMissionTaskType::RACING, info.id, (LWOOBJID)eRacingTaskParam::COMPLETE_ANY_RACING_TASK);

missionComponent->Progress(eMissionTaskType::RACING, info->id, (LWOOBJID)eRacingTaskParam::COMPLETE_TRACK_TASKS);
missionComponent->Progress(eMissionTaskType::RACING, info.id, (LWOOBJID)eRacingTaskParam::COMPLETE_TRACK_TASKS);

auto* missionEmailTable = CDClientManager::Instance().GetTable<CDMissionEmailTable>();

Expand Down Expand Up @@ -441,24 +442,24 @@ void Mission::YieldRewards() {
}

int32_t coinsToSend = 0;
if (info->LegoScore > 0) {
eLootSourceType lootSource = info->isMission ? eLootSourceType::MISSION : eLootSourceType::ACHIEVEMENT;
if (info.LegoScore > 0) {
eLootSourceType lootSource = info.isMission ? eLootSourceType::MISSION : eLootSourceType::ACHIEVEMENT;
if (levelComponent->GetLevel() >= Game::zoneManager->GetWorldConfig()->levelCap) {
// Since the character is at the level cap we reward them with coins instead of UScore.
coinsToSend += info->LegoScore * Game::zoneManager->GetWorldConfig()->levelCapCurrencyConversion;
coinsToSend += info.LegoScore * Game::zoneManager->GetWorldConfig()->levelCapCurrencyConversion;
} else {
characterComponent->SetUScore(characterComponent->GetUScore() + info->LegoScore);
GameMessages::SendModifyLEGOScore(entity, entity->GetSystemAddress(), info->LegoScore, lootSource);
characterComponent->SetUScore(characterComponent->GetUScore() + info.LegoScore);
GameMessages::SendModifyLEGOScore(entity, entity->GetSystemAddress(), info.LegoScore, lootSource);
}
}

if (m_Completions > 0) {
std::vector<std::pair<LOT, uint32_t>> items;

items.emplace_back(info->reward_item1_repeatable, info->reward_item1_repeat_count);
items.emplace_back(info->reward_item2_repeatable, info->reward_item2_repeat_count);
items.emplace_back(info->reward_item3_repeatable, info->reward_item3_repeat_count);
items.emplace_back(info->reward_item4_repeatable, info->reward_item4_repeat_count);
items.emplace_back(info.reward_item1_repeatable, info.reward_item1_repeat_count);
items.emplace_back(info.reward_item2_repeatable, info.reward_item2_repeat_count);
items.emplace_back(info.reward_item3_repeatable, info.reward_item3_repeat_count);
items.emplace_back(info.reward_item4_repeatable, info.reward_item4_repeat_count);

for (const auto& pair : items) {
// Some missions reward zero of an item and so they must be allowed through this clause,
Expand All @@ -478,20 +479,20 @@ void Mission::YieldRewards() {
inventoryComponent->AddItem(pair.first, count, IsMission() ? eLootSourceType::MISSION : eLootSourceType::ACHIEVEMENT);
}

if (info->reward_currency_repeatable > 0 || coinsToSend > 0) {
eLootSourceType lootSource = info->isMission ? eLootSourceType::MISSION : eLootSourceType::ACHIEVEMENT;
character->SetCoins(character->GetCoins() + info->reward_currency_repeatable + coinsToSend, lootSource);
if (info.reward_currency_repeatable > 0 || coinsToSend > 0) {
eLootSourceType lootSource = info.isMission ? eLootSourceType::MISSION : eLootSourceType::ACHIEVEMENT;
character->SetCoins(character->GetCoins() + info.reward_currency_repeatable + coinsToSend, lootSource);
}

return;
}

std::vector<std::pair<LOT, int32_t>> items;

items.emplace_back(info->reward_item1, info->reward_item1_count);
items.emplace_back(info->reward_item2, info->reward_item2_count);
items.emplace_back(info->reward_item3, info->reward_item3_count);
items.emplace_back(info->reward_item4, info->reward_item4_count);
items.emplace_back(info.reward_item1, info.reward_item1_count);
items.emplace_back(info.reward_item2, info.reward_item2_count);
items.emplace_back(info.reward_item3, info.reward_item3_count);
items.emplace_back(info.reward_item4, info.reward_item4_count);

for (const auto& pair : items) {
// Some missions reward zero of an item and so they must be allowed through this clause,
Expand All @@ -511,58 +512,58 @@ void Mission::YieldRewards() {
inventoryComponent->AddItem(pair.first, count, IsMission() ? eLootSourceType::MISSION : eLootSourceType::ACHIEVEMENT);
}

if (info->reward_currency > 0 || coinsToSend > 0) {
eLootSourceType lootSource = info->isMission ? eLootSourceType::MISSION : eLootSourceType::ACHIEVEMENT;
character->SetCoins(character->GetCoins() + info->reward_currency + coinsToSend, lootSource);
if (info.reward_currency > 0 || coinsToSend > 0) {
eLootSourceType lootSource = info.isMission ? eLootSourceType::MISSION : eLootSourceType::ACHIEVEMENT;
character->SetCoins(character->GetCoins() + info.reward_currency + coinsToSend, lootSource);
}

if (info->reward_maxinventory > 0) {
if (info.reward_maxinventory > 0) {
auto* inventory = inventoryComponent->GetInventory(ITEMS);

inventory->SetSize(inventory->GetSize() + info->reward_maxinventory);
inventory->SetSize(inventory->GetSize() + info.reward_maxinventory);
}

if (info->reward_bankinventory > 0) {
if (info.reward_bankinventory > 0) {
auto* inventory = inventoryComponent->GetInventory(eInventoryType::VAULT_ITEMS);
auto modelInventory = inventoryComponent->GetInventory(eInventoryType::VAULT_MODELS);

inventory->SetSize(inventory->GetSize() + info->reward_bankinventory);
modelInventory->SetSize(modelInventory->GetSize() + info->reward_bankinventory);
inventory->SetSize(inventory->GetSize() + info.reward_bankinventory);
modelInventory->SetSize(modelInventory->GetSize() + info.reward_bankinventory);
}

if (info->reward_reputation > 0) {
missionComponent->Progress(eMissionTaskType::EARN_REPUTATION, 0, 0L, "", info->reward_reputation);
if (info.reward_reputation > 0) {
missionComponent->Progress(eMissionTaskType::EARN_REPUTATION, 0, 0L, "", info.reward_reputation);
auto character = entity->GetComponent<CharacterComponent>();
if (character) {
character->SetReputation(character->GetReputation() + info->reward_reputation);
character->SetReputation(character->GetReputation() + info.reward_reputation);
GameMessages::SendUpdateReputation(entity->GetObjectID(), character->GetReputation(), entity->GetSystemAddress());
}
}

if (info->reward_maxhealth > 0) {
destroyableComponent->SetMaxHealth(destroyableComponent->GetMaxHealth() + static_cast<float>(info->reward_maxhealth), true);
if (info.reward_maxhealth > 0) {
destroyableComponent->SetMaxHealth(destroyableComponent->GetMaxHealth() + static_cast<float>(info.reward_maxhealth), true);
}

if (info->reward_maximagination > 0) {
destroyableComponent->SetMaxImagination(destroyableComponent->GetMaxImagination() + static_cast<float>(info->reward_maximagination), true);
if (info.reward_maximagination > 0) {
destroyableComponent->SetMaxImagination(destroyableComponent->GetMaxImagination() + static_cast<float>(info.reward_maximagination), true);
}

Game::entityManager->SerializeEntity(entity);

if (info->reward_emote > 0) {
character->UnlockEmote(info->reward_emote);
if (info.reward_emote > 0) {
character->UnlockEmote(info.reward_emote);
}

if (info->reward_emote2 > 0) {
character->UnlockEmote(info->reward_emote2);
if (info.reward_emote2 > 0) {
character->UnlockEmote(info.reward_emote2);
}

if (info->reward_emote3 > 0) {
character->UnlockEmote(info->reward_emote3);
if (info.reward_emote3 > 0) {
character->UnlockEmote(info.reward_emote3);
}

if (info->reward_emote4 > 0) {
character->UnlockEmote(info->reward_emote4);
if (info.reward_emote4 > 0) {
character->UnlockEmote(info.reward_emote4);
}
}

Expand Down Expand Up @@ -599,7 +600,7 @@ void Mission::SetMissionState(const eMissionState state, const bool sendingRewar
return;
}

GameMessages::SendNotifyMission(entity, entity->GetParentUser()->GetSystemAddress(), info->id, static_cast<int>(state), sendingRewards);
GameMessages::SendNotifyMission(entity, entity->GetParentUser()->GetSystemAddress(), info.id, static_cast<int>(state), sendingRewards);
}

void Mission::SetMissionTypeState(eMissionLockState state, const std::string& type, const std::string& subType) {
Expand Down
2 changes: 1 addition & 1 deletion dGame/dMission/Mission.h
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ class Mission final
/**
* The database information that corresponds to this mission
*/
const CDMissions* info;
CDMissions info;

/**
* The current state this mission is in
Expand Down

0 comments on commit 0337449

Please sign in to comment.