Debian Codegraph @

xbmc/games/dialogs/osd/DialogInGameSaves.cpp

raw
1 /*
2 * Copyright (C) 2020-2021 Team Kodi
3 * This file is part of Kodi - https://kodi.tv
4 *
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 * See LICENSES/README.md for more information.
7 */
8
9 #include "DialogInGameSaves.h"
10
11 #include "ServiceBroker.h"
12 #include "URL.h"
13 #include "XBDateTime.h"
14 #include "cores/RetroPlayer/guibridge/GUIGameRenderManager.h"
15 #include "cores/RetroPlayer/guibridge/GUIGameSettingsHandle.h"
16 #include "cores/RetroPlayer/guicontrols/GUIGameControl.h"
17 #include "cores/RetroPlayer/playback/IPlayback.h"
18 #include "cores/RetroPlayer/savestates/ISavestate.h"
19 #include "cores/RetroPlayer/savestates/SavestateDatabase.h"
20 #include "dialogs/GUIDialogContextMenu.h"
21 #include "dialogs/GUIDialogOK.h"
22 #include "dialogs/GUIDialogYesNo.h"
23 #include "games/dialogs/DialogGameDefines.h"
24 #include "guilib/GUIKeyboardFactory.h"
25 #include "guilib/GUIMessage.h"
26 #include "guilib/LocalizeStrings.h"
27 #include "guilib/WindowIDs.h"
28 #include "settings/GameSettings.h"
29 #include "settings/MediaSettings.h"
30 #include "utils/log.h"
31
32 using namespace KODI;
33 using namespace GAME;
34 using namespace RETRO;
35
36 namespace
37 {
38 CFileItemPtr CreateNewSaveItem()
39 {
40 CFileItemPtr item = std::make_shared<CFileItem>(g_localizeStrings.Get(15314)); // "Save"
41
42 // A nonexistent path ensures a gamewindow control won't render any pixels
43 item->SetPath(NO_PIXEL_DATA);
44 item->SetArt("icon", "DefaultAddSource.png");
45 item->SetProperty(SAVESTATE_CAPTION,
46 g_localizeStrings.Get(15315)); // "Save progress to a new save file"
47
48 return item;
49 }
50 } // namespace
51
52 CDialogInGameSaves::CDialogInGameSaves()
53 : CDialogGameVideoSelect(WINDOW_DIALOG_IN_GAME_SAVES), m_newSaveItem(CreateNewSaveItem())
54 {
55 }
56
57 bool CDialogInGameSaves::OnMessage(CGUIMessage& message)
58 {
59 switch (message.GetMessage())
60 {
61 case GUI_MSG_REFRESH_THUMBS:
62 {
63 if (message.GetControlId() == GetID())
64 {
65 const std::string& itemPath = message.GetStringParam();
66 std::shared_ptr<CGUIListItem> itemInfo = message.GetItem();
67
68 if (!itemPath.empty())
69 {
70 OnItemRefresh(itemPath, itemInfo);
71 }
72 else
73 {
74 InitSavedGames();
75 RefreshList();
76 }
77
78 return true;
79 }
80 break;
81 }
82 default:
83 break;
84 }
85
86 return CDialogGameVideoSelect::OnMessage(message);
87 }
88
89 std::string CDialogInGameSaves::GetHeading()
90 {
91 return g_localizeStrings.Get(35249); // "Save / Load"
92 }
93
94 void CDialogInGameSaves::PreInit()
95 {
96 InitSavedGames();
97 }
98
99 void CDialogInGameSaves::InitSavedGames()
100 {
101 m_savestateItems.Clear();
102
103 auto gameSettings = CServiceBroker::GetGameRenderManager().RegisterGameSettingsDialog();
104
105 CSavestateDatabase db;
106 db.GetSavestatesNav(m_savestateItems, gameSettings->GetPlayingGame(),
107 gameSettings->GameClientID());
108
109 m_savestateItems.Sort(SortByDate, SortOrderDescending);
110 }
111
112 void CDialogInGameSaves::GetItems(CFileItemList& items)
113 {
114 items.Add(m_newSaveItem);
115 std::for_each(m_savestateItems.cbegin(), m_savestateItems.cend(),
116 [&items](const auto& item) { items.Add(item); });
117 }
118
119 void CDialogInGameSaves::OnItemFocus(unsigned int index)
120 {
121 if (static_cast<int>(index) < 1 + m_savestateItems.Size())
122 m_focusedItemIndex = index;
123 }
124
125 unsigned int CDialogInGameSaves::GetFocusedItem() const
126 {
127 return m_focusedControl;
128 }
129
130 void CDialogInGameSaves::OnItemRefresh(const std::string& itemPath,
131 const std::shared_ptr<CGUIListItem>& itemInfo)
132 {
133 // Turn the message params into a savestate item
134 CFileItemPtr item = TranslateMessageItem(itemPath, itemInfo);
135 if (item)
136 {
137 // Look up existing savestate by path
138 auto it =
139 std::find_if(m_savestateItems.cbegin(), m_savestateItems.cend(),
140 [&itemPath](const CFileItemPtr& item) { return item->GetPath() == itemPath; });
141
142 // Update savestate or add a new one
143 if (it != m_savestateItems.cend())
144 **it = *item;
145 else
146 m_savestateItems.AddFront(item, 0);
147
148 RefreshList();
149 }
150 }
151
152 void CDialogInGameSaves::PostExit()
153 {
154 m_savestateItems.Clear();
155 }
156
157 bool CDialogInGameSaves::OnClickAction()
158 {
159 if (static_cast<int>(m_focusedItemIndex) < 1 + m_savestateItems.Size())
160 {
161 if (m_focusedItemIndex <= 0)
162 {
163 OnNewSave();
164 return true;
165 }
166 else
167 {
168 CFileItemPtr focusedItem = m_savestateItems[m_focusedItemIndex - 1];
169 if (focusedItem)
170 {
171 OnLoad(*focusedItem);
172 return true;
173 }
174 }
175 }
176
177 return false;
178 }
179
180 bool CDialogInGameSaves::OnMenuAction()
181 {
182 // Start at index 1 to account for leading "Save" item
183 if (1 <= m_focusedItemIndex && static_cast<int>(m_focusedItemIndex) < 1 + m_savestateItems.Size())
184 {
185 CFileItemPtr focusedItem = m_savestateItems[m_focusedItemIndex - 1];
186 if (focusedItem)
187 {
188 CContextButtons buttons;
189
190 buttons.Add(0, 13206); // "Overwrite"
191 buttons.Add(1, 118); // "Rename"
192 buttons.Add(2, 117); // "Delete"
193
194 const int index = CGUIDialogContextMenu::Show(buttons);
195
196 if (index == 0)
197 OnOverwrite(*focusedItem);
198 if (index == 1)
199 OnRename(*focusedItem);
200 else if (index == 2)
201 OnDelete(*focusedItem);
202
203 return true;
204 }
205 }
206
207 return false;
208 }
209
210 bool CDialogInGameSaves::OnOverwriteAction()
211 {
212 // Start at index 1 to account for leading "Save" item
213 if (1 <= m_focusedItemIndex && static_cast<int>(m_focusedItemIndex) < 1 + m_savestateItems.Size())
214 {
215 CFileItemPtr focusedItem = m_savestateItems[m_focusedItemIndex - 1];
216 if (focusedItem)
217 {
218 OnOverwrite(*focusedItem);
219 return true;
220 }
221 }
222
223 return false;
224 }
225
226 bool CDialogInGameSaves::OnRenameAction()
227 {
228 // Start at index 1 to account for leading "Save" item
229 if (1 <= m_focusedItemIndex && static_cast<int>(m_focusedItemIndex) < 1 + m_savestateItems.Size())
230 {
231 CFileItemPtr focusedItem = m_savestateItems[m_focusedItemIndex - 1];
232 if (focusedItem)
233 {
234 OnRename(*focusedItem);
235 return true;
236 }
237 }
238
239 return false;
240 }
241
242 bool CDialogInGameSaves::OnDeleteAction()
243 {
244 // Start at index 1 to account for leading "Save" item
245 if (1 <= m_focusedItemIndex && static_cast<int>(m_focusedItemIndex) < 1 + m_savestateItems.Size())
246 {
247 CFileItemPtr focusedItem = m_savestateItems[m_focusedItemIndex - 1];
248 if (focusedItem)
249 {
250 OnDelete(*focusedItem);
251 return true;
252 }
253 }
254
255 return false;
256 }
257
258 void CDialogInGameSaves::OnNewSave()
259 {
260 auto gameSettings = CServiceBroker::GetGameRenderManager().RegisterGameSettingsDialog();
261
262 const std::string savestatePath = gameSettings->CreateSavestate(false);
263 if (savestatePath.empty())
264 {
265 // "Error"
266 // "An unknown error has occurred."
267 CGUIDialogOK::ShowAndGetInput(257, 24071);
268 return;
269 }
270
271 // Create a simulated savestate to update the GUI faster. We will be notified
272 // of the real savestate info via OnMessage() when the savestate creation
273 // completes.
274 auto savestate = RETRO::CSavestateDatabase::AllocateSavestate();
275
276 savestate->SetType(SAVE_TYPE::MANUAL);
277 savestate->SetCreated(CDateTime::GetUTCDateTime());
278
279 savestate->Finalize();
280
281 CFileItemPtr item = std::make_shared<CFileItem>();
282 CSavestateDatabase::GetSavestateItem(*savestate, savestatePath, *item);
283
284 m_savestateItems.AddFront(item, 0);
285
286 RefreshList();
287 }
288
289 void CDialogInGameSaves::OnLoad(CFileItem& focusedItem)
290 {
291 auto gameSettings = CServiceBroker::GetGameRenderManager().RegisterGameSettingsDialog();
292
293 // Load savestate
294 if (gameSettings->LoadSavestate(focusedItem.GetPath()))
295 {
296 // Close OSD on successful load
297 gameSettings->CloseOSD();
298 }
299 else
300 {
301 // "Error"
302 // "An unknown error has occurred."
303 CGUIDialogOK::ShowAndGetInput(257, 24071);
304 }
305 }
306
307 void CDialogInGameSaves::OnOverwrite(CFileItem& focusedItem)
308 {
309 std::string savestatePath = focusedItem.GetPath();
310 if (savestatePath.empty())
311 return;
312
313 auto gameSettings = CServiceBroker::GetGameRenderManager().RegisterGameSettingsDialog();
314
315 // Update savestate
316 if (gameSettings->UpdateSavestate(savestatePath))
317 {
318 // Create a simulated savestate to update the GUI faster. We will be
319 // notified of the real savestate info via OnMessage() when the
320 // overwriting completes.
321 auto savestate = RETRO::CSavestateDatabase::AllocateSavestate();
322
323 savestate->SetType(SAVE_TYPE::MANUAL);
324 savestate->SetLabel(focusedItem.GetProperty(SAVESTATE_LABEL).asString());
325 savestate->SetCaption(focusedItem.GetProperty(SAVESTATE_CAPTION).asString());
326 savestate->SetCreated(CDateTime::GetUTCDateTime());
327
328 savestate->Finalize();
329
330 CSavestateDatabase::GetSavestateItem(*savestate, savestatePath, focusedItem);
331
332 RefreshList();
333 }
334 else
335 {
336 // Log an error and notify the user
337 CLog::Log(LOGERROR, "Failed to overwrite savestate at {}", CURL::GetRedacted(savestatePath));
338
339 // "Error"
340 // "An unknown error has occurred."
341 CGUIDialogOK::ShowAndGetInput(257, 24071);
342 }
343 }
344
345 void CDialogInGameSaves::OnRename(CFileItem& focusedItem)
346 {
347 const std::string& savestatePath = focusedItem.GetPath();
348 if (savestatePath.empty())
349 return;
350
351 RETRO::CSavestateDatabase db;
352
353 std::string label;
354
355 std::unique_ptr<RETRO::ISavestate> savestate = RETRO::CSavestateDatabase::AllocateSavestate();
356 if (db.GetSavestate(savestatePath, *savestate))
357 label = savestate->Label();
358
359 // "Enter new filename"
360 if (CGUIKeyboardFactory::ShowAndGetInput(label, CVariant{g_localizeStrings.Get(16013)}, true) &&
361 label != savestate->Label())
362 {
363 std::unique_ptr<RETRO::ISavestate> newSavestate = db.RenameSavestate(savestatePath, label);
364 if (newSavestate)
365 {
366 RETRO::CSavestateDatabase::GetSavestateItem(*newSavestate, savestatePath, focusedItem);
367
368 RefreshList();
369 }
370 else
371 {
372 // "Error"
373 // "An unknown error has occurred."
374 CGUIDialogOK::ShowAndGetInput(257, 24071);
375 }
376 }
377 }
378
379 void CDialogInGameSaves::OnDelete(CFileItem& focusedItem)
380 {
381 // "Confirm delete"
382 // "Would you like to delete the selected file(s)?[CR]Warning - this action can't be undone!"
383 if (CGUIDialogYesNo::ShowAndGetInput(CVariant{122}, CVariant{125}))
384 {
385 RETRO::CSavestateDatabase db;
386 if (db.DeleteSavestate(focusedItem.GetPath()))
387 {
388 m_savestateItems.Remove(&focusedItem);
389
390 RefreshList();
391
392 auto gameSettings = CServiceBroker::GetGameRenderManager().RegisterGameSettingsDialog();
393 gameSettings->FreeSavestateResources(focusedItem.GetPath());
394 }
395 else
396 {
397 // "Error"
398 // "An unknown error has occurred."
399 CGUIDialogOK::ShowAndGetInput(257, 24071);
400 }
401 }
402 }
403
404 CFileItemPtr CDialogInGameSaves::TranslateMessageItem(
405 const std::string& messagePath, const std::shared_ptr<CGUIListItem>& messageItem)
406 {
407 CFileItemPtr item;
408
409 if (messageItem && messageItem->IsFileItem())
410 item = std::static_pointer_cast<CFileItem>(messageItem);
411 else if (messageItem)
412 item = std::make_shared<CFileItem>(*messageItem);
413 else if (!messagePath.empty())
414 {
415 item = std::make_shared<CFileItem>();
416
417 // Load savestate if no item info was given
418 auto savestate = RETRO::CSavestateDatabase::AllocateSavestate();
419 RETRO::CSavestateDatabase db;
420 if (db.GetSavestate(messagePath, *savestate))
421 RETRO::CSavestateDatabase::GetSavestateItem(*savestate, messagePath, *item);
422 else
423 item.reset();
424 }
425
426 return item;
427 }