Skip to content

Commit

Permalink
Protect cheat mode also after loading game.
Browse files Browse the repository at this point in the history
Used to be possible to enter cheat mode without the
special key command if you had loaded a savegame.

Closes ticket:4612, patch by Forgon.
  • Loading branch information
perim committed Aug 27, 2017
1 parent 3a871f2 commit 4b91398
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 46 deletions.
5 changes: 4 additions & 1 deletion src/clparse.cpp
Expand Up @@ -492,11 +492,14 @@ bool ParseCommandLine(int argc, const char **argv)
NetPlay.bComms = false;
bMultiPlayer = false;
bMultiMessages = false;
NetPlay.players[0].allocated = true;
for (int i = 0; i < MAX_PLAYERS; i++)
{
NET_InitPlayer(i, true, false);
}

//NET_InitPlayer deallocates Player 0, who must be allocated so that a later invocation of processDebugMappings does not trigger DEBUG mode
NetPlay.players[0].allocated = true;

if (!strcmp(token, "CAM_1A") || !strcmp(token, "CAM_2A") || !strcmp(token, "CAM_3A"))
{
game.type = CAMPAIGN;
Expand Down
51 changes: 23 additions & 28 deletions src/game.cpp
Expand Up @@ -1765,6 +1765,26 @@ static void setIniDroidOrder(WzConfig &ini, QString const &key, DroidOrder const
setIniStructureStats(ini, key + "/stats", order.psStats);
}

static void allocatePlayers()
{
for (int i = 0; i < MAX_PLAYERS; i++)
{
NetPlay.players[i].ai = saveGameData.sNetPlay.players[i].ai;
NetPlay.players[i].difficulty = saveGameData.sNetPlay.players[i].difficulty;
sstrcpy(NetPlay.players[i].name, saveGameData.sNetPlay.players[i].name);
if (saveGameData.sGame.skDiff[i] == UBYTE_MAX || (game.type == CAMPAIGN && i == 0))
{
NetPlay.players[i].allocated = true;
//processDebugMappings ensures game does not start in DEBUG mode
processDebugMappings(i, false);
}
else
{
NetPlay.players[i].allocated = false;
}
}
}

// -----------------------------------------------------------------------------------------
// UserSaveGame ... this is true when you are loading a players save game
bool loadGame(const char *pGameToLoad, bool keepObjects, bool freeMem, bool UserSaveGame)
Expand Down Expand Up @@ -1985,20 +2005,8 @@ bool loadGame(const char *pGameToLoad, bool keepObjects, bool freeMem, bool User
bMultiMessages = bMultiPlayer;

NetPlay.bComms = (saveGameData.sNetPlay).bComms;
for (i = 0; i < MAX_PLAYERS; i++)
{
NetPlay.players[i].ai = saveGameData.sNetPlay.players[i].ai;
NetPlay.players[i].difficulty = saveGameData.sNetPlay.players[i].difficulty;
sstrcpy(NetPlay.players[i].name, saveGameData.sNetPlay.players[i].name);
if (saveGameData.sGame.skDiff[i] == UBYTE_MAX || (game.type == CAMPAIGN && i == 0))
{
NetPlay.players[i].allocated = true;
}
else
{
NetPlay.players[i].allocated = false;
}
}

allocatePlayers();

if (bMultiPlayer)
{
Expand Down Expand Up @@ -3738,20 +3746,7 @@ bool gameLoadV(PHYSFS_file *fileHandle, unsigned int version)
powerSaved[i].extractedPower = saveGameData.power[i].extractedPower;
}

for (player = 0; player < MAX_PLAYERS; player++)
{
NetPlay.players[player].ai = saveGameData.sNetPlay.players[player].ai;
NetPlay.players[player].difficulty = saveGameData.sNetPlay.players[player].difficulty;
sstrcpy(NetPlay.players[player].name, saveGameData.sNetPlay.players[player].name);
if ((saveGameData.sGame.skDiff[player] == UBYTE_MAX || game.type == CAMPAIGN) && player == 0)
{
NetPlay.players[player].allocated = true;
}
else
{
NetPlay.players[player].allocated = false;
}
}
allocatePlayers();

IsScenario = false;
//copy the level name across
Expand Down
9 changes: 3 additions & 6 deletions src/init.cpp
Expand Up @@ -741,7 +741,7 @@ void systemShutdown()

shutdownEffectsSystem();
wzSceneEnd(nullptr); // Might want to end the "Main menu loop" or "Main game loop".
keyClearMappings();
keyMappings.clear();

// free up all the load functions (all the data should already have been freed)
resReleaseAll();
Expand Down Expand Up @@ -834,9 +834,7 @@ bool frontendInitialise(const char *ResourceFile)
return false;
}

// keymappings
// clear out any existing mappings
keyClearMappings();
// reinitialise key mappings
keyInitMappings(false);

// Set the default uncoloured cursor here, since it looks slightly
Expand Down Expand Up @@ -1098,8 +1096,7 @@ bool stageTwoInitialise()
return false;
}

// keymappings
keyClearMappings();
// reinitialise key mappings
keyInitMappings(false);

// Set the default uncoloured cursor here, since it looks slightly
Expand Down
2 changes: 1 addition & 1 deletion src/keyedit.cpp
Expand Up @@ -191,7 +191,7 @@ bool runKeyMapEditor()
}
if (id == KM_DEFAULT)
{
keyClearMappings();
// reinitialise key mappings
keyInitMappings(true);
widgDelete(psWScreen, FRONTEND_BACKDROP); // readd the widgets
startKeyMapEditor(false);
Expand Down
9 changes: 0 additions & 9 deletions src/keymap.cpp
Expand Up @@ -268,7 +268,6 @@ void keyInitMappings(bool bForceDefaults)
processDebugMappings(n, false);
}


for (unsigned i = 0; i < NUM_QWERTY_KEYS; i++)
{
qwertyKeyMappings[i].psMapping = nullptr;
Expand Down Expand Up @@ -520,14 +519,6 @@ KEY_MAPPING *keyFindMapping(KEY_CODE metaCode, KEY_CODE subCode)
return mapping != keyMappings.end()? &*mapping : nullptr;
}

// ----------------------------------------------------------------------------------
/* clears the mappings list and frees the memory */
void keyClearMappings()
{
keyMappings.clear();
}


// ----------------------------------------------------------------------------------
/* Removes a mapping specified by a pointer */
static bool keyRemoveMappingPt(KEY_MAPPING *psToRemove)
Expand Down
1 change: 0 additions & 1 deletion src/keymap.h
Expand Up @@ -55,7 +55,6 @@ struct KEY_MAPPING
KEY_MAPPING *keyAddMapping(KEY_STATUS status, KEY_CODE metaCode, KEY_CODE subcode, KEY_ACTION action, void (*pKeyMapFunc)(), const char *name);
KEY_MAPPING *keyGetMappingFromFunction(void (*function)());
KEY_MAPPING *keyFindMapping(KEY_CODE metaCode, KEY_CODE subCode);
void keyClearMappings();
void keyProcessMappings(bool bExclude);
void keyInitMappings(bool bForceDefaults);
KEY_CODE getLastSubKey();
Expand Down

0 comments on commit 4b91398

Please sign in to comment.