2020-04-29 18:02:36 +02:00
# include "hooks.h"
2022-05-14 14:00:20 +02:00
# include "plugin/PluginContainer.h"
2021-09-25 14:26:18 +02:00
# include "utils/StorageUtils.h"
2022-02-04 16:25:44 +01:00
# include "utils/logger.h"
2022-05-14 14:00:20 +02:00
# include <memory>
# include <vector>
2020-04-29 18:02:36 +02:00
2022-02-04 16:25:44 +01:00
static const char * * hook_names = ( const char * [ ] ) {
2020-05-03 11:16:22 +02:00
" WUPS_LOADER_HOOK_INIT_WUT_MALLOC " ,
" WUPS_LOADER_HOOK_FINI_WUT_MALLOC " ,
" WUPS_LOADER_HOOK_INIT_WUT_NEWLIB " ,
" WUPS_LOADER_HOOK_FINI_WUT_NEWLIB " ,
" WUPS_LOADER_HOOK_INIT_WUT_STDCPP " ,
" WUPS_LOADER_HOOK_FINI_WUT_STDCPP " ,
2021-03-16 17:55:32 +01:00
" WUPS_LOADER_HOOK_INIT_WUT_DEVOPTAB " ,
" WUPS_LOADER_HOOK_FINI_WUT_DEVOPTAB " ,
2021-04-17 14:05:39 +02:00
" WUPS_LOADER_HOOK_INIT_WUT_SOCKETS " ,
" WUPS_LOADER_HOOK_FINI_WUT_SOCKETS " ,
2021-03-16 17:55:32 +01:00
2022-01-23 21:16:38 +01:00
" WUPS_LOADER_HOOK_INIT_WRAPPER " ,
" WUPS_LOADER_HOOK_FINI_WRAPPER " ,
2021-04-07 00:23:23 +02:00
" WUPS_LOADER_HOOK_GET_CONFIG " ,
" WUPS_LOADER_HOOK_CONFIG_CLOSED " ,
" WUPS_LOADER_HOOK_INIT_STORAGE " ,
2020-05-03 11:16:22 +02:00
" WUPS_LOADER_HOOK_INIT_PLUGIN " ,
" WUPS_LOADER_HOOK_DEINIT_PLUGIN " ,
2021-03-16 17:55:32 +01:00
" WUPS_LOADER_HOOK_APPLICATION_STARTS " ,
2020-05-03 11:16:22 +02:00
" WUPS_LOADER_HOOK_RELEASE_FOREGROUND " ,
" WUPS_LOADER_HOOK_ACQUIRED_FOREGROUND " ,
2021-03-16 17:55:32 +01:00
" WUPS_LOADER_HOOK_APPLICATION_REQUESTS_EXIT " ,
2021-04-07 00:23:23 +02:00
" WUPS_LOADER_HOOK_APPLICATION_ENDS " } ;
2020-04-29 18:02:36 +02:00
2022-05-14 14:00:20 +02:00
void CallHook ( const std : : vector < std : : unique_ptr < PluginContainer > > & plugins , wups_loader_hook_type_t hook_type ) {
2021-04-07 00:23:23 +02:00
DEBUG_FUNCTION_LINE_VERBOSE ( " Calling hook of type %s [%d] " , hook_names [ hook_type ] , hook_type ) ;
2022-05-14 14:00:20 +02:00
for ( auto & plugin : plugins ) {
CallHook ( plugin , hook_type ) ;
}
}
2020-04-29 18:02:36 +02:00
2022-05-14 14:00:20 +02:00
void CallHook ( const std : : unique_ptr < PluginContainer > & plugin , wups_loader_hook_type_t hook_type ) {
for ( const auto & hook : plugin - > getPluginInformation ( ) - > getHookDataList ( ) ) {
if ( hook - > getType ( ) = = hook_type ) {
DEBUG_FUNCTION_LINE_VERBOSE ( " Calling hook of type %s for plugin %s [%d] " , hook_names [ hook - > getType ( ) ] , plugin - > metaInformation - > getName ( ) . c_str ( ) , hook_type ) ;
void * func_ptr = hook - > getFunctionPointer ( ) ;
if ( func_ptr ! = nullptr ) {
2022-10-03 16:27:28 +02:00
switch ( hook_type ) {
case WUPS_LOADER_HOOK_INIT_WUT_MALLOC :
case WUPS_LOADER_HOOK_FINI_WUT_MALLOC :
case WUPS_LOADER_HOOK_INIT_WUT_NEWLIB :
case WUPS_LOADER_HOOK_FINI_WUT_NEWLIB :
case WUPS_LOADER_HOOK_INIT_WUT_STDCPP :
case WUPS_LOADER_HOOK_FINI_WUT_STDCPP :
case WUPS_LOADER_HOOK_INIT_WUT_DEVOPTAB :
case WUPS_LOADER_HOOK_FINI_WUT_DEVOPTAB :
case WUPS_LOADER_HOOK_INIT_WUT_SOCKETS :
case WUPS_LOADER_HOOK_FINI_WUT_SOCKETS :
case WUPS_LOADER_HOOK_INIT_WRAPPER :
case WUPS_LOADER_HOOK_FINI_WRAPPER :
case WUPS_LOADER_HOOK_GET_CONFIG :
case WUPS_LOADER_HOOK_CONFIG_CLOSED :
case WUPS_LOADER_HOOK_INIT_PLUGIN :
case WUPS_LOADER_HOOK_DEINIT_PLUGIN :
case WUPS_LOADER_HOOK_APPLICATION_STARTS :
case WUPS_LOADER_HOOK_RELEASE_FOREGROUND :
case WUPS_LOADER_HOOK_ACQUIRED_FOREGROUND :
case WUPS_LOADER_HOOK_APPLICATION_REQUESTS_EXIT :
case WUPS_LOADER_HOOK_APPLICATION_ENDS :
// clang-format off
( ( void ( * ) ( ) ) ( ( uint32_t * ) func_ptr ) ) ( ) ;
// clang-format on
break ;
case WUPS_LOADER_HOOK_INIT_STORAGE : {
wups_loader_init_storage_args_t args ;
args . open_storage_ptr = & StorageUtils : : OpenStorage ;
args . close_storage_ptr = & StorageUtils : : CloseStorage ;
args . plugin_id = plugin - > getMetaInformation ( ) - > getStorageId ( ) . c_str ( ) ;
// clang-format off
( ( void ( * ) ( wups_loader_init_storage_args_t ) ) ( ( uint32_t * ) func_ptr ) ) ( args ) ;
// clang-format on
break ;
}
default : {
DEBUG_FUNCTION_LINE_ERR ( " ###################################### " ) ;
DEBUG_FUNCTION_LINE_ERR ( " Hook is not implemented %s [%d] " , hook_names [ hook_type ] , hook_type ) ;
DEBUG_FUNCTION_LINE_ERR ( " ###################################### " ) ;
}
2020-04-29 18:02:36 +02:00
}
2022-05-14 14:00:20 +02:00
} else {
DEBUG_FUNCTION_LINE_ERR ( " Failed to call hook. It was not defined " ) ;
2020-04-29 18:02:36 +02:00
}
}
}
2022-05-14 14:00:20 +02:00
}