Index: src/saveload.cpp =================================================================== --- src/saveload.cpp (revision 13972) +++ src/saveload.cpp (working copy) @@ -21,6 +21,7 @@ #include "town.h" #include "saveload.h" #include "network/network.h" +#include "network/network_internal.h" #include "variables.h" #include "window_func.h" #include "strings_func.h" @@ -30,6 +31,7 @@ #include "core/endian_func.hpp" #include "vehicle_base.h" #include "autoreplace_base.h" +#include "string_func.h" #include #include "table/strings.h" @@ -76,6 +78,8 @@ enum NeedLengthValues {NL_NONE = 0, NL_WANTLENGTH = 1, NL_CALCLENGTH = 2}; +enum SaveGameLoadSourceValues {SGLS_DEFAULT = 0, SGLS_SAVE_DIR = 1, SGLS_BASE_DIR = 2}; + /** Error handler, calls longjmp to simulate an exception. * @todo this was used to have a central place to handle errors, but it is * pretty ugly, and seriously interferes with any multithreaded approaches */ @@ -1586,12 +1590,14 @@ * handled. It opens the savegame, selects format and checks versions * @param filename The name of the savegame being created/loaded * @param mode Save or load. Load can also be a TTD(Patch) game. Use SL_LOAD, SL_OLD_LOAD or SL_SAVE + * @param sb The directory path the file is being created in/loaded from * @return Return the results of the action. SL_OK, SL_ERROR or SL_REINIT ("unload" the game) */ SaveOrLoadResult SaveOrLoad(const char *filename, int mode, Subdirectory sb) { uint32 hdr[2]; const SaveLoadFormat *fmt; + uint8 saveGameLoadSource = SGLS_DEFAULT; /* An instance of saving is already active, so don't go saving again */ if (_ts.saveinprogress && mode == SL_SAVE) { @@ -1615,15 +1621,30 @@ _sl.excpt_uninit = NULL; try { _sl.fh = (mode == SL_SAVE) ? FioFOpenFile(filename, "wb", sb) : FioFOpenFile(filename, "rb", sb); + saveGameLoadSource = SGLS_DEFAULT; /* Make it a little easier to load savegames from the console */ - if (_sl.fh == NULL && mode == SL_LOAD) _sl.fh = FioFOpenFile(filename, "rb", SAVE_DIR); - if (_sl.fh == NULL && mode == SL_LOAD) _sl.fh = FioFOpenFile(filename, "rb", BASE_DIR); + if (_sl.fh == NULL && mode == SL_LOAD) { + _sl.fh = FioFOpenFile(filename, "rb", SAVE_DIR); + saveGameLoadSource = SGLS_SAVE_DIR; + } + if (_sl.fh == NULL && mode == SL_LOAD) { + _sl.fh = FioFOpenFile(filename, "rb", BASE_DIR); + saveGameLoadSource = SGLS_BASE_DIR; + } if (_sl.fh == NULL) { SlError(mode == SL_SAVE ? STR_GAME_SAVELOAD_ERROR_FILE_NOT_WRITEABLE : STR_GAME_SAVELOAD_ERROR_FILE_NOT_READABLE); } + /* Perform Player Password Save/Load */ + switch (saveGameLoadSource) { + case SGLS_SAVE_DIR: SaveLoadPlayerPasswords(filename, mode, SAVE_DIR); break; + case SGLS_BASE_DIR: SaveLoadPlayerPasswords(filename, mode, BASE_DIR); break; + default: + SaveLoadPlayerPasswords(filename, mode, sb); + } + _sl.bufe = _sl.bufp = NULL; _sl.offs_base = 0; _sl.save = (mode != 0); @@ -1750,6 +1771,63 @@ SaveOrLoad("exit.sav", SL_SAVE, AUTOSAVE_DIR); } +/** + * Function to save company passwords on Network Server + * @param filename The name of the savegame being created/loaded + * @param mode Save or load. true when Saving, false when Loading + * @param sb The directory path the file is being created in/loaded from + */ +void SaveLoadPlayerPasswords(const char *filename, int mode, Subdirectory sb) +{ + if(_network_server) { + char password_filename[MAX_PATH] = ""; + char pass_string[NETWORK_PASSWORD_LENGTH * MAX_PLAYERS*2] = ""; + FILE *f = NULL; + int index1 = 0; + + try { + + NetworkPopulateCompanyInfo(); + /*Create Filename for Saving Passwords to */ + ttd_strlcat(password_filename, filename, lengthof(password_filename)); + ttd_strlcat(password_filename, ".pw", lengthof(password_filename)); + + if(mode == SL_SAVE) { //Are We Saving?? + f = FioFOpenFile(password_filename, "wb", sb); + if (f) { + int index2 = 0; + for (int i = 0; i != NETWORK_PASSWORD_LENGTH; i++) { + for(int j = 0; j != MAX_PLAYERS; j++) { + pass_string[index1++] = _network_player_info[j].password[i]; + pass_string[index1++] = filename[index2++]; + if (!filename[index2]) index2 = 0; + } + } + fwrite(pass_string, lengthof(pass_string), 1, f); + fclose(f); + } + } else if (mode == SL_LOAD){ // Or Loading?? + f = FioFOpenFile(password_filename, "rb", sb); + if (f) { + fread(pass_string, NETWORK_PASSWORD_LENGTH * MAX_PLAYERS*2, 1, f); + for (int i = 0; i != NETWORK_PASSWORD_LENGTH; i++) { + for(int j=0; j != MAX_PLAYERS; j++) { + _network_player_info[j].password[i] = pass_string[index1++]; + index1++; + } + } + fclose(f); + } + } + } + catch (...) { + if (f != NULL) fclose(f); + + f = NULL; + } + } +} + #if 0 /** * Function to get the type of the savegame by looking at the file header. Index: src/saveload.h =================================================================== --- src/saveload.h (revision 13972) +++ src/saveload.h (working copy) @@ -322,4 +322,6 @@ void SaveFileStart(); void SaveFileDone(); void SaveFileError(); + +void SaveLoadPlayerPasswords(const char *filename, int mode, Subdirectory sb); #endif /* SAVELOAD_H */