From b03633e0108d587e2bca36186cd6d43742df7179 Mon Sep 17 00:00:00 2001 From: skymz4 <99190689+skymz4@users.noreply.github.com> Date: Sat, 16 Apr 2022 16:02:44 +0200 Subject: [PATCH] ___IMPLEMENT_FS2__INSPIERED_BY_3DSHELL__ --- renderd7.cpp | 122 +++++++++++++++++++++++++++++++++++++++++++++++++++ renderd7.hpp | 30 ++++++++++--- 2 files changed, 146 insertions(+), 6 deletions(-) diff --git a/renderd7.cpp b/renderd7.cpp index 1e9bb49..d6a208a 100644 --- a/renderd7.cpp +++ b/renderd7.cpp @@ -2,6 +2,8 @@ #include "log.hpp" #include +FS_Archive archive, sdmc_archive, nand_archive; + #define RGBA8(r, g, b, a) ((((r) & 0xFF) << 0) | (((g) & 0xFF) << 8) | (((b) & 0xFF) << 16) | (((a) & 0xFF) << 24)) #define D7_NOTHING C2D_Color32(0, 0, 0, 0) #define CFGVER "1" @@ -1055,3 +1057,123 @@ bool RenderD7::Console::Update() return dr_sc; } */ +//FS2 +Result RenderD7::FS2::OpenArchive(FS_Archive *archive, FS_ArchiveID id) +{ + Result ret = 0; + if (R_FAILED(ret = FSUSER_OpenArchive(archive, id, fsMakePath(PATH_EMPTY, "")))) + return ret; + + return 0; +} + +Result RenderD7::FS2::CloseArchive(FS_Archive archive) +{ + Result ret = 0; + + if (R_FAILED(ret = FSUSER_CloseArchive(archive))) + return ret; + + return 0; +} + +bool RenderD7::FS2::DirExists(FS_Archive archive, const std::string &path) +{ + Handle handle; + std::u16string path_u16 = std::wstring_convert, char16_t>{}.from_bytes(path.data()); + if (R_FAILED(FSUSER_OpenDirectory(&handle, archive, fsMakePath(PATH_UTF16, path_u16.c_str())))) + return false; + + if (R_FAILED(FSDIR_Close(handle))) + return false; + return true; +} + +u64 RenderD7::FS2::GetTotalStorage(FS_SystemMediaType mediatype) { + Result ret = 0; + FS_ArchiveResource resource = { 0 }; + if (R_FAILED(ret = FSUSER_GetArchiveResource(&resource, mediatype))) + { + + return ret; + } + return (static_cast(resource.totalClusters) * static_cast(resource.clusterSize)); +} + +u64 RenderD7::FS2::GetUsedStorage(FS_SystemMediaType mediatype) +{ + Result ret = 0; + FS_ArchiveResource resource = { 0 }; + if (R_FAILED(ret = FSUSER_GetArchiveResource(&resource, mediatype))) + { + + return ret; + } + return ((static_cast(resource.totalClusters) * static_cast(resource.clusterSize)) - + (static_cast(resource.freeClusters) * static_cast(resource.clusterSize))); +} + +static bool Sort(const FS_DirectoryEntry &entryA, const FS_DirectoryEntry &entryB) { + if ((entryA.attributes & FS_ATTRIBUTE_DIRECTORY) && !(entryB.attributes & FS_ATTRIBUTE_DIRECTORY)) + return true; + else if (!(entryA.attributes & FS_ATTRIBUTE_DIRECTORY) && (entryB.attributes & FS_ATTRIBUTE_DIRECTORY)) + return false; + else { + std::u16string entryA_name = reinterpret_cast(entryA.name); + std::u16string entryB_name = reinterpret_cast(entryB.name); + std::transform(entryA_name.begin(), entryA_name.end(), entryA_name.begin(), [](unsigned char c){ return std::tolower(c); }); + std::transform(entryB_name.begin(), entryB_name.end(), entryB_name.begin(), [](unsigned char c){ return std::tolower(c); }); + + if (entryA_name.compare(entryB_name) < 0) + return true; + break; + } + return false; + } + +Result RenderD7::FS2::GetDirList(const std::string &path, std::vector &entries) +{ + if (!entries.empty()) + entries.clear(); + Result ret = 0; + Handle dir = 0; + std::u16string path_u16 = std::wstring_convert, char16_t>{}.from_bytes(path.data()); + if (R_FAILED(ret = FSUSER_OpenDirectory(&dir, archive, fsMakePath(PATH_UTF16, path_u16.c_str())))) { + + return ret; + } + u32 entry_count = 0; + do { + FS_DirectoryEntry entry; + if (R_FAILED(ret = FSDIR_Read(dir, &entry_count, 1, &entry))) { + + return ret; + } + if (entry_count == 1) + entries.push_back(entry); + } while(entry_count > 0); + std::sort(entries.begin(), entries.end(), Sort); + if (R_FAILED(ret = FSDIR_Close(dir))) { + + return ret; + } + return 0; +} + +void RenderD7::FS2::Convert(std::vector &entries, std::vector &converted) +{ + RenderD7::DirContent temp; + for (int i = 0; i < (int)entries.size(); i++) + { + const std::u16string entry_name_utf16 = reinterpret_cast(entries[i].name); + const std::string filename = std::wstring_convert, char16_t>{}.to_bytes(entry_name_utf16.data()); + temp.name = filename; + if ((entryA.attributes & FS_ATTRIBUTE_DIRECTORY)) temp.isDir = true; + converted.push_back(temp); + } +} + +void RenderD7::FS2::GetContents(std::string path, std::vector &converted) +{ + +} \ No newline at end of file diff --git a/renderd7.hpp b/renderd7.hpp index 20707d4..4e1fbdd 100644 --- a/renderd7.hpp +++ b/renderd7.hpp @@ -16,6 +16,8 @@ #include #include #include +#include +#include #include "external/lodepng.h" #include "external/fs.h" #include @@ -26,6 +28,8 @@ #include "stringtool.hpp" #include "Clock.hpp" +extern FS_Archive archive, sdmc_archive, nand_archive; + #define RENDERD7VSTRING "0.7.0" #define CHANGELOG "0.6.2: \n0.6.10: rewrite Threadsystem, Improve framerate\n0.6.02: Fix Code in lang.hpp\nadd Draw Text Left Function.\nadd changelog\n0.6.01: add Threading system." #define DEFAULT_CENTER 0.5f @@ -227,10 +231,29 @@ namespace RenderD7 inline int StringtoInt(std::string inp){return std::atoi(inp.c_str());} inline bool FloatToBool(float inp){if(inp == 1)return true; else return false;} } + + struct DirContent + { + std::string name; + std::string path; + bool isDir; + }; + namespace FS { bool FileExist(const std::string& path); } + namespace FS2 + { + Result OpenArchive(FS_Archive *archive, FS_ArchiveID id); + Result CloseArchive(FS_Archive archive); + bool DirExists(FS_Archive archive, const std::string &path); + u64 GetTotalStorage(FS_SystemMediaType mediatype); + u64 GetUsedStorage(FS_SystemMediaType mediatype); + Result GetDirList(const std::string &path, std::vector &entries); + void Convert(std::vector &entries, std::vector &converted); + void GetContents(std::string path, std::vector &converted); + } bool IsNdspInit(); void SetupLog(void); std::string GetFramerate(); @@ -303,12 +326,7 @@ namespace RenderD7 void DrawSTObject(std::vector tobject, int tobjectindex, u32 color, u32 txtcolor); bool touchTObj(touchPosition touch, RenderD7::TObject button); void DrawTLBtns(std::vector btns, u32 color, int selection = -1, u32 selbgcolor = RenderD7::Color::Hex("#2D98AF"), u32 selcolor = RenderD7::Color::Hex("#000000")); - struct DirContent - { - std::string name; - std::string path; - bool isDir; - }; + struct Checkbox { float x, y, s;