Profile(Manager),SaveTool: improve error handling and fix bugs.

This commit is contained in:
Guillaume Jacquemin 2022-02-24 14:00:47 +01:00
parent 350ad59f8e
commit a1c17b7138
4 changed files with 67 additions and 51 deletions

View file

@ -45,7 +45,13 @@ Profile::Profile(const std::string& path):
_type = ProfileType::FullGame; _type = ProfileType::FullGame;
} }
_account = Utility::String::ltrim(Utility::String::rtrim(_filename, ".sav"), (_type == ProfileType::Demo ? "Demo" : "") + std::string{"Profile"}); auto account_prop = _profile.at<StringProperty>("Account");
if(!account_prop) {
_lastError = "Couldn't find an account ID in " + _filename;
_valid = false;
return;
}
_account = account_prop->value;
if(Utility::String::beginsWith(_account, "PMCSlot")) { if(Utility::String::beginsWith(_account, "PMCSlot")) {
_version = ProfileVersion::Normal; _version = ProfileVersion::Normal;
@ -55,8 +61,6 @@ Profile::Profile(const std::string& path):
} }
refreshValues(); refreshValues();
_valid = _profile.valid();
} }
auto Profile::valid() const -> bool { auto Profile::valid() const -> bool {
@ -90,7 +94,13 @@ void Profile::refreshValues() {
return; return;
} }
_name = _profile.at<StringProperty>("CompanyName")->value; auto name_prop = _profile.at<StringProperty>("CompanyName");
if(!name_prop) {
_lastError = "No company name in " + _filename;
_valid = false;
return;
}
_name = name_prop->value;
auto prop = _profile.at<IntProperty>("ActiveFrameSlot"); auto prop = _profile.at<IntProperty>("ActiveFrameSlot");
_activeFrameSlot = prop ? prop->value : 0; _activeFrameSlot = prop ? prop->value : 0;
@ -127,6 +137,8 @@ void Profile::refreshValues() {
_muscularConstruction = getResource("ResourceQuarkData", MuscularConstruction); _muscularConstruction = getResource("ResourceQuarkData", MuscularConstruction);
_mineralExoskeletology = getResource("ResourceQuarkData", MineralExoskeletology); _mineralExoskeletology = getResource("ResourceQuarkData", MineralExoskeletology);
_carbonisedSkin = getResource("ResourceQuarkData", CarbonisedSkin); _carbonisedSkin = getResource("ResourceQuarkData", CarbonisedSkin);
_valid = true;
} }
auto Profile::companyName() const -> std::string const& { auto Profile::companyName() const -> std::string const& {
@ -135,11 +147,16 @@ auto Profile::companyName() const -> std::string const& {
auto Profile::renameCompany(const std::string& new_name) -> bool { auto Profile::renameCompany(const std::string& new_name) -> bool {
auto name_prop = _profile.at<StringProperty>("CompanyName"); auto name_prop = _profile.at<StringProperty>("CompanyName");
if(!name_prop) {
_lastError = "No company name in " + _filename;
_valid = false;
return false;
}
name_prop->value = new_name; name_prop->value = new_name;
if(!_profile.saveToFile()) { if(!_profile.saveToFile()) {
_lastError = "Couldn't save the profile."; _lastError = _profile.lastError();
return false; return false;
} }
@ -159,13 +176,14 @@ auto Profile::setCredits(Int amount) -> bool {
if(!credits_prop) { if(!credits_prop) {
credits_prop = new IntProperty; credits_prop = new IntProperty;
credits_prop->name.emplace("Credit");
_profile.appendProperty(IntProperty::ptr{credits_prop}); _profile.appendProperty(IntProperty::ptr{credits_prop});
} }
credits_prop->value = amount; credits_prop->value = amount;
if(!_profile.saveToFile()) { if(!_profile.saveToFile()) {
_lastError = "Couldn't save the profile."; _lastError = _profile.lastError();
return false; return false;
} }
@ -181,13 +199,14 @@ auto Profile::setStoryProgress(Int progress) -> bool {
if(!story_progress_prop) { if(!story_progress_prop) {
story_progress_prop = new IntProperty; story_progress_prop = new IntProperty;
story_progress_prop->name.emplace("StoryProgress");
_profile.appendProperty(IntProperty::ptr{story_progress_prop}); _profile.appendProperty(IntProperty::ptr{story_progress_prop});
} }
story_progress_prop->value = progress; story_progress_prop->value = progress;
if(!_profile.saveToFile()) { if(!_profile.saveToFile()) {
_lastError = "Couldn't save the profile."; _lastError = _profile.lastError();
return false; return false;
} }
@ -358,7 +377,7 @@ auto Profile::setCarbonisedSkin(Int amount) -> bool {
return setResource("ResourceQuarkData", CarbonisedSkin, amount); return setResource("ResourceQuarkData", CarbonisedSkin, amount);
} }
auto Profile::getResource(const char* container, Int id) -> Int { auto Profile::getResource(const char* container, MaterialID id) -> Int {
auto mats_prop = _profile.at<ArrayProperty>(container); auto mats_prop = _profile.at<ArrayProperty>(container);
if(!mats_prop) { if(!mats_prop) {
@ -374,14 +393,16 @@ auto Profile::getResource(const char* container, Int id) -> Int {
return it != mats_prop->items.end() ? static_cast<ResourceItemValue*>(it->get())->quantity : 0; return it != mats_prop->items.end() ? static_cast<ResourceItemValue*>(it->get())->quantity : 0;
} }
auto Profile::setResource(const char* container, Int id, Int amount) -> bool { auto Profile::setResource(const char* container, MaterialID id, Int amount) -> bool {
auto mats_prop = _profile.at<ArrayProperty>(container); auto mats_prop = _profile.at<ArrayProperty>(container);
if(!mats_prop) { if(!mats_prop) {
_lastError = "Couldn't find " + std::string{container} + " in " + _filename;
_valid = false;
return false; return false;
} }
static auto predicate = [&id](UnrealPropertyBase::ptr& prop){ auto predicate = [&id](UnrealPropertyBase::ptr& prop){
auto res_prop = static_cast<ResourceItemValue*>(prop.get()); auto res_prop = static_cast<ResourceItemValue*>(prop.get());
return res_prop->id == id; return res_prop->id == id;
}; };
@ -406,5 +427,5 @@ auto Profile::setResource(const char* container, Int id, Int amount) -> bool {
return false; return false;
} }
return _profile.saveToFile(); return true;
} }

View file

@ -126,8 +126,8 @@ class Profile {
auto setCarbonisedSkin(Int amount) -> bool; auto setCarbonisedSkin(Int amount) -> bool;
private: private:
auto getResource(const char* container, Int id) -> Int; auto getResource(const char* container, MaterialID id) -> Int;
auto setResource(const char* container, Int id, Int amount) -> bool; auto setResource(const char* container, MaterialID id, Int amount) -> bool;
std::string _filename; std::string _filename;

View file

@ -76,7 +76,7 @@ auto ProfileManager::refreshProfiles() -> bool {
} }
if(_profiles.empty()) { if(_profiles.empty()) {
_lastError = "No profiles were found."; _lastError = "No valid profiles were found.";
return false; return false;
} }

View file

@ -225,8 +225,7 @@ void SaveTool::drawGeneralInfo() {
} }
if(drawRenamePopup(name_buf)) { if(drawRenamePopup(name_buf)) {
if(!_currentProfile->renameCompany(name_buf.data())) { if(!_currentProfile->renameCompany(name_buf.data())) {
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Error", _queue.addToast(Toast::Type::Error, _currentProfile->lastError());
_currentProfile->lastError().c_str(), window());
} }
} }
@ -243,8 +242,7 @@ void SaveTool::drawGeneralInfo() {
} }
if(drawIntEditPopup(&credits, 20000000)) { if(drawIntEditPopup(&credits, 20000000)) {
if(!_currentProfile->setCredits(credits)) { if(!_currentProfile->setCredits(credits)) {
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Error", _queue.addToast(Toast::Type::Error, _currentProfile->lastError());
_currentProfile->lastError().c_str(), window());
} }
} }
@ -263,8 +261,7 @@ void SaveTool::drawGeneralInfo() {
if(!sp.after) { if(!sp.after) {
if(ImGui::MenuItem(sp.point)) { if(ImGui::MenuItem(sp.point)) {
if(!_currentProfile->setStoryProgress(sp.id)) { if(!_currentProfile->setStoryProgress(sp.id)) {
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Error", _queue.addToast(Toast::Type::Error, _currentProfile->lastError());
_currentProfile->lastError().c_str(), window());
} }
} }
} }
@ -272,8 +269,7 @@ void SaveTool::drawGeneralInfo() {
if(ImGui::BeginMenu(sp.after)) { if(ImGui::BeginMenu(sp.after)) {
if(ImGui::MenuItem(sp.point)) { if(ImGui::MenuItem(sp.point)) {
if(!_currentProfile->setStoryProgress(sp.id)) { if(!_currentProfile->setStoryProgress(sp.id)) {
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Error", _queue.addToast(Toast::Type::Error, _currentProfile->lastError());
_currentProfile->lastError().c_str(), window());
} }
} }
ImGui::EndMenu(); ImGui::EndMenu();
@ -300,34 +296,33 @@ void SaveTool::drawResearchInventory() {
ImGui::TableSetColumnIndex(2); \ ImGui::TableSetColumnIndex(2); \
ImGui::TextDisabled("Unavailable as of game version " SUPPORTED_GAME_VERSION); ImGui::TextDisabled("Unavailable as of game version " SUPPORTED_GAME_VERSION);
#define matRow(name, tier, var, getter, setter) \ #define matRow(name, tier, var, getter, setter) \
ImGui::TableNextRow(); \ ImGui::TableNextRow(); \
ImGui::TableSetColumnIndex(0); \ ImGui::TableSetColumnIndex(0); \
ImGui::TextUnformatted("T" #tier); \ ImGui::TextUnformatted("T" #tier); \
ImGui::TableSetColumnIndex(1); \ ImGui::TableSetColumnIndex(1); \
ImGui::TextUnformatted(name); \ ImGui::TextUnformatted(name); \
ImGui::TableSetColumnIndex(2); \ ImGui::TableSetColumnIndex(2); \
if(_currentProfile->getter() != -1) { \ if(_currentProfile->getter() != -1) { \
ImGui::Text("%i", _currentProfile->getter()); \ ImGui::Text("%i", _currentProfile->getter()); \
if(_cheatMode) { \ if(_cheatMode) { \
ImGui::TableSetColumnIndex(3); \ ImGui::TableSetColumnIndex(3); \
ImGui::PushID(#setter); \ ImGui::PushID(#setter); \
static Int var = _currentProfile->getter(); \ static Int var = _currentProfile->getter(); \
if(drawUnsafeWidget([]{ return ImGui::SmallButton(ICON_FA_EDIT); })) { \ if(drawUnsafeWidget(ImGui::SmallButton, ICON_FA_EDIT)) { \
(var) = _currentProfile->getter(); \ (var) = _currentProfile->getter(); \
ImGui::OpenPopup("int_edit"); \ ImGui::OpenPopup("int_edit"); \
} \ } \
if(drawIntEditPopup(&(var), 9999)) { \ if(drawIntEditPopup(&(var), 9999)) { \
if(!_currentProfile->set##setter((var))) { \ if(!_currentProfile->set##setter((var))) { \
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Error", \ _queue.addToast(Toast::Type::Error, _currentProfile->lastError()); \
_currentProfile->lastError().c_str(), window()); \ } \
} \ } \
} \ ImGui::PopID(); \
ImGui::PopID(); \ } \
} \ } \
} \ else { \
else { \ ImGui::TextDisabled("Not found in the save file"); \
ImGui::TextDisabled("Not found in the save file"); \
} }
if(ImGui::BeginTable("##ResearchInventoryTable", 4, if(ImGui::BeginTable("##ResearchInventoryTable", 4,
@ -454,7 +449,7 @@ void SaveTool::drawMassManager() {
_massManager->lastError().c_str(), window()); _massManager->lastError().c_str(), window());
} }
} }
else if(const ImGuiPayload* payload = ImGui::AcceptDragDropPayload("Mass")) { else if((payload = ImGui::AcceptDragDropPayload("Mass"))) {
if(payload->DataSize != sizeof(int)) { if(payload->DataSize != sizeof(int)) {
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Fatal error", SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Fatal error",
"payload->DataSize != sizeof(int) in SaveTool::drawMassManager()", "payload->DataSize != sizeof(int) in SaveTool::drawMassManager()",