2020-04-29 18:02:36 +02:00
# include "hooks.h"
2022-05-14 14:00:20 +02:00
# include "plugin/PluginContainer.h"
2023-10-07 22:33:38 +02:00
# include "utils/StorageUtilsDeprecated.h"
2022-02-04 16:25:44 +01:00
# include "utils/logger.h"
2023-10-07 22:38:27 +02:00
# include "utils/storage/StorageUtils.h"
2024-11-27 20:44:20 +01:00
# include <functional>
2023-10-07 22:38:27 +02:00
# include <wups/storage.h>
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 " ,
2023-12-16 17:36:57 +01:00
" WUPS_LOADER_HOOK_GET_CONFIG_DEPRECATED " ,
" WUPS_LOADER_HOOK_CONFIG_CLOSED_DEPRECATED " ,
2021-04-07 00:23:23 +02:00
2023-10-07 22:38:27 +02:00
" WUPS_LOADER_HOOK_INIT_STORAGE_DEPRECATED " ,
2021-04-07 00:23:23 +02:00
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 " ,
2023-10-07 22:38:27 +02:00
" WUPS_LOADER_HOOK_APPLICATION_ENDS " ,
2023-12-16 17:36:57 +01:00
" WUPS_LOADER_HOOK_INIT_STORAGE " ,
" WUPS_LOADER_HOOK_INIT_CONFIG " } ;
2020-04-29 18:02:36 +02:00
2024-11-27 20:44:20 +01:00
void CallHook ( const std : : vector < PluginContainer > & plugins , const wups_loader_hook_type_t hook_type ) {
CallHook ( plugins , hook_type , [ ] ( const auto & ) { return true ; } ) ;
}
void CallHook ( const std : : vector < PluginContainer > & plugins , const wups_loader_hook_type_t hook_type , const std : : function < bool ( const PluginContainer & ) > & pred ) {
2021-04-07 00:23:23 +02:00
DEBUG_FUNCTION_LINE_VERBOSE ( " Calling hook of type %s [%d] " , hook_names [ hook_type ] , hook_type ) ;
2023-11-04 15:32:45 +01:00
for ( const auto & plugin : plugins ) {
2024-11-27 20:44:20 +01:00
if ( pred ( plugin ) ) {
CallHook ( plugin , hook_type ) ;
}
2022-05-14 14:00:20 +02:00
}
}
2020-04-29 18:02:36 +02:00
2023-11-04 15:32:45 +01:00
void CallHook ( const PluginContainer & plugin , wups_loader_hook_type_t hook_type ) {
2024-08-04 14:12:52 +02:00
for ( const auto & hook : plugin . getPluginLinkInformation ( ) . getHookDataList ( ) ) {
2024-03-24 07:40:58 +01:00
if ( hook . getType ( ) = = hook_type ) {
DEBUG_FUNCTION_LINE_VERBOSE ( " Calling hook of type %s for plugin %s [%d] " , hook_names [ hook . getType ( ) ] , plugin . getMetaInformation ( ) . getName ( ) . c_str ( ) , hook_type ) ;
void * func_ptr = hook . getFunctionPointer ( ) ;
2022-05-14 14:00:20 +02:00
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 :
2023-12-16 17:36:57 +01:00
case WUPS_LOADER_HOOK_GET_CONFIG_DEPRECATED :
case WUPS_LOADER_HOOK_CONFIG_CLOSED_DEPRECATED :
2022-10-03 16:27:28 +02:00
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 ;
2023-10-07 22:38:27 +02:00
case WUPS_LOADER_HOOK_INIT_STORAGE :
case WUPS_LOADER_HOOK_INIT_STORAGE_DEPRECATED : {
2023-12-16 17:36:57 +01:00
if ( plugin . getMetaInformation ( ) . getWUPSVersion ( ) < = WUPSVersion ( 0 , 7 , 1 ) ) {
2023-10-07 22:38:27 +02:00
WUPSStorageDeprecated : : wups_loader_init_storage_args_t_ args { } ;
2023-10-07 22:33:38 +02:00
args . open_storage_ptr = & WUPSStorageDeprecated : : StorageUtils : : OpenStorage ;
args . close_storage_ptr = & WUPSStorageDeprecated : : StorageUtils : : CloseStorage ;
args . plugin_id = plugin . getMetaInformation ( ) . getStorageId ( ) . c_str ( ) ;
// clang-format off
2023-10-07 22:38:27 +02:00
( ( void ( * ) ( WUPSStorageDeprecated : : wups_loader_init_storage_args_t_ ) ) ( ( uint32_t * ) func_ptr ) ) ( args ) ;
2023-10-07 22:33:38 +02:00
// clang-format on
break ;
}
2023-10-07 22:38:27 +02:00
wups_loader_init_storage_args_t_ args { } ;
args . version = WUPS_STORAGE_CUR_API_VERSION ;
2023-12-16 12:44:20 +01:00
args . root_item = plugin . getStorageRootItem ( ) ;
args . save_function_ptr = & StorageUtils : : API : : SaveStorage ;
args . force_reload_function_ptr = & StorageUtils : : API : : ForceReloadStorage ;
args . wipe_storage_function_ptr = & StorageUtils : : API : : WipeStorage ;
2023-10-07 22:38:27 +02:00
args . delete_item_function_ptr = & StorageUtils : : API : : DeleteItem ;
args . create_sub_item_function_ptr = & StorageUtils : : API : : CreateSubItem ;
args . get_sub_item_function_ptr = & StorageUtils : : API : : GetSubItem ;
args . store_item_function_ptr = & StorageUtils : : API : : StoreItem ;
args . get_item_function_ptr = & StorageUtils : : API : : GetItem ;
args . get_item_size_function_ptr = & StorageUtils : : API : : GetItemSize ;
// clang-format off
auto res = ( ( WUPSStorageError ( * ) ( wups_loader_init_storage_args_t_ ) ) ( ( uint32_t * ) func_ptr ) ) ( args ) ;
// clang-format on
2023-12-16 17:36:57 +01:00
if ( res ! = WUPS_STORAGE_ERROR_SUCCESS ) {
2023-10-07 22:38:27 +02:00
// TODO: More error handling? Notification?
2023-12-16 12:44:20 +01:00
DEBUG_FUNCTION_LINE_ERR ( " WUPS_LOADER_HOOK_INIT_STORAGE failed for plugin %s: %s " , plugin . getMetaInformation ( ) . getName ( ) . c_str ( ) , WUPSStorageAPI_GetStatusStr ( res ) ) ;
2023-12-16 17:36:57 +01:00
}
break ;
}
case WUPS_LOADER_HOOK_INIT_CONFIG : {
wups_loader_init_config_args_t args { . arg_version = 1 , . plugin_identifier = plugin . getHandle ( ) } ;
auto res = ( ( WUPSConfigAPIStatus ( * ) ( wups_loader_init_config_args_t ) ) ( ( uint32_t * ) func_ptr ) ) ( args ) ;
// clang-format on
if ( res ! = WUPSCONFIG_API_RESULT_SUCCESS ) {
// TODO: More error handling? Notification?
DEBUG_FUNCTION_LINE_ERR ( " WUPS_LOADER_HOOK_INIT_CONFIG failed for plugin %s: %s " , plugin . getMetaInformation ( ) . getName ( ) . c_str ( ) , WUPSConfigAPI_GetStatusStr ( res ) ) ;
2023-10-07 22:38:27 +02:00
}
break ;
2022-10-03 16:27:28 +02:00
}
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-12-30 21:08:41 +01:00
break ;
2020-04-29 18:02:36 +02:00
}
}
2022-05-14 14:00:20 +02:00
}