Skip to content

Commit c58633f

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.
1 parent 67244d9 commit c58633f

File tree

7 files changed

+86
-14
lines changed

7 files changed

+86
-14
lines changed

BUILD.md

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -195,15 +195,18 @@ cmake --preset linux -DBUILD_TESTING=ON
195195

196196
You can also use the [cmake-gui](https://cmake.org/cmake/help/latest/manual/cmake-gui.1.html) front-end to easily set and view CMake cache variable values, and generate build
197197

198-
| Option | Description | Default |
199-
|--------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------|
200-
| `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. The build type can also be set using the `--config` argument with a preset. | `Debug` |
201-
| `BUILD_EDITOR` | _(Windows-only)_ Build internal editor. | `OFF` |
202-
| `BUILD_TESTING` | Enable testing. Requires GTest. | `OFF` |
203-
| `ENABLE_LOGGER` | Enable logging to the terminal. | `OFF` |
204-
| `ENABLE_MEM_RTL` | Enable Real-time library memory management functions (disable to verbose memory allocations). | `ON` |
205-
| `FORCE_COLORED_OUTPUT` | Always produce ANSI-colored compiler warnings/errors (GCC/Clang only; esp. useful with Ninja). | `OFF` |
206-
| `FORCE_PORTABLE_INSTALL` | Install all files into local directory defined by `CMAKE_INSTALL_PREFIX`. | `ON` |
198+
| Option | Description | Default |
199+
|---------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------|
200+
| `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. The build type can also be set using the `--config` argument with a preset. | `Debug` |
201+
| `BUILD_EDITOR` | _(Windows-only)_ Build internal editor. | `OFF` |
202+
| `BUILD_TESTING` | Enable testing. Requires GTest. | `OFF` |
203+
| `DEFAULT_ADDITIONAL_DIRS` | A semi-colon separated list of paths that Descent 3 will use as read-only base directories (see [USAGE.md’s Base directories section][1]). Each item in this list is placed between quotation marks in order to form a C++ expression. This will cause issues if the items in the list are not properly escaped. You can use C++ escape sequences in order to embed special characters (like `\`, `"` and `;`) in paths. Example: `C:\\Games\\Descent3\\;D:\\` | A list with one item in it. The item is an empty string. |
204+
| `ENABLE_LOGGER` | Enable logging to the terminal. | `OFF` |
205+
| `ENABLE_MEM_RTL` | Enable Real-time library memory management functions (disable to verbose memory allocations). | `ON` |
206+
| `FORCE_COLORED_OUTPUT` | Always produce ANSI-colored compiler warnings/errors (GCC/Clang only; esp. useful with Ninja). | `OFF` |
207+
| `FORCE_PORTABLE_INSTALL` | Install all files into local directory defined by `CMAKE_INSTALL_PREFIX`. | `ON` |
207208
| `OFF` |
208-
| `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. |
209-
| `CODESIGN_IDENTITY` | Sets the macOS code signing identity. If set to something besides the empty string, then the dynamic libraries put into the hog files will be signed using this identity. | The empty string, `""`. |
209+
| `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. |
210+
| `CODESIGN_IDENTITY` | Sets the macOS code signing identity. If set to something besides the empty string, then the dynamic libraries put into the hog files will be signed using this identity. | The empty string, `""`. |
211+
212+
[1]: ./USAGE.md#base-directories

Descent3/init.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/*
22
* Descent 3
33
* Copyright (C) 2024 Parallax Software
4+
* Copyright (C) 2024–2025 Descent Developers
45
*
56
* This program is free software: you can redistribute it and/or modify
67
* it under the terms of the GNU General Public License as published by
@@ -1424,6 +1425,9 @@ void InitIOSystems(bool editor) {
14241425
LOG_INFO << "Setting writable preference path " << pref_path;
14251426
cf_AddBaseDirectory(pref_path);
14261427

1428+
// Set the default base directories
1429+
cf_AddDefaultBaseDirectories();
1430+
14271431
// Additional paths
14281432
int additionaldirarg = 0;
14291433
while (0 != (additionaldirarg = FindArg("-additionaldir", additionaldirarg))) {

USAGE.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,18 +87,19 @@ Descent 3 has two types of base directories:
8787
- 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.
8888
- 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.
8989

90-
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)).
90+
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)).
9191

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

9494
- the last read-only base directory that was specified on the command-line,
9595
- the second-to-last read-only base directory that was specified on the command-line,
9696
- the third-to-last read-only base directory that was specified on the command-line,
9797
-
98-
- the first read-only base directory that was specified on the command-line and, finally,
98+
- the first read-only base directory that was specified on the command-line,
99+
- all of the items on the `DEFAULT_ADDITIONAL_DIRS` list in reverse order, and, finally,
99100
- the writable base directory.
100101

101-
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:
102+
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:
102103

103104
```
104105
Descent3 -setdir /home/user/my-writable-base-directory -additionaldir /home/user/my-read-only-base-directory

cfile/CMakeLists.txt

Lines changed: 13 additions & 0 deletions
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,6 +9,17 @@ set(CPPS
89
inffile.cpp
910
)
1011

12+
set(DEFAULT_ADDITIONAL_DIRS "" CACHE STRING "A list of directories that Descent 3 will use as read-only base directories.")
13+
string(REPLACE ";" "\",\n \"" DEFAULT_ADDITIONAL_DIRS_CPP_EXPR "${DEFAULT_ADDITIONAL_DIRS}")
14+
string(PREPEND DEFAULT_ADDITIONAL_DIRS_CPP_EXPR "\"")
15+
string(APPEND DEFAULT_ADDITIONAL_DIRS_CPP_EXPR "\"")
16+
17+
configure_file(
18+
${CMAKE_CURRENT_SOURCE_DIR}/default_base_directories.h.in
19+
${CMAKE_CURRENT_BINARY_DIR}/default_base_directories.h
20+
@ONLY
21+
)
22+
1123
add_library(cfile STATIC ${HEADERS} ${CPPS})
1224
target_link_libraries(cfile PRIVATE
1325
ddebug
@@ -20,6 +32,7 @@ target_include_directories(cfile PUBLIC
2032
$<BUILD_INTERFACE:
2133
${PROJECT_SOURCE_DIR}/cfile
2234
>
35+
PRIVATE ${CMAKE_CURRENT_BINARY_DIR} # For default_base_directories.h
2336
)
2437

2538
if(BUILD_TESTING)

cfile/cfile.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/*
22
* Descent 3
33
* Copyright (C) 2024 Parallax Software
4+
* Copyright (C) 2024–2025 Descent Developers
45
*
56
* This program is free software: you can redistribute it and/or modify
67
* it under the terms of the GNU General Public License as published by
@@ -30,6 +31,7 @@
3031
#include "byteswap.h"
3132
#include "crossplat.h"
3233
#include "cfile.h"
34+
#include "default_base_directories.h"
3335
#include "ddio.h"
3436
#include "hogfile.h" //info about library file
3537
#include "log.h"
@@ -75,6 +77,17 @@ cfile_error cfe;
7577
// The message for unexpected end of file
7678
const char *eof_error = "Unexpected end of file";
7779

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

cfile/cfile.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/*
22
* Descent 3
33
* Copyright (C) 2024 Parallax Software
4+
* Copyright (C) 2024–2025 Descent Developers
45
*
56
* This program is free software: you can redistribute it and/or modify
67
* it under the terms of the GNU General Public License as published by
@@ -155,6 +156,13 @@ extern std::vector<std::filesystem::path> Base_directories;
155156
*/
156157
void cf_AddBaseDirectory(const std::filesystem::path &base_directory);
157158

159+
/* The user can specify a list of default read-only base directories by setting
160+
* the -DDEFAULT_ADDITIONAL_DIRS CMake option. This function adds those base
161+
* directories to the list of base directories that the game is currently
162+
* using.
163+
*/
164+
void cf_AddDefaultBaseDirectories();
165+
158166
/* After you call this function, you must call cf_AddBaseDirectory() at least
159167
* once before you use anything else from this module.
160168
*/
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Descent 3
3+
* Copyright (C) 2024–2025 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+
namespace D3 {
25+
const std::initializer_list<std::filesystem::path> Default_read_only_base_directories = {
26+
@DEFAULT_ADDITIONAL_DIRS_CPP_EXPR@
27+
};
28+
} // namespace D3
29+
30+
#endif

0 commit comments

Comments
 (0)