2022-02-01 17:09:40 +00:00
# include <hex/api/plugin_manager.hpp>
2023-06-26 12:01:45 +00:00
# include <hex/api/imhex_api.hpp>
2020-12-22 17:10:01 +00:00
2021-08-29 20:15:18 +00:00
# include <hex/helpers/logger.hpp>
2023-11-10 19:47:08 +00:00
# include <hex/helpers/fmt.hpp>
2021-08-29 20:15:18 +00:00
2023-03-12 17:43:05 +00:00
# include <wolv/utils/string.hpp>
2020-12-22 17:10:01 +00:00
# include <filesystem>
2023-11-30 09:22:15 +00:00
# if defined(OS_WINDOWS)
# include <windows.h>
# else
# include <dlfcn.h>
# endif
2020-12-22 17:10:01 +00:00
namespace hex {
2022-03-04 10:36:37 +00:00
Plugin : : Plugin ( const std : : fs : : path & path ) : m_path ( path ) {
2023-10-04 10:00:32 +00:00
2022-06-29 19:34:17 +00:00
# if defined(OS_WINDOWS)
2023-12-19 12:10:25 +00:00
m_handle = uintptr_t ( LoadLibraryW ( path . c_str ( ) ) ) ;
2022-06-29 19:34:17 +00:00
2023-12-19 12:10:25 +00:00
if ( m_handle = = uintptr_t ( INVALID_HANDLE_VALUE ) | | m_handle = = 0 ) {
2022-06-29 19:34:17 +00:00
log : : error ( " LoadLibraryW failed: {}! " , std : : system_category ( ) . message ( : : GetLastError ( ) ) ) ;
return ;
}
# else
2023-12-19 12:10:25 +00:00
m_handle = uintptr_t ( dlopen ( wolv : : util : : toUTF8String ( path ) . c_str ( ) , RTLD_LAZY ) ) ;
2022-06-29 19:34:17 +00:00
2023-12-19 12:10:25 +00:00
if ( m_handle = = 0 ) {
2022-06-29 19:34:17 +00:00
log : : error ( " dlopen failed: {}! " , dlerror ( ) ) ;
return ;
}
# endif
2020-12-22 17:10:01 +00:00
2023-12-19 12:10:25 +00:00
m_functions . initializePluginFunction = getPluginFunction < PluginFunctions : : InitializePluginFunc > ( " initializePlugin " ) ;
m_functions . getPluginNameFunction = getPluginFunction < PluginFunctions : : GetPluginNameFunc > ( " getPluginName " ) ;
m_functions . getPluginAuthorFunction = getPluginFunction < PluginFunctions : : GetPluginAuthorFunc > ( " getPluginAuthor " ) ;
m_functions . getPluginDescriptionFunction = getPluginFunction < PluginFunctions : : GetPluginDescriptionFunc > ( " getPluginDescription " ) ;
m_functions . getCompatibleVersionFunction = getPluginFunction < PluginFunctions : : GetCompatibleVersionFunc > ( " getCompatibleVersion " ) ;
m_functions . setImGuiContextFunction = getPluginFunction < PluginFunctions : : SetImGuiContextFunc > ( " setImGuiContext " ) ;
m_functions . isBuiltinPluginFunction = getPluginFunction < PluginFunctions : : IsBuiltinPluginFunc > ( " isBuiltinPlugin " ) ;
m_functions . getSubCommandsFunction = getPluginFunction < PluginFunctions : : GetSubCommandsFunc > ( " getSubCommands " ) ;
2023-10-04 10:00:32 +00:00
}
Plugin : : Plugin ( hex : : PluginFunctions functions ) {
2023-12-19 12:10:25 +00:00
m_handle = 0 ;
m_functions = functions ;
2020-12-22 17:10:01 +00:00
}
2023-10-04 10:00:32 +00:00
2021-02-19 12:22:12 +00:00
Plugin : : Plugin ( Plugin & & other ) noexcept {
2023-12-19 12:10:25 +00:00
m_handle = other . m_handle ;
2023-11-30 09:22:15 +00:00
other . m_handle = 0 ;
2023-10-04 10:00:32 +00:00
2023-12-19 12:10:25 +00:00
m_path = std : : move ( other . m_path ) ;
2022-02-01 21:09:44 +00:00
2023-12-19 12:10:25 +00:00
m_functions = other . m_functions ;
2023-10-04 10:00:32 +00:00
other . m_functions = { } ;
2021-02-07 13:57:13 +00:00
}
2020-12-22 17:10:01 +00:00
Plugin : : ~ Plugin ( ) {
2022-06-29 19:34:17 +00:00
# if defined(OS_WINDOWS)
2023-12-19 12:10:25 +00:00
if ( m_handle ! = 0 )
FreeLibrary ( HMODULE ( m_handle ) ) ;
2022-06-29 19:34:17 +00:00
# else
2023-12-19 12:10:25 +00:00
if ( m_handle ! = 0 )
dlclose ( reinterpret_cast < void * > ( m_handle ) ) ;
2022-06-29 19:34:17 +00:00
# endif
2020-12-22 17:10:01 +00:00
}
2022-01-17 19:06:00 +00:00
bool Plugin : : initializePlugin ( ) const {
2023-12-19 12:10:25 +00:00
const auto pluginName = wolv : : util : : toUTF8String ( m_path . filename ( ) ) ;
2023-07-15 12:29:14 +00:00
2022-01-23 22:28:56 +00:00
const auto requestedVersion = getCompatibleVersion ( ) ;
2023-06-26 12:01:45 +00:00
if ( requestedVersion ! = ImHexApi : : System : : getImHexVersion ( ) ) {
if ( requestedVersion . empty ( ) ) {
2023-12-19 12:10:25 +00:00
log : : warn ( " Plugin '{}' did not specify a compatible version, assuming it is compatible with the current version of ImHex. " , wolv : : util : : toUTF8String ( m_path . filename ( ) ) ) ;
2023-06-26 12:01:45 +00:00
} else {
2023-12-19 12:10:25 +00:00
log : : error ( " Refused to load plugin '{}' which was built for a different version of ImHex: '{}' " , wolv : : util : : toUTF8String ( m_path . filename ( ) ) , requestedVersion ) ;
2023-06-26 12:01:45 +00:00
return false ;
}
2022-01-23 22:28:56 +00:00
}
2023-12-19 12:10:25 +00:00
if ( m_functions . initializePluginFunction ! = nullptr ) {
2023-07-15 12:29:14 +00:00
try {
2023-12-19 12:10:25 +00:00
m_functions . initializePluginFunction ( ) ;
2023-07-15 12:29:14 +00:00
} catch ( const std : : exception & e ) {
log : : error ( " Plugin '{}' threw an exception on init: {} " , pluginName , e . what ( ) ) ;
2023-08-06 19:33:15 +00:00
return false ;
2023-07-15 12:29:14 +00:00
} catch ( . . . ) {
log : : error ( " Plugin '{}' threw an exception on init " , pluginName ) ;
2023-08-06 19:33:15 +00:00
return false ;
2023-07-15 12:29:14 +00:00
}
2022-01-17 19:06:00 +00:00
} else {
2023-10-04 10:00:32 +00:00
log : : error ( " Plugin '{}' does not have a proper entrypoint " , pluginName ) ;
2022-01-17 19:06:00 +00:00
return false ;
}
2022-01-23 22:28:56 +00:00
2023-12-01 13:07:10 +00:00
log : : info ( " Plugin '{}' initialized successfully " , pluginName ) ;
2023-12-19 12:10:25 +00:00
m_initialized = true ;
2022-01-23 22:28:56 +00:00
return true ;
2021-02-19 12:22:12 +00:00
}
std : : string Plugin : : getPluginName ( ) const {
2023-12-19 12:10:25 +00:00
if ( m_functions . getPluginNameFunction ! = nullptr )
return m_functions . getPluginNameFunction ( ) ;
2021-02-19 12:22:12 +00:00
else
2023-12-19 12:10:25 +00:00
return hex : : format ( " Unknown Plugin @ 0x{0:016X} " , m_handle ) ;
2021-02-19 12:22:12 +00:00
}
std : : string Plugin : : getPluginAuthor ( ) const {
2023-12-19 12:10:25 +00:00
if ( m_functions . getPluginAuthorFunction ! = nullptr )
return m_functions . getPluginAuthorFunction ( ) ;
2021-02-19 12:22:12 +00:00
else
return " Unknown " ;
}
std : : string Plugin : : getPluginDescription ( ) const {
2023-12-19 12:10:25 +00:00
if ( m_functions . getPluginDescriptionFunction ! = nullptr )
return m_functions . getPluginDescriptionFunction ( ) ;
2021-02-19 12:22:12 +00:00
else
return " " ;
2020-12-22 17:10:01 +00:00
}
2022-01-23 22:28:56 +00:00
std : : string Plugin : : getCompatibleVersion ( ) const {
2023-12-19 12:10:25 +00:00
if ( m_functions . getCompatibleVersionFunction ! = nullptr )
return m_functions . getCompatibleVersionFunction ( ) ;
2022-01-23 22:28:56 +00:00
else
return " " ;
}
2021-08-20 22:52:11 +00:00
void Plugin : : setImGuiContext ( ImGuiContext * ctx ) const {
2023-12-19 12:10:25 +00:00
if ( m_functions . setImGuiContextFunction ! = nullptr )
m_functions . setImGuiContextFunction ( ctx ) ;
2021-08-20 22:52:11 +00:00
}
2022-02-01 17:09:40 +00:00
[[nodiscard]] bool Plugin : : isBuiltinPlugin ( ) const {
2023-12-19 12:10:25 +00:00
if ( m_functions . isBuiltinPluginFunction ! = nullptr )
return m_functions . isBuiltinPluginFunction ( ) ;
2022-02-01 17:09:40 +00:00
else
return false ;
}
2022-03-04 10:36:37 +00:00
const std : : fs : : path & Plugin : : getPath ( ) const {
2023-12-19 12:10:25 +00:00
return m_path ;
2022-01-17 19:06:00 +00:00
}
2022-01-23 22:28:56 +00:00
bool Plugin : : isLoaded ( ) const {
2023-12-19 12:10:25 +00:00
return m_initialized ;
2022-01-17 23:10:10 +00:00
}
2023-07-13 12:08:23 +00:00
std : : span < SubCommand > Plugin : : getSubCommands ( ) const {
2023-12-19 12:10:25 +00:00
if ( m_functions . getSubCommandsFunction ! = nullptr ) {
auto result = m_functions . getSubCommandsFunction ( ) ;
2023-11-10 19:47:08 +00:00
return * static_cast < std : : vector < SubCommand > * > ( result ) ;
2023-07-13 12:08:23 +00:00
} else
return { } ;
}
2022-01-17 19:06:00 +00:00
2023-11-10 19:47:08 +00:00
void * Plugin : : getPluginFunction ( const std : : string & symbol ) const {
2022-06-29 19:34:17 +00:00
# if defined(OS_WINDOWS)
2023-12-19 12:10:25 +00:00
return reinterpret_cast < void * > ( GetProcAddress ( HMODULE ( m_handle ) , symbol . c_str ( ) ) ) ;
2022-06-29 19:34:17 +00:00
# else
2023-12-19 12:10:25 +00:00
return dlsym ( reinterpret_cast < void * > ( m_handle ) , symbol . c_str ( ) ) ;
2022-06-29 19:34:17 +00:00
# endif
2022-01-23 22:28:56 +00:00
}
2022-03-04 10:36:37 +00:00
bool PluginManager : : load ( const std : : fs : : path & pluginFolder ) {
2023-03-12 17:27:29 +00:00
if ( ! wolv : : io : : fs : : exists ( pluginFolder ) )
2021-04-20 19:46:48 +00:00
return false ;
2020-12-22 17:10:01 +00:00
2023-10-04 10:00:32 +00:00
getPluginPaths ( ) . push_back ( pluginFolder ) ;
2020-12-22 17:10:01 +00:00
2022-03-04 10:36:37 +00:00
for ( auto & pluginPath : std : : fs : : directory_iterator ( pluginFolder ) ) {
2021-02-07 12:40:47 +00:00
if ( pluginPath . is_regular_file ( ) & & pluginPath . path ( ) . extension ( ) = = " .hexplug " )
2023-10-04 10:00:32 +00:00
getPlugins ( ) . emplace_back ( pluginPath . path ( ) ) ;
2021-01-12 15:50:15 +00:00
}
2021-04-20 19:46:48 +00:00
2023-10-04 10:00:32 +00:00
if ( getPlugins ( ) . empty ( ) )
2021-04-20 19:46:48 +00:00
return false ;
return true ;
2020-12-22 17:10:01 +00:00
}
2021-04-20 19:46:48 +00:00
void PluginManager : : unload ( ) {
2023-10-04 10:00:32 +00:00
getPlugins ( ) . clear ( ) ;
getPluginPaths ( ) . clear ( ) ;
2020-12-22 17:10:01 +00:00
}
2021-04-20 19:46:48 +00:00
void PluginManager : : reload ( ) {
2023-10-04 10:00:32 +00:00
auto paths = getPluginPaths ( ) ;
2021-04-20 19:46:48 +00:00
PluginManager : : unload ( ) ;
2023-10-04 10:00:32 +00:00
for ( const auto & path : paths )
PluginManager : : load ( path ) ;
}
void PluginManager : : addPlugin ( hex : : PluginFunctions functions ) {
getPlugins ( ) . emplace_back ( functions ) ;
2023-07-26 11:50:51 +00:00
}
2023-10-04 10:00:32 +00:00
std : : vector < Plugin > & PluginManager : : getPlugins ( ) {
static std : : vector < Plugin > plugins ;
return plugins ;
}
std : : vector < std : : fs : : path > & PluginManager : : getPluginPaths ( ) {
static std : : vector < std : : fs : : path > pluginPaths ;
return pluginPaths ;
2020-12-22 17:10:01 +00:00
}
}