2022-10-06 23:12:45 +02:00
|
|
|
#include "main.h"
|
2022-08-25 15:53:37 +02:00
|
|
|
#include "globals.h"
|
2019-11-24 14:35:38 +01:00
|
|
|
#include "utils/TcpReceiver.h"
|
2022-02-04 15:47:35 +01:00
|
|
|
#include "utils/logger.h"
|
2022-05-14 16:19:26 +02:00
|
|
|
#include <coreinit/debug.h>
|
2023-11-27 17:52:54 +01:00
|
|
|
#include <notifications/notifications.h>
|
2022-05-14 16:19:26 +02:00
|
|
|
#include <rpxloader/rpxloader.h>
|
2022-02-04 15:47:35 +01:00
|
|
|
#include <wups.h>
|
2022-10-06 23:09:33 +02:00
|
|
|
#include <wups/config/WUPSConfigItemBoolean.h>
|
2019-11-24 14:35:38 +01:00
|
|
|
|
|
|
|
WUPS_PLUGIN_NAME("Wiiload");
|
|
|
|
WUPS_PLUGIN_DESCRIPTION("Wiiload Server");
|
2022-10-06 23:12:45 +02:00
|
|
|
WUPS_PLUGIN_VERSION(VERSION_FULL);
|
2019-11-24 14:35:38 +01:00
|
|
|
WUPS_PLUGIN_AUTHOR("Maschell");
|
|
|
|
WUPS_PLUGIN_LICENSE("GPL");
|
|
|
|
|
2021-09-24 20:51:39 +02:00
|
|
|
WUPS_USE_WUT_DEVOPTAB();
|
2019-11-24 14:35:38 +01:00
|
|
|
|
2022-10-06 23:09:33 +02:00
|
|
|
WUPS_USE_STORAGE("wiiload"); // Unqiue id for the storage api
|
|
|
|
#define WIILOAD_ENABLED_STRING "enabled"
|
|
|
|
|
2023-12-26 20:30:28 +01:00
|
|
|
std::unique_ptr<TcpReceiver> tcpReceiverThread = nullptr;
|
|
|
|
|
|
|
|
|
|
|
|
static WUPSConfigAPICallbackStatus ConfigMenuOpenedCallback(WUPSConfigCategoryHandle rootHandle);
|
|
|
|
static void ConfigMenuClosedCallback();
|
2019-11-24 14:35:38 +01:00
|
|
|
|
2022-05-14 16:19:26 +02:00
|
|
|
INITIALIZE_PLUGIN() {
|
|
|
|
RPXLoaderStatus error;
|
2022-08-25 15:59:48 +02:00
|
|
|
if ((error = RPXLoader_InitLibrary()) != RPX_LOADER_RESULT_SUCCESS) {
|
2022-05-14 16:19:26 +02:00
|
|
|
DEBUG_FUNCTION_LINE_ERR("WiiLoad Plugin: Failed to init RPXLoader. Error %d", error);
|
2022-08-25 15:53:37 +02:00
|
|
|
} else {
|
|
|
|
gLibRPXLoaderInitDone = true;
|
2022-05-14 16:19:26 +02:00
|
|
|
}
|
2022-10-14 20:45:42 +02:00
|
|
|
|
2023-11-27 17:52:54 +01:00
|
|
|
NotificationModuleStatus res;
|
|
|
|
if ((res = NotificationModule_InitLibrary()) != NOTIFICATION_MODULE_RESULT_SUCCESS) {
|
2023-12-26 20:30:28 +01:00
|
|
|
DEBUG_FUNCTION_LINE_ERR("Failed to init NotificationModule: %s", NotificationModule_GetStatusStr(res));
|
2023-11-27 17:52:54 +01:00
|
|
|
gNotificationModuleLoaded = false;
|
|
|
|
} else {
|
|
|
|
NotificationModule_SetDefaultValue(NOTIFICATION_MODULE_NOTIFICATION_TYPE_ERROR, NOTIFICATION_MODULE_DEFAULT_OPTION_DURATION_BEFORE_FADE_OUT, 10.0f);
|
|
|
|
gNotificationModuleLoaded = true;
|
|
|
|
}
|
|
|
|
|
2023-12-26 20:30:28 +01:00
|
|
|
WUPSConfigAPIOptionsV1 configOptions = {.name = "Wiiload Plugin"};
|
|
|
|
if (WUPSConfigAPI_Init(configOptions, ConfigMenuOpenedCallback, ConfigMenuClosedCallback) != WUPSCONFIG_API_RESULT_SUCCESS) {
|
|
|
|
DEBUG_FUNCTION_LINE_ERR("Failed to init config api");
|
|
|
|
}
|
2022-10-14 20:45:42 +02:00
|
|
|
|
2023-12-26 20:30:28 +01:00
|
|
|
if (WUPSStorageAPI::GetOrStoreDefault(WIILOAD_ENABLED_STRING, gWiiloadServerEnabled, true) != WUPS_STORAGE_ERROR_SUCCESS) {
|
|
|
|
DEBUG_FUNCTION_LINE_ERR("Failed to get or create item \"%s\"", WIILOAD_ENABLED_STRING);
|
2022-10-14 20:45:42 +02:00
|
|
|
}
|
|
|
|
|
2023-12-26 20:30:28 +01:00
|
|
|
if (WUPSStorageAPI::SaveStorage() != WUPS_STORAGE_ERROR_SUCCESS) {
|
|
|
|
DEBUG_FUNCTION_LINE_ERR("Failed to save storage");
|
|
|
|
}
|
2022-05-14 16:19:26 +02:00
|
|
|
}
|
|
|
|
|
2022-10-06 23:09:33 +02:00
|
|
|
void gServerEnabledChanged(ConfigItemBoolean *item, bool newValue) {
|
2023-12-26 20:30:28 +01:00
|
|
|
if (std::string_view(WIILOAD_ENABLED_STRING) != item->identifier) {
|
|
|
|
DEBUG_FUNCTION_LINE_WARN("Unexpected identifier in bool callback: %s", item->identifier);
|
|
|
|
return;
|
|
|
|
}
|
2022-10-06 23:09:33 +02:00
|
|
|
DEBUG_FUNCTION_LINE_VERBOSE("New value in gWiiloadServerEnabled: %d", newValue);
|
|
|
|
gWiiloadServerEnabled = newValue;
|
2023-12-26 20:30:28 +01:00
|
|
|
|
|
|
|
tcpReceiverThread.reset();
|
|
|
|
|
2022-10-06 23:09:33 +02:00
|
|
|
if (gWiiloadServerEnabled) {
|
|
|
|
DEBUG_FUNCTION_LINE("Starting server!");
|
2023-12-26 20:30:28 +01:00
|
|
|
tcpReceiverThread = std::make_unique<TcpReceiver>(4299);
|
|
|
|
|
|
|
|
if (tcpReceiverThread == nullptr) {
|
2022-10-06 23:09:33 +02:00
|
|
|
DEBUG_FUNCTION_LINE_ERR("Failed to create wiiload thread");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
DEBUG_FUNCTION_LINE("Wiiload server has been stopped!");
|
|
|
|
}
|
|
|
|
// If the value has changed, we store it in the storage.
|
2023-12-26 20:30:28 +01:00
|
|
|
auto res = WUPSStorageAPI::Store(item->identifier, gWiiloadServerEnabled);
|
2022-10-06 23:09:33 +02:00
|
|
|
if (res != WUPS_STORAGE_ERROR_SUCCESS) {
|
2023-12-26 20:30:28 +01:00
|
|
|
DEBUG_FUNCTION_LINE_ERR("Failed to store gWiiloadServerEnabled: %s", WUPSStorageAPI_GetStatusStr(res));
|
2022-10-06 23:09:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-26 20:30:28 +01:00
|
|
|
static WUPSConfigAPICallbackStatus ConfigMenuOpenedCallback(WUPSConfigCategoryHandle rootHandle) {
|
|
|
|
try {
|
|
|
|
WUPSConfigCategory root = WUPSConfigCategory(rootHandle);
|
2022-10-06 23:09:33 +02:00
|
|
|
|
2023-12-26 20:30:28 +01:00
|
|
|
root.add(WUPSConfigItemBoolean::Create(WIILOAD_ENABLED_STRING, "Enable Wiiload",
|
|
|
|
true, gWiiloadServerEnabled,
|
|
|
|
&gServerEnabledChanged));
|
2022-10-06 23:09:33 +02:00
|
|
|
|
2023-12-26 20:30:28 +01:00
|
|
|
} catch (std::exception &e) {
|
|
|
|
OSReport("Exception T_T : %s\n", e.what());
|
|
|
|
return WUPSCONFIG_API_CALLBACK_RESULT_ERROR;
|
|
|
|
}
|
|
|
|
return WUPSCONFIG_API_CALLBACK_RESULT_SUCCESS;
|
2022-10-06 23:09:33 +02:00
|
|
|
}
|
|
|
|
|
2023-12-26 20:30:28 +01:00
|
|
|
static void ConfigMenuClosedCallback() {
|
2022-10-06 23:09:33 +02:00
|
|
|
// Save all changes
|
2023-12-26 20:30:28 +01:00
|
|
|
if (WUPSStorageAPI::SaveStorage() != WUPS_STORAGE_ERROR_SUCCESS) {
|
2022-10-06 23:09:33 +02:00
|
|
|
DEBUG_FUNCTION_LINE_ERR("Failed to close storage");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-06 11:55:58 +02:00
|
|
|
/* Entry point */
|
|
|
|
ON_APPLICATION_START() {
|
|
|
|
initLogging();
|
2022-10-06 23:09:33 +02:00
|
|
|
if (gWiiloadServerEnabled) {
|
|
|
|
DEBUG_FUNCTION_LINE("Start wiiload thread");
|
2023-12-26 20:30:28 +01:00
|
|
|
tcpReceiverThread = std::make_unique<TcpReceiver>(4299);
|
|
|
|
if (tcpReceiverThread == nullptr) {
|
2022-10-06 23:09:33 +02:00
|
|
|
DEBUG_FUNCTION_LINE_ERR("Failed to create wiiload thread");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
DEBUG_FUNCTION_LINE("Wiiload server is disabled");
|
2022-10-06 23:07:36 +02:00
|
|
|
}
|
2019-11-24 14:35:38 +01:00
|
|
|
}
|
|
|
|
|
2022-10-06 12:19:26 +02:00
|
|
|
ON_APPLICATION_ENDS() {
|
2023-12-26 20:30:28 +01:00
|
|
|
tcpReceiverThread.reset();
|
2022-01-30 17:49:03 +01:00
|
|
|
deinitLogging();
|
2021-01-10 18:22:17 +01:00
|
|
|
}
|