I think the last missing work for v1.0.0? :)

This commit is contained in:
VoltZ
2019-11-03 03:09:43 +01:00
committed by GitHub
parent 1246809b53
commit 7b16ed5c77
9 changed files with 119 additions and 20 deletions
+1
View File
@@ -70,6 +70,7 @@ namespace Gui
void DrawBottom(void); void DrawBottom(void);
void DisplayWarnMsg(std::string Text); void DisplayWarnMsg(std::string Text);
bool promptMsg(std::string promptMsg);
} }
void DisplayMsg(std::string text); void DisplayMsg(std::string text);
+1
View File
@@ -56,4 +56,5 @@ private:
int fastMode = false; int fastMode = false;
}; };
Result createFile(const char * path);
#endif #endif
+1 -1
View File
@@ -58,5 +58,5 @@ using json = nlohmann::json;
extern char * arg0; extern char * arg0;
#define WORKING_DIR "/3ds/" #define WORKING_DIR "/"
#define SCRIPTS_PATH "/3ds/Universal-Updater/scripts/" // The Scripts will be here. #define SCRIPTS_PATH "/3ds/Universal-Updater/scripts/" // The Scripts will be here.
+3
View File
@@ -28,5 +28,8 @@
#include "common.hpp" #include "common.hpp"
Result makeDirs(const char * path);
Result openFile(Handle* fileHandle, const char * path, bool write); Result openFile(Handle* fileHandle, const char * path, bool write);
Result deleteFile(const char * path); Result deleteFile(const char * path);
Result removeDir(const char * path);
Result removeDirRecursive(const char * path);
+4 -1
View File
@@ -27,5 +27,8 @@
"ENTER_BLUE_RGB": "Enter the Blue RGB.", "ENTER_BLUE_RGB": "Enter the Blue RGB.",
"WHAT_DO_YOU_TRY": "What are you trying to do? :P", "WHAT_DO_YOU_TRY": "What are you trying to do? :P",
"INCOMPATIBLE_SCRIPT": "You have an incompatible script." "INCOMPATIBLE_SCRIPT": "You have an incompatible script.",
"DELETE_PROMPT": "Are you sure you want to delete this Directory?",
"CONFIRM_OR_CANCEL": "Press A to confirm, B to cancel."
} }
+24
View File
@@ -174,3 +174,27 @@ void Gui::DrawBottom(void) {
Gui::Draw_Rect(0, 30, 320, 180, Config::Color3); Gui::Draw_Rect(0, 30, 320, 180, Config::Color3);
Gui::Draw_Rect(0, 210, 320, 30, Config::Color1); Gui::Draw_Rect(0, 210, 320, 30, Config::Color1);
} }
// Display a Message, which needs to be confirmed with A/B.
bool Gui::promptMsg(std::string promptMsg)
{
Gui::clearTextBufs();
C3D_FrameBegin(C3D_FRAME_SYNCDRAW);
C2D_TargetClear(top, BLACK);
C2D_TargetClear(bottom, BLACK);
Gui::DrawTop();
Gui::DrawString((400-Gui::GetStringWidth(0.6f, promptMsg.c_str()))/2, 100, 0.6f, Config::TxtColor, promptMsg.c_str(), 400);
Gui::DrawString((400-Gui::GetStringWidth(0.72f, Lang::get("CONFIRM_OR_CANCEL")))/2, 214, 0.72f, Config::TxtColor, Lang::get("CONFIRM_OR_CANCEL"), 400);
Gui::DrawBottom();
C3D_FrameEnd(0);
while(1)
{
gspWaitForVBlank();
hidScanInput();
if(hidKeysDown() & KEY_A) {
return true;
} else if(hidKeysDown() & KEY_B) {
return false;
}
}
}
-1
View File
@@ -68,7 +68,6 @@ int main()
Config::saveConfig(); Config::saveConfig();
} }
Lang::load(1); Lang::load(1);
Gui::setScreen(std::make_unique<MainMenu>()); Gui::setScreen(std::make_unique<MainMenu>());
osSetSpeedupEnable(true); // Enable speed-up for New 3DS users osSetSpeedupEnable(true); // Enable speed-up for New 3DS users
+39 -6
View File
@@ -45,6 +45,7 @@ struct Info {
}; };
std::string choice; std::string choice;
std::string currentFile; std::string currentFile;
std::string selectedTitle;
Info parseInfo(std::string fileName) { Info parseInfo(std::string fileName) {
FILE* file = fopen(fileName.c_str(), "rt"); FILE* file = fopen(fileName.c_str(), "rt");
@@ -95,6 +96,14 @@ std::vector<std::string> parseObjects(std::string fileName) {
return objs; return objs;
} }
// Because we need `#include <fstream>`.
Result createFile(const char * path) {
std::ofstream ofstream;
ofstream.open(path, std::ofstream::out | std::ofstream::app);
ofstream.close();
return 0;
}
void runFunctions(void) { void runFunctions(void) {
FILE* file = fopen(currentFile.c_str(), "rt"); FILE* file = fopen(currentFile.c_str(), "rt");
nlohmann::json json = nlohmann::json::parse(file, nullptr, false); nlohmann::json json = nlohmann::json::parse(file, nullptr, false);
@@ -151,6 +160,32 @@ void runFunctions(void) {
else missing = true; else missing = true;
if(json.at(choice).at(i).contains("message")) message = json.at(choice).at(i).at("message"); if(json.at(choice).at(i).contains("message")) message = json.at(choice).at(i).at("message");
if(!missing) download::installFileList(file, message); if(!missing) download::installFileList(file, message);
} else if (type == "mkdir") {
bool missing = false;
std::string directory, message;
if(json.at(choice).at(i).contains("directory")) directory = json.at(choice).at(i).at("directory");
else missing = true;
if(!missing) makeDirs(directory.c_str());
} else if (type == "rmdir") {
bool missing = false;
std::string directory, message, promptmsg;
if(json.at(choice).at(i).contains("directory")) directory = json.at(choice).at(i).at("directory");
else missing = true;
promptmsg = Lang::get("DELETE_PROMPT") + "\n" + directory;
if(!missing) {
if (Gui::promptMsg(promptmsg)) {
removeDirRecursive(directory.c_str());
}
}
} else if (type == "mkfile") {
bool missing = false;
std::string file;
if(json.at(choice).at(i).contains("file")) file = json.at(choice).at(i).at("file");
else missing = true;
if(!missing) createFile(file.c_str());
} }
} }
doneMsg(); doneMsg();
@@ -201,6 +236,7 @@ void ScriptList::DrawSingleObject(void) const {
std::string info; std::string info;
Gui::DrawTop(); Gui::DrawTop();
Gui::DrawStringCentered(0, 2, 0.7f, Config::TxtColor, "Universal-Updater", 400); Gui::DrawStringCentered(0, 2, 0.7f, Config::TxtColor, "Universal-Updater", 400);
Gui::DrawStringCentered(0, 214, 0.7f, Config::TxtColor, selectedTitle, 400);
Gui::DrawBottom(); Gui::DrawBottom();
for(int i=0;i<ENTRIES_PER_SCREEN && i<(int)fileInfo2.size();i++) { for(int i=0;i<ENTRIES_PER_SCREEN && i<(int)fileInfo2.size();i++) {
info = fileInfo2[screenPos2 + i]; info = fileInfo2[screenPos2 + i];
@@ -248,10 +284,9 @@ void ScriptList::ListSelection(u32 hDown, u32 hHeld) {
} }
if (hDown & KEY_A) { if (hDown & KEY_A) {
if (fileInfo.size() == 0) { if (fileInfo.size() != 0) {
Gui::DisplayWarnMsg(Lang::get("WHAT_DO_YOU_TRY"));
} else {
currentFile = dirContents[selection].name; currentFile = dirContents[selection].name;
selectedTitle = fileInfo[selection].title;
checkForValidate(); checkForValidate();
fileInfo2 = parseObjects(currentFile); fileInfo2 = parseObjects(currentFile);
selection = 0; selection = 0;
@@ -293,9 +328,7 @@ void ScriptList::SelectFunction(u32 hDown, u32 hHeld) {
} }
} }
if (hDown & KEY_A) { if (hDown & KEY_A) {
if (fileInfo2.size() == 0) { if (fileInfo2.size() != 0) {
Gui::DisplayWarnMsg(Lang::get("WHAT_DO_YOU_TRY"));
} else {
choice = fileInfo2[selection2]; choice = fileInfo2[selection2];
runFunctions(); runFunctions();
} }
+45 -10
View File
@@ -1,12 +1,22 @@
#include "utils/files.h" #include "utils/files.h"
// Blacklist!
#define BLACKLIST_CTRNAND "ctrnand:/"
#define BLACKLIST_TWLP "twlp:/"
#define BLACKLIST_TWLN "twln:/"
#define BLACKLIST_SDROOT "sdmc:/"
#define BLACKLIST_ROOT "/"
#define BLACKLIST_NINTENDO3DS "sdmc:/Nintendo 3DS"
#define BLACKLIST_NINTENDO3DS2 "/Nintendo"
FS_Path getPathInfo(const char * path, FS_ArchiveID * archive) FS_Path getPathInfo(const char * path, FS_ArchiveID * archive)
{ {
*archive = ARCHIVE_SDMC; *archive = ARCHIVE_SDMC;
FS_Path filePath = {0}; FS_Path filePath = {0};
unsigned int prefixlen = 0; unsigned int prefixlen = 0;
if (!strncmp(path, "ctrnand:/", 9)) { /* if (!strncmp(path, "ctrnand:/", 9)) {
*archive = ARCHIVE_NAND_CTR_FS; *archive = ARCHIVE_NAND_CTR_FS;
prefixlen = 8; prefixlen = 8;
} }
@@ -17,8 +27,8 @@ FS_Path getPathInfo(const char * path, FS_ArchiveID * archive)
else if (!strncmp(path, "twln:/", 6)) { else if (!strncmp(path, "twln:/", 6)) {
*archive = ARCHIVE_NAND_TWL_FS; *archive = ARCHIVE_NAND_TWL_FS;
prefixlen = 5; prefixlen = 5;
} }*/
else if (!strncmp(path, "sdmc:/", 6)) { if (!strncmp(path, "sdmc:/", 6)) {
prefixlen = 5; prefixlen = 5;
} }
else if (*path != '/') { else if (*path != '/') {
@@ -36,9 +46,11 @@ FS_Path getPathInfo(const char * path, FS_ArchiveID * archive)
return filePath; return filePath;
} }
Result makeDirs(FS_ArchiveID archiveID, char * path) Result makeDirs(const char * path)
{ {
Result ret = 0; Result ret = 0;
FS_ArchiveID archiveID;
FS_Path filePath = getPathInfo(path, &archiveID);
FS_Archive archive; FS_Archive archive;
ret = FSUSER_OpenArchive(&archive, archiveID, fsMakePath(PATH_EMPTY, "")); ret = FSUSER_OpenArchive(&archive, archiveID, fsMakePath(PATH_EMPTY, ""));
@@ -46,21 +58,18 @@ Result makeDirs(FS_ArchiveID archiveID, char * path)
for (char * slashpos = strchr(path+1, '/'); slashpos != NULL; slashpos = strchr(slashpos+1, '/')) { for (char * slashpos = strchr(path+1, '/'); slashpos != NULL; slashpos = strchr(slashpos+1, '/')) {
char bak = *(slashpos); char bak = *(slashpos);
*(slashpos) = '\0'; *(slashpos) = '\0';
FS_Path dirpath = fsMakePath(PATH_ASCII, path);
Handle dirHandle; Handle dirHandle;
ret = FSUSER_OpenDirectory(&dirHandle, archive, dirpath); ret = FSUSER_OpenDirectory(&dirHandle, archive, filePath);
if (R_SUCCEEDED(ret)) if (R_SUCCEEDED(ret))
FSDIR_Close(dirHandle); FSDIR_Close(dirHandle);
else else
ret = FSUSER_CreateDirectory(archive, dirpath, FS_ATTRIBUTE_DIRECTORY); ret = FSUSER_CreateDirectory(archive, filePath, FS_ATTRIBUTE_DIRECTORY);
*(slashpos) = bak; *(slashpos) = bak;
} }
FSUSER_CloseArchive(archive); FSUSER_CloseArchive(archive);
free(path);
return ret; return ret;
} }
@@ -72,7 +81,7 @@ Result openFile(Handle* fileHandle, const char * path, bool write)
u32 flags = (write ? (FS_OPEN_CREATE | FS_OPEN_WRITE) : FS_OPEN_READ); u32 flags = (write ? (FS_OPEN_CREATE | FS_OPEN_WRITE) : FS_OPEN_READ);
Result ret = 0; Result ret = 0;
ret = makeDirs(archive, strdup(path)); ret = makeDirs(strdup(path));
ret = FSUSER_OpenFileDirectly(fileHandle, archive, fsMakePath(PATH_EMPTY, ""), filePath, flags, 0); ret = FSUSER_OpenFileDirectly(fileHandle, archive, fsMakePath(PATH_EMPTY, ""), filePath, flags, 0);
if (write) if (write)
ret = FSFILE_SetSize(*fileHandle, 0); //truncate the file to remove previous contents before writing ret = FSFILE_SetSize(*fileHandle, 0); //truncate the file to remove previous contents before writing
@@ -93,3 +102,29 @@ Result deleteFile(const char * path)
return ret; return ret;
} }
Result removeDir(const char *path) {
FS_ArchiveID archiveID;
FS_Path filePath = getPathInfo(path, &archiveID);
FS_Archive archive;
Result ret = FSUSER_OpenArchive(&archive, archiveID, fsMakePath(PATH_EMPTY, ""));
if (R_FAILED(ret)) return ret;
ret = FSUSER_DeleteDirectory(archive, filePath);
FSUSER_CloseArchive(archive);
return ret;
}
Result removeDirRecursive(const char *path) {
FS_ArchiveID archiveID;
FS_Path filePath = getPathInfo(path, &archiveID);
FS_Archive archive;
Result ret = FSUSER_OpenArchive(&archive, archiveID, fsMakePath(PATH_EMPTY, ""));
if (R_FAILED(ret)) return ret;
ret = FSUSER_DeleteDirectoryRecursively(archive, filePath);
FSUSER_CloseArchive(archive);
return ret;
}