unifork.old/source/utils/fileBrowse.cpp
VoltZ 715c09179b Implement Script Downloading and parsing of the Informations.
To-Do: Revision checking of the current and new Scripts.
2019-11-06 13:54:45 +01:00

174 lines
No EOL
4.7 KiB
C++

#include "utils/fileBrowse.h"
#include "gui.hpp"
#include <3ds.h>
#include <cstring>
#include <dirent.h>
#include <sys/stat.h>
#include <unistd.h>
#include <vector>
#include <algorithm>
#include <functional>
#include <array>
#include <iostream>
int file_count = 0;
extern bool continueNdsScan;
extern uint selectedFile;
extern int keyRepeatDelay;
extern bool dirChanged;
extern std::vector<DirEntry> dirContents;
/**
* Get the title ID.
* @param ndsFile DS ROM image.
* @param buf Output buffer for title ID. (Must be at least 4 characters.)
* @return 0 on success; non-zero on error.
*/
int grabTID(FILE *ndsFile, char *buf) {
fseek(ndsFile, offsetof(sNDSHeadertitlecodeonly, gameCode), SEEK_SET);
size_t read = fread(buf, 1, 4, ndsFile);
return !(read == 4);
}
void findNdsFiles(vector<DirEntry>& dirContents) {
struct stat st;
DIR *pdir = opendir(".");
if (pdir == NULL) {
DisplayMsg("Unable to open the directory.");
for(int i=0;i<120;i++)
gspWaitForVBlank();
} else {
while (continueNdsScan)
{
DirEntry dirEntry;
struct dirent* pent = readdir(pdir);
if (pent == NULL) break;
stat(pent->d_name, &st);
dirEntry.name = pent->d_name;
char scanningMessage[512];
snprintf(scanningMessage, sizeof(scanningMessage), "Scanning SD card for DS roms...\n\n(Press B to cancel)\n\n\n\n\n\n\n\n\n%s", dirEntry.name.c_str());
DisplayMsg(scanningMessage);
dirEntry.isDirectory = (st.st_mode & S_IFDIR) ? true : false;
if(!(dirEntry.isDirectory) && dirEntry.name.length() >= 3) {
if (strcasecmp(dirEntry.name.substr(dirEntry.name.length()-3, 3).c_str(), "nds") == 0) {
// Get game's TID
FILE *f_nds_file = fopen(dirEntry.name.c_str(), "rb");
// char game_TID[5];
grabTID(f_nds_file, dirEntry.tid);
dirEntry.tid[4] = 0;
fclose(f_nds_file);
// dirEntry.tid = game_TID;
dirContents.push_back(dirEntry);
file_count++;
}
} else if (dirEntry.isDirectory
&& dirEntry.name.compare(".") != 0
&& dirEntry.name.compare("_nds") != 0
&& dirEntry.name.compare("3ds") != 0
&& dirEntry.name.compare("DCIM") != 0
&& dirEntry.name.compare("gm9") != 0
&& dirEntry.name.compare("luma") != 0
&& dirEntry.name.compare("Nintendo 3DS") != 0
&& dirEntry.name.compare("private") != 0
&& dirEntry.name.compare("retroarch") != 0) {
chdir(dirEntry.name.c_str());
findNdsFiles(dirContents);
chdir("..");
}
}
closedir(pdir);
}
}
off_t getFileSize(const char *fileName)
{
FILE* fp = fopen(fileName, "rb");
off_t fsize = 0;
if (fp) {
fseek(fp, 0, SEEK_END);
fsize = ftell(fp); // Get source file's size
fseek(fp, 0, SEEK_SET);
}
fclose(fp);
return fsize;
}
bool nameEndsWith(const std::string& name, const std::vector<std::string> extensionList) {
if(name.substr(0, 2) == "._") return false;
if(name.size() == 0) return false;
if(extensionList.size() == 0) return true;
for(int i = 0; i <(int)extensionList.size(); i++) {
const std::string ext = extensionList.at(i);
if(strcasecmp(name.c_str() + name.size() - ext.size(), ext.c_str()) == 0) return true;
}
return false;
}
bool dirEntryPredicate(const DirEntry& lhs, const DirEntry& rhs) {
if(!lhs.isDirectory && rhs.isDirectory) {
return false;
}
if(lhs.isDirectory && !rhs.isDirectory) {
return true;
}
return strcasecmp(lhs.name.c_str(), rhs.name.c_str()) < 0;
}
void getDirectoryContents(std::vector<DirEntry>& dirContents, const std::vector<std::string> extensionList) {
struct stat st;
dirContents.clear();
DIR *pdir = opendir(".");
if(pdir == NULL) {
DisplayMsg("Unable to open the directory.");
for(int i=0;i<120;i++) gspWaitForVBlank();
} else {
while(true) {
DirEntry dirEntry;
struct dirent* pent = readdir(pdir);
if(pent == NULL) break;
stat(pent->d_name, &st);
dirEntry.name = pent->d_name;
dirEntry.isDirectory = (st.st_mode & S_IFDIR) ? true : false;
if(dirEntry.name.compare(".") != 0 && (dirEntry.isDirectory || nameEndsWith(dirEntry.name, extensionList))) {
dirContents.push_back(dirEntry);
}
}
closedir(pdir);
}
sort(dirContents.begin(), dirContents.end(), dirEntryPredicate);
}
void getDirectoryContents(std::vector<DirEntry>& dirContents) {
getDirectoryContents(dirContents, {});
}
std::vector<std::string> getContents(const std::string &name, const std::vector<std::string> &extensionList) {
std::vector<std::string> dirContents;
DIR* pdir = opendir(name.c_str());
struct dirent *pent;
while ((pent = readdir(pdir)) != NULL) {
if(nameEndsWith(pent->d_name, extensionList))
dirContents.push_back(pent->d_name);
}
closedir(pdir);
return dirContents;
}