Ticket #2385: checks.diff

File checks.diff, 9.9 KB (added by mimo, 10 years ago)
  • 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, engineInfo)
    1212{
    1313    var t = new Date(metadata.time*1000);
    14 
    1514    var date = t.getFullYear()+"-"+twoDigits(1+t.getMonth())+"-"+twoDigits(t.getDate());
    1615    var time = twoDigits(t.getHours())+":"+twoDigits(t.getMinutes())+":"+twoDigits(t.getSeconds());
    17     return "["+date+" "+time+"] "+metadata.initAttributes.map+(metadata.description ? " - "+metadata.description : "");
     16    var label = "["+date+" "+time+"] ";
     17
     18    if (engineInfo)
     19    {
     20        if (!hasSameVersion(metadata, engineInfo))
     21            label = "[color=\"red\"]" + label + "[/color]";
     22        else if (!hasSameMods(metadata, engineInfo))
     23            label = "[color=\"orange\"]" + label + "[/color]";
     24    }
     25
     26    label += metadata.initAttributes.map.replace("maps/","")
     27        + (metadata.description ? " - "+metadata.description : "");
     28    return label;
    1829}
     30
     31/**
     32 * Check the version compatibility between the saved game to be loaded and the engine
     33 */
     34function hasSameVersion(metadata, engineInfo)
     35{
     36    return (metadata.version_major == engineInfo.version_major);
     37}
     38
     39/**
     40 * Check the mod compatibility between the saved game to be loaded and the engine
     41 */
     42function hasSameMods(metadata, engineInfo)
     43{
     44    if (!metadata.mods)         // only here for backwards compatibility with previous saved games
     45        var gameMods = [];
     46    else
     47        var gameMods = metadata.mods;
     48
     49    if (gameMods.length != engineInfo.mods.length)
     50        return false;
     51    for (var i = 0; i < gameMods.length; ++i)
     52        if (gameMods[i] != engineInfo.mods[i])
     53            return false;
     54    return true;
     55}
  • binaries/data/mods/public/gui/savedgames/load.js

     
     1var gameMetadatas = [];
     2
    13function init()
    24{
    35    var gameSelection = Engine.GetGUIObjectByName("gameSelection");
     
    35
    46    var savedGames = Engine.GetSavedGames();
    5     if (savedGames.length == 0)
    6     {
    7         gameSelection.list = [ "No saved games found" ];
    8         gameSelection.selected = 0;
    9         Engine.GetGUIObjectByName("loadGameButton").enabled = false;
    10         Engine.GetGUIObjectByName("deleteGameButton").enabled = false;
    11         return;
    12     }
    137
     8    // get current game version and loaded mods
     9    var engineInfo = Engine.GetEngineInfo();
     10
    1411    savedGames.sort(sortDecreasingDate);
    1512
    16     var gameListIDs = [ game.id for each (game in savedGames) ];
    17     var gameListLabels = [ generateLabel(game.metadata) for each (game in savedGames) ];
     13    var gameListIds = [ game.id for each (game in savedGames) ];
     14    var gameListLabels = [ generateLabel(game.metadata, engineInfo) for each (game in savedGames) ];
     15    gameMetadatas = [ game.metadata for each (game in savedGames) ];
    1816
    1917    gameSelection.list = gameListLabels;
    20     gameSelection.list_data = gameListIDs;
    21     gameSelection.selected = 0;
     18    gameSelection.list_data = gameListIds;
     19    if (gameSelection.selected == -1)
     20        gameSelection.selected = 0;
    2221}
    2322
    2423function loadGame()
    2524{
    2625    var gameSelection = Engine.GetGUIObjectByName("gameSelection");
    27     var gameID = gameSelection.list_data[gameSelection.selected];
     26    var gameId = gameSelection.list_data[gameSelection.selected];
     27    var gameLabel = gameSelection.list[gameSelection.selected];
     28    var metadata = gameMetadatas[gameSelection.selected];
    2829
    29     var metadata = Engine.StartSavedGame(gameID);
     30    // check game compatibility before really loading it
     31    var engineInfo = Engine.GetEngineInfo();
     32    if (!hasSameVersion(metadata, engineInfo) || !hasSameMods(metadata, engineInfo))
     33    {
     34        // version not compatible ... ask for confirmation
     35        var btCaptions = ["Yes", "No"];
     36        var btCode = [function(){ reallyLoadGame(gameId); }, init];
     37        var message = "This saved game may not be compatible:";
     38        if (!hasSameVersion(metadata, engineInfo))
     39            message += "\nIt needs 0AD version " + metadata.version_major
     40                + " while you are running version " + engineInfo.version_major + ".";
     41
     42        if (!hasSameMods(metadata, engineInfo))
     43        {
     44            if (!metadata.mods)         // only for backwards compatibility with previous saved games
     45                metadata.mods = [];
     46            if (metadata.mods.length == 0)
     47                message += "\nIt does not need any mod"
     48                    + " while you are running with \"" + engineInfo.mods.join() + "\".";
     49            else if (engineInfo.mods.length == 0)
     50                message += "\nIt needs the mod \"" + metadata.mods.join() + "\""
     51                    + " while you are running without mod.";
     52            else
     53                message += "\nIt needs the mod \"" + metadata.mods.join() + "\""
     54                    + " while you are running with \"" + engineInfo.mods.join() + "\".";
     55        }
     56        message += "\nDo you still want to proceed ?";
     57        messageBox(500, 250, message, "Warning", 0, btCaptions, btCode);
     58    }
     59    else
     60        reallyLoadGame(gameId);
     61}
     62
     63function reallyLoadGame(gameId)
     64{
     65    var metadata = Engine.StartSavedGame(gameId);
    3066    if (!metadata)
    3167    {
    3268        // Probably the file wasn't found
    3369        // Show error and refresh saved game list
    34         error("Could not load saved game '"+gameID+"'");
     70        error("Could not load saved game '"+gameId+"'");
    3571        init();
    3672    }
  • source/gui/scripting/ScriptFunctions.cpp

     
    194194        g_Game->SetPlayerID(id);
    195195}
    196196
     197CScriptValRooted GetEngineInfo(ScriptInterface::CxPrivate* pCxPrivate)
     198{
     199    return SavedGames::GetEngineInfo(*(pCxPrivate->pScriptInterface));
     200}
     201
    197202void StartNetworkGame(ScriptInterface::CxPrivate* UNUSED(pCxPrivate))
    198203{
    199204    ENSURE(g_NetServer);
     
    822827    scriptInterface.RegisterFunction<void, int, std::string, &AssignNetworkPlayer>("AssignNetworkPlayer");
    823828    scriptInterface.RegisterFunction<void, std::wstring, &SendNetworkChat>("SendNetworkChat");
    824829    scriptInterface.RegisterFunction<std::vector<CScriptValRooted>, &GetAIs>("GetAIs");
     830    scriptInterface.RegisterFunction<CScriptValRooted, &GetEngineInfo>("GetEngineInfo");
    825831
    826832    // Saved games
    827833    scriptInterface.RegisterFunction<CScriptVal, std::wstring, &StartSavedGame>("StartSavedGame");
  • source/ps/GameSetup/Config.cpp

     
    7676// If non-empty, specified map will be automatically loaded
    7777CStr g_AutostartMap = "";
    7878
     79std::vector<std::string> g_modsLoaded;
    7980
    8081//----------------------------------------------------------------------------
    8182// config
  • source/ps/GameSetup/Config.h

     
    9090
    9191extern CStrW g_CursorName;
    9292
     93// list of mods currently loaded
     94extern std::vector<std::string> g_modsLoaded;
     95
    9396class CmdLineArgs;
    9497extern void CONFIG_Init(const CmdLineArgs& args);
    9598
  • source/ps/GameSetup/GameSetup.cpp

     
    391391static std::vector<CStr> GetMods(const CmdLineArgs& args, bool dev)
    392392{
    393393    std::vector<CStr> mods = args.GetMultiple("mod");
     394    // List of the mods, to be used by the Gui
     395    g_modsLoaded.clear();
     396    for (size_t i = 0; i < mods.size(); ++i)
     397        g_modsLoaded.push_back((std::string)mods[i]);
    394398    // TODO: It would be nice to remove this hard-coding
    395399    mods.insert(mods.begin(), "public");
    396400
  • source/ps/SavedGame.cpp

     
    2424#include "lib/file/archive/archive_zip.h"
    2525#include "ps/CLogger.h"
    2626#include "ps/Filesystem.h"
     27#include "ps/GameSetup/Config.h"
    2728#include "scriptinterface/ScriptInterface.h"
    2829#include "simulation2/Simulation2.h"
    2930
     
    7677    simulation.GetScriptInterface().Eval("({})", metadata);
    7778    simulation.GetScriptInterface().SetProperty(metadata.get(), "version_major", SAVED_GAME_VERSION_MAJOR);
    7879    simulation.GetScriptInterface().SetProperty(metadata.get(), "version_minor", SAVED_GAME_VERSION_MINOR);
     80    simulation.GetScriptInterface().SetProperty(metadata.get(), "mods", g_modsLoaded);
    7981    simulation.GetScriptInterface().SetProperty(metadata.get(), "time", (double)now);
    8082    simulation.GetScriptInterface().SetProperty(metadata.get(), "player", playerID);
    8183    simulation.GetScriptInterface().SetProperty(metadata.get(), "initAttributes", simulation.GetInitAttributes());
     
    241243    // Successfully deleted file
    242244    return true;
    243245}
     246
     247CScriptValRooted SavedGames::GetEngineInfo(ScriptInterface& scriptInterface)
     248{
     249    CScriptValRooted metainfo;
     250    scriptInterface.Eval("({})", metainfo);
     251    scriptInterface.SetProperty(metainfo.get(), "version_major", SAVED_GAME_VERSION_MAJOR);
     252    scriptInterface.SetProperty(metainfo.get(), "version_minor", SAVED_GAME_VERSION_MINOR);
     253    scriptInterface.SetProperty(metainfo.get(), "mods"         , g_modsLoaded);
     254    return metainfo;
     255}
     256
  • source/ps/SavedGame.h

     
    9292 */
    9393bool DeleteSavedGame(const std::wstring& name);
    9494
     95/**
     96 * Gets info (version and mods loaded) on the running engine
     97 *
     98 * @param scriptInterface the ScriptInterface in which to create the return data.
     99 * @return list of objects containing saved game data
     100 */
     101CScriptValRooted GetEngineInfo(ScriptInterface& scriptInterface);
     102
    95103}
    96104
    97105#endif // INCLUDED_SAVEDGAME