Skip to content

Commit a2dc357

Browse files
committed
Add DEFAULT_ADDITIONAL_DIRS CMake option
The main motivation behind this change is to make it easier to package Descent 3 for Linux. For example, let’s say that you’re packaging Descent 3 for Debian and you want to make the package work well with Debian’s game-data-packager. The package for the Descent 3 engine will put d3-linux.hog in one directory, and the package for the proprietary Descent 3 game data will put d3.hog into a different directory [1]. The Descent 3 engine package can use the DEFAULT_ADDITIONAL_DIRS CMake option in order to make sure that both d3-linux.hog and d3.hog are located automatically. For the example value in USAGE.md, I decided to use Windows paths in order to showcase the fact that you have to escape backslashes. [1]: <#373 (comment)>
1 parent d143d08 commit a2dc357

8 files changed

Lines changed: 98 additions & 15 deletions

File tree

BUILD.md

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -179,14 +179,15 @@ cmake --preset linux -DENABLE_LOGGER=ON
179179

180180
**NOTE:** CMake variables, or more technically _CMake cache entries_, will persist in their values until they are explicitly cleared. So, if you set a variable and then run another CMake command _without_ that variable specified, the variable will still be set. Variables must be explicitly unset, or the `builds/` directory cleaned, in order to be cleared.
181181

