From 6ece7cfbc41d582012bc989afff984ba5259b0bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Einar=20Johan=20Tr=C3=B8an=20S=C3=B8ma=CC=8Aen?= Date: Sun, 9 Apr 2023 10:11:30 +0200 Subject: [PATCH] WATCHMAKER: Optimize t3dGetFileDate slightly By avoiding opening the files just to check their existence. --- engines/watchmaker/ll/ll_system.cpp | 38 +++++++++++++++++++++-------- engines/watchmaker/ll/ll_system.h | 1 + 2 files changed, 29 insertions(+), 10 deletions(-) diff --git a/engines/watchmaker/ll/ll_system.cpp b/engines/watchmaker/ll/ll_system.cpp index 7f8b1d53a8c..32b8fbe3221 100644 --- a/engines/watchmaker/ll/ll_system.cpp +++ b/engines/watchmaker/ll/ll_system.cpp @@ -162,20 +162,38 @@ int t3dAccessFile(char *name) { } bool t3dGetFileDate(uint32 *date, uint32 *time, const char *name) { - auto stream = openFile(name); - warning("TODO: t3dGetFileDate is currently super-inefficient"); - if (stream) - return true; + warning("TODO: t3dGetFileDate is currently super-inefficient: %s", name); + return checkFileExists(name); +} + +Common::String adjustPath(const Common::String &path) { + Common::String adjustedPath; + if (path.hasPrefix("./")) { + adjustedPath = path.substr(2, path.size()); + } else { + adjustedPath = path; + } + return adjustedPath; +} + +bool checkFileExists(const Common::String &filename) { + Common::String adjustedPath = adjustPath(filename); + + Common::ArchiveMemberList files; + SearchMan.listMatchingMembers(files, adjustedPath); + + for (Common::ArchiveMemberList::iterator it = files.begin(); it != files.end(); ++it) { + if ((*it)->getName().equalsIgnoreCase(lastPathComponent(adjustedPath, '/'))) { + return true; + } + } + return false; } Common::SharedPtr openFile(const Common::String &filename, int offset, int size) { - Common::String adjustedPath; - if (filename.hasPrefix("./")) { - adjustedPath = filename.substr(2, filename.size()); - } else { - adjustedPath = filename; - } + Common::String adjustedPath = adjustPath(filename); + Common::SeekableReadStream *file = nullptr; // Try directly from SearchMan first Common::ArchiveMemberList files; diff --git a/engines/watchmaker/ll/ll_system.h b/engines/watchmaker/ll/ll_system.h index 01ee7577dfc..3423c917284 100644 --- a/engines/watchmaker/ll/ll_system.h +++ b/engines/watchmaker/ll/ll_system.h @@ -58,6 +58,7 @@ bool t3dFastFileInit(const char *name); void t3dForceNOFastFile(char valore); int t3dAccessFile(char *name); bool t3dGetFileDate(uint32 *date, uint32 *time, const char *name); +bool checkFileExists(const Common::String &filename); Common::SeekableReadStream *resolveFile(const char *path); Common::SharedPtr openFile(const Common::String &filename, int offset = 0, int size = -1);