This repository has been archived by the owner on Sep 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
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
12 changed files
with
554 additions
and
41 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
Submodule core
updated
from ec8808 to c78e81
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,37 @@ | ||
#pragma once | ||
|
||
#include <filesystem> | ||
#include <hydra/core.h> | ||
#include <hydra/dynlib/dynlib.hxx> | ||
|
||
namespace hydra::core | ||
{ | ||
struct Wrapper final | ||
{ | ||
private: | ||
using hcGetCoreInfoPtr = void (*)(HcCoreInfo* coreInfo); | ||
using hcCreatePtr = HcResult (*)(HcEnvironmentInfo* environmentInfo); | ||
using hcDestroyPtr = HcResult (*)(const HcDestroyInfo* destroyInfo); | ||
using hcResetPtr = HcResult (*)(const HcResetInfo* resetInfo); | ||
using hcSetRunStatePtr = HcResult (*)(const HcRunStateInfo* runInfo); | ||
using hcLoadContentPtr = HcResult (*)(const HcContentLoadInfo* info); | ||
using hcGetErrorPtr = const char* (*)(); | ||
|
||
public: | ||
Wrapper(const std::filesystem::path& path); | ||
~Wrapper(); | ||
|
||
bool okay() const; | ||
|
||
hcGetCoreInfoPtr hcGetCoreInfo; | ||
hcCreatePtr hcCreate; | ||
hcDestroyPtr hcDestroy; | ||
hcResetPtr hcReset; | ||
hcSetRunStatePtr hcSetRunState; | ||
hcLoadContentPtr hcLoadContent; | ||
hcGetErrorPtr hcGetError; | ||
|
||
private: | ||
hydra::dynlib::handle_t handle; | ||
}; | ||
} // namespace hydra::core |
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,90 @@ | ||
#if defined(HYDRA_LIBDL) | ||
#include <dlfcn.h> | ||
#elif defined(HYDRA_WINDOWS) | ||
#include <windows.h> | ||
#endif | ||
|
||
#include <filesystem> | ||
#include <string> | ||
#include <string_view> | ||
|
||
namespace hydra::dynlib | ||
{ | ||
typedef void* handle_t; | ||
|
||
inline handle_t open(const std::filesystem::path& path) | ||
{ | ||
#if defined(HYDRA_LIBDL) | ||
return dlopen(path.string().c_str(), RTLD_LAZY); | ||
#elif defined(HYDRA_WINDOWS) | ||
std::string spath = path.string(); | ||
std::wstring wpath = std::wstring(spath.c_str(), spath.c_str() + spath.size()); | ||
printf("Trying to convert string to wstring to load library with loadlibraryw, this is " | ||
"untested\n"); | ||
return (void*)LoadLibraryW(wpath.c_str()); | ||
#else | ||
#pragma message("dynlib_open not implemented for this platform") | ||
#error dynlib_open not implemented for this platform | ||
return nullptr; | ||
#endif | ||
} | ||
|
||
inline void* symbol(handle_t handle, const char* name) | ||
{ | ||
#if defined(HYDRA_LIBDL) | ||
return dlsym(handle, name); | ||
#elif defined(HYDRA_WINDOWS) | ||
return (void*)GetProcAddress((HMODULE)handle, name); | ||
#else | ||
#pragma message("dynlib_get_symbol not implemented for this platform") | ||
#error dynlib_get_symbol not implemented for this platform | ||
return nullptr; | ||
#endif | ||
} | ||
|
||
inline void close(handle_t handle) | ||
{ | ||
#if defined(HYDRA_LIBDL) | ||
dlclose(handle); | ||
#elif defined(HYDRA_WINDOWS) | ||
FreeLibrary((HMODULE)handle); | ||
#else | ||
#pragma message("dynlib_close not implemented for this platform") | ||
#error dynlib_close not implemented for this platform | ||
#endif | ||
} | ||
|
||
inline std::string_view extension() | ||
{ | ||
#if defined(HYDRA_LINUX) || defined(HYDRA_ANDROID) || defined(HYDRA_FREEBSD) | ||
return ".so"; | ||
#elif defined(HYDRA_WEB) | ||
return ".wasm"; | ||
#elif defined(HYDRA_MACOS) | ||
return ".dylib"; | ||
#elif defined(HYDRA_WINDOWS) | ||
return ".dll"; | ||
#else | ||
#pragma message("dynlib_get_extension not implemented for this platform") | ||
#error dynlib_get_extension not implemented for this platform | ||
return ""; | ||
#endif | ||
} | ||
|
||
inline std::string error() | ||
{ | ||
#if defined(HYDRA_LIBDL) | ||
return dlerror(); | ||
#elif defined(HYDRA_WINDOWS) | ||
// DWORD error = GetLastError(); | ||
// LPVOID buffer; | ||
// FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, NULL, error, | ||
// 0, | ||
// (LPWSTR)&buffer, 0, NULL); | ||
// std::string ret = (char*)buffer; | ||
// LocalFree(buffer); | ||
// return ret; | ||
return ""; // TODO: fix | ||
#endif | ||
} | ||
} // namespace hydra::dynlib |
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
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,91 @@ | ||
// Define the hydra core API imports | ||
|
||
#include "compatibility.hxx" | ||
#include <cstdio> | ||
#include <hydra/core.h> | ||
|
||
extern "C" { | ||
|
||
void hydra_hcGetHostInfo(HcHostInfo* hostInfo) | ||
{ | ||
printf("STUB: hcGetHostInfo\n"); | ||
} | ||
|
||
HcResult hydra_hcGetInputsSync(const HcInputRequest* const* requests, int requestCount, | ||
const int64_t* const* values) | ||
{ | ||
printf("STUB: hcGetInputsSync\n"); | ||
return HC_SUCCESS; | ||
} | ||
|
||
HcResult hydra_hcReconfigureEnvironment(const HcEnvironmentInfo* environmentInfo) | ||
{ | ||
printf("STUB: hcReconfigureEnvironment\n"); | ||
return HC_SUCCESS; | ||
} | ||
|
||
HcResult hydra_hcPushSamples(const HcAudioData* audioData) | ||
{ | ||
printf("STUB: hcPushSamples\n"); | ||
return HC_SUCCESS; | ||
} | ||
|
||
HcResult hydra_hcSwPushVideoFrame(const HcImageData* image) | ||
{ | ||
printf("STUB: hcSwPushVideoFrame\n"); | ||
return HC_SUCCESS; | ||
} | ||
|
||
HcResult hydra_hcGlMakeCurrent() | ||
{ | ||
printf("STUB: hcGlMakeCurrent\n"); | ||
return HC_SUCCESS; | ||
} | ||
|
||
HcResult hydra_hcGlSwapBuffers() | ||
{ | ||
printf("STUB: hcGlSwapBuffers\n"); | ||
return HC_SUCCESS; | ||
} | ||
|
||
void* hydra_hcGlGetProcAddress(const char* name) | ||
{ | ||
printf("STUB: hcGlGetProcAddress\n"); | ||
return nullptr; | ||
} | ||
|
||
HcResult hydra_hcSetCallbacks(const HcCallbacks* callbacks) | ||
{ | ||
printf("STUB: hcSetCallbacks\n"); | ||
return HC_SUCCESS; | ||
} | ||
|
||
void* hydra_GetAddress(const char* name) | ||
{ | ||
uint32_t hash = hydra::str_hash(name); | ||
|
||
switch (hash) | ||
{ | ||
case hydra::str_hash("hcGetHostInfo"): | ||
return (void*)hydra_hcGetHostInfo; | ||
case hydra::str_hash("hcGetInputsSync"): | ||
return (void*)hydra_hcGetInputsSync; | ||
case hydra::str_hash("hcReconfigureEnvironment"): | ||
return (void*)hydra_hcReconfigureEnvironment; | ||
case hydra::str_hash("hcPushSamples"): | ||
return (void*)hydra_hcPushSamples; | ||
case hydra::str_hash("hcSwPushVideoFrame"): | ||
return (void*)hydra_hcSwPushVideoFrame; | ||
case hydra::str_hash("hcGlMakeCurrent"): | ||
return (void*)hydra_hcGlMakeCurrent; | ||
case hydra::str_hash("hcGlSwapBuffers"): | ||
return (void*)hydra_hcGlSwapBuffers; | ||
case hydra::str_hash("hcGlGetProcAddress"): | ||
return (void*)hydra_hcGlGetProcAddress; | ||
case hydra::str_hash("hcSetCallbacks"): | ||
return (void*)hydra_hcSetCallbacks; | ||
default: | ||
return nullptr; | ||
} | ||
} | ||
} |
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,78 @@ | ||
#include <hydra/common/log.hxx> | ||
#include <hydra/core.h> | ||
#include <hydra/core/wrapper.hxx> | ||
|
||
extern "C" void* hydra_GetAddress(const char* name); | ||
|
||
namespace hydra::core | ||
{ | ||
|
||
Wrapper::Wrapper(const std::filesystem::path& path) : handle(nullptr) | ||
{ | ||
if (!std::filesystem::exists(path) || !std::filesystem::is_regular_file(path)) | ||
{ | ||
hydra::panic("Core not found: {}", path.string()); | ||
return; | ||
} | ||
|
||
if (path.extension() != hydra::dynlib::extension()) | ||
{ | ||
hydra::panic("Not a dynamic library: {}", path.string()); | ||
return; | ||
} | ||
|
||
handle = hydra::dynlib::open(path); | ||
if (!handle) | ||
{ | ||
hydra::panic("Failed to load core: {}\nError: {}", path.string(), | ||
hydra::dynlib::error()); | ||
return; | ||
} | ||
|
||
hcGetCoreInfo = (hcGetCoreInfoPtr)hydra::dynlib::symbol(handle, "hcGetCoreInfo"); | ||
hcCreate = (hcCreatePtr)hydra::dynlib::symbol(handle, "hcCreate"); | ||
hcDestroy = (hcDestroyPtr)hydra::dynlib::symbol(handle, "hcDestroy"); | ||
hcReset = (hcResetPtr)hydra::dynlib::symbol(handle, "hcReset"); | ||
hcSetRunState = (hcSetRunStatePtr)hydra::dynlib::symbol(handle, "hcSetRunState"); | ||
hcLoadContent = (hcLoadContentPtr)hydra::dynlib::symbol(handle, "hcLoadContent"); | ||
hcGetError = (hcGetErrorPtr)hydra::dynlib::symbol(handle, "hcGetError"); | ||
|
||
if (!hcGetCoreInfo || !hcCreate || !hcDestroy || !hcReset || !hcSetRunState || | ||
!hcLoadContent || !hcGetError) | ||
{ | ||
hydra::log("Failed to load core functions: {}\nError: {}", path.string(), | ||
hydra::dynlib::error()); | ||
hydra::dynlib::close(handle); | ||
handle = nullptr; | ||
} | ||
|
||
void* (*hcInternalLoadFunctions)(void* (*loadFunctionPtr)(const char*)) = | ||
(decltype(hcInternalLoadFunctions))hydra::dynlib::symbol(handle, | ||
"hcInternalLoadFunctions"); | ||
if (hcInternalLoadFunctions) | ||
{ | ||
hcInternalLoadFunctions(hydra_GetAddress); | ||
} | ||
else | ||
{ | ||
hydra::log("Failed to load internal functions: {}\nError: {}", path.string(), | ||
hydra::dynlib::error()); | ||
hydra::dynlib::close(handle); | ||
handle = nullptr; | ||
} | ||
} | ||
|
||
Wrapper::~Wrapper() | ||
{ | ||
if (handle) | ||
{ | ||
hydra::dynlib::close(handle); | ||
} | ||
} | ||
|
||
bool Wrapper::okay() const | ||
{ | ||
return handle != nullptr; | ||
} | ||
|
||
} // namespace hydra::core |
Oops, something went wrong.