@@ -105,19 +105,32 @@ const char *Cfile_cdrom_dir = NULL;
105105// Function prototypes for internally-called functions
106106//
107107int cfget_cfile_block ();
108- CFILE *cf_open_fill_cfblock (FILE * fp, int type);
109- CFILE *cf_open_packed_cfblock (FILE *fp, int type, int offset, int size);
108+ CFILE *cf_open_fill_cfblock (const char * source, int line, FILE * fp, int type);
109+ CFILE *cf_open_packed_cfblock (const char * source, int line, FILE *fp, int type, int offset, int size);
110110
111111#if defined _WIN32
112- CFILE *cf_open_mapped_fill_cfblock (HANDLE hFile, int type);
112+ CFILE *cf_open_mapped_fill_cfblock (const char * source, int line, HANDLE hFile, int type);
113113#elif defined SCP_UNIX
114- CFILE *cf_open_mapped_fill_cfblock (FILE *fp, int type);
114+ CFILE *cf_open_mapped_fill_cfblock (const char * source, int line, FILE *fp, int type);
115115#endif
116116
117117void cf_chksum_long_init ();
118118
119+ static void dump_opened_files ()
120+ {
121+ for (int i = 0 ; i < MAX_CFILE_BLOCKS; i++) {
122+ auto cb = &Cfile_block_list[i];
123+ if (cb->type != CFILE_BLOCK_UNUSED) {
124+ mprintf ((" %s:%d\n " , cb->source_file , cb->line_num ));
125+ }
126+ }
127+ }
128+
119129void cfile_close ()
120130{
131+ mprintf ((" Still opened files:\n " ));
132+ dump_opened_files ();
133+
121134 cf_free_secondary_filelist ();
122135}
123136
@@ -631,7 +644,7 @@ extern int game_cd_changed();
631644// error ==> NULL
632645//
633646
634- CFILE *cfopen ( const char *file_path, const char *mode, int type, int dir_type, bool localize)
647+ CFILE *_cfopen ( const char * source, int line, const char *file_path, const char *mode, int type, int dir_type, bool localize)
635648{
636649 /* Bobboau, what is this doing here? 31 is way too short... - Goober5000
637650 if( strlen(file_path) > 31 )
@@ -716,7 +729,7 @@ CFILE *cfopen(const char *file_path, const char *mode, int type, int dir_type, b
716729
717730 FILE *fp = fopen (longname, happy_mode);
718731 if (fp) {
719- return cf_open_fill_cfblock (fp, dir_type);
732+ return cf_open_fill_cfblock (source, line, fp, dir_type);
720733 }
721734 return NULL ;
722735 }
@@ -745,12 +758,12 @@ CFILE *cfopen(const char *file_path, const char *mode, int type, int dir_type, b
745758 hFile = CreateFile (longname, GENERIC_READ, FILE_SHARE_READ, NULL , OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL );
746759
747760 if (hFile != INVALID_HANDLE_VALUE) {
748- return cf_open_mapped_fill_cfblock (hFile, dir_type);
761+ return cf_open_mapped_fill_cfblock (source, line, hFile, dir_type);
749762 }
750763#elif defined SCP_UNIX
751764 FILE *fp = fopen ( longname, " rb" );
752765 if (fp) {
753- return cf_open_mapped_fill_cfblock (fp, dir_type);
766+ return cf_open_mapped_fill_cfblock (source, line, fp, dir_type);
754767 }
755768#endif
756769 }
@@ -762,10 +775,10 @@ CFILE *cfopen(const char *file_path, const char *mode, int type, int dir_type, b
762775 if ( fp ) {
763776 if ( offset ) {
764777 // Found it in a pack file
765- return cf_open_packed_cfblock (fp, dir_type, offset, size );
778+ return cf_open_packed_cfblock (source, line, fp, dir_type, offset, size );
766779 } else {
767780 // Found it in a normal file
768- return cf_open_fill_cfblock (fp, dir_type);
781+ return cf_open_fill_cfblock (source, line, fp, dir_type);
769782 }
770783 }
771784 }
@@ -786,7 +799,7 @@ CFILE *cfopen(const char *file_path, const char *mode, int type, int dir_type, b
786799// returns: success ==> address of CFILE structure
787800// error ==> NULL
788801//
789- CFILE *cfopen_special ( const char *file_path, const char *mode, const int size, const int offset, int dir_type)
802+ CFILE *_cfopen_special ( const char * source, int line, const char *file_path, const char *mode, const int size, const int offset, int dir_type)
790803{
791804 if ( !cfile_inited) {
792805 Int3 ();
@@ -811,10 +824,10 @@ CFILE *cfopen_special(const char *file_path, const char *mode, const int size, c
811824
812825 if ( offset ) {
813826 // it's in a pack file
814- return cf_open_packed_cfblock (fp, dir_type, offset, size);
827+ return cf_open_packed_cfblock (source, line, fp, dir_type, offset, size);
815828 } else {
816829 // it's a normal file
817- return cf_open_fill_cfblock (fp, dir_type);
830+ return cf_open_fill_cfblock (source, line, fp, dir_type);
818831 }
819832}
820833
@@ -833,7 +846,7 @@ CFILE *ctmpfile()
833846 FILE *fp;
834847 fp = tmpfile ();
835848 if ( fp )
836- return cf_open_fill_cfblock (fp, 0 );
849+ return cf_open_fill_cfblock (LOCATION, fp, 0 );
837850 else
838851 return NULL ;
839852}
@@ -863,7 +876,12 @@ int cfget_cfile_block()
863876
864877 // If we've reached this point, a free Cfile_block could not be found
865878 nprintf ((" Warning" ," A free Cfile_block could not be found.\n " ));
866- Assert (0 ); // out of free cfile blocks
879+
880+ // Dump a list of all opened files
881+ mprintf ((" Out of cfile blocks! Currently opened files:\n " ));
882+
883+ Assertion (false , " There are no more free cfile blocks. This means that there are too many files opened by FSO.\n "
884+ " This is probably caused by a programming or scripting error where a file does not get closed." ); // out of free cfile blocks
867885 return -1 ;
868886}
869887
@@ -941,7 +959,7 @@ int cf_is_valid(CFILE *cfile)
941959// returns: success ==> ptr to CFILE structure.
942960// error ==> NULL
943961//
944- CFILE *cf_open_fill_cfblock (FILE *fp, int type)
962+ CFILE *cf_open_fill_cfblock (const char * source, int line, FILE *fp, int type)
945963{
946964 int cfile_block_index;
947965
@@ -960,6 +978,9 @@ CFILE *cf_open_fill_cfblock(FILE *fp, int type)
960978 cfbp->fp = fp;
961979 cfbp->dir_type = type;
962980 cfbp->max_read_len = 0 ;
981+
982+ cfbp->source_file = source;
983+ cfbp->line_num = line;
963984
964985 int pos = ftell (fp);
965986 if (pos == -1L )
@@ -977,7 +998,7 @@ CFILE *cf_open_fill_cfblock(FILE *fp, int type)
977998// returns: success ==> ptr to CFILE structure.
978999// error ==> NULL
9791000//
980- CFILE *cf_open_packed_cfblock (FILE *fp, int type, int offset, int size)
1001+ CFILE *cf_open_packed_cfblock (const char * source, int line, FILE *fp, int type, int offset, int size)
9811002{
9821003 // Found it in a pack file
9831004 int cfile_block_index;
@@ -999,6 +1020,9 @@ CFILE *cf_open_packed_cfblock(FILE *fp, int type, int offset, int size)
9991020 cfbp->dir_type = type;
10001021 cfbp->max_read_len = 0 ;
10011022
1023+ cfbp->source_file = source;
1024+ cfbp->line_num = line;
1025+
10021026 cf_init_lowlevel_read_code (cfp,offset, size, 0 );
10031027
10041028 return cfp;
@@ -1014,9 +1038,9 @@ CFILE *cf_open_packed_cfblock(FILE *fp, int type, int offset, int size)
10141038// returns: ptr CFILE structure.
10151039//
10161040#if defined _WIN32
1017- CFILE *cf_open_mapped_fill_cfblock (HANDLE hFile, int type)
1041+ CFILE *cf_open_mapped_fill_cfblock (const char * source, int line, HANDLE hFile, int type)
10181042#elif defined SCP_UNIX
1019- CFILE *cf_open_mapped_fill_cfblock (FILE *fp, int type)
1043+ CFILE *cf_open_mapped_fill_cfblock (const char * source, int line, FILE *fp, int type)
10201044#endif
10211045{
10221046 int cfile_block_index;
@@ -1043,6 +1067,9 @@ CFILE *cf_open_mapped_fill_cfblock(FILE *fp, int type)
10431067#endif
10441068 cfbp->dir_type = type;
10451069
1070+ cfbp->source_file = source;
1071+ cfbp->line_num = line;
1072+
10461073 cf_init_lowlevel_read_code (cfp, 0 , 0 , 0 );
10471074#if defined _WIN32
10481075 cfbp->hMapFile = CreateFileMapping (cfbp->hInFile , NULL , PAGE_READONLY, 0 , 0 , NULL );
0 commit comments