diff --git a/README.md b/README.md index 9f46186..76d7a01 100644 --- a/README.md +++ b/README.md @@ -26,6 +26,19 @@ Mii Maker,0x000500101004A200 The title id decides how the entry is launched: vWii titles through the vWii, Wii U titles directly. Entries whose title is not installed are left out of the menu. Up to 4 entries are shown, as many as fit: the menu has room for 7 rows in all. System applets (title ids starting with 00050030, like the Internet Browser) can't be custom entries. A custom entry can be set as the autoboot title like any other. +### Menu order + +The menu normally shows the built-in entries first and custom entries after them. To choose the order, name built-in entries in `bootOptions.cfg` with an `@` line; entries then appear in the order of the lines: + +``` +My vWii Channel,0x0001000152494654 +@wiiu_menu +@vwii_homebrew_channel +@vwii_system_menu +``` + +The names are the ones `autoboot.cfg` uses: `@wiiu_menu`, `@homebrew_launcher`, `@vwii_system_menu` and `@vwii_homebrew_channel`. Built-in entries without a line follow the listed ones in the usual order, so none can disappear by accident, and an entry that isn't available (the Homebrew Launcher without its installer, the vWii Homebrew Channel without HBC) is still left out. Without `@` lines the menu keeps the usual order. With `@` lines and no autoboot entry, the cursor starts on the first entry instead of the Wii U Menu. + ## Buildflags ### Logging diff --git a/source/MenuUtils.cpp b/source/MenuUtils.cpp index 7b9bfef..48f7090 100644 --- a/source/MenuUtils.cpp +++ b/source/MenuUtils.cpp @@ -22,6 +22,7 @@ #include #include #include +#include #include #include @@ -49,21 +50,56 @@ static void trimInPlace(char *str) { memmove(str, start, end - start + 1); } -std::vector readCustomBootOptions(const std::string &path) { +// The built-in entry an "@" line names, by the same names autoboot.cfg uses, in any case; -1 if none. +static int32_t builtInOptionNamed(const char *name) { + for (uint32_t i = 0; i < sizeof(autoboot_config_strings) / sizeof(char *); i++) { + if (strcasecmp(autoboot_config_strings[i], name) == 0) { + return i; + } + } + return -1; +} + +std::vector readCustomBootOptions(const std::string &path, std::vector &menuOrder) { std::vector result; + menuOrder.clear(); FILE *f = fopen(path.c_str(), "r"); if (!f) { return result; } // One entry per line: "Name,0x0001000152494654". The title id decides how it is launched. + // A line "@name" places a built-in entry ("@wiiu_menu") between them. + std::vector order; + bool namesBuiltIns = false; + bool tooMany = false; char line[256]; + bool firstLine = true; while (fgets(line, sizeof(line), f)) { + // Some editors start the file with a UTF-8 byte order mark. + if (firstLine && strncmp(line, "\xEF\xBB\xBF", 3) == 0) { + memmove(line, line + 3, strlen(line + 3) + 1); + } + firstLine = false; trimInPlace(line); if (line[0] == '\0' || line[0] == '#') { continue; } + // A line with a comma is a custom entry, even if its name starts with @. + if (line[0] == '@' && !strchr(line, ',')) { + char *name = line + 1; + trimInPlace(name); + int32_t builtIn = builtInOptionNamed(name); + if (builtIn < 0) { + DEBUG_FUNCTION_LINE_WARN("bootOptions.cfg: \"%s\" is not a built-in entry", line); + continue; + } + order.push_back(builtIn); + namesBuiltIns = true; + continue; + } + char *comma = strrchr(line, ','); if (!comma) { DEBUG_FUNCTION_LINE_WARN("bootOptions.cfg: no comma in \"%s\"", line); @@ -102,12 +138,20 @@ std::vector readCustomBootOptions(const std::string &path) { } if (result.size() >= MAX_CUSTOM_BOOT_OPTIONS) { - DEBUG_FUNCTION_LINE_WARN("bootOptions.cfg: only the first %d entries are used", MAX_CUSTOM_BOOT_OPTIONS); - break; + // Keep reading: "@" lines further down still count. + if (!tooMany) { + DEBUG_FUNCTION_LINE_WARN("bootOptions.cfg: only the first %d entries are used", MAX_CUSTOM_BOOT_OPTIONS); + tooMany = true; + } + continue; } + order.push_back(BOOT_OPTION_CUSTOM_BASE + result.size()); result.push_back({line, titleId}); } fclose(f); + if (namesBuiltIns) { + menuOrder = order; + } return result; } @@ -160,7 +204,7 @@ void writeAutobootOption(std::string &configPath, int32_t autobootOption, const } } -void drawMenuScreen(const std::map &menu, uint32_t selectedIndex, uint32_t autobootIndex, bool updatesBlocked) { +void drawMenuScreen(const BootMenu &menu, uint32_t selectedIndex, uint32_t autobootIndex, bool updatesBlocked) { DrawUtils::beginDraw(); DrawUtils::clear(COLOR_BACKGROUND); @@ -209,8 +253,8 @@ void drawMenuScreen(const std::map &menu, uint32_t select DrawUtils::endDraw(); } -int32_t handleMenuScreen(std::string &configPath, int32_t autobootOptionInput, const std::map &menu, - const std::vector &customOptions) { +int32_t handleMenuScreen(std::string &configPath, int32_t autobootOptionInput, const BootMenu &menu, + const std::vector &customOptions, bool ordered) { auto screenBuffer = DrawUtils::InitOSScreen(); if (!screenBuffer) { OSFatal("AutobootModule: Failed to alloc memory for screen"); @@ -224,31 +268,25 @@ int32_t handleMenuScreen(std::string &configPath, int32_t autobootOptionInput, c OSFatal("AutobootModule: Failed to init font"); } - int32_t selectedIndex = autobootOptionInput > 0 ? autobootOptionInput : 0; - int autobootIndex = autobootOptionInput; - // Set by X or Y: the saved choice is written even if it matches the input, so X can clear - // a saved custom entry whose title isn't installed (the menu gets -1 for it). - bool autobootChanged = false; - - // "Convert" id to index - int32_t offset = 0; - for (auto &item : menu) { - if ((uint32_t) selectedIndex == item.first) { - selectedIndex = offset; - break; - } - offset++; - } - if (autobootIndex > 0) { - offset = 0; - for (auto &item : menu) { - if ((uint32_t) autobootIndex == item.first) { - autobootIndex = offset; - break; + // Convert option ids to positions in the menu (-1 if not shown). With no + // autoboot option, the cursor starts on the first entry when bootOptions.cfg + // sets the order, else on the Wii U Menu. + auto indexOf = [&menu](int32_t id) { + for (uint32_t i = 0; i < menu.size(); i++) { + if ((int32_t) menu[i].first == id) { + return (int32_t) i; } - offset++; } + return -1; + }; + int autobootIndex = autobootOptionInput >= 0 ? indexOf(autobootOptionInput) : -1; + int32_t selectedIndex = autobootIndex >= 0 ? autobootIndex : (ordered ? 0 : indexOf(BOOT_OPTION_WII_U_MENU)); + if (selectedIndex < 0) { + selectedIndex = 0; } + // Set by X or Y: the saved choice is written even if it matches the input, so X can clear + // a saved custom entry whose title isn't installed (the menu gets -1 for it). + bool autobootChanged = false; { PairMenu pairMenu; diff --git a/source/MenuUtils.h b/source/MenuUtils.h index 9c71dc9..0dc72db 100644 --- a/source/MenuUtils.h +++ b/source/MenuUtils.h @@ -6,6 +6,7 @@ #include #include #include +#include #include #define UPDATE_SKIP_PATH "fs:/vol/external01/wiiu/environments/skipUpdateWarn" @@ -35,19 +36,27 @@ enum { // The menu doesn't scroll, and an eighth row would cover the update status line. #define MAX_MENU_ROWS 7 +// Boot menu entries (option id, name) in the order they are shown. +using BootMenu = std::vector>; + struct CustomBootOption { std::string name; uint64_t titleId; }; -std::vector readCustomBootOptions(const std::string &path); +// Reads the custom entries from bootOptions.cfg. When the file also has "@" lines naming +// built-in entries, `menuOrder` gets every listed entry's option id in the file's order; +// without them it stays empty and the menu keeps the usual order. +std::vector readCustomBootOptions(const std::string &path, std::vector &menuOrder); int32_t readAutobootOption(std::string &configPath, const std::vector &customOptions); void writeAutobootOption(std::string &configPath, int32_t autobootOption, const std::vector &customOptions); -int32_t handleMenuScreen(std::string &configPath, int32_t autobootOptionInput, const std::map &menu, - const std::vector &customOptions); +// `ordered`: bootOptions.cfg sets the menu order; with no autoboot entry the cursor then +// starts on the first entry instead of the Wii U Menu. +int32_t handleMenuScreen(std::string &configPath, int32_t autobootOptionInput, const BootMenu &menu, + const std::vector &customOptions, bool ordered); nn::act::SlotNo handleAccountSelectScreen(const std::vector> &data); diff --git a/source/main.cpp b/source/main.cpp index e2c805a..4ccb75e 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -5,6 +5,7 @@ #include "QuickStartUtils.h" #include "StorageUtils.h" #include "logger.h" +#include #include #include #include @@ -100,33 +101,73 @@ int32_t main(int32_t argc, char **argv) { } } - auto customOptions = readCustomBootOptions(bootOptionsPath); + std::vector menuOrder; + auto customOptions = readCustomBootOptions(bootOptionsPath, menuOrder); + const bool ordered = !menuOrder.empty(); int32_t bootSelection = readAutobootOption(configPath, customOptions); - std::map menu; - menu[BOOT_OPTION_WII_U_MENU] = "Wii U Menu"; - if (showHBL) { - menu[BOOT_OPTION_HOMEBREW_LAUNCHER] = "Homebrew Launcher"; - } - menu[BOOT_OPTION_VWII_SYSTEM_MENU] = "vWii System Menu"; - if (showvHBL) { - menu[BOOT_OPTION_VWII_HOMEBREW_CHANNEL] = "vWii Homebrew Channel"; - } + // The usual order: the built-in entries, then the custom ones. Entries that bootOptions.cfg + // orders with "@" lines come first; the ones it doesn't list follow in the usual order. + menuOrder.insert(menuOrder.end(), {BOOT_OPTION_WII_U_MENU, BOOT_OPTION_HOMEBREW_LAUNCHER, + BOOT_OPTION_VWII_SYSTEM_MENU, BOOT_OPTION_VWII_HOMEBREW_CHANNEL}); for (uint32_t i = 0; i < customOptions.size(); i++) { - // Only offer titles that are installed, so a stale entry can never be launched. - if (!titleExists(customOptions[i].titleId)) { - DEBUG_FUNCTION_LINE_WARN("bootOptions.cfg: %016llx (%s) is not installed", customOptions[i].titleId, customOptions[i].name.c_str()); - } else if (menu.size() >= MAX_MENU_ROWS) { - DEBUG_FUNCTION_LINE_WARN("bootOptions.cfg: no room in the menu for %016llx (%s)", customOptions[i].titleId, customOptions[i].name.c_str()); - } else { - menu[BOOT_OPTION_CUSTOM_BASE + i] = customOptions[i].name; + menuOrder.push_back(BOOT_OPTION_CUSTOM_BASE + i); + } + + BootMenu menu; + // Built-in entries always get their row; custom entries share what's left, in file order. + uint32_t customRows = MAX_MENU_ROWS - (2 + (showHBL ? 1 : 0) + (showvHBL ? 1 : 0)); + std::vector placed; + for (uint32_t id : menuOrder) { + if (std::find(placed.begin(), placed.end(), id) != placed.end()) { + continue; + } + placed.push_back(id); + switch (id) { + case BOOT_OPTION_WII_U_MENU: + menu.emplace_back(id, "Wii U Menu"); + break; + case BOOT_OPTION_HOMEBREW_LAUNCHER: + if (showHBL) { + menu.emplace_back(id, "Homebrew Launcher"); + } + break; + case BOOT_OPTION_VWII_SYSTEM_MENU: + menu.emplace_back(id, "vWii System Menu"); + break; + case BOOT_OPTION_VWII_HOMEBREW_CHANNEL: + if (showvHBL) { + menu.emplace_back(id, "vWii Homebrew Channel"); + } + break; + default: { + const CustomBootOption &option = customOptions[id - BOOT_OPTION_CUSTOM_BASE]; + // Only offer titles that are installed, so a stale entry can never be launched. + if (!titleExists(option.titleId)) { + DEBUG_FUNCTION_LINE_WARN("bootOptions.cfg: %016llx (%s) is not installed", option.titleId, option.name.c_str()); + } else if (customRows == 0) { + DEBUG_FUNCTION_LINE_WARN("bootOptions.cfg: no room in the menu for %016llx (%s)", option.titleId, option.name.c_str()); + } else { + menu.emplace_back(id, option.name); + customRows--; + } + break; + } } } + auto inMenu = [&menu](int32_t id) { + for (auto &item : menu) { + if ((int32_t) item.first == id) { + return true; + } + } + return false; + }; if ((bootSelection == -1) || (bootSelection == BOOT_OPTION_HOMEBREW_LAUNCHER && !showHBL) || (bootSelection == BOOT_OPTION_VWII_HOMEBREW_CHANNEL && !showvHBL) || - (bootSelection >= BOOT_OPTION_CUSTOM_BASE && menu.count(bootSelection) == 0) || + (bootSelection >= BOOT_OPTION_CUSTOM_BASE && !inMenu(bootSelection)) || (buttons.hold & VPAD_BUTTON_PLUS)) { if (!AXIsInit()) { AXInit(); @@ -134,8 +175,8 @@ int32_t main(int32_t argc, char **argv) { // A saved entry the menu doesn't show (a custom title that's gone, or a missing Homebrew // Launcher or vWii Homebrew Channel) stays saved and comes back once it's available again, // instead of being preselected or overwritten by the menu. - int32_t menuInput = (bootSelection >= 0 && menu.count(bootSelection) == 0) ? -1 : bootSelection; - bootSelection = handleMenuScreen(configPath, menuInput, menu, customOptions); + int32_t menuInput = (bootSelection >= 0 && !inMenu(bootSelection)) ? -1 : bootSelection; + bootSelection = handleMenuScreen(configPath, menuInput, menu, customOptions, ordered); hadMenu = true; } @@ -162,7 +203,7 @@ int32_t main(int32_t argc, char **argv) { bootHomebrewChannel(); break; default: - if (bootSelection >= BOOT_OPTION_CUSTOM_BASE && menu.count(bootSelection) != 0) { + if (bootSelection >= BOOT_OPTION_CUSTOM_BASE && inMenu(bootSelection)) { bootCustomTitle(customOptions[bootSelection - BOOT_OPTION_CUSTOM_BASE].titleId); break; }