Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
SamVanheer committed Apr 4, 2022
2 parents fd18ad5 + 609eafd commit 36054a5
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 9 deletions.
4 changes: 2 additions & 2 deletions dlls/nodes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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())
{
Expand Down
11 changes: 7 additions & 4 deletions game_shared/filesystem_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ void FileSystem_FreeFileSystem()
}
}

std::vector<std::byte> FileSystem_LoadFileIntoBuffer(const char* fileName, const char* pathID)
std::vector<std::byte> FileSystem_LoadFileIntoBuffer(const char* fileName, FileContentFormat format, const char* pathID)
{
assert(nullptr != g_pFileSystem);

Expand All @@ -103,14 +103,17 @@ std::vector<std::byte> FileSystem_LoadFileIntoBuffer(const char* fileName, const
{
const auto size = file.Size();

//Null terminate it in case it's actually text.
std::vector<std::byte> 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;
}
Expand Down
14 changes: 11 additions & 3 deletions game_shared/filesystem_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<char*>(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<std::byte> FileSystem_LoadFileIntoBuffer(const char* fileName, const char* pathID = nullptr);
std::vector<std::byte> FileSystem_LoadFileIntoBuffer(const char* fileName, FileContentFormat format, const char* pathID = nullptr);

/**
* @brief Writes a text file to disk.
Expand Down

0 comments on commit 36054a5

Please sign in to comment.