avoid using file stat when possible. it's slow.

This commit is contained in:
Daryl Borth 2018-08-28 14:47:21 -06:00
parent cd2094a93c
commit b2e5ba1674
7 changed files with 63 additions and 94 deletions

View File

@ -161,41 +161,40 @@ bool FindGameGenie()
if(GCSettings.LoadMethod == DEVICE_AUTO)
return false;
char * tmpbuffer = (char *) memalign(32, 512 * 1024);
if(!tmpbuffer)
return false;
AllocSaveBuffer();
size_t romSize = 0;
char filepath[1024];
sprintf (filepath, "%s%s/gg.rom", pathPrefix[GCSettings.LoadMethod], APPFOLDER);
romSize = LoadFile(tmpbuffer, filepath, 0, SILENT);
romSize = LoadFile(filepath, SILENT);
if(romSize == 0 && strlen(appPath) > 0)
{
sprintf (filepath, "%s/gg.rom", appPath);
romSize = LoadFile(tmpbuffer, filepath, 0, SILENT);
romSize = LoadFile(filepath, SILENT);
}
if (romSize > 0)
{
GENIEROM=(uint8 *)malloc(4096+1024);
if(tmpbuffer[0]==0x4E) /* iNES ROM image */
if(savebuffer[0]==0x4E) /* iNES ROM image */
{
memcpy(GENIEROM,tmpbuffer+16,4096);
memcpy(GENIEROM+4096,tmpbuffer+16400,256);
memcpy(GENIEROM,savebuffer+16,4096);
memcpy(GENIEROM+4096,savebuffer+16400,256);
}
else
{
memcpy(GENIEROM,tmpbuffer,4352);
memcpy(GENIEROM,savebuffer,4352);
}
/* Workaround for the FCE Ultra CHR page size only being 1KB */
for(int x=0; x<4; x++)
memcpy(GENIEROM+4096+(x<<8),GENIEROM+4096,256);
}
free(tmpbuffer);
FreeSaveBuffer ();
if(romSize > 0)
return true;

View File

