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>
2022-10-04 07:10:58 +00:00
# include <hex/helpers/utils.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>
2022-06-29 19:34:17 +00:00
# include <system_error>
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 ) {
2022-06-29 19:34:17 +00:00
# if defined(OS_WINDOWS)
this - > m_handle = LoadLibraryW ( path . c_str ( ) ) ;
2023-07-15 12:29:14 +00:00
if ( this - > m_handle = = INVALID_HANDLE_VALUE | | this - > m_handle = = nullptr ) {
2022-06-29 19:34:17 +00:00
log : : error ( " LoadLibraryW failed: {}! " , std : : system_category ( ) . message ( : : GetLastError ( ) ) ) ;
return ;
}
# else
2023-03-12 17:43:05 +00:00
this - > m_handle = dlopen ( wolv : : util : : toUTF8String ( path ) . c_str ( ) , RTLD_LAZY ) ;
2022-06-29 19:34:17 +00:00
if ( this - > m_handle = = nullptr ) {
log : : error ( " dlopen failed: {}! " , dlerror ( ) ) ;
return ;
}
# endif
2020-12-22 17:10:01 +00:00
2022-02-01 21:09:44 +00:00
this - > m_initializePluginFunction = getPluginFunction < InitializePluginFunc > ( " initializePlugin " ) ;
this - > m_getPluginNameFunction = getPluginFunction < GetPluginNameFunc > ( " getPluginName " ) ;
this - > m_getPluginAuthorFunction = getPluginFunction < GetPluginAuthorFunc > ( " getPluginAuthor " ) ;
this - > m_getPluginDescriptionFunction = getPluginFunction < GetPluginDescriptionFunc > ( " getPluginDescription " ) ;
this - > m_getCompatibleVersionFunction = getPluginFunction < GetCompatibleVersionFunc > ( " getCompatibleVersion " ) ;
this - > m_setImGuiContextFunction = getPluginFunction < SetImGuiContextFunc > ( " setImGuiContext " ) ;
this - > m_isBuiltinPluginFunction = getPluginFunction < IsBuiltinPluginFunc > ( " isBuiltinPlugin " ) ;
2023-07-13 12:08:23 +00:00
this - > m_getSubCommandsFunction = getPluginFunction < GetSubCommandsFunc > ( " getSubCommands " ) ;
2020-12-22 17:10:01 +00:00
}
2021-02-19 12:22:12 +00:00
Plugin : : Plugin ( Plugin & & other ) noexcept {
2021-02-07 13:57:13 +00:00
this - > m_handle = other . m_handle ;
2022-02-01 21:09:44 +00:00
this - > m_path = std : : move ( other . m_path ) ;
this - > m_initializePluginFunction = other . m_initializePluginFunction ;
this - > m_getPluginNameFunction = other . m_getPluginNameFunction ;
this - > m_getPluginAuthorFunction = other . m_getPluginAuthorFunction ;
this - > m_getPluginDescriptionFunction = other . m_getPluginDescriptionFunction ;
this - > m_getCompatibleVersionFunction = other . m_getCompatibleVersionFunction ;
this - > m_setImGuiContextFunction = other . m_setImGuiContextFunction ;
this - > m_isBuiltinPluginFunction = other . m_isBuiltinPluginFunction ;
2023-07-13 12:08:23 +00:00
this - > m_getSubCommandsFunction = other . m_getSubCommandsFunction ;
2022-02-01 21:09:44 +00:00
other . m_handle = nullptr ;
other . m_initializePluginFunction = nullptr ;
other . m_getPluginNameFunction = nullptr ;
other . m_getPluginAuthorFunction = nullptr ;
other . m_getPluginDescriptionFunction = nullptr ;
other . m_getCompatibleVersionFunction = nullptr ;
other . m_setImGuiContextFunction = nullptr ;
other . m_isBuiltinPluginFunction = nullptr ;
2023-07-13 12:08:23 +00:00
other . m_getSubCommandsFunction = nullptr ;
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)
if ( this - > m_handle ! = nullptr )
FreeLibrary ( this - > m_handle ) ;
# else
if ( this - > m_handle ! = nullptr )
dlclose ( this - > m_handle ) ;
# endif
2020-12-22 17:10:01 +00:00
}
2022-01-17 19:06:00 +00:00
bool Plugin : : initializePlugin ( ) const {
2023-06-18 08:18:41 +00:00
if ( this - > m_handle = = nullptr )
return false ;
2023-07-15 12:29:14 +00:00
const auto pluginName = wolv : : util : : toUTF8String ( this - > m_path . filename ( ) ) ;
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 ( ) ) {
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 22:28:56 +00:00
}
2022-01-17 19:06:00 +00:00
if ( this - > m_initializePluginFunction ! = nullptr ) {
2023-07-15 12:29:14 +00:00
try {
this - > m_initializePluginFunction ( ) ;
} catch ( const std : : exception & e ) {
log : : error ( " Plugin '{}' threw an exception on init: {} " , pluginName , e . what ( ) ) ;
} catch ( . . . ) {
log : : error ( " Plugin '{}' threw an exception on init " , pluginName ) ;
}
2022-01-17 19:06:00 +00:00
} else {
return false ;
}
2022-01-23 22:28:56 +00:00
this - > m_initialized = true ;
return true ;
2021-02-19 12:22:12 +00:00
}
std : : string Plugin : : getPluginName ( ) const {
if ( this - > m_getPluginNameFunction ! = nullptr )
return this - > m_getPluginNameFunction ( ) ;
else
2021-05-29 19:51:00 +00:00
return hex : : format ( " Unknown Plugin @ 0x{0:016X} " , reinterpret_cast < intptr_t > ( this - > m_handle ) ) ;
2021-02-19 12:22:12 +00:00
}
std : : string Plugin : : getPluginAuthor ( ) const {
if ( this - > m_getPluginAuthorFunction ! = nullptr )
return this - > m_getPluginAuthorFunction ( ) ;
else
return " Unknown " ;
}
std : : string Plugin : : getPluginDescription ( ) const {
if ( this - > m_getPluginDescriptionFunction ! = nullptr )
return this - > m_getPluginDescriptionFunction ( ) ;
else
return " " ;
2020-12-22 17:10:01 +00:00
}
2022-01-23 22:28:56 +00:00
std : : string Plugin : : getCompatibleVersion ( ) const {
if ( this - > m_getCompatibleVersionFunction ! = nullptr )
return this - > m_getCompatibleVersionFunction ( ) ;
else
return " " ;
}
2021-08-20 22:52:11 +00:00
void Plugin : : setImGuiContext ( ImGuiContext * ctx ) const {
if ( this - > m_setImGuiContextFunction ! = nullptr )
this - > m_setImGuiContextFunction ( ctx ) ;
}
2022-02-01 17:09:40 +00:00
[[nodiscard]] bool Plugin : : isBuiltinPlugin ( ) const {
if ( this - > m_isBuiltinPluginFunction ! = nullptr )
return this - > m_isBuiltinPluginFunction ( ) ;
else
return false ;
}
2022-03-04 10:36:37 +00:00
const std : : fs : : path & Plugin : : getPath ( ) const {
2022-01-17 19:06:00 +00:00
return this - > m_path ;
}
2022-01-23 22:28:56 +00:00
bool Plugin : : isLoaded ( ) const {
return this - > m_initialized ;
2022-01-17 23:10:10 +00:00
}
2023-07-13 12:08:23 +00:00
std : : span < SubCommand > Plugin : : getSubCommands ( ) const {
if ( this - > m_getSubCommandsFunction ! = nullptr ) {
auto result = this - > m_getSubCommandsFunction ( ) ;
2023-07-21 12:12:08 +00:00
return * reinterpret_cast < std : : vector < SubCommand > * > ( result ) ;
2023-07-13 12:08:23 +00:00
} else
return { } ;
}
2022-01-17 19:06:00 +00:00
2022-01-24 19:53:17 +00:00
void * Plugin : : getPluginFunction ( const std : : string & symbol ) {
2022-06-29 19:34:17 +00: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 22:28:56 +00:00
}
2022-01-17 19:06:00 +00:00
2023-07-26 11:50:51 +00:00
namespace {
std : : fs : : path s_pluginFolder ;
std : : vector < Plugin > s_plugins ;
}
2022-02-01 17:09:40 +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-07-26 11:50:51 +00:00
s_pluginFolder = 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-07-26 11:50:51 +00:00
s_plugins . emplace_back ( pluginPath . path ( ) ) ;
2021-01-12 15:50:15 +00:00
}
2021-04-20 19:46:48 +00:00
2023-07-26 11:50:51 +00:00
if ( s_plugins . 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-07-26 11:50:51 +00:00
s_plugins . clear ( ) ;
s_pluginFolder . clear ( ) ;
2020-12-22 17:10:01 +00:00
}
2021-04-20 19:46:48 +00:00
void PluginManager : : reload ( ) {
PluginManager : : unload ( ) ;
2023-07-26 11:50:51 +00:00
PluginManager : : load ( s_pluginFolder ) ;
}
const std : : vector < Plugin > & PluginManager : : getPlugins ( ) {
return s_plugins ;
2020-12-22 17:10:01 +00:00
}
}