aboutsummaryrefslogtreecommitdiff
path: root/xbmc/games/addons/GameClientProperties.cpp
blob: ae081afa923ce07a820845bba1541119882af870 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/*
 *  Copyright (C) 2012-2018 Team Kodi
 *  This file is part of Kodi - https://kodi.tv
 *
 *  SPDX-License-Identifier: GPL-2.0-or-later
 *  See LICENSES/README.md for more information.
 */

#include "GameClientProperties.h"

#include "FileItem.h"
#include "FileItemList.h"
#include "GameClient.h"
#include "ServiceBroker.h"
#include "addons/AddonManager.h"
#include "addons/GameResource.h"
#include "addons/IAddon.h"
#include "addons/addoninfo/AddonInfo.h"
#include "addons/addoninfo/AddonType.h"
#include "dialogs/GUIDialogYesNo.h"
#include "filesystem/Directory.h"
#include "filesystem/SpecialProtocol.h"
#include "guilib/LocalizeStrings.h"
#include "messaging/helpers/DialogOKHelper.h"
#include "utils/StringUtils.h"
#include "utils/Variant.h"
#include "utils/log.h"

#include <cstring>

using namespace KODI;
using namespace ADDON;
using namespace GAME;
using namespace XFILE;

CGameClientProperties::CGameClientProperties(const CGameClient& parent, AddonProps_Game& props)
  : m_parent(parent), m_properties(props)
{
}

void CGameClientProperties::ReleaseResources(void)
{
  for (auto& it : m_proxyDllPaths)
    delete[] it;
  m_proxyDllPaths.clear();

  for (auto& it : m_resourceDirectories)
    delete[] it;
  m_resourceDirectories.clear();

  for (auto& it : m_extensions)
    delete[] it;
  m_extensions.clear();
}

bool CGameClientProperties::InitializeProperties(void)
{
  ReleaseResources();

  ADDON::VECADDONS addons;
  if (!GetProxyAddons(addons))
    return false;

  m_properties.game_client_dll_path = GetLibraryPath();
  m_properties.proxy_dll_paths = GetProxyDllPaths(addons);
  m_properties.proxy_dll_count = GetProxyDllCount();
  m_properties.resource_directories = GetResourceDirectories();
  m_properties.resource_directory_count = GetResourceDirectoryCount();
  m_properties.profile_directory = GetProfileDirectory();
  m_properties.supports_vfs = m_parent.SupportsVFS();
  m_properties.extensions = GetExtensions();
  m_properties.extension_count = GetExtensionCount();

  return true;
}

const char* CGameClientProperties::GetLibraryPath(void)
{
  if (m_strLibraryPath.empty())
  {
    // Get the parent add-on's real path
    std::string strLibPath = m_parent.CAddonDll::LibPath();
    m_strLibraryPath = CSpecialProtocol::TranslatePath(strLibPath);
    URIUtils::RemoveSlashAtEnd(m_strLibraryPath);
  }
  return m_strLibraryPath.c_str();
}

const char** CGameClientProperties::GetProxyDllPaths(const ADDON::VECADDONS& addons)
{
  if (m_proxyDllPaths.empty())
  {
    for (const auto& addon : addons)
      AddProxyDll(std::static_pointer_cast<CGameClient>(addon));
  }

  if (!m_proxyDllPaths.empty())
    return const_cast<const char**>(m_proxyDllPaths.data());

  return nullptr;
}

unsigned int CGameClientProperties::GetProxyDllCount(void) const
{
  return static_cast<unsigned int>(m_proxyDllPaths.size());
}

const char** CGameClientProperties::GetResourceDirectories(void)
{
  if (m_resourceDirectories.empty())
  {
    // Add all other game resources
    const auto& dependencies = m_parent.GetDependencies();
    for (auto it = dependencies.begin(); it != dependencies.end(); ++it)
    {
      const std::string& strAddonId = it->id;
      AddonPtr addon;
      if (CServiceBroker::GetAddonMgr().GetAddon(strAddonId, addon, AddonType::RESOURCE_GAMES,
                                                 OnlyEnabled::CHOICE_YES))
      {
        std::shared_ptr<CGameResource> resource = std::static_pointer_cast<CGameResource>(addon);

        std::string resourcePath = resource->GetFullPath("");
        URIUtils::RemoveSlashAtEnd(resourcePath);

        char* resourceDir = new char[resourcePath.length() + 1];
        std::strcpy(resourceDir, resourcePath.c_str());
        m_resourceDirectories.push_back(resourceDir);
      }
    }

    // Add resource directories for profile and path
    std::string addonProfile = CSpecialProtocol::TranslatePath(m_parent.Profile());
    std::string addonPath = m_parent.Path();

    addonProfile = URIUtils::AddFileToFolder(addonProfile, GAME_CLIENT_RESOURCES_DIRECTORY);
    addonPath = URIUtils::AddFileToFolder(addonPath, GAME_CLIENT_RESOURCES_DIRECTORY);

    if (!CDirectory::Exists(addonProfile))
    {
      CLog::Log(LOGDEBUG, "Creating resource directory: {}", addonProfile);
      CDirectory::Create(addonProfile);
    }

    // Only add user profile directory if non-empty
    CFileItemList items;
    if (CDirectory::GetDirectory(addonProfile, items, "", DIR_FLAG_DEFAULTS))
    {
      if (!items.IsEmpty())
      {
        char* addonProfileDir = new char[addonProfile.length() + 1];
        std::strcpy(addonProfileDir, addonProfile.c_str());
        m_resourceDirectories.push_back(addonProfileDir);
      }
    }

    char* addonPathDir = new char[addonPath.length() + 1];
    std::strcpy(addonPathDir, addonPath.c_str());
    m_resourceDirectories.push_back(addonPathDir);
  }

  if (!m_resourceDirectories.empty())
    return const_cast<const char**>(m_resourceDirectories.data());

  return nullptr;
}

