Ticket #2361: save_check.patch

File save_check.patch, 12.4 KB (added by Michael, 10 years ago)

Checks the major version of the save game.

  • binaries/data/mods/public/gui/common/functions_utility_loadsave.js

     
    88    return n < 10 ? "0" + n : n;
    99}
    1010
    11 function generateLabel(metadata)
     11function generateLabel(metadata, majorVersion)
    1212{
    1313    var t = new Date(metadata.time*1000);
    1414
    1515    var date = t.getFullYear()+"-"+twoDigits(1+t.getMonth())+"-"+twoDigits(t.getDate());
    1616    var time = twoDigits(t.getHours())+":"+twoDigits(t.getMinutes())+":"+twoDigits(t.getSeconds());
    17     return "["+date+" "+time+"] "+metadata.initAttributes.map+(metadata.description ? " - "+metadata.description : "");
     17    var label = "["+date+" "+time+"] "+metadata.initAttributes.map.replace("maps/","")+(metadata.description ? " - "+metadata.description : "");
     18    if (majorVersion)
     19        label = label + (majorVersion != metadata.version_major ? "\nIncompatible version [" + metadata.version_major+"."+metadata.version_minor+"]": "");
     20    return label;
    1821}
  • binaries/data/mods/public/gui/savedgames/load.js

     
    1212        return;
    1313    }
    1414
     15    // get current save game version from the game
     16    var saveGameVersion = Engine.GetCurrentSaveGameVersion();
     17    var majorVersion = saveGameVersion.saveGameVersionMajor;
     18    var minorVersion = saveGameVersion.saveGameVersionMinor;
     19
    1520    savedGames.sort(sortDecreasingDate);
    1621
    1722    var gameListIDs = [ game.id for each (game in savedGames) ];
    18     var gameListLabels = [ generateLabel(game.metadata) for each (game in savedGames) ];
     23    var gameListLabels = [ generateLabel(game.metadata, majorVersion) for each (game in savedGames) ];
     24    var gameListCompatible = [ (majorVersion == game.metadata.version_major ? 1 : 0) for each (game in savedGames) ];
    1925
    2026    gameSelection.list = gameListLabels;
    2127    gameSelection.list_data = gameListIDs;
     28    gameSelection.list_selectable = gameListCompatible;
    2229    gameSelection.selected = 0;
    2330}
    2431
  • source/gui/CDropDown.cpp

     
    497497        if (m_HideScrollBar)
    498498            *scrollbar = false;
    499499
    500         DrawList(m_ElementHighlight, "sprite_list", "sprite_selectarea", "textcolor");
     500        DrawList(m_ElementHighlight, "sprite_list", "sprite_selectarea", "textcolor", "warncolor");
    501501       
    502502        if (m_HideScrollBar)
    503503            *scrollbar = old;
  • source/gui/CList.cpp

     
    5454    AddSetting(GUIST_int,                   "selected");    // Index selected. -1 is none.
    5555    AddSetting(GUIST_CStrW,                 "tooltip");
    5656    AddSetting(GUIST_CStr,                  "tooltip_style");
    57     // Each list item has both a name (in 'list') and an associated data string (in 'list_data')
     57    // Each list item has three: a name (in 'list') and an associated data string (in 'list_data') and a value in list_selectable (0 or 1)
    5858    AddSetting(GUIST_CGUIList,              "list");
    5959    AddSetting(GUIST_CGUIList,              "list_data"); // TODO: this should be a list of raw strings, not of CGUIStrings
    60 
     60    AddSetting(GUIST_CGUIList,              "list_selectable");
     61   
    6162    GUI<bool>::SetSetting(this, "scrollbar", false);
    6263
    6364    // Nothing is selected as default.
     
    232233       
    233234        if (set != -1)
    234235        {
    235             GUI<int>::SetSetting(this, "selected", set);
    236             UpdateAutoScroll();
     236            // check, if it can be selected
     237            if(this->canItemBeSelected(set))
     238            {
     239                GUI<int>::SetSetting(this, "selected", set);
     240                UpdateAutoScroll();
    237241
    238             CStrW soundPath;
    239             if (g_SoundManager && GUI<CStrW>::GetSetting(this, "sound_selected", soundPath) == PSRETURN_OK && !soundPath.empty())
    240                 g_SoundManager->PlayAsUI(soundPath.c_str(), false);
     242                CStrW soundPath;
     243                if (g_SoundManager && GUI<CStrW>::GetSetting(this, "sound_selected", soundPath) == PSRETURN_OK && !soundPath.empty())
     244                    g_SoundManager->PlayAsUI(soundPath.c_str(), false);
     245            }
    241246        }
    242247        break;
    243248    }
     
    257262    IGUITextOwner::HandleMessage(Message);
    258263}
    259264
     265bool CList::canItemBeSelected(unsigned int id)
     266{
     267    CGUIList *pListSelectable;
     268    GUI<CGUIList>::GetSettingPointer(this, "list_selectable", pListSelectable);
     269   
     270    return pListSelectable->m_Items.size() <= id || pListSelectable->m_Items[id].GetOriginalString().ToInt() != 0;
     271}
     272
    260273InReaction CList::ManuallyHandleEvent(const SDL_Event_* ev)
    261274{
    262275    int szChar = ev->ev.key.keysym.sym;
     
    303316    int selected;
    304317    GUI<int>::GetSetting(this, "selected", selected);
    305318   
    306     DrawList(selected, "sprite", "sprite_selectarea", "textcolor");
     319    DrawList(selected, "sprite", "sprite_selectarea", "textcolor", "warncolor");
    307320}
    308321
    309322void CList::DrawList(const int &selected,
    310323                     const CStr& _sprite,
    311324                     const CStr& _sprite_selected,
    312                      const CStr& _textcolor)
     325                     const CStr& _textcolor,
     326                     const CStr& _inactiveTextcolor)
    313327{
    314328    float bz = GetBufferedZ();
    315329
     
    376390            }
    377391        }
    378392
    379         CColor color;
     393        CColor color, unselColor, colorElement;
    380394        GUI<CColor>::GetSetting(this, _textcolor, color);
     395        //GUI<CColor>::GetSetting(this, _inactiveTextcolor, unselColor); // TODO: add the color there...'warncolor'
     396       
     397        unselColor = CColor(255/255.0f, 128/255.0f, 0/255.0f, 1.0f);;
    381398
    382399        for (int i=0; i<(int)pList->m_Items.size(); ++i)
    383400        {
     
    399416                    cliparea.left = GetScrollBar(0).GetOuterRect().right;
    400417            }
    401418
    402             DrawText(i, color, rect.TopLeft() - CPos(0.f, scroll - m_ItemsYPositions[i]), bz+0.1f, cliparea);
     419            // select color
     420            if( this->canItemBeSelected(i) )
     421                colorElement = color;
     422            else
     423                colorElement = unselColor;
     424
     425            DrawText(i, colorElement, rect.TopLeft() - CPos(0.f, scroll - m_ItemsYPositions[i]), bz+0.1f, cliparea);
    403426        }
    404427    }
    405428}
    406429
    407 void CList::AddItem(const CStrW& str, const CStrW& data)
     430void CList::AddItem(const CStrW& str, const CStrW& data, bool selectable)
    408431{
    409     CGUIList *pList, *pListData;
     432    //TODO: this seems not to work also in version in SVN or does it ?!
     433    CGUIList *pList, *pListData, *pListSelectable;
    410434    GUI<CGUIList>::GetSettingPointer(this, "list", pList);
    411435    GUI<CGUIList>::GetSettingPointer(this, "list_data", pListData);
     436    GUI<CGUIList>::GetSettingPointer(this, "list_selectable", pListSelectable);
    412437
    413438    CGUIString gui_string;
    414439    gui_string.SetValue(str);
     
    418443    data_string.SetValue(data);
    419444    pListData->m_Items.push_back( data_string );
    420445
     446    CGUIString sel_string;
     447    sel_string.SetValue(selectable ? L"1" : L"0");
     448    pListSelectable->m_Items.push_back( sel_string );
     449
    421450    // TODO Temp
    422451    SetupText();
    423452}
     
    428457
    429458    if (child.GetNodeName() == elmt_item)
    430459    {
    431         AddItem(child.GetText().FromUTF8(), child.GetText().FromUTF8());
    432        
     460        XMBAttributeList attrs = child.GetAttributes();
     461        CStr selectable = attrs.GetNamedItem(pFile->GetAttributeID("selectable"));
     462        AddItem(child.GetText().FromUTF8(), child.GetText().FromUTF8(), !selectable.empty() || selectable.GetHashCode() == CStr8("true").GetHashCode());
    433463        return true;
    434464    }
    435     else
    436     {
    437         return false;
    438     }
     465    return false;
    439466}
    440467
    441468void CList::SelectNextElement()
  • source/gui/CList.h

     
    7979    /**
    8080     * Adds an item last to the list.
    8181     */
    82     virtual void AddItem(const CStrW& str, const CStrW& data);
     82    virtual void AddItem(const CStrW& str, const CStrW& data, bool selectable = true);
    8383
    8484protected:
    8585    /**
     
    122122    // Extended drawing interface, this is so that classes built on the this one
    123123    //  can use other sprite names.
    124124    virtual void DrawList(const int &selected, const CStr& _sprite,
    125                   const CStr& _sprite_selected, const CStr& _textcolor);
     125                  const CStr& _sprite_selected, const CStr& _textcolor, const CStr& _inactiveTextcolor);
    126126
    127127    // Get the area of the list. This is so that it can easily be changed, like in CDropDown
    128128    //  where the area is not equal to m_CachedActualSize.
     
    132132    // (and thus whether list items have possibly changed).
    133133    virtual bool GetModified() const { return m_Modified; }
    134134
     135    // checks, if a item can be selected
     136    bool canItemBeSelected(unsigned int id);
     137
    135138    // List of items.
    136139    //CGUIList m_List;
    137140
  • source/gui/scripting/ScriptFunctions.cpp

     
    364364    return SavedGames::DeleteSavedGame(name);
    365365}
    366366
     367CScriptValRooted GetCurrentSaveGameVersion(ScriptInterface::CxPrivate* pCxPrivate)
     368{
     369    return SavedGames::GetCurrentSaveGameVersion(*(pCxPrivate->pScriptInterface));
     370}
     371
    367372void OpenURL(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), std::string url)
    368373{
    369374    sys_open_url(url);
     
    831836    scriptInterface.RegisterFunction<void, std::wstring, std::wstring, CScriptVal, &SaveGamePrefix>("SaveGamePrefix");
    832837    scriptInterface.RegisterFunction<void, &QuickSave>("QuickSave");
    833838    scriptInterface.RegisterFunction<void, &QuickLoad>("QuickLoad");
     839    scriptInterface.RegisterFunction<CScriptValRooted, &GetCurrentSaveGameVersion>("GetCurrentSaveGameVersion");
    834840
    835841    // Misc functions
    836842    scriptInterface.RegisterFunction<std::wstring, std::wstring, &SetCursor>("SetCursor");
  • source/ps/SavedGame.cpp

     
    1919
    2020#include "SavedGame.h"
    2121
     22#include "graphics/Camera.h"
     23#include "graphics/GameView.h"
    2224#include "gui/GUIManager.h"
    2325#include "lib/allocators/shared_ptr.h"
    2426#include "lib/file/archive/archive_zip.h"
    2527#include "ps/CLogger.h"
    2628#include "ps/Filesystem.h"
     29#include "ps/Game.h"
    2730#include "scriptinterface/ScriptInterface.h"
    2831#include "simulation2/Simulation2.h"
    2932
    3033static const int SAVED_GAME_VERSION_MAJOR = 1; // increment on incompatible changes to the format
    3134static const int SAVED_GAME_VERSION_MINOR = 0; // increment on compatible changes to the format
    32 // TODO: we ought to check version numbers when loading files
    3335
    34 
    3536Status SavedGames::SavePrefix(const std::wstring& prefix, const std::wstring& description, CSimulation2& simulation, shared_ptr<ScriptInterface::StructuredClone> guiMetadataClone, int playerID)
    3637{
    3738    // Determine the filename to save under
     
    4748    return Save(filename.Filename().string(), description, simulation, guiMetadataClone, playerID);
    4849}
    4950
     51
    5052Status SavedGames::Save(const std::wstring& name, const std::wstring& description, CSimulation2& simulation, shared_ptr<ScriptInterface::StructuredClone> guiMetadataClone, int playerID)
    5153{
    5254    // Determine the filename to save under
     
    213215        CScriptValRooted game;
    214216        scriptInterface.Eval("({})", game);
    215217        scriptInterface.SetProperty(game.get(), "id", pathnames[i].Basename());
     218        scriptInterface.SetProperty(game.get(), "saveGameVersionMajor", SAVED_GAME_VERSION_MAJOR);
     219        scriptInterface.SetProperty(game.get(), "saveGameVersionMinor", SAVED_GAME_VERSION_MINOR);
    216220        scriptInterface.SetProperty(game.get(), "metadata", metadata);
    217221        games.push_back(game);
    218222    }
     
    241245    // Successfully deleted file
    242246    return true;
    243247}
     248
     249
     250CScriptValRooted SavedGames::GetCurrentSaveGameVersion(ScriptInterface& scriptInterface)
     251{
     252    CScriptValRooted metainfo;
     253    scriptInterface.Eval("({})", metainfo);
     254    scriptInterface.SetProperty(metainfo.get(), "saveGameVersionMajor", SAVED_GAME_VERSION_MAJOR);
     255    scriptInterface.SetProperty(metainfo.get(), "saveGameVersionMinor", SAVED_GAME_VERSION_MINOR);
     256    return metainfo;
     257}
     258 No newline at end of file
  • source/ps/SavedGame.h

     
    9292 */
    9393bool DeleteSavedGame(const std::wstring& name);
    9494
     95/**
     96 * Gets the current version of the save game format.
     97 *
     98 * @param scriptInterface the ScriptInterface in which to create the return data.
     99 * @return list of objects containing saved game data
     100 */
     101CScriptValRooted GetCurrentSaveGameVersion(ScriptInterface& scriptInterface);
    95102}
    96103
    97104#endif // INCLUDED_SAVEDGAME