Skip to content

Commit

Permalink
Merge pull request #60 from w3irDv/fix/commonsaves
Browse files Browse the repository at this point in the history
fix for common savedata not detected and for unnecessary folders created during restore
  • Loading branch information
w3irDv authored Aug 22, 2024
2 parents 2ee1332 + 093dcc4 commit 9aa4171
Show file tree
Hide file tree
Showing 11 changed files with 90 additions and 80 deletions.
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
7 changes: 2 additions & 5 deletions include/utils/DrawUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ class DrawUtils {

static void LogConsoleFree();

static void initBuffers(void *tvBuffer, void *drcBuffer);

static void beginDraw();

static void endDraw();
Expand Down Expand Up @@ -77,9 +75,9 @@ class DrawUtils {

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

static uint32_t ConsoleProcCallbackAcquired(void *context);
static uint32_t initScreen();

static uint32_t ConsoleProcCallbackReleased(void *context);
static uint32_t deinitScreen();



Expand All @@ -92,7 +90,6 @@ class DrawUtils {
static uint8_t *tvBuffer;
static uint8_t *drcBuffer;

static void *sBufferTV, *sBufferDRC;
static uint32_t sBufferSizeTV, sBufferSizeDRC;
static BOOL sConsoleHasForeground;
};
4 changes: 4 additions & 0 deletions include/utils/StateUtils.h
Original file line number Diff line number Diff line change
@@ -1,11 +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
9 changes: 6 additions & 3 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 @@ -394,6 +395,8 @@ int main() {
OSFatal("Failed to initialize OSSCreen");
}

State::registerProcUICallbacks();

if (!DrawUtils::initFont()) {
OSFatal("Failed to init font");
}
Expand Down
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
71 changes: 23 additions & 48 deletions src/utils/DrawUtils.cpp
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
#include <coreinit/cache.h>
#include <coreinit/screen.h>
#include <utils/DrawUtils.h>
#include <utils/Colors.h>
#include <cstdlib>
#include <cstring>
#include <memory>
#include <tga_reader.h>
#include <utils/DrawUtils.h>
#include <utils/StateUtils.h>
#include <malloc.h>
#include <tga_reader.h>
#include <coreinit/debug.h>

#include <coreinit/memheap.h>
#include <coreinit/cache.h>
#include <coreinit/screen.h>
#include <coreinit/memheap.h>
#include <coreinit/memfrmheap.h>
#include <coreinit/memory.h>
#include <proc_ui/procui.h>
Expand All @@ -26,62 +24,50 @@ bool DrawUtils::isBackBuffer;

uint8_t *DrawUtils::tvBuffer = nullptr;
uint8_t *DrawUtils::drcBuffer = nullptr;
/*
uint32_t DrawUtils::sBufferSizeTV = 0;
uint32_t DrawUtils::sBufferSizeDRC = 0;
*/
uint32_t DrawUtils::sBufferSizeTV = 0, DrawUtils::sBufferSizeDRC = 0;
BOOL DrawUtils::sConsoleHasForeground = TRUE;

static SFT pFont = {};

static Color font_col(0xFFFFFFFF);
static Color black(0x000000FF);

void *DrawUtils::sBufferTV = NULL, *DrawUtils::sBufferDRC = NULL;
uint32_t DrawUtils::sBufferSizeTV = 0, DrawUtils::sBufferSizeDRC = 0;
BOOL DrawUtils::sConsoleHasForeground = TRUE;

uint32_t
DrawUtils::ConsoleProcCallbackAcquired(void *context)
DrawUtils::initScreen()
{
MEMHeapHandle heap = MEMGetBaseHeapHandle(MEM_BASE_HEAP_MEM1);
MEMRecordStateForFrmHeap(heap, CONSOLE_FRAME_HEAP_TAG);

if (sBufferSizeTV) {
sBufferTV = MEMAllocFromFrmHeapEx(heap, sBufferSizeTV, 4);
DrawUtils::tvBuffer = static_cast<uint8_t *>(MEMAllocFromFrmHeapEx(heap, sBufferSizeTV, 4));
}

if (sBufferSizeDRC) {
sBufferDRC = MEMAllocFromFrmHeapEx(heap, sBufferSizeDRC, 4);
DrawUtils::drcBuffer = static_cast<uint8_t *>(MEMAllocFromFrmHeapEx(heap, sBufferSizeDRC, 4));
}


sConsoleHasForeground = TRUE;

OSScreenSetBufferEx(SCREEN_TV, sBufferTV);
OSScreenSetBufferEx(SCREEN_DRC, sBufferDRC);
DrawUtils::initBuffers(sBufferTV, sBufferDRC);
OSScreenSetBufferEx(SCREEN_TV, DrawUtils::tvBuffer);
OSScreenSetBufferEx(SCREEN_DRC, DrawUtils::drcBuffer);

OSScreenEnableEx(SCREEN_TV, 1);
OSScreenEnableEx(SCREEN_DRC, 1);


for (int i = 0; i<2; i++) // both buffers to black
{
DrawUtils::clear(black);
DrawUtils::endDraw();
}
DrawUtils::endDraw(); // flip buffers

DrawUtils::setRedraw(true); // force a redraw when reentering

return 0;

}

uint32_t
DrawUtils::ConsoleProcCallbackReleased(void *context)
DrawUtils::deinitScreen()
{

MEMHeapHandle heap = MEMGetBaseHeapHandle(MEM_BASE_HEAP_MEM1);
MEMFreeByStateToFrmHeap(heap, CONSOLE_FRAME_HEAP_TAG);
sConsoleHasForeground = FALSE;
return 0;

}


Expand All @@ -92,9 +78,10 @@ DrawUtils::LogConsoleInit()
sBufferSizeTV = OSScreenGetBufferSizeEx(SCREEN_TV);
sBufferSizeDRC = OSScreenGetBufferSizeEx(SCREEN_DRC);

ConsoleProcCallbackAcquired(NULL);

State::registerProcUICallbacks();
initScreen();

OSScreenEnableEx(SCREEN_TV, 1);
OSScreenEnableEx(SCREEN_DRC, 1);

return FALSE;
}
Expand All @@ -103,8 +90,8 @@ void
DrawUtils::LogConsoleFree()
{
if (sConsoleHasForeground) {
deinitScreen();
OSScreenShutdown();
ConsoleProcCallbackReleased(NULL);
}
}

Expand All @@ -120,11 +107,6 @@ void DrawUtils::setRedraw(bool value) {
redraw = value;
}

void DrawUtils::initBuffers(void *sBufferTV_, void *sBufferDRC_) {
DrawUtils::tvBuffer = (uint8_t *) sBufferTV_;
DrawUtils::drcBuffer = (uint8_t *) sBufferDRC_;
}

void DrawUtils::beginDraw() {
uint32_t pixel = *(uint32_t *) tvBuffer;

Expand All @@ -141,13 +123,6 @@ void DrawUtils::beginDraw() {
}

void DrawUtils::endDraw() {
// OSScreenFlipBuffersEx already flushes the cache?
// DCFlushRange(tvBuffer, sBufferSizeTV);
// DCFlushRange(drcBuffer, sBufferSizeDRC);

DCFlushRange(sBufferTV, sBufferSizeTV);
DCFlushRange(sBufferDRC, sBufferSizeDRC);

OSScreenFlipBuffersEx(SCREEN_DRC);
OSScreenFlipBuffersEx(SCREEN_TV);
}
Expand Down
Loading

0 comments on commit 9aa4171

Please sign in to comment.