From 609eafda578c27960510c08d1d69f59447bdb264 Mon Sep 17 00:00:00 2001 From: Sam V Date: Mon, 4 Apr 2022 15:56:53 +0200 Subject: [PATCH] Fix node graphs being loaded from search paths other than GAMECONFIG path Resolves #145 --- dlls/nodes.cpp | 4 ++-- game_shared/filesystem_utils.cpp | 11 +++++++---- game_shared/filesystem_utils.h | 14 +++++++++++--- 3 files changed, 20 insertions(+), 9 deletions(-) diff --git a/dlls/nodes.cpp b/dlls/nodes.cpp index fa2c0663..bc4edb0f 100644 --- a/dlls/nodes.cpp +++ b/dlls/nodes.cpp @@ -2325,9 +2325,9 @@ bool CGraph::FLoadGraph(const char* szMapName) const std::string fileName{std::string{"maps/graphs/"} + szMapName + ".nod"}; - //Note: "GAME" path ID to allow loading graphs from addon content. + //Note: Allow loading graphs only from the mod directory itself. //Do not allow loading from other games since they may have a different graph format. - const auto buffer = FileSystem_LoadFileIntoBuffer(fileName.c_str(), "GAME"); + const auto buffer = FileSystem_LoadFileIntoBuffer(fileName.c_str(), FileContentFormat::Binary, "GAMECONFIG"); if (buffer.empty()) { diff --git a/game_shared/filesystem_utils.cpp b/game_shared/filesystem_utils.cpp index 5e26ea6f..c21e5525 100644 --- a/game_shared/filesystem_utils.cpp +++ b/game_shared/filesystem_utils.cpp @@ -90,7 +90,7 @@ void FileSystem_FreeFileSystem() } } -std::vector FileSystem_LoadFileIntoBuffer(const char* fileName, const char* pathID) +std::vector FileSystem_LoadFileIntoBuffer(const char* fileName, FileContentFormat format, const char* pathID) { assert(nullptr != g_pFileSystem); @@ -103,14 +103,17 @@ std::vector FileSystem_LoadFileIntoBuffer(const char* fileName, const { const auto size = file.Size(); - //Null terminate it in case it's actually text. std::vector buffer; - buffer.resize(size + 1); + buffer.resize(size + (format == FileContentFormat::Text ? 1 : 0)); file.Read(buffer.data(), size); - buffer[size] = std::byte{'\0'}; + if (format == FileContentFormat::Text) + { + //Null terminate it in case it's actually text. + buffer[size] = std::byte{'\0'}; + } return buffer; } diff --git a/game_shared/filesystem_utils.h b/game_shared/filesystem_utils.h index 95a0ea80..2da70aaf 100644 --- a/game_shared/filesystem_utils.h +++ b/game_shared/filesystem_utils.h @@ -34,20 +34,28 @@ inline IFileSystem* g_pFileSystem = nullptr; bool FileSystem_LoadFileSystem(); void FileSystem_FreeFileSystem(); +enum class FileContentFormat +{ + Binary = 0, + Text = 1 +}; + /** * @brief Loads a file from disk into a buffer. * -* @details If the returned buffer contains text data it is safe to cast the data pointer to char*: +* @details If the returned buffer contains text data and @p format is @c FileContentFormat::Text it is safe to cast the data pointer to char*: * @code{.cpp} * auto text = reinterpret_cast(buffer.data()); * @endcode * * @param fileName Name of the file to load. +* @param format If @c FileContentFormat::Text, a null terminator will be appended. * @param pathID If not null, only looks for the file in this search path. -* @return If the file was successfully loaded, the contents of the buffer, with a zero byte (null terminator) appended to it in case it's a text file. +* @return If the file was successfully loaded the contents of the buffer, +* with a zero byte (null terminator) appended to it if @p format is @c FileContentFormat::Text. * If the file could not be loaded an empty buffer is returned. */ -std::vector FileSystem_LoadFileIntoBuffer(const char* fileName, const char* pathID = nullptr); +std::vector FileSystem_LoadFileIntoBuffer(const char* fileName, FileContentFormat format, const char* pathID = nullptr); /** * @brief Writes a text file to disk.