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