2023-07-15 14:29:14 +02:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <functional>
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
2024-05-17 21:01:35 +02:00
|
|
|
#include <hex/helpers/utils.hpp>
|
|
|
|
|
|
|
|
#if defined(OS_WINDOWS)
|
|
|
|
#include <Windows.h>
|
|
|
|
#else
|
|
|
|
#include <dlfcn.h>
|
|
|
|
#endif
|
2023-07-15 14:29:14 +02:00
|
|
|
|
|
|
|
namespace hex::script::loader {
|
|
|
|
|
2024-05-17 21:01:35 +02:00
|
|
|
class ScriptLoader;
|
|
|
|
|
2023-07-15 14:29:14 +02:00
|
|
|
struct Script {
|
|
|
|
std::string name;
|
2024-06-07 21:27:01 +02:00
|
|
|
std::fs::path path;
|
|
|
|
|
2024-05-08 22:30:53 +02:00
|
|
|
bool background;
|
2023-07-15 14:29:14 +02:00
|
|
|
std::function<void()> entryPoint;
|
2024-05-17 21:01:35 +02:00
|
|
|
const ScriptLoader *loader;
|
2023-07-15 14:29:14 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
class ScriptLoader {
|
|
|
|
public:
|
2024-05-17 21:01:35 +02:00
|
|
|
ScriptLoader(std::string typeName) : m_typeName(std::move(typeName)) {}
|
2023-07-15 14:29:14 +02:00
|
|
|
virtual ~ScriptLoader() = default;
|
|
|
|
|
|
|
|
virtual bool initialize() = 0;
|
|
|
|
virtual bool loadAll() = 0;
|
2024-06-07 21:27:01 +02:00
|
|
|
virtual void clearScripts() = 0;
|
2023-07-15 14:29:14 +02:00
|
|
|
|
2024-06-07 21:27:01 +02:00
|
|
|
void addScript(std::string name, std::fs::path path, bool background, std::function<void()> entryPoint) {
|
|
|
|
m_scripts.emplace_back(std::move(name), std::move(path), background, std::move(entryPoint), this);
|
2023-07-15 14:29:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const auto& getScripts() const {
|
2023-12-19 13:10:25 +01:00
|
|
|
return m_scripts;
|
2023-07-15 14:29:14 +02:00
|
|
|
}
|
|
|
|
|
2024-05-17 21:01:35 +02:00
|
|
|
const std::string& getTypeName() const {
|
|
|
|
return m_typeName;
|
|
|
|
}
|
|
|
|
|
2023-07-15 14:29:14 +02:00
|
|
|
protected:
|
2024-06-07 21:27:01 +02:00
|
|
|
auto& getScripts() {
|
|
|
|
return m_scripts;
|
2023-07-15 14:29:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::vector<Script> m_scripts;
|
2024-05-17 21:01:35 +02:00
|
|
|
std::string m_typeName;
|
2023-07-15 14:29:14 +02:00
|
|
|
};
|
|
|
|
|
2024-05-17 21:01:35 +02:00
|
|
|
#if defined(OS_WINDOWS)
|
|
|
|
inline void *loadLibrary(const wchar_t *path) {
|
|
|
|
try {
|
|
|
|
HMODULE h = ::LoadLibraryW(path);
|
|
|
|
return h;
|
|
|
|
} catch (...) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void *loadLibrary(const char *path) {
|
|
|
|
try {
|
|
|
|
auto utf16Path = hex::utf8ToUtf16(path);
|
|
|
|
HMODULE h = ::LoadLibraryW(utf16Path.c_str());
|
|
|
|
return h;
|
|
|
|
} catch (...) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
T getExport(void *h, const char *name) {
|
|
|
|
try {
|
|
|
|
FARPROC f = ::GetProcAddress(static_cast<HMODULE>(h), name);
|
|
|
|
return reinterpret_cast<T>(reinterpret_cast<uintptr_t>(f));
|
|
|
|
} catch (...) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
inline void *loadLibrary(const char *path) {
|
|
|
|
void *h = dlopen(path, RTLD_LAZY);
|
|
|
|
return h;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
T getExport(void *h, const char *name) {
|
|
|
|
void *f = dlsym(h, name);
|
|
|
|
return reinterpret_cast<T>(f);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2023-07-15 14:29:14 +02:00
|
|
|
}
|