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;
}
_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")) {
_version = ProfileVersion::Normal;
@ -55,8 +61,6 @@ Profile::Profile(const std::string& path):
}
refreshValues();
_valid = _profile.valid();
}
auto Profile::valid() const -> bool {
@ -90,7 +94,13 @@ void Profile::refreshValues() {
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");
_activeFrameSlot = prop ? prop->value : 0;
@ -127,6 +137,8 @@ void Profile::refreshValues() {
_muscularConstruction = getResource("ResourceQuarkData", MuscularConstruction);
_mineralExoskeletology = getResource("ResourceQuarkData", MineralExoskeletology);
_carbonisedSkin = getResource("ResourceQuarkData", CarbonisedSkin);
_valid = true;
}
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 name_prop = _profile.at<StringProperty>("CompanyName");
if(!name_prop) {
_lastError = "No company name in " + _filename;
_valid = false;
return false;
}
name_prop->value = new_name;
if(!_profile.saveToFile()) {
_lastError = "Couldn't save the profile.";
_lastError = _profile.lastError();
return false;
}
@ -159,13 +176,14 @@ auto Profile::setCredits(Int amount) -> bool {
if(!credits_prop) {
credits_prop = new IntProperty;
credits_prop->name.emplace("Credit");
_profile.appendProperty(IntProperty::ptr{credits_prop});
}
credits_prop->value = amount;
if(!_profile.saveToFile()) {
_lastError = "Couldn't save the profile.";
_lastError = _profile.lastError();
return false;
}
@ -181,13 +199,14 @@ auto Profile::setStoryProgress(Int progress) -> bool {
if(!story_progress_prop) {
story_progress_prop = new IntProperty;
story_progress_prop->name.emplace("StoryProgress");
_profile.appendProperty(IntProperty::ptr{story_progress_prop});
}
story_progress_prop->value = progress;
if(!_profile.saveToFile()) {
_lastError = "Couldn't save the profile.";
_lastError = _profile.lastError();
return false;
}
@ -358,7 +377,7 @@ auto Profile::setCarbonisedSkin(Int amount) -> bool {
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);
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;
}
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);
if(!mats_prop) {
_lastError = "Couldn't find " + std::string{container} + " in " + _filename;
_valid = false;
return false;
}
static auto predicate = [&id](UnrealPropertyBase::ptr& prop){
auto predicate = [&id](UnrealPropertyBase::ptr& prop){
auto res_prop = static_cast<ResourceItemValue*>(prop.get());
return res_prop->id == id;
};
@ -406,5 +427,5 @@ auto Profile::setResource(const char* container, Int id, Int amount) -> bool {
return false;
}
return _profile.saveToFile();
return true;
}

View File

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

View File

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

View File

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