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