Implementing base directory populating on init

After initialization, we get following base directories list (in priority order):

* Writable preference path
* User defined paths (cmd-line and configuration)
* Platform defined paths (such as /usr/share/Descent3 on Linux or Steam installation paths on all platforms (TBD))
* Directory of executable

Removing `-setdir` and `-useexedir` as redundant (both can be replaced with `-additionaldir` option).
This commit is contained in:
Azamat H. Hackimov
2024-10-08 11:54:14 +03:00
committed by Louis Gombert
parent e511926408
commit 8800f1b4c7
7 changed files with 106 additions and 45 deletions

View File

@@ -103,6 +103,12 @@ if(FORCE_PORTABLE_INSTALL)
set(CMAKE_INSTALL_LIBDIR ".")
set(CMAKE_INSTALL_DATADIR ".")
set(CMAKE_INSTALL_DOCDIR ".")
set(D3_DATADIR "${CMAKE_INSTALL_DATADIR}")
else()
set(CMAKE_INSTALL_DATADIR "${CMAKE_INSTALL_DATADIR}/Descent3")
# On system install files will go into /usr/share/Descent3
set(D3_DATADIR "${CMAKE_INSTALL_FULL_DATADIR}/Descent3")
endif()
# Get info about multi-config environment

View File

@@ -294,6 +294,12 @@ if(WIN32)
)
endif()
configure_file(
${CMAKE_SOURCE_DIR}/lib/d3_platform_path.h.in
${CMAKE_BINARY_DIR}/lib/d3_platform_path.h
@ONLY
)
if(UNIX AND NOT APPLE)
set(PLATFORM_LIBS m ${CMAKE_DL_LIBS})
endif()

View File

