Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for softlocks when initializing/exiting the application and minor issues with backup/restore #60

Merged
merged 13 commits into from
Aug 22, 2024
Merged
5 changes: 0 additions & 5 deletions include/savemng.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,6 @@
#define M_OFF 1
#define Y_OFF 1

#define COLOR_WHITE Color(0xffffffff)
#define COLOR_BLACK Color(0, 0, 0, 255)
#define COLOR_BACKGROUND Color(0x00006F00)
#define COLOR_TEXT COLOR_WHITE

struct Title {
uint32_t highID;
uint32_t lowID;
Expand Down
6 changes: 6 additions & 0 deletions include/utils/Colors.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#pragma once

#define COLOR_WHITE Color(0xffffffff)
#define COLOR_BLACK Color(0, 0, 0, 255)
#define COLOR_BACKGROUND Color(0x00006F00)
#define COLOR_TEXT COLOR_WHITE
17 changes: 14 additions & 3 deletions include/utils/DrawUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ class DrawUtils {

static bool getRedraw() { return redraw; }

static void initBuffers(void *tvBuffer, uint32_t tvSize, void *drcBuffer, uint32_t drcSize);
static BOOL LogConsoleInit();

static void LogConsoleFree();

static void beginDraw();

Expand Down Expand Up @@ -73,12 +75,21 @@ class DrawUtils {

static void drawRGB5A3(int x, int y, float scale, uint8_t *fileContent);

static uint32_t initScreen();

static uint32_t deinitScreen();




private:
static bool redraw;
static bool isBackBuffer;


static uint8_t *tvBuffer;
static uint32_t tvSize;
static uint8_t *drcBuffer;
static uint32_t drcSize;

static uint32_t sBufferSizeTV, sBufferSizeDRC;
static BOOL sConsoleHasForeground;
};
5 changes: 5 additions & 0 deletions include/utils/StateUtils.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
#pragma once

#include <cstdint>

class State {
public:
static void init();
static bool AppRunning();
static void shutdown();
static void registerProcUICallbacks();
static uint32_t ConsoleProcCallbackAcquired(void *context);
static uint32_t ConsoleProcCallbackReleased(void *context);

private:
static bool aroma;
Expand Down
2 changes: 1 addition & 1 deletion include/version.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

#define VERSION_MAJOR 1
#define VERSION_MINOR 6
#define VERSION_MICRO 1
#define VERSION_MICRO 2
52 changes: 23 additions & 29 deletions src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
#include <coreinit/debug.h>
#include <coreinit/mcp.h>
#include <coreinit/screen.h>
#include <cstdlib>
#include <cstring>
#include <icon.h>
Expand All @@ -10,11 +7,15 @@
#include <savemng.h>
#include <sndcore2/core.h>
#include <utils/DrawUtils.h>
#include <utils/Colors.h>
#include <utils/InputUtils.h>
#include <utils/LanguageUtils.h>
#include <utils/StateUtils.h>
#include <utils/StringUtils.h>
#include <version.h>
#include <coreinit/debug.h>
#include <coreinit/mcp.h>
#include <coreinit/screen.h>

static int wiiuTitlesCount = 0, vWiiTitlesCount = 0;

Expand Down Expand Up @@ -91,10 +92,11 @@ static Title *loadWiiUTitles(int run) {
path = StringUtils::stringFormat("%s/usr/save/%08x/%s/meta/meta.xml", (i == 0) ? getUSB().c_str() : "storage_mlc01:", highIDs[a],
data->d_name);
if (checkEntry(path.c_str()) == 1) {
for (int i = 0; i < usable; i++) {
if (contains(highIDs, savesl[i].highID) &&
(strtoul(data->d_name, nullptr, 16) == savesl[i].lowID)) {
savesl[i].found = true;
for (int ii = 0; ii < usable; ii++) {
if (contains(highIDs, savesl[ii].highID) &&
(strtoul(data->d_name, nullptr, 16) == savesl[ii].lowID) &&
savesl[ii].dev == i ) {
savesl[ii].found = true;
tNoSave--;
break;
}
Expand All @@ -109,7 +111,7 @@ static Title *loadWiiUTitles(int run) {
}

foundCount += tNoSave;
auto *saves = (Saves *) malloc((foundCount + tNoSave) * sizeof(Saves));
auto *saves = (Saves *) malloc((foundCount) * sizeof(Saves));
if (saves == nullptr) {
promptError(LanguageUtils::gettext("Out of memory."));
return nullptr;
Expand Down Expand Up @@ -386,24 +388,14 @@ static void unloadTitles(Title *titles, int count) {
int main() {
AXInit();
AXQuit();
OSScreenInit();

uint32_t tvBufferSize = OSScreenGetBufferSizeEx(SCREEN_TV);
uint32_t drcBufferSize = OSScreenGetBufferSizeEx(SCREEN_DRC);

auto *screenBuffer = (uint8_t *) memalign(0x100, tvBufferSize + drcBufferSize);
if (!screenBuffer) {
OSFatal("Fail to allocate screenBuffer");

State::init();

if (DrawUtils::LogConsoleInit()) {
OSFatal("Failed to initialize OSSCreen");
}
memset(screenBuffer, 0, tvBufferSize + drcBufferSize);

OSScreenSetBufferEx(SCREEN_TV, screenBuffer);
OSScreenSetBufferEx(SCREEN_DRC, screenBuffer + tvBufferSize);

OSScreenEnableEx(SCREEN_TV, TRUE);
OSScreenEnableEx(SCREEN_DRC, TRUE);

DrawUtils::initBuffers(screenBuffer, tvBufferSize, screenBuffer + tvBufferSize, drcBufferSize);
State::registerProcUICallbacks();

if (!DrawUtils::initFont()) {
OSFatal("Failed to init font");
Expand All @@ -412,8 +404,8 @@ int main() {
WPADInit();
KPADInit();
WPADEnableURCC(1);

loadWiiUTitles(0);
State::init();

int res = romfsInit();
if (res) {
Expand All @@ -422,20 +414,20 @@ int main() {
State::shutdown();
return 0;
}

Swkbd_LanguageType systemLanguage = LanguageUtils::getSystemLanguage();
LanguageUtils::loadLanguage(systemLanguage);

if (!initFS()) {
promptError(LanguageUtils::gettext("initFS failed. Please make sure your MochaPayload is up-to-date"));
DrawUtils::endDraw();
State::shutdown();
State::shutdown();
return 0;
}

DrawUtils::beginDraw();
DrawUtils::clear(COLOR_BLACK);
DrawUtils::endDraw();

Title *wiiutitles = loadWiiUTitles(1);
Title *wiititles = loadWiiTitles();
getAccountsWiiU();
Expand All @@ -445,8 +437,9 @@ int main() {

Input input{};
std::unique_ptr<MainMenuState> state = std::make_unique<MainMenuState>(wiiutitles, wiititles, wiiuTitlesCount,
vWiiTitlesCount);
vWiiTitlesCount);
while (State::AppRunning()) {

input.read();

if (input.get(TRIGGER, PAD_BUTTON_ANY))
Expand All @@ -473,12 +466,13 @@ int main() {

unloadTitles(wiiutitles, wiiuTitlesCount);
unloadTitles(wiititles, vWiiTitlesCount);

shutdownFS();
LanguageUtils::gettextCleanUp();
romfsExit();

DrawUtils::deinitFont();
DrawUtils::LogConsoleFree();

State::shutdown();
return 0;
}
1 change: 1 addition & 0 deletions src/menu/WiiUTitleListState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <utils/InputUtils.h>
#include <utils/LanguageUtils.h>
#include <utils/StringUtils.h>
#include <utils/Colors.h>

#define MAX_TITLE_SHOW 14
static int cursorPos = 0;
Expand Down
1 change: 1 addition & 0 deletions src/menu/vWiiTitleListState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <savemng.h>
#include <utils/InputUtils.h>
#include <utils/LanguageUtils.h>
#include <utils/Colors.h>

#define MAX_TITLE_SHOW 14
static int cursorPos = 0;
Expand Down
42 changes: 30 additions & 12 deletions src/savemng.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
#include <sys/stat.h>
#include <utils/LanguageUtils.h>
#include <utils/StringUtils.h>
#include <utils/Colors.h>
#include <malloc.h>


#define __FSAShimSend ((FSError(*)(FSAShimBuffer *, uint32_t))(0x101C400 + 0x042d90))
#define IO_MAX_FILE_BUFFER (1024 * 1024) // 1 MB

Expand Down Expand Up @@ -202,14 +204,18 @@ static bool folderEmpty(const char *fPath) {
if (dir == nullptr)
return false;

int c = 0;
bool empty = true;
struct dirent *data;
while ((data = readdir(dir)) != nullptr)
if (++c > 2)
break;
while ((data = readdir(dir)) != nullptr) {
// rewritten to work wether ./.. are returned or not
if(strcmp(data->d_name,".") == 0 || strcmp(data->d_name,"..") == 0)
continue;
empty = false;
break;
}

closedir(dir);
return c < 3;
return empty;
}

static bool createFolder(const char *path) {
Expand Down Expand Up @@ -851,16 +857,23 @@ void copySavedata(Title *title, Title *titleb, int8_t allusers, int8_t allusers_
std::string srcPath = StringUtils::stringFormat("%s/%08x/%08x/%s", path.c_str(), highID, lowID, "user");
std::string dstPath = StringUtils::stringFormat("%s/%08x/%08x/%s", pathb.c_str(), highIDb, lowIDb, "user");
createFolderUnlocked(dstPath);
FSAMakeQuotaFromDir(srcPath.c_str(), dstPath.c_str(), titleb->accountSaveSize);

if (allusers > -1)
if (common)
if (allusers > -1) {
if (common) {
FSAMakeQuota(handle, newlibtoFSA(dstPath + "/common").c_str(), 0x666, titleb->accountSaveSize);
if (!copyDir(srcPath + "/common", dstPath + "/common"))
promptError(LanguageUtils::gettext("Common save not found."));
}
srcPath.append(StringUtils::stringFormat("/%s", wiiuacc[allusers].persistentID));
dstPath.append(StringUtils::stringFormat("/%s", wiiuacc[allusers_d].persistentID));
FSAMakeQuota(handle, newlibtoFSA(dstPath).c_str(), 0x666, titleb->accountSaveSize);
} else {
FSAMakeQuotaFromDir(srcPath.c_str(), dstPath.c_str(), titleb->accountSaveSize);
}

if (!copyDir(srcPath + StringUtils::stringFormat("/%s", wiiuacc[allusers].persistentID),
dstPath + StringUtils::stringFormat("/%s", wiiuacc[allusers_d].persistentID)))
if (!copyDir(srcPath, dstPath))
promptError(LanguageUtils::gettext("Copy failed."));

if (!titleb->saveInit) {
std::string userPath = StringUtils::stringFormat("%s/%08x/%08x/user", pathb.c_str(), highIDb, lowIDb);

Expand Down Expand Up @@ -1006,19 +1019,24 @@ void restoreSavedata(Title *title, uint8_t slot, int8_t sdusers, int8_t allusers
srcPath = getBackupPath(highID, lowID, slot);
std::string dstPath = StringUtils::stringFormat("%s/%08x/%08x/%s", path.c_str(), highID, lowID, isWii ? "data" : "user");
createFolderUnlocked(dstPath);
FSAMakeQuotaFromDir(srcPath.c_str(), dstPath.c_str(), title->accountSaveSize);


if ((sdusers > -1) && !isWii) {
if (common) {
FSAMakeQuota(handle, newlibtoFSA(dstPath + "/common").c_str(), 0x666, title->accountSaveSize);
if (!copyDir(srcPath + "/common", dstPath + "/common"))
promptError(LanguageUtils::gettext("Common save not found."));
}
srcPath.append(StringUtils::stringFormat("/%s", sdacc[sdusers].persistentID));
dstPath.append(StringUtils::stringFormat("/%s", wiiuacc[allusers].persistentID));
FSAMakeQuota(handle, newlibtoFSA(dstPath).c_str(), 0x666, title->accountSaveSize);
} else {
FSAMakeQuotaFromDir(srcPath.c_str(), dstPath.c_str(), title->accountSaveSize);
}

if (!copyDir(srcPath, dstPath))
promptError(LanguageUtils::gettext("Restore failed."));


if (!title->saveInit && !isWii) {
std::string userPath = StringUtils::stringFormat("%s/%08x/%08x/user", path.c_str(), highID, lowID);

Expand Down
Loading
Loading