@ -82,21 +82,21 @@ int GCMemROM(int size)
if (FDSBIOS[1] == 0)
{
size_t biosSize = 0;
char * tmpbuffer = (char *) memalign(32, 64 * 1024);
char filepath[1024];
AllocSaveBuffer ();
sprintf (filepath, "%s%s/disksys.rom", pathPrefix[GCSettings.LoadMethod], APPFOLDER);
biosSize = LoadFile(tmpbuffer, filepath, 0, SILENT);
biosSize = LoadFile(filepath, SILENT);
if(biosSize == 0 && strlen(appPath) > 0)
{
sprintf (filepath, "%s/disksys.rom", appPath);
biosSize = LoadFile(tmpbuffer, filepath, 0, SILENT);
biosSize = LoadFile(filepath, SILENT);
}
if (biosSize == 8192)
{
memcpy(FDSBIOS, tmpbuffer, 8192);
memcpy(FDSBIOS, savebuffer, 8192);
}
else
{
@ -107,7 +107,7 @@ int GCMemROM(int size)
else
ErrorPrompt("FDS BIOS file not found!");
}
free(tmpbuffer);
FreeSaveBuffer ();
}
if (FDSBIOS[1] != 0)
{

View File

@ -344,14 +344,6 @@ int FileSortCallback(const void *f1, const void *f2)
***************************************************************************/
static bool IsValidROM()
{
// file size should be between 8K and 3MB
if(browserList[browser.selIndex].length < (1024*8) ||
browserList[browser.selIndex].length > (1024*1024*4))
{
ErrorPrompt("Invalid file size!");
return false;
}
if (strlen(browserList[browser.selIndex].filename) > 4)
{
char * p = strrchr(browserList[browser.selIndex].filename, '.');
@ -477,8 +469,6 @@ int BrowserLoadFile()
if(!FindDevice(browser.dir, &device))
return 0;
GetFileSize(browser.selIndex);
// check that this is a valid ROM
if(!IsValidROM())
goto done;
@ -490,27 +480,29 @@ int BrowserLoadFile()
if(!MakeFilePath(filepath, FILE_ROM))
goto done;
filesize = LoadFile ((char *)nesrom, filepath, browserList[browser.selIndex].length, NOTSILENT);
filesize = LoadFile ((char *)nesrom, filepath, 0, (1024*1024*4), NOTSILENT);
// check nesrom for PocketNES embedded roms
const char *ext = strrchr(filepath, '.');
if (ext != NULL && strcmp(ext, ".gba") == 0)
{
const pocketnes_romheader* rom1 = pocketnes_first_rom(nesrom, filesize);
const pocketnes_romheader* rom2 = NULL;
if (rom1 != NULL) {
rom2 = pocketnes_next_rom(nesrom, filesize, rom1);
}
if (rom1 == NULL)
ErrorPrompt("No NES ROMs found in this file.");
else if (rom2 != NULL)
ErrorPrompt("More than one NES ROM found in this file. Only files with one ROM are supported.");
else
if(filesize > 0) {
// check nesrom for PocketNES embedded roms
const char *ext = strrchr(filepath, '.');
if (ext != NULL && strcmp(ext, ".gba") == 0)
{
const void* rom = rom1 + 1;
filesize = little_endian_conv_32(rom1->filesize);
memcpy(nesrom, rom, filesize);
const pocketnes_romheader* rom1 = pocketnes_first_rom(nesrom, filesize);
const pocketnes_romheader* rom2 = NULL;
if (rom1 != NULL) {
rom2 = pocketnes_next_rom(nesrom, filesize, rom1);
}
if (rom1 == NULL)
ErrorPrompt("No NES ROMs found in this file.");
else if (rom2 != NULL)
ErrorPrompt("More than one NES ROM found in this file. Only files with one ROM are supported.");
else
{
const void* rom = rom1 + 1;
filesize = little_endian_conv_32(rom1->filesize);
memcpy(nesrom, rom, filesize);
}
}
}
}

View File

@ -475,22 +475,6 @@ static char *GetExt(char *file)
return ext;
}
bool GetFileSize(int i)
{
if(browserList[i].length > 0)
return true;
struct stat filestat;
char path[MAXPATHLEN+1];
snprintf(path, MAXPATHLEN, "%s%s", browser.dir, browserList[i].filename);
if(stat(path, &filestat) < 0)
return false;
browserList[i].length = filestat.st_size;
return true;
}
void FindAndSelectLastLoadedFile ()
{
int indexFound = -1;
@ -675,7 +659,6 @@ ParseDirectory(bool waitParse, bool filter)
AddBrowserEntry();
sprintf(browserList[0].filename, "..");
sprintf(browserList[0].displayname, "Up One Level");
browserList[0].length = 0;
browserList[0].isdir = 1; // flag this as a dir
browserList[0].icon = ICON_FOLDER;
browser.numEntries++;
@ -777,7 +760,7 @@ LoadSzFile(char * filepath, unsigned char * rbuffer)
* LoadFile
***************************************************************************/
size_t
LoadFile (char * rbuffer, char *filepath, size_t length, bool silent)
LoadFile (char * rbuffer, char *filepath, size_t length, size_t buffersize, bool silent)
{
char zipbuffer[2048];
size_t size = 0, offset = 0, readsize = 0;
@ -829,7 +812,7 @@ LoadFile (char * rbuffer, char *filepath, size_t length, bool silent)
if (IsZipFile (zipbuffer))
{
size = UnZipBuffer ((unsigned char *)rbuffer); // unzip
size = UnZipBuffer ((unsigned char *)rbuffer, buffersize); // unzip
}
else
{
@ -837,18 +820,23 @@ LoadFile (char * rbuffer, char *filepath, size_t length, bool silent)
size = ftello(file);
fseeko(file,0,SEEK_SET);
while(!feof(file))
{
ShowProgress ("Loading...", offset, size);
readsize = fread (rbuffer + offset, 1, 4096, file); // read in next chunk
if(readsize <= 0)
break; // reading finished (or failed)
offset += readsize;
if(size > buffersize) {
size = 0;
}
else {
while(!feof(file))
{
ShowProgress ("Loading...", offset, size);
readsize = fread (rbuffer + offset, 1, 4096, file); // read in next chunk
if(readsize <= 0)
break; // reading finished (or failed)
offset += readsize;
}
size = offset;
CancelAction();
}
size = offset;
CancelAction();
}
}
retry = 0;
@ -863,19 +851,7 @@ LoadFile (char * rbuffer, char *filepath, size_t length, bool silent)
size_t LoadFile(char * filepath, bool silent)
{
struct stat filestat;
if(stat(filepath, &filestat) != 0) {
return 0;
}
int size = filestat.st_size;
if(size >= SAVEBUFFERSIZE) {
return 0;
}
return LoadFile((char *)savebuffer, filepath, 0, silent);
return LoadFile((char *)savebuffer, filepath, 0, SAVEBUFFERSIZE, silent);
}
/****************************************************************************

View File

@ -29,13 +29,12 @@ char * StripDevice(char * path);
bool ChangeInterface(int device, bool silent);
bool ChangeInterface(char * filepath, bool silent);
void CreateAppPath(char * origpath);
bool GetFileSize(int i);
void FindAndSelectLastLoadedFile();
int ParseDirectory(bool waitParse = false, bool filter = true);
bool CreateDirectory(char * path);
void AllocSaveBuffer();
void FreeSaveBuffer();
size_t LoadFile(char * rbuffer, char *filepath, size_t length, bool silent);
size_t LoadFile(char * rbuffer, char *filepath, size_t length, size_t buffersize, bool silent);
size_t LoadFile(char * filepath, bool silent);
size_t LoadSzFile(char * filepath, unsigned char * rbuffer);
size_t SaveFile(char * buffer, char *filepath, size_t datasize, bool silent);

View File

@ -97,7 +97,7 @@ IsZipFile (char *buffer)
******************************************************************************/
size_t
UnZipBuffer (unsigned char *outbuffer)
UnZipBuffer (unsigned char *outbuffer, size_t buffersize)
{
PKZIPHEADER pkzip;
size_t zipoffset = 0;
@ -122,6 +122,10 @@ UnZipBuffer (unsigned char *outbuffer)
pkzip.uncompressedSize = FLIP32 (pkzip.uncompressedSize);
if(pkzip.uncompressedSize > buffersize) {
return 0;
}
ShowProgress ("Loading...", 0, pkzip.uncompressedSize);
/*** Prepare the zip stream ***/
@ -211,7 +215,7 @@ GetFirstZipFilename ()
return NULL;
// read start of ZIP
if(LoadFile (tempbuffer, filepath, ZIPCHUNK, NOTSILENT) < 35)
if(LoadFile (tempbuffer, filepath, ZIPCHUNK, ZIPCHUNK, NOTSILENT) < 35)
return NULL;
tempbuffer[28] = 0; // truncate - filename length is 2 bytes long (bytes 26-27)
@ -428,7 +432,6 @@ int SzParse(char * filepath)
sprintf(browserList[0].filename, "..");
sprintf(browserList[0].displayname, "Up One Level");
browserList[0].isdir = 1;
browserList[0].length = filelen;
browserList[0].icon = ICON_FOLDER;
// get contents and parse them into file list structure

View File

@ -13,7 +13,7 @@
int IsZipFile (char *buffer);
char * GetFirstZipFilename();
size_t UnZipBuffer (unsigned char *outbuffer);
size_t UnZipBuffer (unsigned char *outbuffer, size_t buffersize);
int SzParse(char * filepath);
size_t SzExtractFile(int i, unsigned char *buffer);
void SzClose();