@@ -921,6 +921,7 @@
#include "init.h"
#include "config.h"
#include "3d.h"
#include "d3_platform_path.h"
#include "hlsoundlib.h"
#include "manage.h"
#include "bitmap.h"
@@ -1004,6 +1005,9 @@ bool Running_editor = false; // didn't we have a variable like this somewhere
static bool Init_in_editor = false;
// Base directory from configuration path
std::filesystem::path config_base_directory;
// used to update load bar.
static void SetInitMessageLength(const char *c, float amount); // portion of total bar to fill (0 to 1)
extern void UpdateInitMessage(float amount); // amount is 0 to 1
@@ -1196,6 +1200,9 @@ void SaveGameSettings() {
Database->write("Default_pilot", Default_pilot.c_str(), strlen(Default_pilot.c_str()) + 1);
else
Database->write("Default_pilot", " ", 2);
Database->write("GAME_base_directory", config_base_directory.u8string().c_str(),
strlen(config_base_directory.u8string().c_str()) + 1);
}
/*
@@ -1258,6 +1265,11 @@ void LoadGameSettings() {
}
}
templen = TEMPBUFFERSIZE;
if (Database->read("GAME_base_directory", tempbuffer, &templen)) {
config_base_directory = tempbuffer;
}
templen = TEMPBUFFERSIZE;
if (Database->read("RS_gamma", tempbuffer, &templen)) {
Render_preferred_state.gamma = strtod(tempbuffer, &stoptemp);
@@ -1392,45 +1404,58 @@ void LoadGameSettings() {
I/O systems initialization
*/
void InitIOSystems(bool editor) {
ddio_init_info io_info;
// Read in stuff from the registry
INIT_MESSAGE(("Reading settings."));
LoadGameSettings();
// Set the writable base directory
int dirarg = FindArg("-setdir");
int exedirarg = FindArg("-useexedir");
std::filesystem::path writable_base_directory;
if (dirarg) {
writable_base_directory = GameArgs[dirarg + 1];
} else if (exedirarg) {
char exec_path[_MAX_PATH];
memset(exec_path, 0, sizeof(exec_path));
// Populate exec_path with the executable path
if (!ddio_GetBinaryPath(exec_path, sizeof(exec_path))) {
Error("Failed to get executable path\n");
} else {
std::filesystem::path executablePath(exec_path);
writable_base_directory = executablePath.parent_path();
LOG_INFO << "Using working directory of " << writable_base_directory;
}
} else {
writable_base_directory = std::filesystem::current_path();
/*
* Populate base directories. In result, we have the following list of directories (in priority order):
* - Writable preference path
* - User defined paths (cmd-line and configuration)
* - Platform defined paths (such as /usr/share/Descent3 on Linux or Steam installation paths)
* - Directory of executable
*/
// Writable preference path
std::filesystem::path pref_path = ddio_GetPrefPath(D3_PREF_ORG, D3_PREF_APP);
if (pref_path.empty()) {
Error("Failed to get preference path!");
}
LOG_INFO << "Setting writable preference path " << pref_path;
cf_AddBaseDirectory(pref_path);
ddio_SetWorkingDir(writable_base_directory.u8string().c_str());
cf_AddBaseDirectory(writable_base_directory);
// Set any additional base directories
auto additionaldirarg = 0;
// Additional paths
int additionaldirarg = 0;
while (0 != (additionaldirarg = FindArg("-additionaldir", additionaldirarg))) {
const auto dir_to_add = GetArg(additionaldirarg + 1);
if (dir_to_add == NULL) {
LOG_WARNING
<< "-additionaldir was at the end of the argument list. It should never be at the end of the argument list.";
if (dir_to_add == nullptr) {
LOG_WARNING << "-additionaldir requires directory path as value.";
break;
} else {
cf_AddBaseDirectory(std::filesystem::path(dir_to_add));
cf_AddBaseDirectory(dir_to_add);
additionaldirarg += 2;
}
}
// Path from configuration
if (!config_base_directory.empty()) {
cf_AddBaseDirectory(config_base_directory);
}
// Platform dependent paths
std::filesystem::path platform_dir = std::filesystem::canonical(D3_DATADIR);
cf_AddBaseDirectory(platform_dir);
// TODO: add Steam/registry locations
// Add path of executable
std::filesystem::path exec_path = ddio_GetBasePath();
// Populate exec_path with the executable path
if (exec_path.empty() || exec_path == platform_dir) {
LOG_DEBUG << "Skipping adding executable path (empty or redundant path).";
} else {
cf_AddBaseDirectory(exec_path);
}
LOG_INFO << "Base directories: " << cf_LocateMultiplePaths("");
Descent->set_defer_handler(D3DeferHandler);
@@ -1447,6 +1472,7 @@ void InitIOSystems(bool editor) {
#endif
// do io init stuff
ddio_init_info io_info{};
io_info.obj = Descent;
INIT_MESSAGE(("Initializing DDIO systems."));
@@ -1461,10 +1487,6 @@ void InitIOSystems(bool editor) {
RTI_MATCENFRAMETIME);
RTP_ENABLEFLAGS(RTI_OBJFRAMETIME | RTI_AIFRAMETIME | RTI_PROCESSKEYTIME);
// Read in stuff from the registry
INIT_MESSAGE(("Reading settings."));
LoadGameSettings();
// Setup temp directory
INIT_MESSAGE(("Setting up temp directory."));
SetupTempDirectory();

View File

@@ -54,13 +54,10 @@ struct library {
FILE *file = nullptr; // pointer to file for this lib, if no one using it
};
/* The "root" directories of the D3 file tree
*
* Directories that come later in the list override directories that come
* earlier in the list. For example, if Base_directories[0] / "d3.hog" exists
* and Base_directories[1] / "d3.hog" also exists, then the one in
* Base_directories[1] will get used. The one in Base_directories[0] will be
* ignored.
/*
* List of base directories of the D3 file tree.
* Directories at the top of the list have higher priority.
* First entry should be a writable directory.
*/
std::vector<std::filesystem::path> Base_directories = {};
@@ -82,7 +79,7 @@ const char *eof_error = "Unexpected end of file";
* from this module.
*/
void cf_AddBaseDirectory(const std::filesystem::path &base_directory) {
if (std::filesystem::exists(base_directory)) {
if (std::filesystem::exists(base_directory) && std::filesystem::is_directory(base_directory)) {
Base_directories.push_back(base_directory);
} else {
LOG_WARNING << "Ignoring nonexistent base directory: " << base_directory;

28
lib/d3_platform_path.h.in Normal file
View File

@@ -0,0 +1,28 @@
/*
* Descent 3
* Copyright (C) 2024 Descent Developers
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
// Platform-specific compile-time defined location of data files
#define D3_DATADIR "@D3_DATADIR@"
// Part of preference path (organization)
#define D3_PREF_ORG "Outrage Entertainment"
// Part of preference path (application)
#define D3_PREF_APP "Descent 3"

View File

@@ -19,6 +19,7 @@ target_include_directories(linux PUBLIC
$<BUILD_INTERFACE:
${PROJECT_SOURCE_DIR}/linux
>
${PROJECT_BINARY_DIR}/lib
${SDL3_INCLUDE_DIRS}
)
target_link_libraries(linux PRIVATE

View File

@@ -55,6 +55,8 @@
#endif
#include "appdatabase.h"
#include "d3_platform_path.h"
#include "ddio.h"
#include "linux/lnxdatabase.h"
#include "log.h"
#include "pserror.h"
@@ -68,13 +70,12 @@ oeLnxAppDatabase::oeLnxAppDatabase() {
// Open up the database file, for reading, read in all data and keep it in memory
// then close the database
const char* prefPath = SDL_GetPrefPath("Outrage Entertainment", "Descent 3");
if (prefPath == nullptr) {
std::filesystem::path prefPath = ddio_GetPrefPath(D3_PREF_ORG, D3_PREF_APP);
if (prefPath.empty()) {
LOG_FATAL << "Couldn't find preference directory!";
exit(43);
}
std::filesystem::path fileName = std::filesystem::path(prefPath) / REGISTRY_FILENAME;
SDL_free((void *)prefPath);
std::filesystem::path fileName = prefPath / REGISTRY_FILENAME;
database = new CRegistry(fileName.u8string().c_str());
database->Import();