forked from kodi-game/game.libretro
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
222 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
* Copyright (C) 2020 Team Kodi (https://kodi.tv) | ||
* | ||
* SPDX-License-Identifier: GPL-2.0-or-later | ||
* See LICENSE.md for more information. | ||
*/ | ||
|
||
#include "CheevosEnvironment.h" | ||
#include "CheevosFrontendBridge.h" | ||
|
||
#include <rcheevos/rhash.h> | ||
|
||
using namespace LIBRETRO; | ||
|
||
CCheevosEnvironment& CCheevosEnvironment::Get(void) | ||
{ | ||
static CCheevosEnvironment _instance; | ||
return _instance; | ||
} | ||
|
||
void CCheevosEnvironment::Initialize() | ||
{ | ||
static rc_hash_filereader fileReader = { | ||
CCheevosFrontendBridge::OpenFile, | ||
CCheevosFrontendBridge::Seek, | ||
CCheevosFrontendBridge::GetPosition, | ||
CCheevosFrontendBridge::ReadFile, | ||
CCheevosFrontendBridge::CloseFile, | ||
}; | ||
|
||
rc_hash_init_custom_filereader(&fileReader); | ||
} | ||
|
||
void CCheevosEnvironment::Deinitialize() | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/* | ||
* Copyright (C) 2020 Team Kodi (https://kodi.tv) | ||
* | ||
* SPDX-License-Identifier: GPL-2.0-or-later | ||
* See LICENSE.md for more information. | ||
*/ | ||
|
||
#pragma once | ||
|
||
namespace LIBRETRO | ||
{ | ||
class CCheevosEnvironment | ||
{ | ||
public: | ||
static CCheevosEnvironment& Get(void); | ||
|
||
void Initialize(); | ||
|
||
void Deinitialize(); | ||
|
||
private: | ||
CCheevosEnvironment() = default; | ||
}; | ||
} // namespace LIBRETRO |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
/* | ||
* Copyright (C) 2020 Team Kodi (https://kodi.tv) | ||
* | ||
* SPDX-License-Identifier: GPL-2.0-or-later | ||
* See LICENSE.md for more information. | ||
*/ | ||
|
||
#include "CheevosFrontendBridge.h" | ||
|
||
#include <kodi/Filesystem.h> | ||
#include <stdio.h> | ||
|
||
using namespace LIBRETRO; | ||
|
||
void *CCheevosFrontendBridge::OpenFile(const char* path_utf8) | ||
{ | ||
// Return NULL for error | ||
if (path_utf8 == nullptr) | ||
return nullptr; | ||
|
||
// TODO: Handle UTF-8? | ||
std::unique_ptr<FileHandle> fileHandle(new FileHandle{ path_utf8 }); | ||
fileHandle->file.reset(new kodi::vfs::CFile); | ||
|
||
if (!fileHandle->file->OpenFile(fileHandle->path, 0)) | ||
return nullptr; | ||
|
||
// Return the opaque file handle on success | ||
return static_cast<void*>(fileHandle.release()); | ||
} | ||
|
||
void CCheevosFrontendBridge::CloseFile(void* file_handle) | ||
{ | ||
if (file_handle == nullptr) | ||
return; | ||
|
||
FileHandle *fileHandle = static_cast<FileHandle*>(file_handle); | ||
|
||
fileHandle->file->Close(); | ||
delete fileHandle; | ||
} | ||
|
||
size_t CCheevosFrontendBridge::GetPosition(void* file_handle) | ||
{ | ||
// Return 0 for error | ||
if (file_handle == nullptr) | ||
return 0; | ||
|
||
FileHandle *fileHandle = static_cast<FileHandle*>(file_handle); | ||
|
||
const int64_t currentPosition = fileHandle->file->GetPosition(); | ||
|
||
if (currentPosition < 0) | ||
return 0; | ||
|
||
// Return the current read / write position for the file | ||
return currentPosition; | ||
} | ||
|
||
void CCheevosFrontendBridge::Seek(void* file_handle, size_t offset, int origin) | ||
{ | ||
if (file_handle == nullptr) | ||
return; | ||
|
||
FileHandle *fileHandle = static_cast<FileHandle*>(file_handle); | ||
|
||
int whence = -1; | ||
|
||
switch (origin) | ||
{ | ||
case 0: | ||
whence = SEEK_SET; | ||
break; | ||
case 1: | ||
whence = SEEK_CUR; | ||
break; | ||
case 2: | ||
whence = SEEK_END; | ||
break; | ||
default: | ||
break; | ||
} | ||
|
||
if (whence == -1) | ||
return; | ||
|
||
fileHandle->file->Seek(offset, whence); | ||
} | ||
|
||
size_t CCheevosFrontendBridge::ReadFile(void* file_handle, void* buffer, size_t requested_bytes) | ||
{ | ||
// Return 0 for error | ||
if (file_handle == nullptr) | ||
return 0; | ||
|
||
FileHandle *fileHandle = static_cast<FileHandle*>(file_handle); | ||
|
||
const ssize_t bytesRead = fileHandle->file->Read(buffer, requested_bytes); | ||
|
||
if (bytesRead < 0) | ||
return 0; | ||
|
||
// Return 0 if no bytes are available to read (end of file was reached) or | ||
// undetectable error occurred | ||
if (bytesRead == 0) | ||
return 0; | ||
|
||
// Return the number of bytes read | ||
return static_cast<size_t>(bytesRead); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
* Copyright (C) 2020 Team Kodi (https://kodi.tv) | ||
* | ||
* SPDX-License-Identifier: GPL-2.0-or-later | ||
* See LICENSE.md for more information. | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include <memory> | ||
#include <rcheevos/rhash.h> | ||
#include <string> | ||
|
||
namespace kodi | ||
{ | ||
namespace vfs | ||
{ | ||
class CFile; | ||
} | ||
} | ||
|
||
namespace LIBRETRO | ||
{ | ||
/*! | ||
* Provide a bridge for frontend callbacks given to the rcheevos library. | ||
* | ||
* These functions are given to the rcheevos library in | ||
* CheevosEnvironment::Initialize(). | ||
*/ | ||
class CCheevosFrontendBridge | ||
{ | ||
public: | ||
// Forward to Kodi VFS API | ||
static void *OpenFile(const char* path_utf8); | ||
static void CloseFile(void* file_handle); | ||
static size_t GetPosition(void* file_handle); | ||
static void Seek(void* file_handle, size_t offset, int origin); | ||
static size_t ReadFile(void* file_handle, void* buffer, size_t requested_bytes); | ||
|
||
private: | ||
struct FileHandle | ||
{ | ||
std::string path; | ||
std::unique_ptr<kodi::vfs::CFile> file; | ||
}; | ||
}; | ||
} // namespace LIBRETRO |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters