mirror of
https://github.com/rvtr/GodMode9i.git
synced 2025-11-02 00:11:07 -04:00
Speed up directory loading
Doesn't load the file size until displaying it which makes it *way* faster
This commit is contained in:
parent
9ec144f4ea
commit
162700115d
@ -51,8 +51,8 @@
|
|||||||
|
|
||||||
static char path[PATH_MAX];
|
static char path[PATH_MAX];
|
||||||
|
|
||||||
bool extension(const std::string &filename, const std::vector<std::string> &extensions) {
|
bool extension(const std::string_view filename, const std::vector<std::string_view> &extensions) {
|
||||||
for(const std::string &ext : extensions) {
|
for(const std::string_view &ext : extensions) {
|
||||||
if(filename.length() > ext.length() && strcasecmp(filename.substr(filename.length() - ext.length()).data(), ext.data()) == 0)
|
if(filename.length() > ext.length() && strcasecmp(filename.substr(filename.length() - ext.length()).data(), ext.data()) == 0)
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@ -77,58 +77,41 @@ bool dirEntryPredicate (const DirEntry& lhs, const DirEntry& rhs) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void getDirectoryContents(std::vector<DirEntry>& dirContents) {
|
void getDirectoryContents(std::vector<DirEntry>& dirContents) {
|
||||||
struct stat st;
|
|
||||||
|
|
||||||
dirContents.clear();
|
dirContents.clear();
|
||||||
|
|
||||||
DIR *pdir = opendir (".");
|
DIR *pdir = opendir (".");
|
||||||
|
|
||||||
if (pdir == NULL) {
|
if (pdir == nullptr) {
|
||||||
font->print(0, 0, true, "Unable to open the directory.");
|
font->print(0, 0, true, "Unable to open the directory.");
|
||||||
font->update(true);
|
font->update(true);
|
||||||
} else {
|
} else {
|
||||||
|
while (true) {
|
||||||
|
dirent *pent = readdir(pdir);
|
||||||
|
if (pent == nullptr)
|
||||||
|
break;
|
||||||
|
|
||||||
while(true) {
|
if (strcmp(pent->d_name, ".") == 0 || strcmp(pent->d_name, "..") == 0)
|
||||||
DirEntry dirEntry;
|
continue;
|
||||||
|
|
||||||
struct dirent* pent = readdir(pdir);
|
bool isApp = false;
|
||||||
if(pent == NULL) break;
|
if (extension(pent->d_name, {"nds", "argv", "dsi", "ids", "app", "srl"})) {
|
||||||
|
isApp = (currentDrive == 0 && sdMounted) || (currentDrive == 1 && flashcardMounted);
|
||||||
stat(pent->d_name, &st);
|
} else if (extension(pent->d_name, {"firm"})) {
|
||||||
if (strcmp(pent->d_name, "..") != 0) {
|
isApp = (isDSiMode() && is3DS && sdMounted);
|
||||||
dirEntry.name = pent->d_name;
|
|
||||||
dirEntry.isDirectory = st.st_mode & S_IFDIR;
|
|
||||||
if (!dirEntry.isDirectory) {
|
|
||||||
dirEntry.size = getFileSize(dirEntry.name.c_str());
|
|
||||||
}
|
|
||||||
if (extension(dirEntry.name, {"nds", "argv", "dsi", "ids", "app", "srl"})) {
|
|
||||||
dirEntry.isApp = ((currentDrive == 0 && sdMounted) || (currentDrive == 1 && flashcardMounted));
|
|
||||||
} else if (extension(dirEntry.name, {"firm"})) {
|
|
||||||
dirEntry.isApp = (isDSiMode() && is3DS && sdMounted);
|
|
||||||
} else {
|
|
||||||
dirEntry.isApp = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (dirEntry.name.compare(".") != 0) {
|
|
||||||
dirContents.push_back (dirEntry);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
dirContents.emplace_back(pent->d_name, pent->d_type == DT_DIR ? 0 : -1, pent->d_type == DT_DIR, isApp);
|
||||||
}
|
}
|
||||||
|
|
||||||
closedir(pdir);
|
closedir(pdir);
|
||||||
}
|
}
|
||||||
|
|
||||||
sort(dirContents.begin(), dirContents.end(), dirEntryPredicate);
|
std::sort(dirContents.begin(), dirContents.end(), dirEntryPredicate);
|
||||||
|
|
||||||
DirEntry dirEntry;
|
// Add ".." to top of list
|
||||||
dirEntry.name = ".."; // ".." entry
|
dirContents.insert(dirContents.begin(), {"..", 0, true, false});
|
||||||
dirEntry.isDirectory = true;
|
|
||||||
dirEntry.isApp = false;
|
|
||||||
dirContents.insert (dirContents.begin(), dirEntry); // Add ".." to top of list
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void showDirectoryContents (const std::vector<DirEntry>& dirContents, int fileOffset, int startRow) {
|
void showDirectoryContents(std::vector<DirEntry> &dirContents, int fileOffset, int startRow) {
|
||||||
getcwd(path, PATH_MAX);
|
getcwd(path, PATH_MAX);
|
||||||
|
|
||||||
font->clear(true);
|
font->clear(true);
|
||||||
@ -147,7 +130,7 @@ void showDirectoryContents (const std::vector<DirEntry>& dirContents, int fileOf
|
|||||||
|
|
||||||
// Print directory listing
|
// Print directory listing
|
||||||
for (int i = 0; i < ((int)dirContents.size() - startRow) && i < ENTRIES_PER_SCREEN; i++) {
|
for (int i = 0; i < ((int)dirContents.size() - startRow) && i < ENTRIES_PER_SCREEN; i++) {
|
||||||
const DirEntry *entry = &dirContents[i + startRow];
|
DirEntry *entry = &dirContents[i + startRow];
|
||||||
|
|
||||||
Palette pal;
|
Palette pal;
|
||||||
if ((fileOffset - startRow) == i) {
|
if ((fileOffset - startRow) == i) {
|
||||||
@ -160,6 +143,10 @@ void showDirectoryContents (const std::vector<DirEntry>& dirContents, int fileOf
|
|||||||
pal = Palette::gray;
|
pal = Palette::gray;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Load size if not loaded yet
|
||||||
|
if(entry->size == -1)
|
||||||
|
entry->size = getFileSize(entry->name.c_str());
|
||||||
|
|
||||||
font->print(0, i + 1, true, entry->name.substr(0, SCREEN_COLS), Alignment::left, pal);
|
font->print(0, i + 1, true, entry->name.substr(0, SCREEN_COLS), Alignment::left, pal);
|
||||||
if (entry->name == "..") {
|
if (entry->name == "..") {
|
||||||
font->print(-1, i + 1, true, "(..)", Alignment::right, pal);
|
font->print(-1, i + 1, true, "(..)", Alignment::right, pal);
|
||||||
@ -586,6 +573,10 @@ void fileBrowse_drawBottomScreen(DirEntry* entry) {
|
|||||||
font->print(0, row--, false, "X - DELETE/[+R] RENAME file\n");
|
font->print(0, row--, false, "X - DELETE/[+R] RENAME file\n");
|
||||||
font->print(0, row--, false, titleName);
|
font->print(0, row--, false, titleName);
|
||||||
|
|
||||||
|
// Load size if not loaded yet
|
||||||
|
if(entry->size == -1)
|
||||||
|
entry->size = getFileSize(entry->name.c_str());
|
||||||
|
|
||||||
Palette pal = entry->selected ? Palette::yellow : (entry->isDirectory ? Palette::blue : Palette::gray);
|
Palette pal = entry->selected ? Palette::yellow : (entry->isDirectory ? Palette::blue : Palette::gray);
|
||||||
font->print(0, 0, false, entry->name, Alignment::left, pal);
|
font->print(0, 0, false, entry->name, Alignment::left, pal);
|
||||||
if (entry->name != "..") {
|
if (entry->name != "..") {
|
||||||
|
|||||||
@ -26,8 +26,11 @@
|
|||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
struct DirEntry {
|
struct DirEntry {
|
||||||
|
DirEntry(std::string name, size_t size, bool isDirectory, bool isApp, bool selected = false) : name(name), size(size), isDirectory(isDirectory), isApp(isApp), selected(selected) {}
|
||||||
|
DirEntry() {}
|
||||||
|
|
||||||
std::string name;
|
std::string name;
|
||||||
size_t size;
|
int size;
|
||||||
bool isDirectory;
|
bool isDirectory;
|
||||||
bool isApp;
|
bool isApp;
|
||||||
bool selected = false;
|
bool selected = false;
|
||||||
@ -50,7 +53,7 @@ enum class FileOperation {
|
|||||||
loadFont,
|
loadFont,
|
||||||
};
|
};
|
||||||
|
|
||||||
bool extension(const std::string &filename, const std::vector<std::string> &extensions);
|
bool extension(const std::string_view filename, const std::vector<std::string_view> &extensions);
|
||||||
void OnKeyPressed(int key);
|
void OnKeyPressed(int key);
|
||||||
|
|
||||||
std::string browseForFile (void);
|
std::string browseForFile (void);
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user