My approach to file management in MxEngine
File management is a very important part of any game engine. A good file manager makes lives of game developers much easier and helps them to write cross-platform code. In this article we will discuss how it can be done and what the common problems can arise when developing such file management system.
Where are we?
The first step the engine is need to perform when it is being launched, is to determine where it actually is in terms of the user file system. This step is obviously required if your application have some kind of config file which is used to setup other subsystems, or if you have some dynamic libraries, which should be loaded on engine startup. Only when the root directory is determined, other files can be processed based on it.
During development it is also a good idea to switch application working directory to the user’s source/resource directory. It is much easier for a game developer to store code sources, game objects and configs in one place, for example in his project directory. Using output binary directory as resource storage is error-prone: it can be cleared accidentally by a programmer or switched to other folder due to different IDE settings. As a bonus, when resource folder is separated from build folder, we do not have to copy all files to the binary directory or force user to hardcode the path to the root directory in the config. Also, binary and source directories can be easily detected and tracked by the build system, such as CMake. For example, in MxEngine I used some interesting tricks to perform working directory setup depending on how the engine is launched:
If the executable is launched from CMake build directory, this means that the game is built by developer and we need to switch to the source directory. Otherwise no actions are performed as the game is probably launched from folder which already contains all necessary resources. Note that in the code above I use C++17 std::filesystem
library to check current working directory and set a new one. In my opinion, filesystem library is one of the best things standard brought to us, and you should definitely use it if you are targeting modern language versions.
Know your files
When we are finally determined the root directory of our game, the good idea is to process all files: check if engine config presents in directory, load all system resources like shaders and lookup-textures, maybe generate some data for later use. Scanning process is very easy: you can simply check if file or directory exists by calling std::filesystem::exists
, get file extensions by invoking member function std::filesystem::filepath::extenstion
or even look through directories recursively using std::filesystem::recursive_directory_iterator
:
Note that in the code above I create a proximate file path from the entry got by directory iterator. This is a good idea if you plan to use these paths later, and especially if you are planning to serialize them for game distribution. Proximate paths are relative to the root directory and so they remain valid even if someone decide to move game folder to another place. Because of that, try to avoid loading resources from arbitrary filepaths - it is better to create a copy of the folder inside your root directory first, and then load from it. When the user decide to release his game, he will not have to look for absolute paths in the project and replace them with local, as everything already lies there it should.
Hashing go brrr
When the user wants to load resource from the code, he usually writes a string which somehow identifies the file. It can be a simple relative path to the requested resource, such as level1/enemies/monster1
or some kind of associated name: Level1.Enemies.Monster1
. And each time we have to allocate a string, convert it to a valid path, go to the file manager and ask: did you have a file ...
? You see that this process is rather slow and memory consuming. To fix this, we will perform the following optimization: let’s convert all our file paths to some fixed format (unix-like paths, dot-separated paths, etc.) and then perform hashing. As a result, it will become possible to use simple integers as resource identifiers through all our system and go back to the path representation only when the file is actually needed to be loaded. The hashing can be performed with any simple algorithm, such as crc32, and can be done at compile-time on modern C++ versions:
With this code any string can be hashed at runtime via crc32
function, or at compile time by wrapping it with STRING_ID("smth")
macro or by adding "smth"_id
postfix. After that we can create a global hash table which will map string hashes to actual paths and perform fast look-ups based on hash. And even if our database become so large that the collision will occur, we will easily spot it at the time of engine initialization, as all our files are contained inside project root directory. In such cases the developer simply need to think up a different name for a file, which is not a big deal in most cases.
There is even more to cover
In this article I tried to describe the techniques which are already implemented in MxEngine. We did not cover actual resource processing or Unreal Engine-like resource databases. If you want a more deep understanding of file management, consider reading chapters 6 and 7 of Game Engine Architecture book which contains a lot of examples of how different engines deal with this task. This field in engine development is rather complex, so there is no silver bullet - feel free to experiment, try something new and do not forget share your ideas with others!
I hope that this little article was useful for you. As always, if you have any questions about the topic or know how to make MxEngine better, feel free to contact me on GitHub or create an issue in project repo describing your ideas. If one day the engine become large enough to write a second part, I will definitely do so and notify you here.