Skip to content

Commit

Permalink
Add support for per-title favorites
Browse files Browse the repository at this point in the history
  • Loading branch information
GaryOderNichts committed Aug 1, 2023
1 parent 30d6ec3 commit f810bdf
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 10 deletions.
35 changes: 28 additions & 7 deletions plugin/source/config/ConfigItemSelectAmiibo.cpp
Expand Up @@ -53,27 +53,38 @@ static bool ConfigItemSelectAmiibo_callCallback(void* context);

static std::vector<std::string> favorites;
static bool favoritesUpdated = false;
static bool favoritesPerTitle = false;

std::vector<std::string>& ConfigItemSelectAmiibo_GetFavorites(void)
{
return favorites;
}

void ConfigItemSelectAmiibo_Init(std::string rootPath)
void ConfigItemSelectAmiibo_Init(std::string rootPath, bool favoritesPerTitle)
{
favorites.clear();
favoritesUpdated = false;

::favoritesPerTitle = favoritesPerTitle;
std::string favoritesKey = "favorites";
if (favoritesPerTitle) {
uint64_t titleId = OSGetTitleID();
char titleIdString[17];
snprintf(titleIdString, sizeof(titleIdString), "%016llx", titleId);
favoritesKey += titleIdString;
}

int32_t favoritesSize;
if (WUPS_GetInt(nullptr, "favoritesSize", &favoritesSize) != WUPS_STORAGE_ERROR_SUCCESS) {
if (WUPS_GetInt(nullptr, (favoritesKey + "Size").c_str(), &favoritesSize) != WUPS_STORAGE_ERROR_SUCCESS) {
return;
}

char* favoritesString = new char[favoritesSize + 1];
if (WUPS_GetString(nullptr, "favorites", favoritesString, favoritesSize) != WUPS_STORAGE_ERROR_SUCCESS) {
if (WUPS_GetString(nullptr, favoritesKey.c_str(), favoritesString, favoritesSize + 1) != WUPS_STORAGE_ERROR_SUCCESS) {
delete[] favoritesString;
return;
}

favorites.clear();

std::stringstream stream(favoritesString);
std::string fav;
while (std::getline(stream, fav, ':')) {
Expand Down Expand Up @@ -104,8 +115,18 @@ static void saveFavorites(ConfigItemSelectAmiibo* item)
// get rid of the last ':'
saveBuf.resize(saveBuf.size() - 1);

WUPS_StoreString(nullptr, "favorites", saveBuf.c_str());
WUPS_StoreInt(nullptr, "favoritesSize", saveBuf.size());
std::string favoritesKey = "favorites";
if (favoritesPerTitle) {
uint64_t titleId = OSGetTitleID();
char titleIdString[17];
snprintf(titleIdString, sizeof(titleIdString), "%016llx", titleId);
favoritesKey += titleIdString;
}

WUPS_StoreString(nullptr, favoritesKey.c_str(), saveBuf.c_str());
WUPS_StoreInt(nullptr, (favoritesKey + "Size").c_str(), saveBuf.size());

favoritesUpdated = false;
}

static void enterSelectionMenu(ConfigItemSelectAmiibo* item)
Expand Down
2 changes: 1 addition & 1 deletion plugin/source/config/ConfigItemSelectAmiibo.hpp
Expand Up @@ -18,7 +18,7 @@ struct ConfigItemSelectAmiibo {

std::vector<std::string>& ConfigItemSelectAmiibo_GetFavorites(void);

void ConfigItemSelectAmiibo_Init(std::string rootPath);
void ConfigItemSelectAmiibo_Init(std::string rootPath, bool favoritesPerTitle);

bool ConfigItemSelectAmiibo_AddToCategory(WUPSConfigCategoryHandle cat, const char* configID, const char* displayName, const char* amiiboFolder, const char* currentAmiibo, AmiiboSelectedCallback callback);

Expand Down
32 changes: 30 additions & 2 deletions plugin/source/main.cpp
@@ -1,5 +1,6 @@
#include <wups.h>
#include <wups/config/WUPSConfigItemMultipleValues.h>
#include <wups/config/WUPSConfigItemBoolean.h>
#include <string>
#include <map>

Expand Down Expand Up @@ -36,6 +37,8 @@ uint32_t currentRemoveAfterOption = 0;

uint32_t currentQuickSelectCombination = 0;

bool favoritesPerTitle = false;

static void nfpiiLogHandler(NfpiiLogVerbosity verb, const char* message)
{
ConfigItemLog_PrintType((LogType) verb, message);
Expand All @@ -54,8 +57,6 @@ INITIALIZE_PLUGIN()
// Read values from config
WUPSStorageError err = WUPS_OpenStorage();
if (err == WUPS_STORAGE_ERROR_SUCCESS) {
ConfigItemSelectAmiibo_Init(TAG_EMULATION_PATH);

int32_t emulationState = (int32_t) NfpiiGetEmulationState();
if ((err = WUPS_GetInt(nullptr, "emulationState", &emulationState)) == WUPS_STORAGE_ERROR_NOT_FOUND) {
WUPS_StoreInt(nullptr, "emulationState", emulationState);
Expand All @@ -81,6 +82,11 @@ INITIALIZE_PLUGIN()
}
}

if ((err = WUPS_GetBool(nullptr, "favoritesPerTitle", &favoritesPerTitle)) == WUPS_STORAGE_ERROR_NOT_FOUND) {
WUPS_StoreBool(nullptr, "favoritesPerTitle", favoritesPerTitle);
}
ConfigItemSelectAmiibo_Init(TAG_EMULATION_PATH, favoritesPerTitle);

if ((err = WUPS_GetInt(nullptr, "quickSelectCombo", (int32_t*) &currentQuickSelectCombination)) == WUPS_STORAGE_ERROR_NOT_FOUND) {
WUPS_StoreInt(nullptr, "quickSelectCombo", currentQuickSelectCombination);
}
Expand All @@ -104,6 +110,17 @@ ON_APPLICATION_START()
WHBLogCafeInit();
WHBLogUdpInit();
}

// Make sure favorites are refreshed for the new title
if (WUPS_OpenStorage() == WUPS_STORAGE_ERROR_SUCCESS) {
ConfigItemSelectAmiibo_Init(TAG_EMULATION_PATH, favoritesPerTitle);

if (WUPS_CloseStorage() != WUPS_STORAGE_ERROR_SUCCESS) {
DEBUG_FUNCTION_LINE("Failed to close storage");
}
} else {
DEBUG_FUNCTION_LINE("Failed to open storage");
}
}

static void stateChangedCallback(ConfigItemMultipleValues* values, uint32_t index)
Expand All @@ -130,6 +147,15 @@ static void amiiboSelectedCallback(ConfigItemSelectAmiibo* amiibos, const char*
NfpiiSetTagEmulationPath(filePath);
}

static void favoritesPerTitleCallback(ConfigItemBoolean* item, bool enable)
{
favoritesPerTitle = enable;
WUPS_StoreBool(nullptr, "favoritesPerTitle", favoritesPerTitle);

// refresh favorites
ConfigItemSelectAmiibo_Init(TAG_EMULATION_PATH, favoritesPerTitle);
}

static void quickSelectComboCallback(ConfigItemButtonCombo* item, uint32_t newValue)
{
currentQuickSelectCombination = newValue;
Expand Down Expand Up @@ -183,6 +209,8 @@ WUPS_GET_CONFIG()
std::string currentAmiiboPath = NfpiiGetTagEmulationPath();
ConfigItemSelectAmiibo_AddToCategoryHandled(config, cat, "select_amiibo", "Select Amiibo", TAG_EMULATION_PATH.c_str(), currentAmiiboPath.c_str(), amiiboSelectedCallback);

WUPSConfigItemBoolean_AddToCategoryHandled(config, cat, "favorites_per_title", "Per-Title Favorites", favoritesPerTitle, favoritesPerTitleCallback);

WUPSConfigItemButtonCombo_AddToCategoryHandled(config, cat, "quick_select_combination", "Quick Select Combo", currentQuickSelectCombination, quickSelectComboCallback);

ConfigItemDumpAmiibo_AddToCategoryHandled(config, cat, "dump_amiibo", "Dump Amiibo", (TAG_EMULATION_PATH + "dumps").c_str());
Expand Down

0 comments on commit f810bdf

Please sign in to comment.