Skip to content

Commit

Permalink
Handle lack of profiles more gracefully
Browse files Browse the repository at this point in the history
  • Loading branch information
IonAgorria committed Dec 31, 2023
1 parent de3cb8a commit c921893
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 74 deletions.
4 changes: 3 additions & 1 deletion Source/UserInterface/BattleMenu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,9 @@ void startBattle(int pos, CShellWindow* pWnd) {
}
}

missionToExec.getActivePlayerData().setName(gameShell->currentSingleProfile.getCurrentProfile().name);
if (const Profile* profile = gameShell->currentSingleProfile.getCurrentProfile()) {
missionToExec.getActivePlayerData().setName(profile->name);
}

//Write current page UI settings into MD
copyToMD();
Expand Down
12 changes: 7 additions & 5 deletions Source/UserInterface/GameShell.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,9 @@ windowClientSize_(1024, 768)

currentSingleProfile.scanProfiles();
currentSingleProfile.setCurrentProfile(getStringSettings("ProfileName"));
if (!MainMenuEnable && currentSingleProfile.getCurrentProfileIndex() == -1) {
if (!currentSingleProfile.getProfilesVector().size()) {
currentSingleProfile.addProfile("Test");
if (!MainMenuEnable && !currentSingleProfile.isValidProfile()) {
if (currentSingleProfile.getProfilesVector().empty()) {
currentSingleProfile.addProfile("Legate");
}
currentSingleProfile.setCurrentProfileIndex(0);
}
Expand Down Expand Up @@ -438,8 +438,10 @@ void GameShell::GameStart(const MissionDescription& mission)
PlayerData* data = &CurrentMission.playersData[i];
std::string playerName;
if (data->realPlayerType == REAL_PLAYER_TYPE_PLAYER && *(data->name()) == 0) {
if (currentSingleProfile.getCurrentProfileIndex() == -1 && currentSingleProfile.getLastGameType() != UserSingleProfile::MULTIPLAYER) {
playerName = currentSingleProfile.getCurrentProfile().name;
if (currentSingleProfile.isValidProfile() && currentSingleProfile.getLastGameType() != UserSingleProfile::MULTIPLAYER) {
playerName = currentSingleProfile.getCurrentProfile()->name;
} else {
playerName = getBelligerentName(data->belligerent);
}
} else if (data->realPlayerType == REAL_PLAYER_TYPE_AI) {
playerName = getBelligerentName(data->belligerent);
Expand Down
14 changes: 9 additions & 5 deletions Source/UserInterface/MainMenu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -960,13 +960,17 @@ int SwitchMenuScreenQuant1( float, float ) {
{
//fill mission list
PlayMusic( mainMenuMusic );
CComboWindow* diffCombo = (CComboWindow*)_shellIconManager.GetWnd(SQSH_MM_DIFFICULTY_COMBO);
diffCombo->pos = gameShell->currentSingleProfile.getDifficulty();
int lastWinnedMissionNumber = 0;
if (const Profile* profile = gameShell->currentSingleProfile.getCurrentProfile()) {
CComboWindow* diffCombo = (CComboWindow*) _shellIconManager.GetWnd(
SQSH_MM_DIFFICULTY_COMBO);
diffCombo->pos = profile->difficulty;
lastWinnedMissionNumber = profile->lastMissionNumber;
}
CListBoxWindow* list = (CListBoxWindow*)_shellIconManager.GetWnd(SQSH_MM_MISSION_LIST);
list->NewItem(1);
list->Clear();

int lastWinnedMissionNumber = gameShell->currentSingleProfile.getLastMissionNumber();
if (lastWinnedMissionNumber >= historyScene.missionCount()) {
lastWinnedMissionNumber = historyScene.missionCount() - 1;
}
Expand Down Expand Up @@ -1105,8 +1109,8 @@ int SwitchMenuScreenQuant1( float, float ) {
case SQSH_MM_MULTIPLAYER_LIST_SCR:
{
std::string name = getStringSettings(regLanName);
if (name.empty() && gameShell->currentSingleProfile.getCurrentProfileIndex() != -1) {
name = gameShell->currentSingleProfile.getCurrentProfile().name;
if (name.empty() && gameShell->currentSingleProfile.isValidProfile()) {
name = gameShell->currentSingleProfile.getCurrentProfile()->name;
}
CEditWindow* input = (CEditWindow*)_shellIconManager.GetWnd(SQSH_MM_MULTIPLAYER_NAME_INPUT);
if (!name.empty()) {
Expand Down
38 changes: 21 additions & 17 deletions Source/UserInterface/Menu/SingleMenu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ void onMMSingleButton(CShellWindow* pWnd, InterfaceEventCode code, int param) {
void showSingleMenu(CShellWindow* pWnd) {
// CShellPushButton* txtWnd = dynamic_cast<CShellPushButton*>(_shellIconManager.GetWnd(SQSH_MM_PROFILE_BTN));
// txtWnd->setText(gameShell->currentSingleProfile.getName());
if (gameShell->currentSingleProfile.getCurrentProfileIndex() == -1) {
_shellIconManager.SwitchMenuScreens(pWnd->m_pParent->ID, SQSH_MM_PROFILE_SCR);
} else {
if (gameShell->currentSingleProfile.isValidProfile()) {
_shellIconManager.SwitchMenuScreens(pWnd->m_pParent->ID, SQSH_MM_SINGLE_SCR);
} else {
_shellIconManager.SwitchMenuScreens(pWnd->m_pParent->ID, SQSH_MM_PROFILE_SCR);
}
}

Expand Down Expand Up @@ -65,7 +65,7 @@ void fillProfileList() {
for (int i = 0, s = profiles.size(); i < s; i++) {
list->AddString( profiles[i].name.c_str(), 0 );
}
if (gameShell->currentSingleProfile.getCurrentProfileIndex() != -1) {
if (gameShell->currentSingleProfile.isValidProfile()) {
list->SetCurSel(gameShell->currentSingleProfile.getCurrentProfileIndex());
} else {
list->SetCurSel(list->GetItemCount() - 1);
Expand All @@ -78,8 +78,10 @@ void onMMProfileList(CShellWindow* pWnd, InterfaceEventCode code, int param) {
int pos = list->GetCurSel();
if (pos != -1) {
gameShell->currentSingleProfile.setCurrentProfileIndex( pos );
putStringSettings("ProfileName", gameShell->currentSingleProfile.getCurrentProfile().name);
showSingleMenu(pWnd);
if (const Profile* profile = gameShell->currentSingleProfile.getCurrentProfile()) {
putStringSettings("ProfileName", profile->name);
showSingleMenu(pWnd);
}
}
}
}
Expand Down Expand Up @@ -132,16 +134,18 @@ void onMMSelectProfileButton(CShellWindow* pWnd, InterfaceEventCode code, int pa
if ( code == EVENT_UNPRESSED && intfCanHandleInput() && pWnd->isEnabled() ) {
CListBoxWindow* list = (CListBoxWindow*)_shellIconManager.GetWnd(SQSH_MM_PROFILE_LIST);
gameShell->currentSingleProfile.setCurrentProfileIndex( list->GetCurSel() );
putStringSettings("ProfileName", gameShell->currentSingleProfile.getCurrentProfile().name);
showSingleMenu(pWnd);
if (const Profile* profile = gameShell->currentSingleProfile.getCurrentProfile()) {
putStringSettings("ProfileName", profile->name);
showSingleMenu(pWnd);
}
} else if (code == EVENT_DRAWWND) {
CListBoxWindow* list = (CListBoxWindow*)_shellIconManager.GetWnd(SQSH_MM_PROFILE_LIST);
pWnd->Enable( list->GetCurSel() >= 0 );
}
}
void onMMBackFromProfileButton(CShellWindow* pWnd, InterfaceEventCode code, int param) {
if ( code == EVENT_UNPRESSED && intfCanHandleInput() ) {
if (gameShell->currentSingleProfile.getCurrentProfileIndex() != -1) {
if (gameShell->currentSingleProfile.isValidProfile()) {
showSingleMenu(pWnd);
} else {
_shellIconManager.SwitchMenuScreens(pWnd->m_pParent->ID, SQSH_MM_START_SCR);
Expand Down Expand Up @@ -287,15 +291,13 @@ void onMMStartMissionButton(CShellWindow* pWnd, InterfaceEventCode code, int par
if( code == EVENT_UNPRESSED && intfCanHandleInput() ) {
historyScene.stopAudio();
historyScene.stop();
// missionToExec = MissionDescription(
// gameShell->currentSingleProfile.getFileNameWithDifficulty( historyScene.getMissionToExecute().fileName ).c_str()
// );
missionToExec = MissionDescription( ("RESOURCE\\MISSIONS\\" + historyScene.getMissionToExecute().fileName).c_str() );

//NOTE: should be removed when difficulty will be implemented for each separate player
// missionToExec.getActivePlayerData().difficulty = gameShell->currentSingleProfile.getDifficulty();
missionToExec.setSinglePlayerDifficulty( gameShell->currentSingleProfile.getDifficulty() );
missionToExec.getActivePlayerData().setName(gameShell->currentSingleProfile.getCurrentProfile().name.c_str());
if (const Profile* profile = gameShell->currentSingleProfile.getCurrentProfile()) {
missionToExec.setSinglePlayerDifficulty(profile->difficulty);
missionToExec.getActivePlayerData().setName(profile->name);
}
//NOTE: Setup all names

gameShell->currentSingleProfile.setLastGameType(UserSingleProfile::SCENARIO);
Expand Down Expand Up @@ -358,8 +360,10 @@ void launchCurrentMission(CShellWindow* pWnd) {
missionToExec = MissionDescription( ("RESOURCE\\MISSIONS\\" + historyScene.getMission(missionNumber).fileName).c_str() );

//NOTE: should be removed when difficulty will be implemented for each separate player
missionToExec.getActivePlayerData().difficulty = gameShell->currentSingleProfile.getDifficulty();
missionToExec.getActivePlayerData().setName(gameShell->currentSingleProfile.getCurrentProfile().name.c_str());
if (const Profile* profile = gameShell->currentSingleProfile.getCurrentProfile()) {
missionToExec.setSinglePlayerDifficulty(profile->difficulty);
missionToExec.getActivePlayerData().setName(profile->name);
}
//NOTE: Setup all names

gameShell->currentSingleProfile.setLastGameType(UserSingleProfile::SCENARIO);
Expand Down
60 changes: 24 additions & 36 deletions Source/UserInterface/UserSingleProfile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@ std::string getMissionNumberKey() {
}

UserSingleProfile::UserSingleProfile() :
currentMissionNumber(-1),
currentProfileIndex(-1),
result(UNIVERSE_INTERFACE_MESSAGE_GAME_RESULT_UNDEFINED),
lastType(UNDEFINED) {
currentMissionNumber(-1),
lastType(UNDEFINED),
result(UNIVERSE_INTERFACE_MESSAGE_GAME_RESULT_UNDEFINED) {
}
void UserSingleProfile::setDifficulty(Difficulty newDifficulty) {
if (!isValidProfile()) return;
profiles[currentProfileIndex].difficulty = newDifficulty;
std::string path = getProfileIniPath(currentProfileIndex);
IniManager man( path.c_str(), true );
Expand All @@ -33,6 +34,7 @@ void UserSingleProfile::setCurrentMissionNumber(int newMissionNumber) {
//save();
}
void UserSingleProfile::setLastMissionNumber(int newMissionNumber) {
if (!isValidProfile()) return;
profiles[currentProfileIndex].lastMissionNumber = newMissionNumber;
std::string path = getProfileIniPath(currentProfileIndex);
IniManager man( path.c_str(), true );
Expand All @@ -49,7 +51,9 @@ void UserSingleProfile::scanProfiles() {
std::string path = std::filesystem::u8path(entry->path_content).filename().u8string();
std::string path_lwr = string_to_lower(path.c_str());
if (startsWith(path_lwr, "profile")) {
profiles.emplace_back(Profile(path));
if (get_content_entry(entry->path_content + PATH_SEP + "data")) {
profiles.emplace_back(path);
}
}
}
}
Expand Down Expand Up @@ -141,13 +145,16 @@ void UserSingleProfile::removeProfile(int index) {
advance(forErase, index);
profiles.erase(forErase);

if ( getCurrentProfileIndex() >= index ) {
if ( getCurrentProfileIndex() || (profiles.size() == 0)) {
setCurrentProfileIndex(getCurrentProfileIndex() - 1);
} else {
setCurrentProfileIndex(0);
}
}
if (profiles.empty()) {
setCurrentProfileIndex(-1);
} else {
if (getCurrentProfileIndex() >= index ) {
setCurrentProfileIndex(getCurrentProfileIndex() - 1);
}
if (!isValidProfile()) {
setCurrentProfileIndex(0);
}
}
}

void UserSingleProfile::setCurrentProfileIndex(int index) {
Expand All @@ -172,14 +179,15 @@ std::string UserSingleProfile::getSavesDirectory() const {
if (getLastGameType() == UserSingleProfile::MULTIPLAYER) {
//Workaround for when inside multiplayer
savesDir += "Multiplayer";
} else {
} else if (isValidProfile()) {
savesDir += profiles[currentProfileIndex].dirName;
}
return savesDir + PATH_SEP;
}

void UserSingleProfile::loadProfile(int index) {
std::string path = getProfileIniPath(index);
if (path.empty()) return;
IniManager man(path.c_str(), false);
profiles[index].name = man.get("General","name");
int missionNumber = firstMissionNumber;
Expand All @@ -188,47 +196,27 @@ void UserSingleProfile::loadProfile(int index) {
profiles[index].difficulty = (Difficulty)man.getInt("General","difficulty");
}

std::string UserSingleProfile::getFileNameWithDifficulty(const std::string& fileName) {
std::string res = MISSIONS_PATH;
#if 0 //TODO apparently thisfunction is not used
std::string fileNameWithoutExt = fileName;
fileNameWithoutExt.erase(fileNameWithoutExt.size() - 4, fileNameWithoutExt.size());
res += "\\";
res += fileNameWithoutExt;
res += missionDifficultyPostfix[getDifficulty()];

WIN32_FIND_DATA FindFileData;
HANDLE hf = FindFirstFile( (res + ".spg").c_str(), &FindFileData );
if (hf == INVALID_HANDLE_VALUE) {
res = MISSIONS_PATH;
res += "\\";
res += fileName;
hf = FindFirstFile( res.c_str(), &FindFileData );
xassert( hf != INVALID_HANDLE_VALUE );
}
#endif
return res;
}

void UserSingleProfile::setCurrentProfile(const std::string& name) {
for (int i = 0, s = profiles.size(); i < s; i++) {
for (size_t i = 0, s = profiles.size(); i < s; i++) {
if (profiles[i].name == name) {
setCurrentProfileIndex(i);
return;
}
}
if (profiles.size() > 0) {
if (!profiles.empty()) {
setCurrentProfileIndex(0);
}
}

void UserSingleProfile::setRecord(const std::string& name, int milis) {
if (!isValidProfile()) return;
std::string path = getProfileIniPath(currentProfileIndex);
IniManager man(path.c_str(), false);
man.putInt("Records", name.c_str(), milis);
}

int UserSingleProfile::getRecord(const std::string& name) {
if (!isValidProfile()) return 0;
std::string path = getProfileIniPath(currentProfileIndex);
IniManager man(path.c_str(), false);
return man.getInt("Records", name.c_str());
Expand Down
25 changes: 15 additions & 10 deletions Source/UserInterface/UserSingleProfile.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ class UserSingleProfile {
};
UserSingleProfile();

bool isValidProfile() const {
return currentProfileIndex >= 0 && currentProfileIndex < profiles.size();
}
void setDifficulty(Difficulty newDifficulty);
void setCurrentMissionNumber(int newMissionNumber);
void setLastMissionNumber(int newMissionNumber);
Expand All @@ -38,15 +41,13 @@ class UserSingleProfile {
void setLastGameType(GameType type) {
lastType = type;
}
Difficulty getDifficulty() const {
xassert(currentProfileIndex >= 0 && currentProfileIndex < profiles.size());
return profiles[currentProfileIndex].difficulty;
}
int getCurrentMissionNumber() const {
return currentMissionNumber;
}
int getLastMissionNumber() const {
xassert(currentProfileIndex >= 0 && currentProfileIndex < profiles.size());
if (!isValidProfile()) {
return 0;
}
return profiles[currentProfileIndex].lastMissionNumber;
}
bool isLastWin() const {
Expand All @@ -58,8 +59,11 @@ class UserSingleProfile {

void scanProfiles();

const Profile& getCurrentProfile() const {
return profiles[currentProfileIndex];
const Profile* getCurrentProfile() const {
if (!isValidProfile()) {
return nullptr;
}
return &profiles[currentProfileIndex];
}
const std::vector<Profile>& getProfilesVector() const {
return profiles;
Expand All @@ -72,8 +76,6 @@ class UserSingleProfile {
}
void setCurrentProfileIndex(int index);

std::string getFileNameWithDifficulty(const std::string& fileName);

void deleteSave(const std::string& name);
static std::string getAllSavesDirectory();
std::string getSavesDirectory() const;
Expand All @@ -89,7 +91,7 @@ class UserSingleProfile {
int getRecord(const std::string& name);

protected:
bool removeDir(const std::string& dir);
static bool removeDir(const std::string& dir);
void loadProfile(int index);

int currentProfileIndex;
Expand All @@ -102,6 +104,9 @@ class UserSingleProfile {
std::vector<Profile> profiles;

std::string getProfileIniPath(int index) const {
if (index < 0 || index >= profiles.size()) {
return "";
}
return getAllSavesDirectory() + profiles[index].dirName + PATH_SEP + "data";
}

Expand Down

0 comments on commit c921893

Please sign in to comment.