unsigned int CGameClientProperties::GetResourceDirectoryCount(void) const
{
  return static_cast<unsigned int>(m_resourceDirectories.size());
}

const char* CGameClientProperties::GetProfileDirectory(void)
{
  if (m_strProfileDirectory.empty())
  {
    m_strProfileDirectory = CSpecialProtocol::TranslatePath(m_parent.Profile());
    URIUtils::RemoveSlashAtEnd(m_strProfileDirectory);
  }

  return m_strProfileDirectory.c_str();
}

const char** CGameClientProperties::GetExtensions(void)
{
  for (auto& extension : m_parent.GetExtensions())
  {
    char* ext = new char[extension.length() + 1];
    std::strcpy(ext, extension.c_str());
    m_extensions.push_back(ext);
  }

  return !m_extensions.empty() ? const_cast<const char**>(m_extensions.data()) : nullptr;
}

unsigned int CGameClientProperties::GetExtensionCount(void) const
{
  return static_cast<unsigned int>(m_extensions.size());
}

bool CGameClientProperties::GetProxyAddons(ADDON::VECADDONS& addons)
{
  ADDON::VECADDONS ret;
  std::vector<std::string> missingDependencies; // ID or name of missing dependencies

  for (const auto& dependency : m_parent.GetDependencies())
  {
    AddonPtr addon;
    if (CServiceBroker::GetAddonMgr().GetAddon(dependency.id, addon, OnlyEnabled::CHOICE_NO))
    {
      // If add-on is disabled, ask the user to enable it
      if (CServiceBroker::GetAddonMgr().IsAddonDisabled(dependency.id))
      {
        // "Failed to play game"
        // "This game depends on a disabled add-on. Would you like to enable it?"
        if (CGUIDialogYesNo::ShowAndGetInput(CVariant{35210}, CVariant{35215}))
        {
          if (!CServiceBroker::GetAddonMgr().EnableAddon(dependency.id))
          {
            CLog::Log(LOGERROR, "Failed to enable add-on {}", dependency.id);
            missingDependencies.emplace_back(addon->Name());
            addon.reset();
          }
        }
        else
        {
          CLog::Log(LOGERROR, "User chose to not enable add-on {}", dependency.id);
          missingDependencies.emplace_back(addon->Name());
          addon.reset();
        }
      }

      if (addon && addon->Type() == AddonType::GAMEDLL)
        ret.emplace_back(std::move(addon));
    }
    else
    {
      if (dependency.optional)
      {
        CLog::Log(LOGDEBUG, "Missing optional dependency {}", dependency.id);
      }
      else
      {
        CLog::Log(LOGERROR, "Missing mandatory dependency {}", dependency.id);
        missingDependencies.emplace_back(dependency.id);
      }
    }
  }

  if (!missingDependencies.empty())
  {
    std::string strDependencies = StringUtils::Join(missingDependencies, ", ");
    std::string dialogText = StringUtils::Format(g_localizeStrings.Get(35223), strDependencies);

    // "Failed to play game"
    // "Add-on is incompatible due to unmet dependencies."
    // ""
    // "Missing: {0:s}"
    MESSAGING::HELPERS::ShowOKDialogLines(CVariant{35210}, CVariant{24104}, CVariant{""},
                                          CVariant{dialogText});

    return false;
  }

  addons = std::move(ret);
  return true;
}

void CGameClientProperties::AddProxyDll(const GameClientPtr& gameClient)
{
  // Get the add-on's real path
  std::string strLibPath = gameClient->CAddon::LibPath();

  // Ignore add-on if it is already added
  if (!HasProxyDll(strLibPath))
  {
    char* libPath = new char[strLibPath.length() + 1];
    std::strcpy(libPath, strLibPath.c_str());
    m_proxyDllPaths.push_back(libPath);
  }
}

bool CGameClientProperties::HasProxyDll(const std::string& strLibPath) const
{
  for (const auto& it : m_proxyDllPaths)
  {
    if (strLibPath == it)
      return true;
  }
  return false;
}