Debian Codegraph @

xbmc/games/GameUtils.cpp

raw
1 /*
2 * Copyright (C) 2012-2018 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 "GameUtils.h"
10
11 #include "FileItem.h"
12 #include "ServiceBroker.h"
13 #include "URL.h"
14 #include "addons/Addon.h"
15 #include "addons/AddonInstaller.h"
16 #include "addons/AddonManager.h"
17 #include "addons/BinaryAddonCache.h"
18 #include "addons/addoninfo/AddonType.h"
19 #include "cores/RetroPlayer/savestates/ISavestate.h"
20 #include "cores/RetroPlayer/savestates/SavestateDatabase.h"
21 #include "filesystem/SpecialProtocol.h"
22 #include "games/addons/GameClient.h"
23 #include "games/dialogs/GUIDialogSelectGameClient.h"
24 #include "games/dialogs/GUIDialogSelectSavestate.h"
25 #include "games/tags/GameInfoTag.h"
26 #include "messaging/helpers/DialogOKHelper.h"
27 #include "utils/StringUtils.h"
28 #include "utils/URIUtils.h"
29 #include "utils/log.h"
30
31 #include <algorithm>
32
33 using namespace KODI;
34 using namespace GAME;
35
36 // Initialize static state
37 ADDON::VECADDONS CGameUtils::m_installableGameAddons;
38 bool CGameUtils::m_checkInstallable{false};
39 std::mutex CGameUtils::m_installableMutex;
40
41 bool CGameUtils::FillInGameClient(CFileItem& item, std::string& savestatePath)
42 {
43 using namespace ADDON;
44
45 if (item.GetGameInfoTag()->GetGameClient().empty())
46 {
47 // If the fileitem is an add-on, fall back to that
48 if (item.HasAddonInfo() && item.GetAddonInfo()->Type() == AddonType::GAMEDLL)
49 {
50 item.GetGameInfoTag()->SetGameClient(item.GetAddonInfo()->ID());
51 }
52 else
53 {
54 if (!CGUIDialogSelectSavestate::ShowAndGetSavestate(item.GetPath(), savestatePath))
55 return false;
56
57 if (!savestatePath.empty())
58 {
59 RETRO::CSavestateDatabase db;
60 std::unique_ptr<RETRO::ISavestate> save = RETRO::CSavestateDatabase::AllocateSavestate();
61 db.GetSavestate(savestatePath, *save);
62 item.GetGameInfoTag()->SetGameClient(save->GameClientID());
63 }
64 else
65 {
66 // No game client specified, need to ask the user
67 GameClientVector candidates;
68 GameClientVector installable;
69 bool bHasVfsGameClient;
70 GetGameClients(item, candidates, installable, bHasVfsGameClient);
71
72 if (candidates.empty() && installable.empty())
73 {
74 // if: "This game can only be played directly from a hard drive or partition. Compressed files must be extracted."
75 // else: "This game isn't compatible with any available emulators."
76 int errorTextId = bHasVfsGameClient ? 35214 : 35212;
77
78 // "Failed to play game"
79 MESSAGING::HELPERS::ShowOKDialogText(CVariant{35210}, CVariant{errorTextId});
80 }
81 else if (candidates.size() == 1 && installable.empty())
82 {
83 // Only 1 option, avoid prompting the user
84 item.GetGameInfoTag()->SetGameClient(candidates[0]->ID());
85 }
86 else
87 {
88 std::string gameClient = CGUIDialogSelectGameClient::ShowAndGetGameClient(
89 item.GetPath(), candidates, installable);
90
91 if (!gameClient.empty())
92 item.GetGameInfoTag()->SetGameClient(gameClient);
93 }
94 }
95 }
96 }
97
98 const std::string gameClient = item.GetGameInfoTag()->GetGameClient();
99 if (gameClient.empty())
100 return false;
101
102 if (Install(gameClient))
103 {
104 // If the addon is disabled we need to enable it
105 if (!Enable(gameClient))
106 {
107 CLog::Log(LOGDEBUG, "Failed to enable game client {}", gameClient);
108 item.GetGameInfoTag()->SetGameClient("");
109 }
110 }
111 else
112 {
113 CLog::Log(LOGDEBUG, "Failed to install game client: {}", gameClient);
114 item.GetGameInfoTag()->SetGameClient("");
115 }
116
117 return !item.GetGameInfoTag()->GetGameClient().empty();
118 }
119
120 void CGameUtils::GetGameClients(const CFileItem& file,
121 GameClientVector& candidates,
122 GameClientVector& installable,
123 bool& bHasVfsGameClient)
124 {
125 using namespace ADDON;
126
127 bHasVfsGameClient = false;
128
129 // Try to resolve path to a local file, as not all game clients support VFS
130 CURL translatedUrl(CSpecialProtocol::TranslatePath(file.GetPath()));
131
132 // Get local candidates
133 VECADDONS localAddons;
134 CBinaryAddonCache& addonCache = CServiceBroker::GetBinaryAddonCache();
135 addonCache.GetAddons(localAddons, AddonType::GAMEDLL);
136
137 bool bVfs = false;
138 GetGameClients(localAddons, translatedUrl, candidates, bVfs);
139 bHasVfsGameClient |= bVfs;
140
141 // Get remote candidates
142 VECADDONS remoteAddons;
143 if (CServiceBroker::GetAddonMgr().GetInstallableAddons(remoteAddons, AddonType::GAMEDLL))
144 {
145 GetGameClients(remoteAddons, translatedUrl, installable, bVfs);
146 bHasVfsGameClient |= bVfs;
147 }
148
149 // Sort by name
150 //! @todo Move to presentation code
151 auto SortByName = [](const GameClientPtr& lhs, const GameClientPtr& rhs)
152 {
153 std::string lhsName = lhs->Name();
154 std::string rhsName = rhs->Name();
155
156 StringUtils::ToLower(lhsName);
157 StringUtils::ToLower(rhsName);
158
159 return lhsName < rhsName;
160 };
161
162 std::sort(candidates.begin(), candidates.end(), SortByName);
163 std::sort(installable.begin(), installable.end(), SortByName);
164 }
165
166 void CGameUtils::GetGameClients(const ADDON::VECADDONS& addons,
167 const CURL& translatedUrl,
168 GameClientVector& candidates,
169 bool& bHasVfsGameClient)
170 {
171 bHasVfsGameClient = false;
172
173 const std::string extension = URIUtils::GetExtension(translatedUrl.Get());
174
175 const bool bIsLocalFile =
176 (translatedUrl.GetProtocol() == "file" || translatedUrl.GetProtocol().empty());
177
178 for (auto& addon : addons)
179 {
180 GameClientPtr gameClient = std::static_pointer_cast<CGameClient>(addon);
181
182 // Filter by extension
183 if (!gameClient->IsExtensionValid(extension))
184 continue;
185
186 // Filter by VFS
187 if (!bIsLocalFile && !gameClient->SupportsVFS())
188 {
189 bHasVfsGameClient = true;
190 continue;
191 }
192
193 candidates.push_back(gameClient);
194 }
195 }
196
197 bool CGameUtils::HasGameExtension(const std::string& path)
198 {
199 using namespace ADDON;
200
201 // Get filename from CURL so that top-level zip directories will become
202 // normal paths:
203 //
204 // zip://%2Fpath_to_zip_file.zip/ -> /path_to_zip_file.zip
205 //
206 std::string filename = CURL(path).GetFileNameWithoutPath();
207
208 // Get the file extension
209 std::string extension = URIUtils::GetExtension(filename);
210 if (extension.empty())
211 return false;
212
213 StringUtils::ToLower(extension);
214
215 // Look for a game client that supports this extension
216 VECADDONS gameClients;
217 CBinaryAddonCache& addonCache = CServiceBroker::GetBinaryAddonCache();
218 addonCache.GetInstalledAddons(gameClients, AddonType::GAMEDLL);
219 for (auto& gameClient : gameClients)
220 {
221 GameClientPtr gc(std::static_pointer_cast<CGameClient>(gameClient));
222 if (gc->IsExtensionValid(extension))
223 return true;
224 }
225
226 // Check remote add-ons
227 std::lock_guard<std::mutex> installableLock(m_installableMutex);
228 LoadInstallableAddons();
229 if (!m_installableGameAddons.empty())
230 {
231 for (auto& gameClient : m_installableGameAddons)
232 {
233 GameClientPtr gc(std::static_pointer_cast<CGameClient>(gameClient));
234 if (gc->IsExtensionValid(extension))
235 return true;
236 }
237 }
238
239 return false;
240 }
241
242 std::set<std::string> CGameUtils::GetGameExtensions()
243 {
244 using namespace ADDON;
245
246 std::set<std::string> extensions;
247
248 VECADDONS gameClients;
249 CBinaryAddonCache& addonCache = CServiceBroker::GetBinaryAddonCache();
250 addonCache.GetAddons(gameClients, AddonType::GAMEDLL);
251 for (auto& gameClient : gameClients)
252 {
253 GameClientPtr gc(std::static_pointer_cast<CGameClient>(gameClient));
254 extensions.insert(gc->GetExtensions().begin(), gc->GetExtensions().end());
255 }
256
257 // Check remote add-ons
258 std::lock_guard<std::mutex> installableLock(m_installableMutex);
259 LoadInstallableAddons();
260 if (!m_installableGameAddons.empty())
261 {
262 for (auto& gameClient : m_installableGameAddons)
263 {
264 GameClientPtr gc(std::static_pointer_cast<CGameClient>(gameClient));
265 extensions.insert(gc->GetExtensions().begin(), gc->GetExtensions().end());
266 }
267 }
268
269 return extensions;
270 }
271
272 bool CGameUtils::IsStandaloneGame(const ADDON::AddonPtr& addon)
273 {
274 using namespace ADDON;
275
276 switch (addon->Type())
277 {
278 case AddonType::GAMEDLL:
279 {
280 return std::static_pointer_cast<GAME::CGameClient>(addon)->SupportsStandalone();
281 }
282 case AddonType::SCRIPT:
283 {
284 return addon->HasType(AddonType::GAME);
285 }
286 default:
287 break;
288 }
289
290 return false;
291 }
292
293 void CGameUtils::UpdateInstallableAddons()
294 {
295 std::lock_guard<std::mutex> installableLock(m_installableMutex);
296 m_checkInstallable = true;
297 }
298
299 bool CGameUtils::Install(const std::string& gameClient)
300 {
301 // If the addon isn't installed we need to install it
302 bool installed = CServiceBroker::GetAddonMgr().IsAddonInstalled(gameClient);
303 if (!installed)
304 {
305 ADDON::AddonPtr installedAddon;
306 installed = ADDON::CAddonInstaller::GetInstance().InstallModal(
307 gameClient, installedAddon, ADDON::InstallModalPrompt::CHOICE_NO);
308 if (!installed)
309 {
310 CLog::Log(LOGERROR, "Game utils: Failed to install {}", gameClient);
311 // "Error"
312 // "Failed to install add-on."
313 MESSAGING::HELPERS::ShowOKDialogText(CVariant{257}, CVariant{35256});
314 }
315 }
316
317 return installed;
318 }
319
320 bool CGameUtils::Enable(const std::string& gameClient)
321 {
322 bool bSuccess = true;
323
324 if (CServiceBroker::GetAddonMgr().IsAddonDisabled(gameClient))
325 bSuccess = CServiceBroker::GetAddonMgr().EnableAddon(gameClient);
326
327 return bSuccess;
328 }
329
330 void CGameUtils::LoadInstallableAddons()
331 {
332 using namespace ADDON;
333
334 if (m_checkInstallable)
335 {
336 m_checkInstallable = false;
337 m_installableGameAddons.clear();
338 CServiceBroker::GetAddonMgr().GetInstallableAddons(m_installableGameAddons, AddonType::GAMEDLL);
339 }
340 }