2023-08-06 21:33:15 +02:00
|
|
|
#include <hex/api/achievement_manager.hpp>
|
2023-11-30 11:23:12 +01:00
|
|
|
#include <hex/api/event_manager.hpp>
|
2023-08-06 21:33:15 +02:00
|
|
|
|
2024-01-30 11:21:34 +01:00
|
|
|
#include <hex/helpers/auto_reset.hpp>
|
|
|
|
|
2023-08-06 21:33:15 +02:00
|
|
|
#include <nlohmann/json.hpp>
|
|
|
|
|
|
|
|
namespace hex {
|
|
|
|
|
|
|
|
std::unordered_map<std::string, std::unordered_map<std::string, std::unique_ptr<Achievement>>> &AchievementManager::getAchievements() {
|
2024-01-30 11:21:34 +01:00
|
|
|
static AutoReset<std::unordered_map<std::string, std::unordered_map<std::string, std::unique_ptr<Achievement>>>> achievements;
|
2023-08-06 21:33:15 +02:00
|
|
|
|
|
|
|
return achievements;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::unordered_map<std::string, std::list<AchievementManager::AchievementNode>>& AchievementManager::getAchievementNodes(bool rebuild) {
|
2024-01-30 11:21:34 +01:00
|
|
|
static AutoReset<std::unordered_map<std::string, std::list<AchievementNode>>> nodeCategoryStorage;
|
2023-08-06 21:33:15 +02:00
|
|
|
|
2024-01-30 11:21:34 +01:00
|
|
|
if (!nodeCategoryStorage->empty() || !rebuild)
|
2023-08-06 21:33:15 +02:00
|
|
|
return nodeCategoryStorage;
|
|
|
|
|
2024-01-30 11:21:34 +01:00
|
|
|
nodeCategoryStorage->clear();
|
2023-08-06 21:33:15 +02:00
|
|
|
|
|
|
|
// Add all achievements to the node storage
|
|
|
|
for (auto &[categoryName, achievements] : getAchievements()) {
|
2024-01-30 11:21:34 +01:00
|
|
|
auto &nodes = (*nodeCategoryStorage)[categoryName];
|
2023-08-06 21:33:15 +02:00
|
|
|
|
|
|
|
for (auto &[achievementName, achievement] : achievements) {
|
|
|
|
nodes.emplace_back(achievement.get());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nodeCategoryStorage;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::unordered_map<std::string, std::vector<AchievementManager::AchievementNode*>>& AchievementManager::getAchievementStartNodes(bool rebuild) {
|
2024-01-30 11:21:34 +01:00
|
|
|
static AutoReset<std::unordered_map<std::string, std::vector<AchievementNode*>>> startNodes;
|
2023-08-06 21:33:15 +02:00
|
|
|
|
2024-01-30 11:21:34 +01:00
|
|
|
if (!startNodes->empty() || !rebuild)
|
2023-08-06 21:33:15 +02:00
|
|
|
return startNodes;
|
|
|
|
|
|
|
|
auto &nodeCategoryStorage = getAchievementNodes();
|
|
|
|
|
2024-01-30 11:21:34 +01:00
|
|
|
startNodes->clear();
|
2023-08-06 21:33:15 +02:00
|
|
|
|
|
|
|
// Add all parents and children to the nodes
|
|
|
|
for (auto &[categoryName, achievements] : nodeCategoryStorage) {
|
|
|
|
for (auto &achievementNode : achievements) {
|
|
|
|
for (auto &requirement : achievementNode.achievement->getRequirements()) {
|
|
|
|
for (auto &[requirementCategoryName, requirementAchievements] : nodeCategoryStorage) {
|
2024-01-30 11:21:34 +01:00
|
|
|
auto iter = std::ranges::find_if(requirementAchievements, [&requirement](auto &node) {
|
2023-08-06 21:33:15 +02:00
|
|
|
return node.achievement->getUnlocalizedName() == requirement;
|
|
|
|
});
|
|
|
|
|
|
|
|
if (iter != requirementAchievements.end()) {
|
|
|
|
achievementNode.parents.emplace_back(&*iter);
|
|
|
|
iter->children.emplace_back(&achievementNode);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto &requirement : achievementNode.achievement->getVisibilityRequirements()) {
|
|
|
|
for (auto &[requirementCategoryName, requirementAchievements] : nodeCategoryStorage) {
|
2024-01-30 11:21:34 +01:00
|
|
|
auto iter = std::ranges::find_if(requirementAchievements, [&requirement](auto &node) {
|
2023-08-06 21:33:15 +02:00
|
|
|
return node.achievement->getUnlocalizedName() == requirement;
|
|
|
|
});
|
|
|
|
|
|
|
|
if (iter != requirementAchievements.end()) {
|
|
|
|
achievementNode.visibilityParents.emplace_back(&*iter);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto &[categoryName, achievements] : nodeCategoryStorage) {
|
|
|
|
for (auto &achievementNode : achievements) {
|
|
|
|
if (!achievementNode.hasParents()) {
|
2024-01-30 11:21:34 +01:00
|
|
|
(*startNodes)[categoryName].emplace_back(&achievementNode);
|
2023-08-06 21:33:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for (const auto &parent : achievementNode.parents) {
|
|
|
|
if (parent->achievement->getUnlocalizedCategory() != achievementNode.achievement->getUnlocalizedCategory())
|
2024-01-30 11:21:34 +01:00
|
|
|
(*startNodes)[categoryName].emplace_back(&achievementNode);
|
2023-08-06 21:33:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return startNodes;
|
|
|
|
}
|
|
|
|
|
2023-12-19 12:22:28 +01:00
|
|
|
void AchievementManager::unlockAchievement(const UnlocalizedString &unlocalizedCategory, const UnlocalizedString &unlocalizedName) {
|
2023-08-06 21:33:15 +02:00
|
|
|
auto &categories = getAchievements();
|
|
|
|
|
|
|
|
auto categoryIter = categories.find(unlocalizedCategory);
|
|
|
|
if (categoryIter == categories.end()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto &[categoryName, achievements] = *categoryIter;
|
|
|
|
|
2024-01-30 11:21:34 +01:00
|
|
|
const auto achievementIter = achievements.find(unlocalizedName);
|
2023-08-06 21:33:15 +02:00
|
|
|
if (achievementIter == achievements.end()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-01-30 11:21:34 +01:00
|
|
|
const auto &nodes = getAchievementNodes()[categoryName];
|
2023-08-06 21:33:15 +02:00
|
|
|
for (const auto &node : nodes) {
|
|
|
|
auto &achievement = node.achievement;
|
|
|
|
|
|
|
|
if (achievement->getUnlocalizedCategory() != unlocalizedCategory) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (achievement->getUnlocalizedName() != unlocalizedName) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (node.achievement->isUnlocked()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const auto &requirement : node.parents) {
|
|
|
|
if (!requirement->achievement->isUnlocked()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
achievement->setUnlocked(true);
|
|
|
|
|
|
|
|
if (achievement->isUnlocked())
|
2023-12-08 10:29:44 +01:00
|
|
|
EventAchievementUnlocked::post(*achievement);
|
2023-10-20 13:34:45 +02:00
|
|
|
|
|
|
|
return;
|
2023-08-06 21:33:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AchievementManager::clear() {
|
|
|
|
getAchievements().clear();
|
|
|
|
getAchievementStartNodes(false).clear();
|
|
|
|
getAchievementNodes(false).clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
void AchievementManager::clearTemporary() {
|
|
|
|
auto &categories = getAchievements();
|
|
|
|
for (auto &[categoryName, achievements] : categories) {
|
|
|
|
std::erase_if(achievements, [](auto &data) {
|
|
|
|
auto &[achievementName, achievement] = data;
|
|
|
|
return achievement->isTemporary();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
std::erase_if(categories, [](auto &data) {
|
|
|
|
auto &[categoryName, achievements] = data;
|
|
|
|
return achievements.empty();
|
|
|
|
});
|
|
|
|
|
|
|
|
getAchievementStartNodes(false).clear();
|
|
|
|
getAchievementNodes(false).clear();
|
|
|
|
}
|
|
|
|
|
2023-11-16 13:23:28 +01:00
|
|
|
std::pair<u32, u32> AchievementManager::getProgress() {
|
|
|
|
u32 unlocked = 0;
|
|
|
|
u32 total = 0;
|
|
|
|
|
|
|
|
for (auto &[categoryName, achievements] : getAchievements()) {
|
|
|
|
for (auto &[achievementName, achievement] : achievements) {
|
|
|
|
total += 1;
|
|
|
|
if (achievement->isUnlocked()) {
|
|
|
|
unlocked += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return { unlocked, total };
|
|
|
|
}
|
|
|
|
|
2023-08-06 21:33:15 +02:00
|
|
|
void AchievementManager::achievementAdded() {
|
|
|
|
getAchievementStartNodes(false).clear();
|
|
|
|
getAchievementNodes(false).clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr static auto AchievementsFile = "achievements.json";
|
|
|
|
|
|
|
|
void AchievementManager::loadProgress() {
|
|
|
|
for (const auto &directory : fs::getDefaultPaths(fs::ImHexPath::Config)) {
|
|
|
|
auto path = directory / AchievementsFile;
|
|
|
|
|
|
|
|
if (!wolv::io::fs::exists(path)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
wolv::io::File file(path, wolv::io::File::Mode::Read);
|
|
|
|
|
|
|
|
if (!file.isValid()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
auto json = nlohmann::json::parse(file.readString());
|
|
|
|
|
|
|
|
for (const auto &[categoryName, achievements] : getAchievements()) {
|
|
|
|
for (const auto &[achievementName, achievement] : achievements) {
|
|
|
|
try {
|
2023-12-31 13:53:28 +01:00
|
|
|
const auto &progress = json[categoryName][achievementName];
|
|
|
|
if (progress.is_null())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
achievement->setProgress(progress);
|
2023-08-06 21:33:15 +02:00
|
|
|
} catch (const std::exception &e) {
|
|
|
|
log::warn("Failed to load achievement progress for '{}::{}': {}", categoryName, achievementName, e.what());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (const std::exception &e) {
|
|
|
|
log::error("Failed to load achievements: {}", e.what());
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AchievementManager::storeProgress() {
|
|
|
|
for (const auto &directory : fs::getDefaultPaths(fs::ImHexPath::Config)) {
|
|
|
|
auto path = directory / AchievementsFile;
|
|
|
|
|
2023-12-27 16:57:44 +01:00
|
|
|
wolv::io::File file(path, wolv::io::File::Mode::Write);
|
2023-08-06 21:33:15 +02:00
|
|
|
|
|
|
|
if (!file.isValid()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
nlohmann::json json;
|
|
|
|
|
|
|
|
for (const auto &[categoryName, achievements] : getAchievements()) {
|
|
|
|
json[categoryName] = nlohmann::json::object();
|
|
|
|
|
|
|
|
for (const auto &[achievementName, achievement] : achievements) {
|
|
|
|
json[categoryName][achievementName] = achievement->getProgress();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-30 11:21:34 +01:00
|
|
|
const auto result = json.dump(4);
|
2023-12-27 16:57:44 +01:00
|
|
|
file.setSize(0);
|
|
|
|
file.writeString(result);
|
2023-08-06 21:33:15 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|