From 6ada7c3c7f8e2a423bbb6205aebe416231c5ee3d Mon Sep 17 00:00:00 2001 From: Jonathan G Rennison Date: Wed, 11 Dec 2019 20:34:04 +0000 Subject: [PATCH] Logging: Log full file names of opened GRFs --- src/fileio.cpp | 22 ++++++++++++++-------- src/fileio_func.h | 4 ++-- src/newgrf.cpp | 12 +++++++----- src/newgrf_config.cpp | 7 ++++--- src/newgrf_config.h | 6 ++++++ 5 files changed, 33 insertions(+), 18 deletions(-) diff --git a/src/fileio.cpp b/src/fileio.cpp index 90a780923d..2ff1b45ff8 100644 --- a/src/fileio.cpp +++ b/src/fileio.cpp @@ -245,14 +245,14 @@ static void FioFreeHandle() * @param filename Name of the file at the disk. * @param subdir The sub directory to search this file in. */ -void FioOpenFile(uint slot, const char *filename, Subdirectory subdir) +void FioOpenFile(uint slot, const char *filename, Subdirectory subdir, char **output_filename) { FILE *f; #if defined(LIMITED_FDS) FioFreeHandle(); #endif /* LIMITED_FDS */ - f = FioFOpenFile(filename, "rb", subdir); + f = FioFOpenFile(filename, "rb", subdir, nullptr, output_filename); if (f == nullptr) usererror("Cannot open file '%s'", filename); long pos = ftell(f); if (pos < 0) usererror("Cannot read file '%s'", filename); @@ -395,7 +395,7 @@ char *FioGetDirectory(char *buf, const char *last, Subdirectory subdir) return buf; } -static FILE *FioFOpenFileSp(const char *filename, const char *mode, Searchpath sp, Subdirectory subdir, size_t *filesize) +static FILE *FioFOpenFileSp(const char *filename, const char *mode, Searchpath sp, Subdirectory subdir, size_t *filesize, char **output_filename) { #if defined(_WIN32) && defined(UNICODE) /* fopen is implemented as a define with ellipses for @@ -430,6 +430,9 @@ static FILE *FioFOpenFileSp(const char *filename, const char *mode, Searchpath s *filesize = ftell(f); fseek(f, 0, SEEK_SET); } + if (output_filename != nullptr) { + *output_filename = (f != nullptr) ? stredup(buf, lastof(buf)) : nullptr; + } return f; } @@ -460,7 +463,7 @@ FILE *FioFOpenFileTar(TarFileListEntry *entry, size_t *filesize) * @param subdir Subdirectory to open. * @return File handle of the opened file, or \c nullptr if the file is not available. */ -FILE *FioFOpenFile(const char *filename, const char *mode, Subdirectory subdir, size_t *filesize) +FILE *FioFOpenFile(const char *filename, const char *mode, Subdirectory subdir, size_t *filesize, char **output_filename) { FILE *f = nullptr; Searchpath sp; @@ -468,7 +471,7 @@ FILE *FioFOpenFile(const char *filename, const char *mode, Subdirectory subdir, assert(subdir < NUM_SUBDIRS || subdir == NO_DIRECTORY); FOR_ALL_SEARCHPATHS(sp) { - f = FioFOpenFileSp(filename, mode, sp, subdir, filesize); + f = FioFOpenFileSp(filename, mode, sp, subdir, filesize, output_filename); if (f != nullptr || subdir == NO_DIRECTORY) break; } @@ -501,6 +504,9 @@ FILE *FioFOpenFile(const char *filename, const char *mode, Subdirectory subdir, TarFileList::iterator it = _tar_filelist[subdir].find(resolved_name); if (it != _tar_filelist[subdir].end()) { f = FioFOpenFileTar(&((*it).second), filesize); + if (output_filename != nullptr && f != nullptr) { + *output_filename = str_fmt("%s" PATHSEP "%s", ((*it).second).tar_filename, filename); + } } } @@ -509,15 +515,15 @@ FILE *FioFOpenFile(const char *filename, const char *mode, Subdirectory subdir, if (f == nullptr && subdir != NO_DIRECTORY) { switch (subdir) { case BASESET_DIR: - f = FioFOpenFile(filename, mode, OLD_GM_DIR, filesize); + f = FioFOpenFile(filename, mode, OLD_GM_DIR, filesize, output_filename); if (f != nullptr) break; FALLTHROUGH; case NEWGRF_DIR: - f = FioFOpenFile(filename, mode, OLD_DATA_DIR, filesize); + f = FioFOpenFile(filename, mode, OLD_DATA_DIR, filesize, output_filename); break; default: - f = FioFOpenFile(filename, mode, NO_DIRECTORY, filesize); + f = FioFOpenFile(filename, mode, NO_DIRECTORY, filesize, output_filename); break; } } diff --git a/src/fileio_func.h b/src/fileio_func.h index 0a30d0d66e..221df80b37 100644 --- a/src/fileio_func.h +++ b/src/fileio_func.h @@ -21,7 +21,7 @@ byte FioReadByte(); uint16 FioReadWord(); uint32 FioReadDword(); void FioCloseAll(); -void FioOpenFile(uint slot, const char *filename, Subdirectory subdir); +void FioOpenFile(uint slot, const char *filename, Subdirectory subdir, char **output_filename = nullptr); void FioReadBlock(void *ptr, size_t size); void FioSkipBytes(int n); @@ -47,7 +47,7 @@ static inline bool IsValidSearchPath(Searchpath sp) #define FOR_ALL_SEARCHPATHS(sp) for (sp = SP_FIRST_DIR; sp < NUM_SEARCHPATHS; sp++) if (IsValidSearchPath(sp)) void FioFCloseFile(FILE *f); -FILE *FioFOpenFile(const char *filename, const char *mode, Subdirectory subdir, size_t *filesize = nullptr); +FILE *FioFOpenFile(const char *filename, const char *mode, Subdirectory subdir, size_t *filesize = nullptr, char **output_filename = nullptr); bool FioCheckFileExists(const char *filename, Subdirectory subdir); char *FioGetFullPath(char *buf, const char *last, Searchpath sp, Subdirectory subdir, const char *filename); char *FioFindFullPath(char *buf, const char *last, Subdirectory subdir, const char *filename); diff --git a/src/newgrf.cpp b/src/newgrf.cpp index d6411a9b13..474b73fa04 100644 --- a/src/newgrf.cpp +++ b/src/newgrf.cpp @@ -386,7 +386,7 @@ void CDECL _intl_grfmsg(int severity, const char *str, ...) vseprintf(buf, lastof(buf), str, va); va_end(va); - DEBUG(grf, severity, "[%s:%d] %s", _cur.grfconfig->filename, _cur.nfo_line, buf); + DEBUG(grf, severity, "[%s:%d] %s", _cur.grfconfig->GetDisplayPath(), _cur.nfo_line, buf); } /** @@ -6814,7 +6814,7 @@ static void ScanInfo(ByteReader *buf) if (grf_version < 2 || grf_version > 8) { SetBit(_cur.grfconfig->flags, GCF_INVALID); - DEBUG(grf, 0, "%s: NewGRF \"%s\" (GRFID %08X) uses GRF version %d, which is incompatible with this version of OpenTTD.", _cur.grfconfig->filename, name, BSWAP32(grfid), grf_version); + DEBUG(grf, 0, "%s: NewGRF \"%s\" (GRFID %08X) uses GRF version %d, which is incompatible with this version of OpenTTD.", _cur.grfconfig->GetDisplayPath(), name, BSWAP32(grfid), grf_version); } /* GRF IDs starting with 0xFF are reserved for internal TTDPatch use */ @@ -7519,7 +7519,7 @@ static void GRFInhibit(ByteReader *buf) /* Unset activation flag */ if (file != nullptr && file != _cur.grfconfig) { - grfmsg(2, "GRFInhibit: Deactivating file '%s'", file->filename); + grfmsg(2, "GRFInhibit: Deactivating file '%s'", file->GetDisplayPath()); GRFError *error = DisableGrf(STR_NEWGRF_ERROR_FORCEFULLY_DISABLED, file); error->data = stredup(_cur.grfconfig->GetName()); } @@ -9922,13 +9922,15 @@ void LoadNewGRFFile(GRFConfig *config, uint file_index, GrfLoadingStage stage, S return; } - FioOpenFile(file_index, filename, subdir); + free(config->full_filename); + config->full_filename = nullptr; + FioOpenFile(file_index, filename, subdir, &(config->full_filename)); _cur.file_index = file_index; // XXX _palette_remap_grf[_cur.file_index] = (config->palette & GRFP_USE_MASK); _cur.grfconfig = config; - DEBUG(grf, 2, "LoadNewGRFFile: Reading NewGRF-file '%s'", filename); + DEBUG(grf, 2, "LoadNewGRFFile: Reading NewGRF-file '%s'", config->GetDisplayPath()); _cur.grf_container_ver = GetGRFContainerVersion(); if (_cur.grf_container_ver == 0) { diff --git a/src/newgrf_config.cpp b/src/newgrf_config.cpp index 20553a1535..fce97bd289 100644 --- a/src/newgrf_config.cpp +++ b/src/newgrf_config.cpp @@ -108,6 +108,7 @@ GRFConfig::~GRFConfig() free(this->filename); delete this->error; } + free(this->full_filename); this->name->Release(); this->info->Release(); this->url->Release(); @@ -658,7 +659,7 @@ GRFListCompatibility IsGoodGRFConfigList(GRFConfig *grfconfig) f = FindGRFConfig(c->ident.grfid, FGCM_COMPATIBLE, nullptr, c->version); if (f != nullptr) { md5sumToString(buf, lastof(buf), c->ident.md5sum); - DEBUG(grf, 1, "NewGRF %08X (%s) not found; checksum %s. Compatibility mode on", BSWAP32(c->ident.grfid), c->filename, buf); + DEBUG(grf, 1, "NewGRF %08X (%s) not found; checksum %s. Compatibility mode on", BSWAP32(c->ident.grfid), c->GetDisplayPath(), buf); if (!HasBit(c->flags, GCF_COMPATIBLE)) { /* Preserve original_md5sum after it has been assigned */ SetBit(c->flags, GCF_COMPATIBLE); @@ -672,13 +673,13 @@ GRFListCompatibility IsGoodGRFConfigList(GRFConfig *grfconfig) /* No compatible grf was found, mark it as disabled */ md5sumToString(buf, lastof(buf), c->ident.md5sum); - DEBUG(grf, 0, "NewGRF %08X (%s) not found; checksum %s", BSWAP32(c->ident.grfid), c->filename, buf); + DEBUG(grf, 0, "NewGRF %08X (%s) not found; checksum %s", BSWAP32(c->ident.grfid), c->GetDisplayPath(), buf); c->status = GCS_NOT_FOUND; res = GLC_NOT_FOUND; } else { compatible_grf: - DEBUG(grf, 1, "Loading GRF %08X from %s", BSWAP32(f->ident.grfid), f->filename); + DEBUG(grf, 1, "Loading GRF %08X from %s", BSWAP32(f->ident.grfid), f->GetDisplayPath()); /* The filename could be the filename as in the savegame. As we need * to load the GRF here, we need the correct filename, so overwrite that * in any case and set the name and info when it is not set already. diff --git a/src/newgrf_config.h b/src/newgrf_config.h index c630e09f17..276c97a559 100644 --- a/src/newgrf_config.h +++ b/src/newgrf_config.h @@ -156,6 +156,7 @@ struct GRFConfig : ZeroedMemoryAllocator { GRFIdentifier ident; ///< grfid and md5sum to uniquely identify newgrfs uint8 original_md5sum[16]; ///< MD5 checksum of original file if only a 'compatible' file was loaded char *filename; ///< Filename - either with or without full path + char *full_filename; ///< NOSAVE: Full filename GRFTextWrapper *name; ///< NOSAVE: GRF name (Action 0x08) GRFTextWrapper *info; ///< NOSAVE: GRF info (author, copyright, ...) (Action 0x08) GRFTextWrapper *url; ///< NOSAVE: URL belonging to this GRF. @@ -182,6 +183,11 @@ struct GRFConfig : ZeroedMemoryAllocator { const char *GetDescription() const; const char *GetURL() const; + const char *GetDisplayPath() const + { + return this->full_filename ? this->full_filename : this->filename; + } + void SetParameterDefaults(); void SetSuitablePalette(); void FinalizeParameterInfo();