xbmc
GameClient.h
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 #pragma once
10 
11 #include "GameClientSubsystem.h"
12 #include "addons/binary-addons/AddonDll.h"
13 #include "addons/kodi-dev-kit/include/kodi/addon-instance/Game.h"
14 #include "threads/CriticalSection.h"
15 
16 #include <atomic>
17 #include <memory>
18 #include <set>
19 #include <stdint.h>
20 #include <string>
21 #include <utility>
22 
23 class CFileItem;
24 
25 namespace KODI
26 {
27 namespace RETRO
28 {
29 class IStreamManager;
30 }
31 
32 namespace GAME
33 {
34 
35 class CGameClientCheevos;
36 class CGameClientInGameSaves;
37 class CGameClientInput;
38 class CGameClientProperties;
39 class IGameInputCallback;
40 
46 {
47 public:
49  {
50  // Create "C" interface structures, used as own parts to prevent API problems on update
52  info->id = "";
53  info->version = kodi::addon::GetTypeVersion(ADDON_INSTANCE_GAME);
54  info->type = ADDON_INSTANCE_GAME;
55  info->kodi = this;
56  info->parent = nullptr;
57  info->first_instance = true;
58  info->functions = new KODI_ADDON_INSTANCE_FUNC_CB();
59  m_ifc.info = info;
60  m_ifc.functions = new KODI_ADDON_INSTANCE_FUNC();
61 
62  m_ifc.game = new AddonInstance_Game;
63  m_ifc.game->props = new AddonProps_Game();
64  m_ifc.game->toKodi = new AddonToKodiFuncTable_Game();
65  m_ifc.game->toAddon = new KodiToAddonFuncTable_Game();
66  }
67 
69  {
70  delete m_ifc.functions;
71  if (m_ifc.info)
72  delete m_ifc.info->functions;
73  delete m_ifc.info;
74  if (m_ifc.game)
75  {
76  delete m_ifc.game->toAddon;
77  delete m_ifc.game->toKodi;
78  delete m_ifc.game->props;
79  delete m_ifc.game;
80  }
81  }
82 
84 };
85 
116 {
117 public:
118  explicit CGameClient(const ADDON::AddonInfoPtr& addonInfo);
119 
120  ~CGameClient() override;
121 
122  // Game subsystems (const)
123  const CGameClientCheevos& Cheevos() const { return *m_subsystems.Cheevos; }
124  const CGameClientInput& Input() const { return *m_subsystems.Input; }
125  const CGameClientProperties& AddonProperties() const { return *m_subsystems.AddonProperties; }
126  const CGameClientStreams& Streams() const { return *m_subsystems.Streams; }
127 
128  // Game subsystems (mutable)
129  CGameClientCheevos& Cheevos() { return *m_subsystems.Cheevos; }
130  CGameClientInput& Input() { return *m_subsystems.Input; }
131  CGameClientProperties& AddonProperties() { return *m_subsystems.AddonProperties; }
132  CGameClientStreams& Streams() { return *m_subsystems.Streams; }
133 
134  // Implementation of IAddon via CAddonDll
135  std::string LibPath() const override;
136  ADDON::AddonPtr GetRunningInstance() const override;
137 
138  // Query properties of the game client
139  bool SupportsStandalone() const { return m_bSupportsStandalone; }
140  bool SupportsPath() const;
141  bool SupportsVFS() const { return m_bSupportsVFS; }
142  const std::set<std::string>& GetExtensions() const { return m_extensions; }
143  bool SupportsAllExtensions() const { return m_bSupportsAllExtensions; }
144  bool IsExtensionValid(const std::string& strExtension) const;
145  const std::string& GetEmulatorName() const { return m_emulatorName; }
146  const std::string& GetPlatforms() const { return m_platforms; }
147 
148  // Start/stop gameplay
149  bool Initialize(void);
150  void Unload();
151  bool OpenFile(const CFileItem& file,
152  RETRO::IStreamManager& streamManager,
153  IGameInputCallback* input);
154  bool OpenStandalone(RETRO::IStreamManager& streamManager, IGameInputCallback* input);
155  void Reset();
156  void CloseFile();
157  const std::string& GetGamePath() const { return m_gamePath; }
158 
159  // Playback control
160  bool RequiresGameLoop() const { return m_bRequiresGameLoop; }
161  bool IsPlaying() const { return m_bIsPlaying; }
162  size_t GetSerializeSize() const { return m_serializeSize; }
163  double GetFrameRate() const { return m_framerate; }
164  double GetSampleRate() const { return m_samplerate; }
165  void RunFrame();
166 
167  // Access memory
168  size_t SerializeSize() const { return m_serializeSize; }
169  bool Serialize(uint8_t* data, size_t size);
170  bool Deserialize(const uint8_t* data, size_t size);
171 
177  AddonInstance_Game* GetInstanceInterface() { return m_ifc.game; }
178 
179  // Helper functions
180  bool LogError(GAME_ERROR error, const char* strMethod) const;
181  void LogException(const char* strFunctionName) const;
182 
183 private:
184  // Private gameplay functions
185  bool InitializeGameplay(const std::string& gamePath,
186  RETRO::IStreamManager& streamManager,
187  IGameInputCallback* input);
188  bool LoadGameInfo();
189  void NotifyError(GAME_ERROR error);
190  std::string GetMissingResource();
191 
192  // Helper functions
193  void LogAddonProperties(void) const;
194 
199  static void cb_close_game(KODI_HANDLE kodiInstance);
200  static KODI_GAME_STREAM_HANDLE cb_open_stream(KODI_HANDLE kodiInstance,
201  const game_stream_properties* properties);
202  static bool cb_get_stream_buffer(KODI_HANDLE kodiInstance,
203  KODI_GAME_STREAM_HANDLE stream,
204  unsigned int width,
205  unsigned int height,
206  game_stream_buffer* buffer);
207  static void cb_add_stream_data(KODI_HANDLE kodiInstance,
208  KODI_GAME_STREAM_HANDLE stream,
209  const game_stream_packet* packet);
210  static void cb_release_stream_buffer(KODI_HANDLE kodiInstance,
211  KODI_GAME_STREAM_HANDLE stream,
212  game_stream_buffer* buffer);
213  static void cb_close_stream(KODI_HANDLE kodiInstance, KODI_GAME_STREAM_HANDLE stream);
214  static game_proc_address_t cb_hw_get_proc_address(KODI_HANDLE kodiInstance, const char* sym);
215  static bool cb_input_event(KODI_HANDLE kodiInstance, const game_input_event* event);
217 
234  static std::pair<std::string, std::string> ParseLibretroName(const std::string& addonName);
235 
236  // Game subsystems
237  GameClientSubsystems m_subsystems;
238 
239  // Game API xml parameters
240  bool m_bSupportsVFS;
241  bool m_bSupportsStandalone;
242  std::set<std::string> m_extensions;
243  bool m_bSupportsAllExtensions;
244  std::string m_emulatorName;
245  std::string m_platforms;
246 
247  // Properties of the current playing file
248  std::atomic_bool m_bIsPlaying; // True between OpenFile() and CloseFile()
249  std::string m_gamePath;
250  bool m_bRequiresGameLoop = false;
251  size_t m_serializeSize;
252  IGameInputCallback* m_input = nullptr; // The input callback passed to OpenFile()
253  double m_framerate = 0.0; // Video frame rate (fps)
254  double m_samplerate = 0.0; // Audio sample rate (Hz)
255  GAME_REGION m_region; // Region of the loaded game
256 
257  // In-game saves
258  std::unique_ptr<CGameClientInGameSaves> m_inGameSaves;
259 
260  CCriticalSection m_critSection;
261 };
262 
263 } // namespace GAME
264 } // namespace KODI
Helper class to have "C" struct created before other parts becomes his pointer.
Definition: GameClient.h:45
Definition: AddonDll.h:51
Game callbacks.
Definition: game.h:1137
Game properties.
Definition: game.h:1075
Input callbacks.
Definition: GameClientCallbacks.h:20
Game instance, see kodi::addon::CInstanceGame.
Definition: versions.h:224
Definition: GameClientInput.h:43
Definition: GameClientCheevos.h:28
void(* game_proc_address_t)(void)
Hardware framebuffer process function address
Definition: game.h:406
Definition: addon_base.h:219
Definition: IStreamManager.h:18
Stream packet and ephemeral metadata
Definition: game.h:528
Definition: addon_base.h:267
Interface between Kodi and Game add-ons.
Definition: GameClient.h:115
Controller configuration window.
Definition: AudioDecoder.h:18
Game function hooks.
Definition: game.h:1160
Stream buffers for hardware rendering and zero-copy support
Definition: game.h:506
Definition: addon_base.h:254
C++ wrapper for properties to pass to the DLL.
Definition: GameClientProperties.h:39
GAME_REGION
Game reguin definition
Definition: game.h:562
Definition: GameClientSubsystem.h:26
Game instance.
Definition: game.h:1223
AddonInstance_Game * GetInstanceInterface()
To get the interface table used between addon and kodi.
Definition: GameClient.h:177
Definition: game.h:996
Immutable stream metadata
Definition: game.h:482
Definition: GameClientStreams.h:29
Definition: addon_base.h:211
Represents a file on a share.
Definition: FileItem.h:102