1+ #include " ./utils/logger.h"
2+ #include < coreinit/filesystem.h>
3+ #include < coreinit/thread.h>
4+ #include < coreinit/time.h>
5+ #include < malloc.h>
6+ #include < string.h>
7+ #include < whb/libmanager.h>
8+ #include < whb/log_cafe.h>
9+ #include < whb/log_module.h>
10+ #include < whb/log_udp.h>
11+ #include < wups.h>
12+ #include < wups/config/WUPSConfigItemBoolean.h>
13+
14+ /* *
15+ Mandatory plugin information.
16+ If not set correctly, the loader will refuse to use the plugin.
17+ **/
18+ WUPS_PLUGIN_NAME (" Example plugin" );
19+ WUPS_PLUGIN_DESCRIPTION (" This is just an example plugin and will log the FSOpenFile function." );
20+ WUPS_PLUGIN_VERSION (" v1.0" );
21+ WUPS_PLUGIN_AUTHOR (" Maschell" );
22+ WUPS_PLUGIN_LICENSE (" BSD" );
23+
24+ /* *
25+ All of this defines can be used in ANY file.
26+ It's possible to split it up into multiple files.
27+
28+ **/
29+
30+ WUPS_USE_WUT_DEVOPTAB (); // Use the wut devoptabs
31+ WUPS_USE_STORAGE (" example_plugin" ); // Unqiue id for the storage api
32+
33+ bool logFSOpen = true ;
34+
35+ /* *
36+ Get's called ONCE when the loader exits, but BEFORE the ON_APPLICATION_START gets called or functions are overridden.
37+ **/
38+ INITIALIZE_PLUGIN () {
39+ if (!WHBLogModuleInit ()) {
40+ WHBLogCafeInit ();
41+ WHBLogUdpInit ();
42+ }
43+ DEBUG_FUNCTION_LINE (" INITIALIZE_PLUGIN of example_plugin!" );
44+
45+ // Open storage to read values
46+ WUPSStorageError storageRes = WUPS_OpenStorage ();
47+ if (storageRes != WUPS_STORAGE_ERROR_SUCCESS) {
48+ DEBUG_FUNCTION_LINE (" Failed to open storage %s (%d)" , WUPS_GetStorageStatusStr (storageRes), storageRes);
49+ } else {
50+ // Try to get value from storage
51+ if ((storageRes = WUPS_GetBool (nullptr , " logFSOpen" , &logFSOpen)) == WUPS_STORAGE_ERROR_NOT_FOUND) {
52+ // Add the value to the storage if it's missing.
53+ if (WUPS_StoreBool (nullptr , " logFSOpen" , logFSOpen) != WUPS_STORAGE_ERROR_SUCCESS) {
54+ DEBUG_FUNCTION_LINE (" Failed to store bool" );
55+ }
56+ } else if (storageRes != WUPS_STORAGE_ERROR_SUCCESS) {
57+ DEBUG_FUNCTION_LINE (" Failed to get bool %s (%d)" , WUPS_GetStorageStatusStr (storageRes), storageRes);
58+ }
59+
60+ // Close storage
61+ if (WUPS_CloseStorage () != WUPS_STORAGE_ERROR_SUCCESS) {
62+ DEBUG_FUNCTION_LINE (" Failed to close storage" );
63+ }
64+ }
65+ }
66+
67+ /* *
68+ Gets called when the plugin loader is re-entered => when the plugin is unloaded.
69+ The overridden functions are restored before this is getting called.
70+ **/
71+ DEINITIALIZE_PLUGIN () {
72+ DEBUG_FUNCTION_LINE (" DEINITIALIZE_PLUGIN of example_plugin!" );
73+ }
74+
75+ /* *
76+ Gets called when an application starts.
77+ This is called BEFORE the functions are overridden.
78+ Make sure to initialize all functions you're using in the overridden functions!
79+ **/
80+ ON_APPLICATION_START () {
81+ if (!WHBLogModuleInit ()) {
82+ WHBLogCafeInit ();
83+ WHBLogUdpInit ();
84+ }
85+
86+ DEBUG_FUNCTION_LINE (" ON_APPLICATION_START of example_plugin!" );
87+ }
88+
89+ /* *
90+ Gets called when an application request to exit.
91+ **/
92+ ON_APPLICATION_REQUESTS_EXIT () {
93+ DEBUG_FUNCTION_LINE (" ON_APPLICATION_REQUESTS_EXIT of example_plugin!" );
94+ }
95+
96+ void logFSOpenChanged (ConfigItemBoolean *item, bool newValue) {
97+ DEBUG_FUNCTION_LINE (" New value in logFSOpenChanged: %d" , newValue);
98+ logFSOpen = newValue;
99+ // If the value has changed, we store it in the storage.
100+ WUPS_StoreInt (nullptr , " logFSOpen" , logFSOpen);
101+ }
102+
103+ WUPS_GET_CONFIG () {
104+ // We open the storage so we can persist the configuration the user did.
105+ if (WUPS_OpenStorage () != WUPS_STORAGE_ERROR_SUCCESS) {
106+ DEBUG_FUNCTION_LINE (" Failed to open storage" );
107+ return 0 ;
108+ }
109+
110+ WUPSConfigHandle config;
111+ WUPSConfig_CreateHandled (&config, " Example Plugin" );
112+
113+ WUPSConfigCategoryHandle cat;
114+ WUPSConfig_AddCategoryByNameHandled (config, " Logging" , &cat);
115+
116+ WUPSConfigItemBoolean_AddToCategoryHandled (config, cat, " logFSOpen" , " Log FSOpen calls" , logFSOpen, &logFSOpenChanged);
117+
118+ return config;
119+ }
120+
121+ WUPS_CONFIG_CLOSED () {
122+ // Save all changes
123+ if (WUPS_CloseStorage () != WUPS_STORAGE_ERROR_SUCCESS) {
124+ DEBUG_FUNCTION_LINE (" Failed to close storage" );
125+ }
126+ }
127+
128+ /* *
129+ This defines a function replacement.
130+ It allows to replace the system function with an own function.
131+ So whenever a game / application calles an overridden function, your function gets called instead.
132+
133+ Currently it's only possible to override functions that are loaded from .rpl files of OSv10 (00050010-1000400A).
134+
135+ Signature of this macro:
136+ DECL_FUNCTION( RETURN_TYPE, ARBITRARY_NAME_OF_FUNCTION , ARGS_SEPERATED_BY_COMMA){
137+ //Your code goes here.
138+ }
139+
140+ Within this macro, two more function get declare you can use.
141+ my_ARBITRARY_NAME_OF_FUNCTION and real_FSOpenFile
142+
143+ RETURN_TYPE my_ARBITRARY_NAME_OF_FUNCTION(ARGS_SEPERATED_BY_COMMA):
144+ is just name of the function that gets declared in this macro.
145+ It has the same effect as calling the overridden function directly.
146+
147+ RETURN_TYPE real_ARBITRARY_NAME_OF_FUNCTION(ARGS_SEPERATED_BY_COMMA):
148+ is the name of the function, that leads to function that was overridden.
149+ Use this to call the original function that will be overridden.
150+ CAUTION: Other plugins may already have manipulated the the return value or arguments.
151+
152+
153+ Use this macro for each function you want to override
154+
155+ **/
156+ DECL_FUNCTION (int , FSOpenFile, FSClient *pClient, FSCmdBlock *pCmd, const char *path, const char *mode, int *handle, int error) {
157+ int result = real_FSOpenFile (pClient, pCmd, path, mode, handle, error);
158+ if (logFSOpen) {
159+ DEBUG_FUNCTION_LINE (" FSOpenFile called for folder %s! Result %d" , path, result);
160+ }
161+ return result;
162+ }
163+
164+ /* *
165+ This tells the loader which functions from which library (.rpl) should be replaced with which function from this file.
166+ The list of possible libraries can be found in the wiki. (In general it's WUPS_LOADER_LIBRARY_ + the name of the RPL in caps lock)
167+
168+ WUPS_MUST_REPLACE(FUNCTION_NAME_IN_THIS_FILE, NAME_OF_LIB_WHICH_CONTAINS_THIS_FUNCTION, NAME_OF_FUNCTION_TO_OVERRIDE)
169+
170+ Define this for each function you want to override.
171+ **/
172+ WUPS_MUST_REPLACE (FSOpenFile, WUPS_LOADER_LIBRARY_COREINIT, FSOpenFile);
0 commit comments