mirror of
https://github.com/coop-deluxe/sm64coopdx.git
synced 2025-10-30 08:01:01 +00:00
Close file pointers immediately after reading/writing
This commit is contained in:
parent
2e1818394e
commit
26a465fd80
3 changed files with 39 additions and 17 deletions
|
|
@ -222,18 +222,28 @@ struct BassAudio* audio_load_internal(const char* filename, bool isStream) {
|
||||||
// remember file
|
// remember file
|
||||||
bassAudio->file = modFile;
|
bassAudio->file = modFile;
|
||||||
|
|
||||||
// copy audio into rawData
|
// open file pointer
|
||||||
|
bool opened = false;
|
||||||
if (modFile->fp == NULL) {
|
if (modFile->fp == NULL) {
|
||||||
modFile->fp = fopen(modFile->cachedPath, "rb");
|
modFile->fp = fopen(modFile->cachedPath, "rb");
|
||||||
if (modFile->fp == NULL) {
|
if (modFile->fp == NULL) {
|
||||||
LOG_ERROR("Could not open mod file: %s", modFile->cachedPath);
|
LOG_ERROR("Could not open mod file: %s", modFile->cachedPath);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
opened = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// copy data
|
||||||
rewind(modFile->fp);
|
rewind(modFile->fp);
|
||||||
bassAudio->rawData = (char*)malloc(modFile->size * sizeof(char));
|
bassAudio->rawData = (char*)malloc(modFile->size * sizeof(char));
|
||||||
fread(bassAudio->rawData, modFile->size, 1, modFile->fp);
|
fread(bassAudio->rawData, modFile->size, 1, modFile->fp);
|
||||||
|
|
||||||
|
// close file pointer
|
||||||
|
if (opened) {
|
||||||
|
fclose(modFile->fp);
|
||||||
|
modFile->fp = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
// load audio and return it
|
// load audio and return it
|
||||||
if (isStream) {
|
if (isStream) {
|
||||||
bassAudio->handle = bassh_create_fx_stream_from_file(bassAudio->rawData, modFile->size, 0);
|
bassAudio->handle = bassh_create_fx_stream_from_file(bassAudio->rawData, modFile->size, 0);
|
||||||
|
|
|
||||||
|
|
@ -71,22 +71,6 @@ void mods_activate(struct Mods* mods) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// open file pointers
|
|
||||||
if (mods != &gRemoteMods) {
|
|
||||||
for (int i = 0; i < gActiveMods.entryCount; i++) {
|
|
||||||
struct Mod* mod = gActiveMods.entries[i];
|
|
||||||
for (int j = 0; j < mod->fileCount; j++) {
|
|
||||||
struct ModFile* file = &mod->files[j];
|
|
||||||
|
|
||||||
file->fp = fopen(file->cachedPath, "rb");
|
|
||||||
if (file->fp == NULL) {
|
|
||||||
LOG_ERROR("Failed to open file '%s'", file->cachedPath);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
mod_cache_save();
|
mod_cache_save();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -265,10 +265,27 @@ void network_send_download(u64 requestOffset) {
|
||||||
u64 fileReadOffset = MAX(((s64)requestOffset - (s64)fileStartOffset), 0);
|
u64 fileReadOffset = MAX(((s64)requestOffset - (s64)fileStartOffset), 0);
|
||||||
u64 fileReadLength = MIN((modFile->size - fileReadOffset), (CHUNK_SIZE - chunkFill));
|
u64 fileReadLength = MIN((modFile->size - fileReadOffset), (CHUNK_SIZE - chunkFill));
|
||||||
|
|
||||||
|
// open file pointer
|
||||||
|
bool opened = false;
|
||||||
|
if (modFile->fp == NULL) {
|
||||||
|
modFile->fp = fopen(modFile->cachedPath, "rb");
|
||||||
|
if (modFile->fp == NULL) {
|
||||||
|
LOG_ERROR("Failed to open mod file during download: %s", modFile->cachedPath);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
opened = true;
|
||||||
|
}
|
||||||
|
|
||||||
// read from file, filling chunk
|
// read from file, filling chunk
|
||||||
fseek(modFile->fp, fileReadOffset, SEEK_SET);
|
fseek(modFile->fp, fileReadOffset, SEEK_SET);
|
||||||
fread(&chunk[chunkFill], sizeof(u8), fileReadLength, modFile->fp);
|
fread(&chunk[chunkFill], sizeof(u8), fileReadLength, modFile->fp);
|
||||||
|
|
||||||
|
// close file pointer
|
||||||
|
if (opened) {
|
||||||
|
fclose(modFile->fp);
|
||||||
|
modFile->fp = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
// increment counters
|
// increment counters
|
||||||
chunkFill += fileReadLength;
|
chunkFill += fileReadLength;
|
||||||
fileStartOffset += modFile->size;
|
fileStartOffset += modFile->size;
|
||||||
|
|
@ -384,8 +401,19 @@ after_group:;
|
||||||
// read from file, filling chunk
|
// read from file, filling chunk
|
||||||
if (!modFile->cachedPath) {
|
if (!modFile->cachedPath) {
|
||||||
open_mod_file(mod, modFile);
|
open_mod_file(mod, modFile);
|
||||||
|
if (modFile->fp == NULL) {
|
||||||
|
LOG_ERROR("Failed to open file for download write: %s", modFile->cachedPath);
|
||||||
|
return;
|
||||||
|
}
|
||||||
fseek(modFile->fp, fileWriteOffset, SEEK_SET);
|
fseek(modFile->fp, fileWriteOffset, SEEK_SET);
|
||||||
fwrite(&chunk[chunkPour], sizeof(u8), fileWriteLength, modFile->fp);
|
fwrite(&chunk[chunkPour], sizeof(u8), fileWriteLength, modFile->fp);
|
||||||
|
|
||||||
|
if (modFile->fp != NULL) {
|
||||||
|
fflush(modFile->fp);
|
||||||
|
fclose(modFile->fp);
|
||||||
|
modFile->fp = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
wroteBytes += fileWriteLength;
|
wroteBytes += fileWriteLength;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue