Ticket #2030: saveAsv3.patch

File saveAsv3.patch, 13.3 KB (added by Crynux, 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  
     1function sortDecreasingDate(a, b)
     2{
     3    return b.metadata.time - a.metadata.time;
     4}
     5
     6
     7function twoDigits(n)
     8{
     9    return n < 10 ? "0" + n : n;
     10}
     11
     12function 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 
    201function init()
    212{
    223    var savedGames = Engine.GetSavedGames();
    function reallyDeleteGame(gameID)  
    8768
    8869    // Run init again to refresh saved game list
    8970    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  
    22
    33<objects>
    44
    5     <script file="gui/savedgames/load.js"/>
    65    <script file="gui/common/functions_global_object.js" />
     6    <script file="gui/common/functions_utility_loadsave.js" />
     7    <script file="gui/savedgames/load.js" />
    78
    89    <!-- Add a translucent black background to fade out the menu page -->
    910    <object type="image" z="0" sprite="BackgroundTranslucent"/>
    1011
    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">
    1213
    1314        <object type="text" style="TitleText" size="50%-128 0%-16 50%+128 16">
    1415            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
     2var descriptions;
     3
     4function 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
     16function 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
     44function 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
     64function 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)  
    167167// Menu functions
    168168// =============================================================================
    169169
     170function openSave(pause)
     171{
     172    getGUIObjectByName("saveGamePanel").hidden = false;
     173    savePanelInit();
     174    if (pause)
     175        pauseGame();
     176}
     177
     178function closeSave(resume)
     179{
     180    getGUIObjectByName("saveGamePanel").hidden = true;
     181    if (resume)
     182        resumeGame();
     183}
     184
    170185function openSettings(pause)
    171186{
    172187    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  
    55    <script file="gui/common/functions_civinfo.js"/>
    66    <script file="gui/common/functions_utility.js" />
    77    <script file="gui/common/functions_global_object.js" />
     8    <script file="gui/common/functions_utility_loadsave.js" />
    89    <script file="gui/common/music.js"/>
    910    <script file="gui/common/timer.js"/>
     11    <script file="gui/savedgames/save.js"/>
    1012    <script file="gui/session/session.js"/>
    1113    <script file="gui/session/selection.js"/>
    1214    <script file="gui/session/placement.js"/>
     
    1719    <script file="gui/session/messages.js"/>
    1820    <script file="gui/session/utility_functions.js"/>
    1921
     22
     23
    2024    <object name="sn" hotkey="session.gui.toggle">
    2125    <action on="Tick">
    2226        onTick();
     
    427431    </object>
    428432
    429433    <!-- ================================  ================================ -->
     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    <!-- ================================  ================================ -->
    430477    <!-- Settings Window -->
    431478    <!-- ================================  ================================ -->
    432479        <object name="settingsDialogPanel"
     
    702749        >
    703750            Save
    704751            <action on="Press">
    705             Engine.SaveGame();
    706752            closeMenu();
     753            openSave(true);
    707754            </action>
    708755        </object>
    709756
  • 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)  
    258258    return metadata.get();
    259259}
    260260
    261 void SaveGame(void* cbdata)
     261void SaveGame(void* cbdata, std::wstring givenname, std::wstring description, bool overwrite)
    262262{
    263263    CGUIManager* guiManager = static_cast<CGUIManager*> (cbdata);
    264264
    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)
    266266        LOGERROR(L"Failed to save game");
    267267}
    268268
    void GuiScriptingInit(ScriptInterface& scriptInterface)  
    669669    scriptInterface.RegisterFunction<CScriptVal, std::wstring, &StartSavedGame>("StartSavedGame");
    670670    scriptInterface.RegisterFunction<std::vector<CScriptValRooted>, &GetSavedGames>("GetSavedGames");
    671671    scriptInterface.RegisterFunction<bool, std::wstring, &DeleteSavedGame>("DeleteSavedGame");
    672     scriptInterface.RegisterFunction<void, &SaveGame>("SaveGame");
     672    scriptInterface.RegisterFunction<void, std::wstring, std::wstring, bool, &SaveGame>("SaveGame");
    673673    scriptInterface.RegisterFunction<void, &QuickSave>("QuickSave");
    674674    scriptInterface.RegisterFunction<void, &QuickLoad>("QuickLoad");
    675675
  • 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  
    3131static const int SAVED_GAME_VERSION_MINOR = 0; // increment on compatible changes to the format
    3232// TODO: we ought to check version numbers when loading files
    3333
    34 Status SavedGames::Save(const std::wstring& prefix, CSimulation2& simulation, CGUIManager* gui, int playerID)
     34Status SavedGames::Save(const std::wstring& givenname, const std::wstring& description, bool overwrite, CSimulation2& simulation, CGUIManager* gui, int playerID)
    3535{
    3636    // 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"));
    3838    const VfsPath filenameFormat = basenameFormat.ChangeExtension(L".0adsave");
    3939    VfsPath filename;
    4040
    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    }
    4652    // ArchiveWriter_Zip can only write to OsPaths, not VfsPaths,
    4753    // but we'd like to handle saved games via VFS.
    4854    // To avoid potential confusion from writing with non-VFS then
    Status SavedGames::Save(const std::wstring& prefix, CSimulation2& simulation, CG  
    7480        CScriptVal guiMetadata = simulation.GetScriptInterface().CloneValueFromOtherContext(gui->GetScriptInterface(), gui->GetSavedGameData().get());
    7581        simulation.GetScriptInterface().SetProperty(metadata.get(), "gui", guiMetadata);
    7682    }
     83    simulation.GetScriptInterface().SetProperty(metadata.get(), "description", description);
    7784   
    7885    std::string metadataString = simulation.GetScriptInterface().StringifyJSON(metadata.get(), true);
    7986   
  • source/ps/SavedGame.h

    diff --git a/source/ps/SavedGame.h b/source/ps/SavedGame.h
    index 019f31b..77b2e22 100644
    a b namespace SavedGames  
    4141/**
    4242 * Create new saved game archive with given prefix and simulation data
    4343 *
    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
    4549 * @param simulation
    4650 * @param gui if not NULL, store some UI-related data with the saved game
    4751 * @param playerID ID of the player who saved this file
    4852 * @return INFO::OK if successfully saved, else an error Status
    4953 */
    50 Status Save(const std::wstring& prefix, CSimulation2& simulation, CGUIManager* gui, int playerID);
     54Status Save(const std::wstring& givenname, const std::wstring& description, bool overwrite, CSimulation2& simulation, CGUIManager* gui, int playerID);
    5155
    5256/**
    5357 * Load saved game archive with the given name