Added an experimental way to rename the company.

This commit is contained in:
Guillaume Jacquemin 2020-10-05 10:36:08 +02:00
parent 932052ab0a
commit 4c0f1b8ce5
4 changed files with 84 additions and 1 deletions

View File

@ -174,6 +174,36 @@ void EvtMainFrame::backupSelectedProfileEvent(wxCommandEvent&) {
}
}
void EvtMainFrame::companyRenameEvent(wxMouseEvent&) {
const static std::string error_prefix = "Rename failed:\n\n";
EvtNameChangeDialog dialog{this};
dialog.setName(_profileManager.currentProfile()->companyName());
int result = dialog.ShowModal();
if(result == wxID_OK) {
switch(_mbManager.gameState()) {
case GameState::Unknown:
errorMessage(error_prefix + "For security reasons, renaming the company is disabled if the game's status is unknown.");
break;
case GameState::NotRunning:
if(!_profileManager.currentProfile()->renameCompany(dialog.getName())) {
errorMessage(error_prefix + _profileManager.currentProfile()->lastError());
}
else {
_profileChoice->SetString(_profileChoice->GetCurrentSelection(),
wxString::Format("%s%s",
_profileManager.currentProfile()->companyName(),
_profileManager.currentProfile()->type() == ProfileType::Demo ? " (Demo)" : ""));
}
break;
case GameState::Running:
errorMessage(error_prefix + "Renaming the company is disabled while the game is running.");
break;
}
}
}
void EvtMainFrame::importMassEvent(wxCommandEvent&) {
const static std::string error_prefix = "Importing failed:\n\n";
@ -537,7 +567,7 @@ void EvtMainFrame::screenshotFileEventHandler(int event_type, const wxString& ev
void EvtMainFrame::updateProfileStats() {
Profile* current_profile = _profileManager.currentProfile();
_companyName->SetLabel(current_profile->companyName());
_companyName->SetLabel(current_profile->getCompanyName());
_credits->SetLabel(wxString::Format("%i", current_profile->getCredits()));
_storyProgress->SetLabel(wxString::Format("%i", current_profile->getStoryProgress()));
_lastMissionId->SetLabel(wxString::Format("%s", mission_id_map.find(current_profile->getLastMissionId()) != mission_id_map.end() ? mission_id_map.at(current_profile->lastMissionId()) : std::to_string(current_profile->lastMissionId())));

View File

@ -45,6 +45,7 @@ class EvtMainFrame: public MainFrame {
// Profile-related events
void profileSelectionEvent(wxCommandEvent&);
void backupSelectedProfileEvent(wxCommandEvent&);
void companyRenameEvent(wxMouseEvent&);
// M.A.S.S.-related events
void importMassEvent(wxCommandEvent&);

View File

@ -96,6 +96,56 @@ auto Profile::companyName() const -> std::string const& {
return _companyName;
}
auto Profile::getCompanyName() -> std::string const& {
auto mmap = Utility::Directory::mapRead(Utility::Directory::join(_profileDirectory, _filename));
auto it = std::search(mmap.begin(), mmap.end(), &company_name_locator[0], &company_name_locator[27]);
if(it == mmap.end()) {
_lastError = "Couldn't find a company name in " + _filename;
_companyName = "";
}
else {
_companyName = std::string{it + 41};
}
return _companyName;
}
auto Profile::renameCompany(const std::string& new_name) -> bool {
char length_difference = static_cast<char>(_companyName.length() - new_name.length());
std::string profile_data = Utility::Directory::readString(Utility::Directory::join(_profileDirectory, _filename));
auto iter = std::search(profile_data.begin(), profile_data.end(), &company_name_locator[0], &company_name_locator[27]);
if(iter != profile_data.end()) {
*(iter + 0x1C) = *(iter + 0x1C) - length_difference;
*(iter + 0x25) = *(iter + 0x25) - length_difference;
while(*(iter + 0x29) != '\0') {
profile_data.erase(iter + 0x29);
}
profile_data.insert(iter + 0x29, new_name.cbegin(), new_name.cend());
if(!Utility::Directory::writeString(Utility::Directory::join(_profileDirectory, _filename), profile_data)) {
_lastError = "The file" + _filename + " couldn't be written to.";
return false;
}
_companyName = new_name;
return true;
}
else {
_lastError = "Couldn't find the company name in " + _filename;
return false;
}
}
auto Profile::activeFrameSlot() const -> std::int8_t {
return _activeFrameSlot;
}

View File

@ -25,6 +25,8 @@ class Profile {
auto steamId() const -> std::string const&;
auto companyName() const -> std::string const&;
auto getCompanyName() -> std::string const&;
auto renameCompany(const std::string& new_name) -> bool;
auto activeFrameSlot() const -> std::int8_t;
auto getActiveFrameSlot() -> std::int8_t;