Implement noconfirmation hotkey for deleting savegames in the save dialog. Refs ...
[0ad.git] / binaries / data / mods / public / gui / common / functions_utility_loadsave.js
blobc6254ca5a24d54200987e9a0353594ba0833ae9e
1 function sortDecreasingDate(a, b)
3         return b.metadata.time - a.metadata.time;
6 function generateLabel(metadata, engineInfo)
8         let dateTimeString = Engine.FormatMillisecondsIntoDateString(metadata.time*1000, translate("yyyy-MM-dd HH:mm:ss"));
9         let dateString = sprintf(translate("\\[%(date)s]"), { "date": dateTimeString });
11         if (engineInfo)
12         {
13                 if (!hasSameSavegameVersion(metadata, engineInfo) || !hasSameEngineVersion(metadata, engineInfo))
14                         dateString = "[color=\"red\"]" + dateString + "[/color]";
15                 else if (!hasSameMods(metadata, engineInfo))
16                         dateString = "[color=\"orange\"]" + dateString + "[/color]";
17         }
19         return sprintf(
20                 metadata.description ?
21                         translate("%(dateString)s %(map)s - %(description)s") :
22                         translate("%(dateString)s %(map)s"),
23                 {
24                         "dateString": dateString,
25                         "map": metadata.initAttributes.map,
26                         "description": metadata.description || ""
27                 }
28         );
31 /**
32  * Check the version compatibility between the saved game to be loaded and the engine
33  */
34 function hasSameSavegameVersion(metadata, engineInfo)
36         return metadata.version_major == engineInfo.version_major;
39 /**
40  * Check the version compatibility between the saved game to be loaded and the engine
41  */
42 function hasSameEngineVersion(metadata, engineInfo)
44         return metadata.engine_version && metadata.engine_version == engineInfo.engine_version;
47 /**
48  * Check the mod compatibility between the saved game to be loaded and the engine
49  *
50  * @param metadata {string[]}
51  * @param engineInfo {string[]}
52  * @returns {boolean}
53  */
54 function hasSameMods(metadata, engineInfo)
56         if (!metadata.mods || !engineInfo.mods)
57                 return false;
59         // Ignore the "user" mod which is loaded for releases but not working-copies
60         let modsA = metadata.mods.filter(mod => mod != "user");
61         let modsB = engineInfo.mods.filter(mod => mod != "user");
63         if (modsA.length != modsB.length)
64                 return false;
66         // Mods must be loaded in the same order
67         return modsA.every((mod, index) => mod == modsB[index]);
70 function deleteGame()
72         let gameSelection = Engine.GetGUIObjectByName("gameSelection");
73         let gameID = gameSelection.list_data[gameSelection.selected];
75         if (!gameID)
76                 return;
78         if (Engine.HotkeyIsPressed("session.savedgames.noconfirmation"))
79                 reallyDeleteGame(gameID);
80         else
81                 messageBox(
82                         500, 200,
83                         sprintf(translate("\"%(label)s\""), {
84                                 "label": gameSelection.list[gameSelection.selected]
85                         }) + "\n" + translate("Saved game will be permanently deleted, are you sure?"),
86                         translate("DELETE"),
87                         [translate("No"), translate("Yes")],
88                         [null, function(){ reallyDeleteGame(gameID); }]
89                 );
92 function reallyDeleteGame(gameID)
94         if (!Engine.DeleteSavedGame(gameID))
95                 error("Could not delete saved game: " + gameID);
97         // Run init again to refresh saved game list
98         init();
101 function deleteTooltip()
103         let deleteTooltip = colorizeHotkey(
104                 translate("Delete the selected entry using %(hotkey)s."),
105                 "session.savedgames.delete");
107         if (deleteTooltip)
108                 deleteTooltip += colorizeHotkey(
109                         "\n"  + translate("Hold %(hotkey)s to delete without confirmation."),
110                         "session.savedgames.noconfirmation");
112         return deleteTooltip;