From 8b2634f75efcc576a364f2e79ce5a61fcab8a17c Mon Sep 17 00:00:00 2001 From: Maschell Date: Fri, 9 Aug 2024 16:41:29 +0200 Subject: [PATCH] Add simple "safe mode" which disable all non-basearoma plugins --- source/main.cpp | 17 +++++++ source/plugin/PluginDataFactory.cpp | 31 +++---------- source/plugin/PluginDataFactory.h | 2 +- source/utils/WUPSBackendSettings.cpp | 17 +++---- source/utils/WUPSBackendSettings.h | 16 +++++-- source/utils/config/ConfigRenderer.cpp | 5 +-- source/utils/input/VPADInput.h | 33 ++++++++------ source/utils/utils.cpp | 62 ++++++++++++++++++++++++++ source/utils/utils.h | 6 ++- 9 files changed, 133 insertions(+), 56 deletions(-) diff --git a/source/main.cpp b/source/main.cpp index 366db18..1d624e3 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -6,6 +6,7 @@ #include "patcher/hooks_patcher_static.h" #include "plugin/PluginDataFactory.h" #include "utils/WUPSBackendSettings.h" +#include "utils/input/VPADInput.h" #include "utils/logger.h" #include "utils/utils.h" #include "version.h" @@ -42,6 +43,22 @@ WUMS_INITIALIZE() { } } + VPadInput vpadInput; + vpadInput.update(1280, 720); + auto buttomComboSafeMode = Input::eButtons::BUTTON_L | Input::eButtons::BUTTON_UP | Input::eButtons::BUTTON_MINUS; + if ((vpadInput.data.buttons_h & (buttomComboSafeMode)) == buttomComboSafeMode) { + auto tobeIgnoredFilePath = getNonBaseAromaPluginFilenames(getPluginPath()); + WUPSBackendSettings::LoadSettings(); + std::set inactivePlugins = WUPSBackendSettings::GetInactivePluginFilenames(); + + inactivePlugins.insert(tobeIgnoredFilePath.begin(), tobeIgnoredFilePath.end()); + for (const auto &d : inactivePlugins) { + DEBUG_FUNCTION_LINE_INFO("%s should be ignores", d.c_str()); + } + WUPSBackendSettings::SetInactivePluginFilenames(inactivePlugins); + WUPSBackendSettings::SaveSettings(); + } + deinitLogging(); } diff --git a/source/plugin/PluginDataFactory.cpp b/source/plugin/PluginDataFactory.cpp index 1f07c32..e5b1d17 100644 --- a/source/plugin/PluginDataFactory.cpp +++ b/source/plugin/PluginDataFactory.cpp @@ -26,37 +26,18 @@ #include #include - -std::vector PluginDataFactory::loadDir(std::string_view path, const std::vector &inactivePluginsFilenames) { +std::vector PluginDataFactory::loadDir(const std::string_view path, const std::set &inactivePluginsFilenames) { std::vector result; - dirent *dp; - DIR *dfd; - if (path.empty()) { - DEBUG_FUNCTION_LINE_ERR("Failed to load Plugins from dir: Path was empty"); - return result; - } + for (const auto &full_file_path : getPluginFilePaths(path)) { + std::string fileName = StringTools::FullpathToFilename(full_file_path.c_str()); - if ((dfd = opendir(path.data())) == nullptr) { - DEBUG_FUNCTION_LINE_ERR("Couldn't open dir %s", path.data()); - return result; - } - - while ((dp = readdir(dfd)) != nullptr) { - if (dp->d_type == DT_DIR) { - continue; - } - if (std::string_view(dp->d_name).starts_with('.') || std::string_view(dp->d_name).starts_with('_') || !std::string_view(dp->d_name).ends_with(".wps")) { - DEBUG_FUNCTION_LINE_WARN("Skip file %s/%s", path.data(), dp->d_name); - continue; - } - - auto full_file_path = string_format("%s/%s", path.data(), dp->d_name); DEBUG_FUNCTION_LINE("Loading plugin: %s", full_file_path.c_str()); if (auto pluginData = load(full_file_path)) { bool shouldBeLoadedAndLinked = true; - if (std::ranges::find(inactivePluginsFilenames, dp->d_name) != inactivePluginsFilenames.end()) { + + if (inactivePluginsFilenames.contains(fileName)) { shouldBeLoadedAndLinked = false; } result.emplace_back(std::move(pluginData), shouldBeLoadedAndLinked); @@ -67,8 +48,6 @@ std::vector PluginDataFactory::loadDir(std::string_view path, } } - closedir(dfd); - return result; } diff --git a/source/plugin/PluginDataFactory.h b/source/plugin/PluginDataFactory.h index 2e3880c..97fc999 100644 --- a/source/plugin/PluginDataFactory.h +++ b/source/plugin/PluginDataFactory.h @@ -25,7 +25,7 @@ class PluginDataFactory { public: - static std::vector loadDir(std::string_view path, const std::vector &inactivePluginsFilenames); + static std::vector loadDir(std::string_view path, const std::set &inactivePluginsFilenames); static std::unique_ptr load(std::string_view path); diff --git a/source/utils/WUPSBackendSettings.cpp b/source/utils/WUPSBackendSettings.cpp index 6699701..1d3f483 100644 --- a/source/utils/WUPSBackendSettings.cpp +++ b/source/utils/WUPSBackendSettings.cpp @@ -9,7 +9,7 @@ namespace WUPSBackendSettings { namespace { - std::vector sInactivePlugins; + std::set sInactivePlugins; } #define INACTIVE_PLUGINS_KEY "inactive_plugins" @@ -20,14 +20,14 @@ namespace WUPSBackendSettings { std::string filePath = folderPath + "wupsbackend.json"; if (!ParseJsonFromFile(filePath, j)) { - sInactivePlugins.clear(); return false; } + sInactivePlugins.clear(); if (j.contains(INACTIVE_PLUGINS_KEY) && j[INACTIVE_PLUGINS_KEY].is_array()) { for (auto &cur : j[INACTIVE_PLUGINS_KEY]) { if (cur.is_string()) { - sInactivePlugins.push_back(cur); + sInactivePlugins.insert(cur); } } } @@ -63,14 +63,15 @@ namespace WUPSBackendSettings { return true; } - void SetInactivePluginFilenames(std::span filenames) { + void ClearInactivePluginFilenames() { sInactivePlugins.clear(); - for (const auto &filename : filenames) { - sInactivePlugins.emplace_back(filename); - } } - const std::vector &GetInactivePluginFilenames() { + void AddInactivePluginFilename(const std::string &filename) { + sInactivePlugins.insert(filename); + } + + const std::set &GetInactivePluginFilenames() { return sInactivePlugins; } diff --git a/source/utils/WUPSBackendSettings.h b/source/utils/WUPSBackendSettings.h index a15dcfb..6438d1d 100644 --- a/source/utils/WUPSBackendSettings.h +++ b/source/utils/WUPSBackendSettings.h @@ -1,15 +1,25 @@ #pragma once +#include #include #include -#include namespace WUPSBackendSettings { bool LoadSettings(); bool SaveSettings(); - void SetInactivePluginFilenames(std::span filenames); + void ClearInactivePluginFilenames(); - const std::vector &GetInactivePluginFilenames(); + void AddInactivePluginFilename(const std::string &filename); + + template + void SetInactivePluginFilenames(const Iterable &filenames) { + ClearInactivePluginFilenames(); + for (const auto &cur : filenames) { + AddInactivePluginFilename(cur); + } + } + + const std::set &GetInactivePluginFilenames(); }; // namespace WUPSBackendSettings diff --git a/source/utils/config/ConfigRenderer.cpp b/source/utils/config/ConfigRenderer.cpp index 715aa75..623e325 100644 --- a/source/utils/config/ConfigRenderer.cpp +++ b/source/utils/config/ConfigRenderer.cpp @@ -6,8 +6,6 @@ #include "utils/logger.h" #include "utils/utils.h" -#include - ConfigRenderer::ConfigRenderer(std::vector &&vec) : mConfigs(std::move(vec)) { std::copy(mConfigs.begin(), mConfigs.end(), std::back_inserter(mAllConfigs)); @@ -153,7 +151,6 @@ ConfigSubState ConfigRenderer::UpdateStateMain(const Input &input) { return SUB_STATE_RUNNING; } - void ConfigRenderer::RenderStateMain() const { auto &configs = GetConfigList(); @@ -193,7 +190,7 @@ void ConfigRenderer::RenderStateMain() const { // draw top bar DrawUtils::setFontSize(24); if (mSetActivePluginsMode) { - DrawUtils::print(16, 6 + 24, "Please select the plugin that should be active"); + DrawUtils::print(16, 6 + 24, "Please select the plugins that should be active"); } else { DrawUtils::print(16, 6 + 24, "Wii U Plugin System Config Menu"); diff --git a/source/utils/input/VPADInput.h b/source/utils/input/VPADInput.h index abe8d42..2bf7188 100644 --- a/source/utils/input/VPADInput.h +++ b/source/utils/input/VPADInput.h @@ -17,6 +17,8 @@ ****************************************************************************/ #include "Input.h" + +#include #include class VPadInput final : public Input { @@ -31,23 +33,28 @@ public: lastData = data; data = {}; - vpadError = VPAD_READ_NO_SAMPLES; + vpadError = VPAD_READ_UNINITIALIZED; - if (VPADRead(VPAD_CHAN_0, &vpad, 1, &vpadError) > 0 && vpadError == VPAD_READ_SUCCESS) { - data.buttons_r = vpad.release; - data.buttons_h = vpad.hold; - data.buttons_d = vpad.trigger; - data.validPointer = !vpad.tpNormal.validity; - data.touched = vpad.tpNormal.touched; + int maxAttempts = 100; + do { + if (VPADRead(VPAD_CHAN_0, &vpad, 1, &vpadError) > 0 && vpadError == VPAD_READ_SUCCESS) { + data.buttons_r = vpad.release; + data.buttons_h = vpad.hold; + data.buttons_d = vpad.trigger; + data.validPointer = !vpad.tpNormal.validity; + data.touched = vpad.tpNormal.touched; - VPADGetTPCalibratedPoint(VPAD_CHAN_0, &tpCalib, &vpad.tpFiltered1); + VPADGetTPCalibratedPoint(VPAD_CHAN_0, &tpCalib, &vpad.tpFiltered1); - //! calculate the screen offsets - data.x = -(width >> 1) + (int32_t) (((float) tpCalib.x / 1280.0f) * (float) width); - data.y = -(height >> 1) + (int32_t) (float) height - (((float) tpCalib.y / 720.0f) * (float) height); + //! calculate the screen offsets + data.x = -(width >> 1) + (int32_t) (((float) tpCalib.x / 1280.0f) * (float) width); + data.y = -(height >> 1) + (int32_t) (float) height - (((float) tpCalib.y / 720.0f) * (float) height); - return true; - } + return true; + } else { + OSSleepTicks(OSMillisecondsToTicks(1)); + } + } while (--maxAttempts > 0 && vpadError == VPAD_READ_NO_SAMPLES); return false; } diff --git a/source/utils/utils.cpp b/source/utils/utils.cpp index 44b777e..ddc81a5 100644 --- a/source/utils/utils.cpp +++ b/source/utils/utils.cpp @@ -1,4 +1,6 @@ #include "utils.h" + +#include "StringTools.h" #include "fs/CFile.hpp" #include "globals.h" #include "json.hpp" @@ -8,6 +10,7 @@ #include #include #include +#include #include static std::string sPluginPath; @@ -132,4 +135,63 @@ bool ParseJsonFromFile(const std::string &filePath, nlohmann::json &outJson) { file.close(); free(json_data); return result; +} + +std::vector getPluginFilePaths(std::string_view basePath) { + std::vector result; + struct dirent *dp; + DIR *dfd; + + if (basePath.empty()) { + DEBUG_FUNCTION_LINE_ERR("Failed to scan plugin dir: Path was empty"); + return result; + } + + if ((dfd = opendir(basePath.data())) == nullptr) { + DEBUG_FUNCTION_LINE_ERR("Couldn't open dir %s", basePath.data()); + return result; + } + + while ((dp = readdir(dfd)) != nullptr) { + if (dp->d_type == DT_DIR) { + continue; + } + + if (std::string_view(dp->d_name).starts_with('.') || std::string_view(dp->d_name).starts_with('_') || !std::string_view(dp->d_name).ends_with(".wps")) { + DEBUG_FUNCTION_LINE_WARN("Skip file %s/%s", basePath.data(), dp->d_name); + continue; + } + + auto full_file_path = string_format("%s/%s", basePath.data(), dp->d_name); + result.push_back(full_file_path); + } + closedir(dfd); + return result; +} + +std::vector getNonBaseAromaPluginFilenames(std::string_view basePath) { + std::vector result; + + for (const auto &filePath : getPluginFilePaths(basePath)) { + std::string fileName = StringTools::FullpathToFilename(filePath.c_str()); + + const char *baseAromaFileNames[] = { + "AromaBasePlugin.wps", + "drc_region_free.wps", + "homebrew_on_menu.wps", + "regionfree.wps", + }; + + bool found = false; + for (const auto &cur : baseAromaFileNames) { + if (fileName == cur) { + found = true; + break; + } + } + if (!found) { + result.push_back(fileName); + } + } + return result; } \ No newline at end of file diff --git a/source/utils/utils.h b/source/utils/utils.h index a949f9c..2539d54 100644 --- a/source/utils/utils.h +++ b/source/utils/utils.h @@ -144,4 +144,8 @@ OSDynLoad_Error CustomDynLoadAlloc(int32_t size, int32_t align, void **outAddr); void CustomDynLoadFree(void *addr); -bool ParseJsonFromFile(const std::string &filePath, nlohmann::json &outJson); \ No newline at end of file +bool ParseJsonFromFile(const std::string &filePath, nlohmann::json &outJson); + +std::vector getPluginFilePaths(std::string_view basePath); + +std::vector getNonBaseAromaPluginFilenames(std::string_view basePath); \ No newline at end of file