| 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 "GameClientProperties.h" |
| 10 |
|
| 11 |
#include "FileItem.h" |
| 12 |
#include "GameClient.h" |
| 13 |
#include "ServiceBroker.h" |
| 14 |
#include "addons/AddonManager.h" |
| 15 |
#include "addons/GameResource.h" |
| 16 |
#include "addons/IAddon.h" |
| 17 |
#include "addons/addoninfo/AddonInfo.h" |
| 18 |
#include "addons/addoninfo/AddonType.h" |
| 19 |
#include "dialogs/GUIDialogYesNo.h" |
| 20 |
#include "filesystem/Directory.h" |
| 21 |
#include "filesystem/SpecialProtocol.h" |
| 22 |
#include "guilib/LocalizeStrings.h" |
| 23 |
#include "messaging/helpers/DialogOKHelper.h" |
| 24 |
#include "utils/StringUtils.h" |
| 25 |
#include "utils/Variant.h" |
| 26 |
#include "utils/log.h" |
| 27 |
|
| 28 |
#include <cstring> |
| 29 |
|
| 30 |
using namespace KODI; |
| 31 |
using namespace ADDON; |
| 32 |
using namespace GAME; |
| 33 |
using namespace XFILE; |
| 34 |
|
| 35 |
CGameClientProperties::CGameClientProperties(const CGameClient& parent, AddonProps_Game& props) |
| 36 |
: m_parent(parent), m_properties(props) |
| 37 |
{ |
| 38 |
} |
| 39 |
|
| 40 |
void CGameClientProperties::ReleaseResources(void) |
| 41 |
{ |
| 42 |
for (auto& it : m_proxyDllPaths) |
| 43 |
delete[] it; |
| 44 |
m_proxyDllPaths.clear(); |
| 45 |
|
| 46 |
for (auto& it : m_resourceDirectories) |
| 47 |
delete[] it; |
| 48 |
m_resourceDirectories.clear(); |
| 49 |
|
| 50 |
for (auto& it : m_extensions) |
| 51 |
delete[] it; |
| 52 |
m_extensions.clear(); |
| 53 |
} |
| 54 |
|
| 55 |
bool CGameClientProperties::InitializeProperties(void) |
| 56 |
{ |
| 57 |
ReleaseResources(); |
| 58 |
|
| 59 |
ADDON::VECADDONS addons; |
| 60 |
if (!GetProxyAddons(addons)) |
| 61 |
return false; |
| 62 |
|
| 63 |
m_properties.game_client_dll_path = GetLibraryPath(); |
| 64 |
m_properties.proxy_dll_paths = GetProxyDllPaths(addons); |
| 65 |
m_properties.proxy_dll_count = GetProxyDllCount(); |
| 66 |
m_properties.resource_directories = GetResourceDirectories(); |
| 67 |
m_properties.resource_directory_count = GetResourceDirectoryCount(); |
| 68 |
m_properties.profile_directory = GetProfileDirectory(); |
| 69 |
m_properties.supports_vfs = m_parent.SupportsVFS(); |
| 70 |
m_properties.extensions = GetExtensions(); |
| 71 |
m_properties.extension_count = GetExtensionCount(); |
| 72 |
|
| 73 |
return true; |
| 74 |
} |
| 75 |
|
| 76 |
const char* CGameClientProperties::GetLibraryPath(void) |
| 77 |
{ |
| 78 |
if (m_strLibraryPath.empty()) |
| 79 |
{ |
| 80 |
// Get the parent add-on's real path |
| 81 |
std::string strLibPath = m_parent.CAddonDll::LibPath(); |
| 82 |
m_strLibraryPath = CSpecialProtocol::TranslatePath(strLibPath); |
| 83 |
URIUtils::RemoveSlashAtEnd(m_strLibraryPath); |
| 84 |
} |
| 85 |
return m_strLibraryPath.c_str(); |
| 86 |
} |
| 87 |
|
| 88 |
const char** CGameClientProperties::GetProxyDllPaths(const ADDON::VECADDONS& addons) |
| 89 |
{ |
| 90 |
if (m_proxyDllPaths.empty()) |
| 91 |
{ |
| 92 |
for (const auto& addon : addons) |
| 93 |
AddProxyDll(std::static_pointer_cast<CGameClient>(addon)); |
| 94 |
} |
| 95 |
|
| 96 |
if (!m_proxyDllPaths.empty()) |
| 97 |
return const_cast<const char**>(m_proxyDllPaths.data()); |
| 98 |
|
| 99 |
return nullptr; |
| 100 |
} |
| 101 |
|
| 102 |
unsigned int CGameClientProperties::GetProxyDllCount(void) const |
| 103 |
{ |
| 104 |
return static_cast<unsigned int>(m_proxyDllPaths.size()); |
| 105 |
} |
| 106 |
|
| 107 |
const char** CGameClientProperties::GetResourceDirectories(void) |
| 108 |
{ |
| 109 |
if (m_resourceDirectories.empty()) |
| 110 |
{ |
| 111 |
// Add all other game resources |
| 112 |
const auto& dependencies = m_parent.GetDependencies(); |
| 113 |
for (auto it = dependencies.begin(); it != dependencies.end(); ++it) |
| 114 |
{ |
| 115 |
const std::string& strAddonId = it->id; |
| 116 |
AddonPtr addon; |
| 117 |
if (CServiceBroker::GetAddonMgr().GetAddon(strAddonId, addon, AddonType::RESOURCE_GAMES, |
| 118 |
OnlyEnabled::CHOICE_YES)) |
| 119 |
{ |
| 120 |
std::shared_ptr<CGameResource> resource = std::static_pointer_cast<CGameResource>(addon); |
| 121 |
|
| 122 |
std::string resourcePath = resource->GetFullPath(""); |
| 123 |
URIUtils::RemoveSlashAtEnd(resourcePath); |
| 124 |
|
| 125 |
char* resourceDir = new char[resourcePath.length() + 1]; |
| 126 |
std::strcpy(resourceDir, resourcePath.c_str()); |
| 127 |
m_resourceDirectories.push_back(resourceDir); |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
// Add resource directories for profile and path |
| 132 |
std::string addonProfile = CSpecialProtocol::TranslatePath(m_parent.Profile()); |
| 133 |
std::string addonPath = m_parent.Path(); |
| 134 |
|
| 135 |
addonProfile = URIUtils::AddFileToFolder(addonProfile, GAME_CLIENT_RESOURCES_DIRECTORY); |
| 136 |
addonPath = URIUtils::AddFileToFolder(addonPath, GAME_CLIENT_RESOURCES_DIRECTORY); |
| 137 |
|
| 138 |
if (!CDirectory::Exists(addonProfile)) |
| 139 |
{ |
| 140 |
CLog::Log(LOGDEBUG, "Creating resource directory: {}", addonProfile); |
| 141 |
CDirectory::Create(addonProfile); |
| 142 |
} |
| 143 |
|
| 144 |
// Only add user profile directory if non-empty |
| 145 |
CFileItemList items; |
| 146 |
if (CDirectory::GetDirectory(addonProfile, items, "", DIR_FLAG_DEFAULTS)) |
| 147 |
{ |
| 148 |
if (!items.IsEmpty()) |
| 149 |
{ |
| 150 |
char* addonProfileDir = new char[addonProfile.length() + 1]; |
| 151 |
std::strcpy(addonProfileDir, addonProfile.c_str()); |
| 152 |
m_resourceDirectories.push_back(addonProfileDir); |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
char* addonPathDir = new char[addonPath.length() + 1]; |
| 157 |
std::strcpy(addonPathDir, addonPath.c_str()); |
| 158 |
m_resourceDirectories.push_back(addonPathDir); |
| 159 |
} |
| 160 |
|
| 161 |
if (!m_resourceDirectories.empty()) |
| 162 |
return const_cast<const char**>(m_resourceDirectories.data()); |
| 163 |
|
| 164 |
return nullptr; |
| 165 |
} |
| 166 |
|
| 167 |
unsigned int CGameClientProperties::GetResourceDirectoryCount(void) const |
| 168 |
{ |
| 169 |
return static_cast<unsigned int>(m_resourceDirectories.size()); |
| 170 |
} |
| 171 |
|
| 172 |
const char* CGameClientProperties::GetProfileDirectory(void) |
| 173 |
{ |
| 174 |
if (m_strProfileDirectory.empty()) |
| 175 |
{ |
| 176 |
m_strProfileDirectory = CSpecialProtocol::TranslatePath(m_parent.Profile()); |
| 177 |
URIUtils::RemoveSlashAtEnd(m_strProfileDirectory); |
| 178 |
} |
| 179 |
|
| 180 |
return m_strProfileDirectory.c_str(); |
| 181 |
} |
| 182 |
|
| 183 |
const char** CGameClientProperties::GetExtensions(void) |
| 184 |
{ |
| 185 |
for (auto& extension : m_parent.GetExtensions()) |
| 186 |
{ |
| 187 |
char* ext = new char[extension.length() + 1]; |
| 188 |
std::strcpy(ext, extension.c_str()); |
| 189 |
m_extensions.push_back(ext); |
| 190 |
} |
| 191 |
|
| 192 |
return !m_extensions.empty() ? const_cast<const char**>(m_extensions.data()) : nullptr; |
| 193 |
} |
| 194 |
|
| 195 |
unsigned int CGameClientProperties::GetExtensionCount(void) const |
| 196 |
{ |
| 197 |
return static_cast<unsigned int>(m_extensions.size()); |
| 198 |
} |
| 199 |
|
| 200 |
bool CGameClientProperties::GetProxyAddons(ADDON::VECADDONS& addons) |
| 201 |
{ |
| 202 |
ADDON::VECADDONS ret; |
| 203 |
std::vector<std::string> missingDependencies; // ID or name of missing dependencies |
| 204 |
|
| 205 |
for (const auto& dependency : m_parent.GetDependencies()) |
| 206 |
{ |
| 207 |
AddonPtr addon; |
| 208 |
if (CServiceBroker::GetAddonMgr().GetAddon(dependency.id, addon, OnlyEnabled::CHOICE_NO)) |
| 209 |
{ |
| 210 |
// If add-on is disabled, ask the user to enable it |
| 211 |
if (CServiceBroker::GetAddonMgr().IsAddonDisabled(dependency.id)) |
| 212 |
{ |
| 213 |
// "Failed to play game" |
| 214 |
// "This game depends on a disabled add-on. Would you like to enable it?" |
| 215 |
if (CGUIDialogYesNo::ShowAndGetInput(CVariant{35210}, CVariant{35215})) |
| 216 |
{ |
| 217 |
if (!CServiceBroker::GetAddonMgr().EnableAddon(dependency.id)) |
| 218 |
{ |
| 219 |
CLog::Log(LOGERROR, "Failed to enable add-on {}", dependency.id); |
| 220 |
missingDependencies.emplace_back(addon->Name()); |
| 221 |
addon.reset(); |
| 222 |
} |
| 223 |
} |
| 224 |
else |
| 225 |
{ |
| 226 |
CLog::Log(LOGERROR, "User chose to not enable add-on {}", dependency.id); |
| 227 |
missingDependencies.emplace_back(addon->Name()); |
| 228 |
addon.reset(); |
| 229 |
} |
| 230 |
} |
| 231 |
|
| 232 |
if (addon && addon->Type() == AddonType::GAMEDLL) |
| 233 |
ret.emplace_back(std::move(addon)); |
| 234 |
} |
| 235 |
else |
| 236 |
{ |
| 237 |
if (dependency.optional) |
| 238 |
{ |
| 239 |
CLog::Log(LOGDEBUG, "Missing optional dependency {}", dependency.id); |
| 240 |
} |
| 241 |
else |
| 242 |
{ |
| 243 |
CLog::Log(LOGERROR, "Missing mandatory dependency {}", dependency.id); |
| 244 |
missingDependencies.emplace_back(dependency.id); |
| 245 |
} |
| 246 |
} |
| 247 |
} |
| 248 |
|
| 249 |
if (!missingDependencies.empty()) |
| 250 |
{ |
| 251 |
std::string strDependencies = StringUtils::Join(missingDependencies, ", "); |
| 252 |
std::string dialogText = StringUtils::Format(g_localizeStrings.Get(35223), strDependencies); |
| 253 |
|
| 254 |
// "Failed to play game" |
| 255 |
// "Add-on is incompatible due to unmet dependencies." |
| 256 |
// "" |
| 257 |
// "Missing: {0:s}" |
| 258 |
MESSAGING::HELPERS::ShowOKDialogLines(CVariant{35210}, CVariant{24104}, CVariant{""}, |
| 259 |
CVariant{dialogText}); |
| 260 |
|
| 261 |
return false; |
| 262 |
} |
| 263 |
|
| 264 |
addons = std::move(ret); |
| 265 |
return true; |
| 266 |
} |
| 267 |
|
| 268 |
void CGameClientProperties::AddProxyDll(const GameClientPtr& gameClient) |
| 269 |
{ |
| 270 |
// Get the add-on's real path |
| 271 |
std::string strLibPath = gameClient->CAddon::LibPath(); |
| 272 |
|
| 273 |
// Ignore add-on if it is already added |
| 274 |
if (!HasProxyDll(strLibPath)) |
| 275 |
{ |
| 276 |
char* libPath = new char[strLibPath.length() + 1]; |
| 277 |
std::strcpy(libPath, strLibPath.c_str()); |
| 278 |
m_proxyDllPaths.push_back(libPath); |
| 279 |
} |
| 280 |
} |
| 281 |
|
| 282 |
bool CGameClientProperties::HasProxyDll(const std::string& strLibPath) const |
| 283 |
{ |
| 284 |
for (const auto& it : m_proxyDllPaths) |
| 285 |
{ |
| 286 |
if (strLibPath == it) |
| 287 |
return true; |
| 288 |
} |
| 289 |
return false; |
| 290 |
} |