182-
| Option | Description | Default |
183-
|--------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------|
184-
| `CMAKE_BUILD_TYPE` | `Debug` builds are generally larger, slower and contain extra correctness checks that will validate game data and interrupt gameplay when problems are detected.<br>`Release` builds are optimized for size and speed and do not include debugging information, which makes it harder to find problems. | `Debug` |
185-
| `BUILD_EDITOR` | _(Windows-only)_ Build internal editor. | `OFF` |
186-
| `BUILD_TESTING` | Enable testing. Requires GTest. | `OFF` |
187-
| `ENABLE_LOGGER` | Enable logging to the terminal. | `OFF` |
188-
| `ENABLE_MEM_RTL` | Enable Real-time library memory management functions (disable to verbose memory allocations). | `ON` |
189-
| `FORCE_COLORED_OUTPUT` | Always produce ANSI-colored compiler warnings/errors (GCC/Clang only; esp. useful with Ninja). | `OFF` |
190-
| `FORCE_PORTABLE_INSTALL` | Install all files into local directory defined by `CMAKE_INSTALL_PREFIX`. | `ON` |
191-
| `USE_EXTERNAL_PLOG` | Use system plog library. | `OFF` |
192-
| `USE_VCPKG` | Explicitly control whether or not to use vcpkg for dependency resolution. `ON` requires the environment variable `VCPKG_ROOT` to be set. | Determined by the existence of `VCPKG_ROOT` in the environment: If it exists, vcpkg is used. |
182+
| Option | Description | Default |
183+
|---------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------|
184+
| `CMAKE_BUILD_TYPE` | `Debug` builds are generally larger, slower and contain extra correctness checks that will validate game data and interrupt gameplay when problems are detected.<br>`Release` builds are optimized for size and speed and do not include debugging information, which makes it harder to find problems. | `Debug` |
185+
| `BUILD_EDITOR` | _(Windows-only)_ Build internal editor. | `OFF` |
186+
| `BUILD_TESTING` | Enable testing. Requires GTest. | `OFF` |
187+
| `DEFAULT_ADDITIONAL_DIRS` | A list of directories that Descent 3 will use as read-only base directories (see [USAGE.md’s Base directories section](./USAGE.md#base-directories)). This value gets interpreted as a C++ expression, so you’ll need to use C++’s syntax. Here’s an example of a valid value: <pre lang=cpp>`{"C:\\Games\\Descent3\\", "D:\\"}`</pre> | `{}` |
188+
| `ENABLE_LOGGER` | Enable logging to the terminal. | `OFF` |
189+
| `ENABLE_MEM_RTL` | Enable Real-time library memory management functions (disable to verbose memory allocations). | `ON` |
190+
| `FORCE_COLORED_OUTPUT` | Always produce ANSI-colored compiler warnings/errors (GCC/Clang only; esp. useful with Ninja). | `OFF` |
191+
| `FORCE_PORTABLE_INSTALL` | Install all files into local directory defined by `CMAKE_INSTALL_PREFIX`. | `ON` |
192+
| `USE_EXTERNAL_PLOG` | Use system plog library. | `OFF` |
193+
| `USE_VCPKG` | Explicitly control whether or not to use vcpkg for dependency resolution. `ON` requires the environment variable `VCPKG_ROOT` to be set. | Determined by the existence of `VCPKG_ROOT` in the environment: If it exists, vcpkg is used. |

Descent3/init.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1414,6 +1414,9 @@ void InitIOSystems(bool editor) {
14141414
ddio_SetWorkingDir(writable_base_directory.u8string().c_str());
14151415
cf_AddBaseDirectory(writable_base_directory);
14161416

1417+
// Set the default base directories
1418+
cf_AddDefaultBaseDirectories();
1419+
14171420
// Set any additional base directories
14181421
auto additionaldirarg = 0;
14191422
while (0 != (additionaldirarg = FindArg("-additionaldir", additionaldirarg))) {

USAGE.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,18 +92,19 @@ Descent 3 has two types of base directories:
9292
- The writable base directory can contain both read-write files and read-only files. There is only one writeable base directory. By default, the writable base directory gets set to the current working directory.
9393
- The read-only base directories can only contain read-only files. There can be any number of read-only base directories. By default, Descent 3 uses zero read-only base directories.
9494

95-
You can set the writable base directory and the list of read-only base directories using the `-setdir`, `-useexedir` and `-additionaldir` command-line options (see [the next section](#command-line-options)).
95+
You can set the writable base directory and the list of read-only base directories using the `-setdir`, `-useexedir` and `-additionaldir` command-line options (see [the next section](#command-line-options)). Descent 3 also has a list of default read-only base directories. Normally, the list of default read-only base directories is empty, but you can change it by using the `DEFAULT_ADDITIONAL_DIRS` CMake option when compiling Descent 3 (see [BUILD.md’s Build Options section](./BUILD.md#build-options)).
9696

9797
When Descent 3 tries to find a read-only file, then it will look through the list of base directories in this order:
9898

9999
- the last read-only base directory that was specified on the command-line,
100100
- the second-to-last read-only base directory that was specified on the command-line,
101101
- the third-to-last read-only base directory that was specified on the command-line,
102102
-
103-
- the first read-only base directory that was specified on the command-line and, finally,
103+
- the first read-only base directory that was specified on the command-line,
104+
- all of the items on the `DEFAULT_ADDITIONAL_DIRS` list in reverse order, and, finally,
104105
- the writable base directory.
105106

106-
Files that are in base directories that are higher on that list will override files that are in base directories that are lower on that list. For example, lets say that you run Descent 3 like this:
107+
Files that are in base directories that are higher on that list will override files that are in base directories that are lower on that list. For example, lets say that the `DEFAULT_ADDITIONAL_DIRS` list is empty and that you run Descent 3 like this:
107108

108109
```
109110
Descent3 -setdir /home/user/my-writable-base-directory -additionaldir /home/user/my-read-only-base-directory
@@ -527,6 +528,7 @@ The following command-line options are available in Descent 3. You can set comma
527528

528529
**Description:** Tells Descent 3 to use the directory in which the executable is located as the writable base directory.
529530

531+
530532
### Other Options
531533

532534
- `-logfile`

cfile/CMakeLists.txt

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
set(HEADERS
22
cfile.h
3+
default_base_directories.h
34
hogfile.h
45
inffile.h)
56
set(CPPS
@@ -8,7 +9,15 @@ set(CPPS
89
inffile.cpp
910
)
1011

11-
add_library(cfile STATIC ${HEADERS} ${CPPS})
12+
set(DEFAULT_ADDITIONAL_DIRS "{}" CACHE STRING "A list of directories that Descent 3 will use as read-only base directories.")
13+
set(DBD_CPP ${CMAKE_CURRENT_BINARY_DIR}/default_base_directories.cpp)
14+
configure_file(
15+
${CMAKE_CURRENT_SOURCE_DIR}/default_base_directories.cpp.in
16+
${DBD_CPP}
17+
@ONLY
18+
)
19+
20+
add_library(cfile STATIC ${HEADERS} ${CPPS} ${DBD_CPP})
1221
target_link_libraries(cfile PRIVATE
1322
ddebug
1423
ddio

cfile/cfile.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include "byteswap.h"
3131
#include "crossplat.h"
3232
#include "cfile.h"
33+
#include "default_base_directories.h"
3334
#include "ddio.h"
3435
#include "hogfile.h" //info about library file
3536
#include "log.h"
@@ -78,6 +79,17 @@ cfile_error cfe;
7879
// The message for unexpected end of file
7980
const char *eof_error = "Unexpected end of file";
8081

82+
/* The user can specify a list of default read-only base directories by setting
83+
* the -DDEFAULT_ADDITIONAL_DIRS CMake option. This function adds those base
84+
* directories to the list of base directories that the game is currently
85+
* using.
86+
*/
87+
void cf_AddDefaultBaseDirectories() {
88+
for (const auto &base_directory : Default_read_only_base_directories) {
89+
cf_AddBaseDirectory(base_directory);
90+
}
91+
}
92+
8193
/* This function should be called at least once before you use anything else
8294
* from this module.
8395
*/

cfile/cfile.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,13 @@ extern std::vector<std::filesystem::path> Base_directories;
155155
*/
156156
void cf_AddBaseDirectory(const std::filesystem::path &base_directory);
157157

158+
/* The user can specify a list of default read-only base directories by setting
159+
* the -DDEFAULT_ADDITIONAL_DIRS CMake option. This function adds those base
160+
* directories to the list of base directories that the game is currently
161+
* using.
162+
*/
163+
void cf_AddDefaultBaseDirectories();
164+
158165
/* After you call this function, you must call cf_AddBaseDirectory() at least
159166
* once before you use anything else from this module.
160167
*/
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* Descent 3
3+
* Copyright (C) 2024 Descent Developers
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
*/
18+
#include <filesystem>
19+
#include <initializer_list>
20+
21+
#include "default_base_directories.h"
22+
23+
const std::initializer_list<std::filesystem::path> Default_read_only_base_directories = @DEFAULT_ADDITIONAL_DIRS@;

cfile/default_base_directories.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Descent 3
3+
* Copyright (C) 2024 Descent Developers
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
*/
18+
#ifndef DEFAULT_ADDITIONAL_DIRECTORIES_H
19+
#define DEFAULT_ADDITIONAL_DIRECTORIES_H
20+
21+
#include <filesystem>
22+
#include <initializer_list>
23+
24+
extern const std::initializer_list<std::filesystem::path> Default_read_only_base_directories;
25+
26+
#endif

0 commit comments

Comments
 (0)