Implement noconfirmation hotkey for deleting savegames in the save dialog. Refs ...
[0ad.git] / binaries / data / mods / public / gui / savedgames / load.js
blob0a28e3ca6ab1faca00c4deca7ea3e91ffcbd4bed
1 var g_SavedGamesMetadata = [];
3 /**
4  * Needed for formatPlayerInfo to show the player civs in the details.
5  */
6 const g_CivData = loadCivData();
8 function init()
10         let gameSelection = Engine.GetGUIObjectByName("gameSelection");
12         let savedGames = Engine.GetSavedGames().sort(sortDecreasingDate);
13         if (!savedGames.length)
14         {
15                 gameSelection.list = [translate("No saved games found")];
16                 gameSelection.selected = -1;
17                 selectionChanged();
18                 Engine.GetGUIObjectByName("loadGameButton").enabled = false;
19                 Engine.GetGUIObjectByName("deleteGameButton").enabled = false;
20                 return;
21         }
23         // Get current game version and loaded mods
24         let engineInfo = Engine.GetEngineInfo();
26         g_SavedGamesMetadata = savedGames.map(game => game.metadata);
28         gameSelection.list = savedGames.map(game => generateLabel(game.metadata, engineInfo));
29         gameSelection.list_data = savedGames.map(game => game.id);
31         if (gameSelection.selected == -1)
32                 gameSelection.selected = 0;
33         else if (gameSelection.selected >= savedGames.length) // happens when deleting the last saved game
34                 gameSelection.selected = savedGames.length - 1;
35         else
36                 selectionChanged();
38         Engine.GetGUIObjectByName("deleteGameButton").tooltip = deleteTooltip();
41 function selectionChanged()
43         let gameSelection = Engine.GetGUIObjectByName("gameSelection");
44         let selectionEmpty = gameSelection.selected == -1;
45         Engine.GetGUIObjectByName("invalidGame").hidden = !selectionEmpty;
46         Engine.GetGUIObjectByName("validGame").hidden = selectionEmpty;
48         if (selectionEmpty)
49                 return;
51         let metadata = g_SavedGamesMetadata[gameSelection.selected];
53         Engine.GetGUIObjectByName("savedMapName").caption = translate(metadata.initAttributes.settings.Name);
54         let mapData = getMapDescriptionAndPreview(metadata.initAttributes.mapType, metadata.initAttributes.map);
55         setMapPreviewImage("savedInfoPreview", mapData.preview);
57         Engine.GetGUIObjectByName("savedPlayers").caption = metadata.initAttributes.settings.PlayerData.length - 1;
58         Engine.GetGUIObjectByName("savedPlayedTime").caption = timeToString(metadata.gui.timeElapsed ? metadata.gui.timeElapsed : 0);
59         Engine.GetGUIObjectByName("savedMapType").caption = translateMapType(metadata.initAttributes.mapType);
60         Engine.GetGUIObjectByName("savedMapSize").caption = translateMapSize(metadata.initAttributes.settings.Size);
61         Engine.GetGUIObjectByName("savedVictory").caption = translateVictoryCondition(metadata.initAttributes.settings.GameType);
63         let caption = sprintf(translate("Mods: %(mods)s"), { "mods": metadata.mods.join(translate(", ")) });
64         if (!hasSameMods(metadata, Engine.GetEngineInfo()))
65                 caption = "[color=\"orange\"]" + caption + "[/color]";
66         Engine.GetGUIObjectByName("savedMods").caption = caption;
68         Engine.GetGUIObjectByName("savedPlayersNames").caption = formatPlayerInfo(
69                 metadata.initAttributes.settings.PlayerData,
70                 metadata.gui.states
71         );
74 function loadGame()
76         let gameSelection = Engine.GetGUIObjectByName("gameSelection");
77         let gameId = gameSelection.list_data[gameSelection.selected];
78         let metadata = g_SavedGamesMetadata[gameSelection.selected];
80         // Check compatibility before really loading it
81         let engineInfo = Engine.GetEngineInfo();
82         let sameMods = hasSameMods(metadata, engineInfo);
83         let sameEngineVersion = hasSameEngineVersion(metadata, engineInfo);
84         let sameSavegameVersion = hasSameSavegameVersion(metadata, engineInfo);
86         if (sameEngineVersion && sameSavegameVersion && sameMods)
87         {
88                 reallyLoadGame(gameId);
89                 return;
90         }
92         // Version not compatible ... ask for confirmation
93         let message = translate("This saved game may not be compatible:");
95         if (!sameEngineVersion)
96         {
97                 if (metadata.engine_version)
98                         message += "\n" + sprintf(translate("It needs 0 A.D. version %(requiredVersion)s, while you are running version %(currentVersion)s."), {
99                                 "requiredVersion": metadata.engine_version,
100                                 "currentVersion": engineInfo.engine_version
101                         });
102                 else
103                         message += "\n" + translate("It needs an older version of 0 A.D.");
104         }
106         if (!sameSavegameVersion)
107                 message += "\n" + sprintf(translate("It needs 0 A.D. savegame version %(requiredVersion)s, while you have savegame version %(currentVersion)s."), {
108                         "requiredVersion": metadata.version_major,
109                         "currentVersion": engineInfo.version_major
110                 });
112         if (!sameMods)
113         {
114                 if (!metadata.mods)
115                         metadata.mods = [];
117                 message += translate("The savegame needs a different set of mods:") + "\n" +
118                         sprintf(translate("Required: %(mods)s"), {
119                                 "mods": metadata.mods.join(translate(", "))
120                         }) + "\n" +
121                         sprintf(translate("Active: %(mods)s"), {
122                                 "mods": engineInfo.mods.join(translate(", "))
123                         });
124         }
126         message += "\n" + translate("Do you still want to proceed?");
128         messageBox(
129                 500, 250,
130                 message,
131                 translate("Warning"),
132                 [translate("No"), translate("Yes")],
133                 [init, function(){ reallyLoadGame(gameId); }]
134         );
137 function reallyLoadGame(gameId)
139         let metadata = Engine.StartSavedGame(gameId);
140         if (!metadata)
141         {
142                 // Probably the file wasn't found
143                 // Show error and refresh saved game list
144                 error("Could not load saved game: " + gameId);
145                 init();
146                 return;
147         }
149         let pData = metadata.initAttributes.settings.PlayerData[metadata.playerID];
151         Engine.SwitchGuiPage("page_loading.xml", {
152                 "attribs": metadata.initAttributes,
153                 "isNetworked" : false,
154                 "playerAssignments": {
155                         "local": {
156                                 "name": pData ? pData.Name : singleplayerName(),
157                                 "player": metadata.playerID
158                         }
159                 },
160                 "savedGUIData": metadata.gui
161         });