Ticket #2030: saveAsv3.patch
File saveAsv3.patch, 13.3 KB (added by , 11 years ago) |
---|
-
binaries/data/mods/public/gui/common/functions_utility_loadsave.js
diff --git a/binaries/data/mods/public/gui/common/functions_utility_loadsave.js b/binaries/data/mods/public/gui/common/functions_utility_loadsave.js index e69de29..f6ec0db 100644
a b 1 function sortDecreasingDate(a, b) 2 { 3 return b.metadata.time - a.metadata.time; 4 } 5 6 7 function twoDigits(n) 8 { 9 return n < 10 ? "0" + n : n; 10 } 11 12 function generateLabel(metadata) 13 { 14 var t = new Date(metadata.time*1000); 15 16 var date = t.getFullYear()+"-"+twoDigits(1+t.getMonth())+"-"+twoDigits(t.getDate()); 17 var time = twoDigits(t.getHours())+":"+twoDigits(t.getMinutes())+":"+twoDigits(t.getSeconds()); 18 return "["+date+" "+time+"] "+metadata.initAttributes.map+"\n"+(metadata.description?metadata.description:""); 19 } 20 No newline at end of file -
binaries/data/mods/public/gui/savedgames/load.js
diff --git a/binaries/data/mods/public/gui/savedgames/load.js b/binaries/data/mods/public/gui/savedgames/load.js index de1150c..a9429b9 100644
a b 1 function sortDecreasingDate(a, b)2 {3 return b.metadata.time - a.metadata.time;4 }5 6 function twoDigits(n)7 {8 return n < 10 ? "0" + n : n;9 }10 11 function generateLabel(metadata)12 {13 var t = new Date(metadata.time*1000);14 15 var date = t.getFullYear()+"-"+twoDigits(1+t.getMonth())+"-"+twoDigits(t.getDate());16 var time = twoDigits(t.getHours())+":"+twoDigits(t.getMinutes())+":"+twoDigits(t.getSeconds());17 return "["+date+" "+time+"] "+metadata.initAttributes.map;18 }19 20 1 function init() 21 2 { 22 3 var savedGames = Engine.GetSavedGames(); … … function reallyDeleteGame(gameID) 87 68 88 69 // Run init again to refresh saved game list 89 70 init(); 90 } 71 } 72 No newline at end of file -
binaries/data/mods/public/gui/savedgames/load.xml
diff --git a/binaries/data/mods/public/gui/savedgames/load.xml b/binaries/data/mods/public/gui/savedgames/load.xml index e3ed2e8..81f245e 100644
a b 2 2 3 3 <objects> 4 4 5 <script file="gui/savedgames/load.js"/>6 5 <script file="gui/common/functions_global_object.js" /> 6 <script file="gui/common/functions_utility_loadsave.js" /> 7 <script file="gui/savedgames/load.js" /> 7 8 8 9 <!-- Add a translucent black background to fade out the menu page --> 9 10 <object type="image" z="0" sprite="BackgroundTranslucent"/> 10 11 11 <object type="image" style="StoneDialog" size="50%- 190 50%-200 50%+190 50%+200">12 <object type="image" style="StoneDialog" size="50%-300 50%-200 50%+300 50%+200"> 12 13 13 14 <object type="text" style="TitleText" size="50%-128 0%-16 50%+128 16"> 14 15 Load Game -
binaries/data/mods/public/gui/savedgames/save.js
diff --git a/binaries/data/mods/public/gui/savedgames/save.js b/binaries/data/mods/public/gui/savedgames/save.js index e69de29..a5e4013 100644
a b 1 2 var descriptions; 3 4 function selectDescription() 5 { 6 var gameSelection = getGUIObjectByName("gameSelection"); 7 if(gameSelection.selected != -1) 8 { 9 10 getGUIObjectByName("deleteGameButton").enabled = true; 11 var gameID = gameSelection.list_data[gameSelection.selected]; 12 getGUIObjectByName("saveGameDesc").caption = descriptions[gameID]; 13 } 14 } 15 16 function savePanelInit() 17 { 18 var savedGames = Engine.GetSavedGames(); 19 20 var gameSelection = getGUIObjectByName("gameSelection"); 21 22 getGUIObjectByName("deleteGameButton").enabled = false; 23 24 if (savedGames.length == 0) 25 { 26 gameSelection.list = [ "No saved games found" ]; 27 gameSelection.selected = -1; 28 return; 29 } 30 31 savedGames.sort(sortDecreasingDate); 32 33 var gameListIDs = [ game.id for each (game in savedGames) ]; 34 var gameListLabels = [ generateLabel(game.metadata) for each (game in savedGames) ]; 35 36 descriptions = new Array(); 37 [ descriptions[game.id] = (game.metadata.description?game.metadata.description:"none") for each (game in savedGames) ]; 38 39 gameSelection.list = gameListLabels; 40 gameSelection.list_data = gameListIDs; 41 gameSelection.selected = -1; 42 } 43 44 function saveGame() 45 { 46 var gameSelection = getGUIObjectByName("gameSelection"); 47 var gameLabel = gameSelection.list[gameSelection.selected]; 48 var gameID = gameSelection.list_data[gameSelection.selected]; 49 var desc = getGUIObjectByName("saveGameDesc").caption; 50 51 if(gameSelection.selected != -1) 52 { 53 // Ask for confirmation 54 var btCaptions = ["Yes", "No"]; 55 var btCode = [function(){ reallySaveGame(gameID,desc,gameSelection.selected); }, null]; 56 messageBox(500, 200, "\""+gameLabel+"\"\nSaved game will be permanently overwritten, are you sure?", "OVERWRITE SAVE", 0, btCaptions, btCode); 57 } 58 else 59 { 60 reallySaveGame(gameID,desc,gameSelection.selected); 61 } 62 } 63 64 function reallySaveGame(gameID,desc,selected) 65 { 66 Engine.SaveGame(gameID?gameID:"savegame",desc,selected != -1); 67 closeSave(true); 68 savePanelInit(); 69 } 70 No newline at end of file -
binaries/data/mods/public/gui/session/menu.js
diff --git a/binaries/data/mods/public/gui/session/menu.js b/binaries/data/mods/public/gui/session/menu.js index ae3b5b6..64e21f3 100644
a b function openDeleteDialog(selection) 167 167 // Menu functions 168 168 // ============================================================================= 169 169 170 function openSave(pause) 171 { 172 getGUIObjectByName("saveGamePanel").hidden = false; 173 savePanelInit(); 174 if (pause) 175 pauseGame(); 176 } 177 178 function closeSave(resume) 179 { 180 getGUIObjectByName("saveGamePanel").hidden = true; 181 if (resume) 182 resumeGame(); 183 } 184 170 185 function openSettings(pause) 171 186 { 172 187 getGUIObjectByName("settingsDialogPanel").hidden = false; -
binaries/data/mods/public/gui/session/session.xml
diff --git a/binaries/data/mods/public/gui/session/session.xml b/binaries/data/mods/public/gui/session/session.xml index 38d2e5b..2585512 100644
a b 5 5 <script file="gui/common/functions_civinfo.js"/> 6 6 <script file="gui/common/functions_utility.js" /> 7 7 <script file="gui/common/functions_global_object.js" /> 8 <script file="gui/common/functions_utility_loadsave.js" /> 8 9 <script file="gui/common/music.js"/> 9 10 <script file="gui/common/timer.js"/> 11 <script file="gui/savedgames/save.js"/> 10 12 <script file="gui/session/session.js"/> 11 13 <script file="gui/session/selection.js"/> 12 14 <script file="gui/session/placement.js"/> … … 17 19 <script file="gui/session/messages.js"/> 18 20 <script file="gui/session/utility_functions.js"/> 19 21 22 23 20 24 <object name="sn" hotkey="session.gui.toggle"> 21 25 <action on="Tick"> 22 26 onTick(); … … 427 431 </object> 428 432 429 433 <!-- ================================ ================================ --> 434 <!-- Save Window --> 435 <!-- ================================ ================================ --> 436 <object name="saveGamePanel" type="image" style="StoneDialog" size="50%-300 50%-200 50%+300 50%+200" hidden="true"> 437 <object type="image" z="0" sprite="BackgroundTranslucent"/> 438 <object type="text" style="TitleText" size="50%-128 0%-16 50%+128 16"> 439 Save Game 440 </object> 441 442 <object name="gameSelection" 443 style="StoneList" 444 type="list" 445 446 size="24 24 100%-24 100%-124"> 447 <action on="selectionchange"> 448 selectDescription(); 449 </action> 450 </object> 451 452 453 <object size="24 100%-124 100%-24 100%-100" name="descLabel" type="text" style="LeftLabelText">Description:</object> 454 455 <object name="saveGameDesc" size="24 100%-96 100%-24 100%-72" type="input" style="StoneInput" max_length="80"> 456 quick save 457 </object> 458 459 <object name="saveButton" type="button" size="0%+25 100%-60 33%+10 100%-32" style="StoneButton"> 460 Save 461 <action on="Press">saveGame();</action> 462 </object> 463 464 <object name="deleteGameButton" type="button" size="33%+20 100%-60 66%-15 100%-32" style="StoneButton"> 465 Delete 466 <action on="Press">deleteGame();</action> 467 </object> 468 469 <object type="button" style="StoneButton" size="66%-5 100%-60 100%-25 100%-32"> 470 Cancel 471 <action on="Press">closeSave(true);</action> 472 </object> 473 474 </object> 475 476 <!-- ================================ ================================ --> 430 477 <!-- Settings Window --> 431 478 <!-- ================================ ================================ --> 432 479 <object name="settingsDialogPanel" … … 702 749 > 703 750 Save 704 751 <action on="Press"> 705 Engine.SaveGame();706 752 closeMenu(); 753 openSave(true); 707 754 </action> 708 755 </object> 709 756 -
source/gui/scripting/ScriptFunctions.cpp
diff --git a/source/gui/scripting/ScriptFunctions.cpp b/source/gui/scripting/ScriptFunctions.cpp index dd530cb..4e1cfc5 100644
a b CScriptVal StartSavedGame(void* cbdata, std::wstring name) 258 258 return metadata.get(); 259 259 } 260 260 261 void SaveGame(void* cbdata )261 void SaveGame(void* cbdata, std::wstring givenname, std::wstring description, bool overwrite) 262 262 { 263 263 CGUIManager* guiManager = static_cast<CGUIManager*> (cbdata); 264 264 265 if (SavedGames::Save( L"quicksave", *g_Game->GetSimulation2(), guiManager, g_Game->GetPlayerID()) < 0)265 if (SavedGames::Save(givenname, description, overwrite, *g_Game->GetSimulation2(), guiManager, g_Game->GetPlayerID()) < 0) 266 266 LOGERROR(L"Failed to save game"); 267 267 } 268 268 … … void GuiScriptingInit(ScriptInterface& scriptInterface) 669 669 scriptInterface.RegisterFunction<CScriptVal, std::wstring, &StartSavedGame>("StartSavedGame"); 670 670 scriptInterface.RegisterFunction<std::vector<CScriptValRooted>, &GetSavedGames>("GetSavedGames"); 671 671 scriptInterface.RegisterFunction<bool, std::wstring, &DeleteSavedGame>("DeleteSavedGame"); 672 scriptInterface.RegisterFunction<void, &SaveGame>("SaveGame");672 scriptInterface.RegisterFunction<void, std::wstring, std::wstring, bool, &SaveGame>("SaveGame"); 673 673 scriptInterface.RegisterFunction<void, &QuickSave>("QuickSave"); 674 674 scriptInterface.RegisterFunction<void, &QuickLoad>("QuickLoad"); 675 675 -
source/ps/SavedGame.cpp
diff --git a/source/ps/SavedGame.cpp b/source/ps/SavedGame.cpp index 20bb944..0c86abd 100644
a b static const int SAVED_GAME_VERSION_MAJOR = 1; // increment on incompatible chan 31 31 static const int SAVED_GAME_VERSION_MINOR = 0; // increment on compatible changes to the format 32 32 // TODO: we ought to check version numbers when loading files 33 33 34 Status SavedGames::Save(const std::wstring& prefix, CSimulation2& simulation, CGUIManager* gui, int playerID)34 Status SavedGames::Save(const std::wstring& givenname, const std::wstring& description, bool overwrite, CSimulation2& simulation, CGUIManager* gui, int playerID) 35 35 { 36 36 // Determine the filename to save under 37 const VfsPath basenameFormat(L"saves/" + prefix + L"-%04d");37 const VfsPath basenameFormat(L"saves/" + givenname + (overwrite?L"":L"-%04d")); 38 38 const VfsPath filenameFormat = basenameFormat.ChangeExtension(L".0adsave"); 39 39 VfsPath filename; 40 40 41 // Don't make this a static global like NextNumberedFilename expects, because 42 // that wouldn't work when 'prefix' changes, and because it's not thread-safe 43 size_t nextSaveNumber = 0; 44 vfs::NextNumberedFilename(g_VFS, filenameFormat, nextSaveNumber, filename); 45 41 if(!overwrite) 42 { 43 // Don't make this a static global like NextNumberedFilename expects, because 44 // that wouldn't work when 'prefix' changes, and because it's not thread-safe 45 size_t nextSaveNumber = 0; 46 vfs::NextNumberedFilename(g_VFS, filenameFormat, nextSaveNumber, filename); 47 } 48 else 49 { 50 filename = filenameFormat.string().c_str(); 51 } 46 52 // ArchiveWriter_Zip can only write to OsPaths, not VfsPaths, 47 53 // but we'd like to handle saved games via VFS. 48 54 // To avoid potential confusion from writing with non-VFS then … … Status SavedGames::Save(const std::wstring& prefix, CSimulation2& simulation, CG 74 80 CScriptVal guiMetadata = simulation.GetScriptInterface().CloneValueFromOtherContext(gui->GetScriptInterface(), gui->GetSavedGameData().get()); 75 81 simulation.GetScriptInterface().SetProperty(metadata.get(), "gui", guiMetadata); 76 82 } 83 simulation.GetScriptInterface().SetProperty(metadata.get(), "description", description); 77 84 78 85 std::string metadataString = simulation.GetScriptInterface().StringifyJSON(metadata.get(), true); 79 86 -
source/ps/SavedGame.h
diff --git a/source/ps/SavedGame.h b/source/ps/SavedGame.h index 019f31b..77b2e22 100644
a b namespace SavedGames 41 41 /** 42 42 * Create new saved game archive with given prefix and simulation data 43 43 * 44 * @param prefix Create new numbered file starting with this prefix 44 * @param givenname Name to save the game with; can be used as a prefix depending 45 * on the overwrite bool. 46 * @param description A user-given description of the save 47 * @param overwrite if true, overwrite given filename, otherwise consider 48 * filename a prefix 45 49 * @param simulation 46 50 * @param gui if not NULL, store some UI-related data with the saved game 47 51 * @param playerID ID of the player who saved this file 48 52 * @return INFO::OK if successfully saved, else an error Status 49 53 */ 50 Status Save(const std::wstring& prefix, CSimulation2& simulation, CGUIManager* gui, int playerID);54 Status Save(const std::wstring& givenname, const std::wstring& description, bool overwrite, CSimulation2& simulation, CGUIManager* gui, int playerID); 51 55 52 56 /** 53 57 * Load saved game archive with the given name