Skip to content

Commit dc54bc4

Browse files
committed
CMake Support
- Add cmake toolchain file - Add example for cmake
1 parent 51f627d commit dc54bc4

11 files changed

Lines changed: 249 additions & 6 deletions

File tree

.gitignore

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
loader/build/*
22
loader/WiiUPluginLoader.cbp
33
loader/*.elf
4-
example_plugin/bin/*
5-
example_plugin/build/*
6-
example_plugin_pic/*
74
portlib_repos/*
85
plugins/*/bin/*
96
plugins/*/build/*
@@ -29,5 +26,4 @@ debug/
2926
*.cbp
3027
lib/
3128
cmake-build-debug/
32-
CMakeLists.txt
3329
_site/

README.MD

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ On the Discord you can more information about open tasks and how to contribute:
5858

5959
## Format the code via docker
6060

61-
`docker run --rm -v ${PWD}:/src wiiuenv/clang-format:13.0.0-2 -r ./include ./libraries ./plugins/example_plugin/src -i`
61+
`docker run --rm -v ${PWD}:/src wiiuenv/clang-format:13.0.0-2 -r ./include ./libraries ./plugins -i`
6262

6363
# Credits
6464
Some files are based on brainslug by Chadderz:
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
cmake_minimum_required(VERSION 3.2)
2+
3+
include("${DEVKITPRO}/wups/share/wups.cmake" REQUIRED)
4+
5+
project(example_cpp CXX)
6+
7+
add_executable(example_cpp src/main.cpp)
8+
wups_create_plugin(example_cpp)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Example plugin
2+
3+
This is just a simple example plugin using `cmake` which can be used as a template.
4+
5+
## Building
6+
7+
For building you need:
8+
- [wups](https://github.com/Maschell/WiiUPluginSystem)
9+
- [wut](https://github.com/decaf-emu/wut)
10+
11+
Install them (in this order) according to their README's. Don't forget the dependencies of the libs itself.
Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
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);
File renamed without changes.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Example plugin
22

3-
This is just a simple example plugin which can be used as a template.
3+
This is just a simple example plugin using `make` which can be used as a template.
44

55
## Building
66

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#pragma once
2+
3+
#ifdef __cplusplus
4+
extern "C" {
5+
#endif
6+
7+
#include <string.h>
8+
#include <whb/log.h>
9+
10+
#define __FILENAME_X__ (strrchr(__FILE__, '\\') ? strrchr(__FILE__, '\\') + 1 : __FILE__)
11+
#define __FILENAME__ (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILENAME_X__)
12+
13+
#define OSFATAL_FUNCTION_LINE(FMT, ARGS...) \
14+
do { \
15+
OSFatal_printf("[%s]%s@L%04d: " FMT "", __FILENAME__, __FUNCTION__, __LINE__, ##ARGS); \
16+
} while (0)
17+
18+
#define DEBUG_FUNCTION_LINE(FMT, ARGS...) \
19+
do { \
20+
WHBLogPrintf("[%23s]%30s@L%04d: " FMT "", __FILENAME__, __FUNCTION__, __LINE__, ##ARGS); \
21+
} while (0);
22+
23+
#define DEBUG_FUNCTION_LINE_WRITE(FMT, ARGS...) \
24+
do { \
25+
WHBLogWritef("[%23s]%30s@L%04d: " FMT "", __FILENAME__, __FUNCTION__, __LINE__, ##ARGS); \
26+
} while (0);
27+
28+
#ifdef __cplusplus
29+
}
30+
#endif

0 commit comments

Comments
 (0)