Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 22 additions & 16 deletions src/options.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ int ini_load(const char *filename,
FILE *f;
int cnt;

f = fopen(filename, "r");
f = fcache_open(filename, "r");
if (!f) {
return -1;
}
Expand All @@ -69,12 +69,12 @@ int ini_load(const char *filename,
if (fscanf(f, " ;%*[^\n]") != 0 ||
fscanf(f, " \n") != 0) {
fprintf(stderr, "short read from %s!?\n", filename);
fclose(f);
fcache_close(f);
return -1;
}
}

fclose(f);
fcache_close(f);
return 0;
}

Expand Down Expand Up @@ -505,26 +505,32 @@ int identify_dos_exe_version(int filesize) {
void load_dos_exe_modifications(const char* folder_name) {
char filename[POP_MAX_PATH];
snprintf(filename, sizeof(filename), "%s/%s", folder_name, "PRINCE.EXE");
FILE* fp = fopen(filename, "rb");
FILE* fp = fcache_open(filename, "rb");
size_t execSize = 0;

int dos_version = -1;
struct stat info;
if (fp != NULL && fstat(fileno(fp), &info) == 0 && info.st_size > 0) {
dos_version = identify_dos_exe_version(info.st_size);
if (fp != NULL) {
fcache_seek(fp, 0, SEEK_END);
execSize = fcache_tell(fp);
fcache_seek(fp, 0, SEEK_SET);
dos_version = identify_dos_exe_version(execSize);
} else {
// PRINCE.EXE not found, try to search for other .EXE files in the same folder.
directory_listing_type* directory_listing = create_directory_listing_and_find_first_file(folder_name, "exe");
if (directory_listing != NULL) {
do {
char* current_filename = get_current_filename_from_directory_listing(directory_listing);
snprintf(filename, sizeof(filename), "%s/%s", folder_name, current_filename);
fp = fopen(filename, "rb");
if (fp != NULL && fstat(fileno(fp), &info) == 0 && info.st_size > 0) {
dos_version = identify_dos_exe_version(info.st_size);
fp = fcache_open(filename, "rb");
if (fp != NULL) {
fcache_seek(fp, 0, SEEK_END);
execSize = fcache_tell(fp);
fcache_seek(fp, 0, SEEK_SET);
dos_version = identify_dos_exe_version(execSize);
if (dos_version >= 0) {
break; // We found a DOS executable with the right size!
}
fclose(fp);
fcache_close(fp);
fp = NULL;
}
// Keep looking until we find an .EXE with the right size, or until there are no .EXE files left.
Expand All @@ -535,10 +541,10 @@ void load_dos_exe_modifications(const char* folder_name) {

if (dos_version >= 0) {
turn_custom_options_on_off(1);
byte* exe_memory = malloc((size_t) info.st_size);
if (fread(exe_memory, (size_t) info.st_size, 1, fp) != 1) {
byte* exe_memory = malloc(execSize);
if (fcache_read(exe_memory, execSize, 1, fp) != 1) {
fprintf(stderr, "Could not read %s!?\n", filename);
fclose(fp);
fcache_close(fp);
return;
}

Expand All @@ -550,7 +556,7 @@ void load_dos_exe_modifications(const char* folder_name) {
do { \
static const int offsets[6] = __VA_ARGS__; \
int offset = offsets[dos_version]; \
read_ok = read_exe_bytes(x, nbytes, exe_memory, offset, info.st_size); \
read_ok = read_exe_bytes(x, nbytes, exe_memory, offset, execSize); \
} while(0)

// Offsets and comparisons are derived from princehack.xml
Expand Down Expand Up @@ -702,7 +708,7 @@ void load_dos_exe_modifications(const char* folder_name) {
free(exe_memory);
}

if (fp != NULL) fclose(fp);
if (fp != NULL) fcache_close(fp);
}


Expand Down
5 changes: 5 additions & 0 deletions src/proto.h
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,11 @@ void __pascal far draw_right_mark (word arg2, word arg1);
image_type* get_image(short chtab_id, int id);

// SEG009.C
FILE* fcache_open(const char * filename, const char * mode);
size_t fcache_read(void * ptr, size_t size, size_t count, FILE * stream);
int fcache_seek(FILE * stream, long int offset, int origin);
int fcache_tell(FILE * stream);
int fcache_close(FILE* file);
void sdlperror(const char* header);
bool file_exists(const char* filename);
#define locate_file(filename) locate_file_(filename, alloca(POP_MAX_PATH), POP_MAX_PATH)
Expand Down
17 changes: 9 additions & 8 deletions src/seg000.c
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ int process_save(void* data, size_t data_size) {
}

int process_load(void* data, size_t data_size) {
return fread(data, data_size, 1, quick_fp) == 1;
return fcache_read(data, data_size, 1, quick_fp) == 1;
}

typedef int process_func_type(void* data, size_t data_size);
Expand Down Expand Up @@ -426,12 +426,12 @@ int quick_load() {
int ok = 0;
char custom_quick_path[POP_MAX_PATH];
const char* path = get_quick_path(custom_quick_path, sizeof(custom_quick_path));
quick_fp = fopen(path, "rb");
quick_fp = fcache_open(path, "rb");
if (quick_fp != NULL) {
// check quicksave version is compatible
process_load(quick_control, COUNT(quick_control));
if (strcmp(quick_control, quick_version) != 0) {
fclose(quick_fp);
fcache_close(quick_fp);
quick_fp = NULL;
return 0;
}
Expand All @@ -445,7 +445,7 @@ int quick_load() {
word old_rem_tick = rem_tick;

ok = quick_process(process_load);
fclose(quick_fp);
fcache_close(quick_fp);
quick_fp = NULL;

restore_room_after_quick_load();
Expand Down Expand Up @@ -1032,6 +1032,7 @@ void __pascal far load_sounds(int first,int last) {
sound_pointers[current] = load_sound(current);
}
}

if (midi_dat) close_dat(midi_dat);
if (digi1_dat) close_dat(digi1_dat);
// if (digi2_dat) close_dat(digi2_dat);
Expand Down Expand Up @@ -2057,7 +2058,7 @@ void __pascal far save_game() {
if (handle == NULL) goto loc_1DB8;
if (fwrite(&rem_min, 1, 2, handle) == 2) goto loc_1DC9;
loc_1D9B:
fclose(handle);
fcache_close(handle);
if (!success) {
remove(save_path);
}
Expand Down Expand Up @@ -2085,11 +2086,11 @@ short __pascal far load_game() {
success = 0;
char custom_save_path[POP_MAX_PATH];
const char* save_path = get_save_path(custom_save_path, sizeof(custom_save_path));
handle = fopen(save_path, "rb");
handle = fcache_open(save_path, "rb");
if (handle == NULL) goto loc_1E99;
if (fread(&rem_min, 1, 2, handle) == 2) goto loc_1E9E;
if (fcache_read(&rem_min, 1, 2, handle) == 2) goto loc_1E9E;
loc_1E8E:
fclose(handle);
fcache_close(handle);
loc_1E99:
return success;
loc_1E9E:
Expand Down
Loading