Commit Graph

56 Commits

Author SHA1 Message Date
Jason Yundt
f5d2a43863 Add -aditionaldir option
Before this change, Descent 3 would look for all of its game data files
in a single directory. This change allows users to spread out Descent
3’s game data over multiple directories.

Building Descent 3 produces multiple files that can be freely
redistributed (Descent3, d3-linux.hog, online/Direct TCP~IP.d3c, etc.).
Running Descent 3 requires those files and several additional files that
cannot be freely redistributed. Before this change, the files that were
redistributable had to be in the same directory as the files that were
not redistributable. This change makes it so that they can be in
separate directories.

The main motivation behind this change is to allow people to package
Descent 3 for Linux in a reasonable manner. For the most part, binary
packages for Descent 3 will contain all of the freely redistributable
components. Package managers will copy those components into system
directories that are owned by root and that users probably shouldn’t
edit manually. Users will then create a new directory and copy the game
data from their copy of Descent 3 into that new directory. Users will
then be able to run:

  Descent3 -setdir <path-to-proprietary-files> -additionaldir <path-to-open-source-files>

The -additionaldir option can also be used to support more complicated
scenarios. For example, if the user is using Debian’s
game-data-packager [1], then they would do something like this:

  Descent3 -setdir <path-to-writable-directory> -additionaldir <path-to-gdp-directory> -additionaldir <path-to-open-source-files>

The -additionaldir option can also be used to load a mod that replaces
.hog files:

  Descent3 -setdir <path-to-base-game-data> -additionaldir <path-to-mod-files>

[1]: <https://github.com/DescentDevelopers/Descent3/issues/373#issuecomment-2120330650>
2024-09-29 14:07:53 -04:00
Jason Yundt
9d08314986 Consolidate case-sensitive filesystem functions
Descent 3 is case-insensitive. It doesn’t matter if a file is named
“ppics.hog” or “PPPICS.HOG”. Descent 3 will load the file regardless. In
order to accomplish this, Descent 3 has to have special code for
case-sensitive filesystems. That code must take a fake case-insensitive
path and turn it into a real case-sensitive path.

Before this change, there was multiple chunks of code that helped turn
fake case-insensitive paths into real case-sensitive paths. There was
cf_FindRealFileNameCaseInsenstive(), mve_FindMovieFileRealName() and a
chunk of code in open_file_in_directory() that only exists if __LINUX__
is defined. This removes each of those pieces of code and replaces them
with a new cf_LocatePath() function.

Using the new cf_LocatePath() function has two main advantages over the
old way of doing things. First, having a single function is simpler than
having three different pieces of code. Second, the new cf_LocatePath()
function will make it easier to create a future commit. That future
commit will make Descent 3 look for files in more than just the -setdir
directory. Having a single function that’s responsible for determining
the true path of a file will make it much easier to create that future
commit.
2024-09-29 12:51:15 -04:00
Jason Yundt
fc5f732347 Rename cf_FindRealFileNameCaseInsensitive()’s parameters
The main motivation behind this commit is to make it easier to create a
future commit. That futures commit will rename the
cf_FindRealFileNameCaseInsensitive() function and change its code
slightly. I was struggling to create that future commit because I found
the code in cf_FindRealFileNameCaseInsensitive() difficult to
understand. This change will make it easier to create that future commit
by making the code in cf_FindRealFileNameCaseInsensitive() easier to
understand.
2024-09-29 09:38:25 -04:00
Jason Yundt
563b9459f2 Move Base_directory into the cfile module
The main motivation behind this commit is to make it easier to create a
future commit. That future commit will will take multiple different
functions from throughout the codebase and replace them with a new
function named cf_LocatePath().

One of the functions that will get replaced is
cf_FindRealFileNameCaseInsensitive(). There are tests for
cf_FindRealFileNameCaseInsensitive() in cfile/tests/cfile_tests.cpp.
When I make that future commit, I will have to change the tests in
cfile/test/cfile_tests.cpp so that they test the cf_LocatePath()
function instead of the cf_FindRealFileNameCaseInsensitive() function.

There is an important difference between cf_LocatePath() and
cf_FindRealFileNameCaseInsensitive(). cf_LocatePath() depends on the
Base_directory variable. cf_FindRealFileNameCaseInsensitive() does not.
In order to update the tests so that they use cf_LocatePath(), I need to
make sure that the tests have access to the Base_directory variable.

Before this change, the Base_directory variable was declared in
Descent3/init.cpp. That meant that if a program wanted to access
Base_directory, then it would have to link to Descent3/init.cpp. In
other words, we would have to link to Descent3/init.cpp when compiling
the program that currently tests cf_FindRealFileNameCaseInsensitive()
but will test cf_LocatePath() in the future. I tried making that program
link to Descent3/init.cpp, but I gave up after a wile. Descent3/init.cpp
depends on a lot of other things in the codebase.

In order to make it easier to create that future commit, this commit
moves the Base_directory variable into the cfile module. When I create
that future commit, I won’t have to mess with anything linking related
because the cfile tests already link to the cfile module. Additionally,
this change will make compiling that test program more efficient.
There’s not need for the compiler to look at the entirety of
Descent3/init.cpp just because we need a single variable from it.
2024-09-29 09:15:06 -04:00
Azamat H. Hackimov
245f9c6425 Remove unused cf_LibraryFindFirst() and related to it functions 2024-09-26 01:49:55 +03:00
Azamat H. Hackimov
5a5020273e Implementing cf_DoForeachFileInLibrary()
This function is intended to replace cf_LibraryFindFirst()/cf_LibraryFindNext() functions, like ddio_DoForeachFile().
2024-09-26 01:49:55 +03:00
Azamat H. Hackimov
9c430b861b Remove unused cf_ChangeFileAttributes() 2024-09-26 01:49:55 +03:00
Jan Engelhardt
3c9234c4c0 mem_malloc type triviality checks (8/8)
(Manual modification.) Change mem_malloc callsites with
non-fundamental types to mem_rmalloc.
2024-09-16 22:12:14 +02:00
Jan Engelhardt
c7da9daee2 mem_malloc type triviality checks (2/8)
Automated replacement with

```
git grep -l mem_malloc |
xargs perl -i -lpe 's{\((\w+) \*\)mem_malloc\(sizeof\(\1\) \* (\S+)\)}{mem_rmalloc<$1>($2)}'
```
2024-09-16 22:12:12 +02:00
Azamat H. Hackimov
9d2aaf12f2 Convert cfile to use new logging facility 2024-09-10 03:00:25 +03:00
Jan Engelhardt
571c0199e9 Resolve use-after-free in cf_CopyFile
==92396==ERROR: AddressSanitizer: heap-use-after-free on address 0x504000034ae8 at pc 0x000000f86037 bp 0x7fffffffa9d0 sp 0x7fffffffa9c8
READ of size 4 at 0x504000034ae8 thread T0
    f0 cf_CopyFile(std::filesystem::__cxx11::path const&, std::filesystem::__cxx11::path const&, int) $GIT/cfile/cfile.cpp:856
    f1 _extractscript(char*, char*) $GIT/Descent3/OsirisLoadandBind.cpp:3109
    f2 Osiris_ExtractScriptsFromHog(int, bool) $GIT/Descent3/OsirisLoadandBind.cpp:3185
    f3 InitIOSystems $GIT/Descent3/init.cpp:1549
    f4 InitD3Systems1(bool) $GIT/Descent3/init.cpp:1827
2024-08-30 01:25:56 +02:00
Azamat H. Hackimov
79db9aa3e2 Move platform-specific defines and macros to own file crossplat.h 2024-08-20 23:09:27 +03:00
Chris Sarbora
3da53e567f
Programmatically replace all __LINUX__ with POSIX
Steps:
```sh
srcs=$(mktemp)
find . -name "*.cpp" -or -name "*.c" -or -name "*.h" -or -name "*.hpp" -or -name "*.cc" -or -name "*.hh" > $srcs
xargs sed -i 's/ifndef __LINUX__/if !defined(POSIX)/' < $srcs
xargs sed -i 's/ifdef __LINUX__/if defined(POSIX)/' < $srcs
xargs sed -i 's/defined(__LINUX__)/defined(POSIX)/' < $srcs
```
2024-07-12 18:11:54 -05:00
Louis Gombert
de75a80ae7
Merge pull request #475 from winterheart/cfile-std-filesystem
cfile / ddio: use most of functions to std::filesystem::path
2024-07-06 23:25:12 +00:00
Azamat H. Hackimov
2fce643ae8 Use case-insensitive search for cf_OpenLibrary() on all platforms 2024-07-05 01:50:57 +03:00
Azamat H. Hackimov
7a1f08e811 Fix unittests on macOS
Surprisingly on macOS FS is case-insensitive.
2024-07-05 01:50:57 +03:00
Azamat H. Hackimov
b3affdb105 Use new cf_FindRealFileNameCaseInsensitive() function in code
A little speed-up for our slowpoke.
2024-07-05 01:50:57 +03:00
Azamat H. Hackimov
1a3c012228 Refactor cf_FindRealFileNameCaseInsensitive() function
Implementing cf_FindRealFileNameCaseInsensitive_new() based on std::fs::directory_iterator, which runs faster and more reliable.

cf_FindRealFileNameCaseInsensitive_new() works exactly as cf_FindRealFileNameCaseInsensitive().

Benchmark on Release build:

```
-------------------------------------------------------------------
Benchmark                         Time             CPU   Iterations
-------------------------------------------------------------------
cfile_no_exist_old            11607 ns        11399 ns        63035
cfile_no_exist_new             9302 ns         9196 ns        70041
cfile_exist_relative_old       2701 ns         2480 ns       282695
cfile_exist_relative_new        867 ns          866 ns       798478
cfile_exist_absolute_old       3415 ns         3167 ns       228323
cfile_exist_absolute_new       1182 ns         1181 ns       574782
```
2024-07-05 01:50:57 +03:00
Azamat H. Hackimov
f44f6a4bbd CFILE: change signature of cf_FindRealFileNameCaseInsenstive()
Moved directory parameter to 3rd position and make it optional.
2024-07-05 01:50:57 +03:00
Azamat H. Hackimov
65520373e6 CFILE: Remove duplicated code in open_file_in_directory()
Refactoring platform-dependent parts of code into common part, greatly reduce LOC.
2024-07-05 01:50:57 +03:00
Azamat H. Hackimov
15ec76dd64 CFILE: Change cf_FindRealFileNameCaseInsenstive() to use std::fs::path 2024-07-05 01:50:57 +03:00
C.W. Betts
21e1384a3c Create an overloaded cf_ReadInt and cf_ReadShort that takes a bool to indicate little endian reads.
As suggested by @winterheart
2024-07-02 18:34:45 -06:00
Azamat H. Hackimov
81555afd15 CFILE: convert cf_Diff() to use std::fs::path
Convert cf_Diff() and underlying functions to use std::filesystem::path
2024-07-01 12:50:49 +03:00
Azamat H. Hackimov
c47210be75 CFILE: convert cf_CopyFile() cf_CopyFileTime() to use std::fs::path
Convert cf_CopyFile() cf_CopyFileTime() and underlying functions to use std::filesystem::path
2024-07-01 12:50:49 +03:00
Azamat H. Hackimov
8d14ae025c CFILE: migrate more functions to use std::fs::path
Convert cf_IsFileInHog(), cf_OpenFileInLibrary() and cf_GetfileCRC() functions to use std::fs::path.
2024-07-01 12:50:49 +03:00
Azamat H. Hackimov
0ea758084f CFILE: refactor internal paths and extensions
Refactor paths and extensions to use std::map. Rewriting related code in manage.cpp.
2024-07-01 12:50:48 +03:00
Azamat H. Hackimov
33e8802363 CFILE: make path_entry struct using std::fs::path
Simplify related code in `cf_SetSearchPath()`.
2024-07-01 12:50:48 +03:00
Azamat H. Hackimov
82084e9176 Convert some of cfile functions to use std::filesystem
Make cfopen(), open_file_in_directory() and cfexist() use std::filesystem::path arguments.
2024-07-01 12:50:48 +03:00
GravisZro
f7ae658fe2 Modernize struct/enum/unions type declarations 2024-05-30 03:35:17 -04:00
GravisZro
9cf9781369 Properly format/use variadic macros 2024-05-24 20:57:17 -04:00
GravisZro
5e5e0c99c3 Replace "short" with "int16_t" and fix missing headers 2024-05-23 23:49:31 -04:00
GravisZro
26b7776f43 Replace "ubyte" with "uint8_t" 2024-05-23 23:07:26 -04:00
GravisZro
1618040db5 Replace "unsigned char" with "uint8_t" 2024-05-23 22:58:46 -04:00
GravisZro
2147cfa68b Replace "unsigned int" with "uint32_t" 2024-05-23 22:51:16 -04:00
Azamat H. Hackimov
3fa32ff386 Reorder headers related to linux module 2024-05-19 12:21:43 +03:00
Azamat H. Hackimov
43caa3d53c CFILE: Remove throwing exception in cf_ReadBytes()
All functions, that uses cf_ReadBytes(), checks return size, but don't handle potential exceptions. That leads to segfaults on reading of damaged files (i.e. broken savegames).
2024-05-16 23:27:56 +03:00
Azamat H. Hackimov
c505df182b CFILE: Use smart pointers for entries of library 2024-05-16 23:12:07 +03:00
Azamat H. Hackimov
f123dbe770 Replacing raw pointer with shared_ptr in cfile. 2024-05-16 22:31:43 +03:00
Azamat H. Hackimov
b84678de96 Move HOG2 magic check into hogfile
Remove unused cf_ReadHogFileEntry() function.
2024-05-16 19:18:31 +03:00
Azamat H. Hackimov
6703b25d34 Minor fixes to cfile 2024-05-16 18:21:30 +03:00
Jason Yundt
d5e893fcb4 Prevent potential uninitialized memory access
Before this change, cf_OpenLibrary() did something along the lines of
this:

  char id[4];
  fread(id, 4, 1, fp);
  strncmp(id, "HOG2", 4);

If fread() finishes successfully, then that code is fine. However,
fread() might encounter an error or bump into the end of a file. In
those scenarios, the value of id will not necessarily be initialized
[1]. In other words, when fread() fails, strncmp() might operate on
uninitialized memory.

This change makes sure that the value of id only gets used if fread()
succeeds. Additionally, this change fixes a GCC warning about ignoring
fread()’s return value.

[1]: <https://en.cppreference.com/w/cpp/io/c/fread>
2024-05-12 20:38:35 -04:00
Jason Yundt
e042925001 Stop using hard coded value for HOG2 id length
Before this change, cf_OpenLibrary() was inconsistent. Sometimes, it
would refer to the size of a HOG2 id using the literal 4. Other times it
would refer to the size of a HOG2 id using strlen(HOG_TAG_STR). There
was a good reason for this. Some compilers allow you to do this:

  char id[strlen(HOG_TAG_STR)];

Other compilers throw an error if you try to do that [1].

This commit makes cf_OpenLibrary() more consistent. It makes it so that
cf_OpenLibrary() always uses the same constant expression when referring
to HOG_TAG_STR’s length.

[1]: <https://devblogs.microsoft.com/oldnewthing/20221114-00/?p=107393>
2024-05-12 20:38:32 -04:00
C.W. Betts
c6da74b069
Mark functions and variables as static (#185)
* Begin by marking functions and variables as static when needed.

* More work.

* More work.

* More pokes.

* More work.

* More work.

* Initial work on the netgames.

* Revert changes to the license header on source files.

* clutter.cpp poke.

* One final poke.

* Move some declarations to headers:

Move paged_in_count and paged_in_num to gamesequence.h
Move DoneLightInstance and StartLightInstance to polymodel.h

* Look over the AI script/plug-ins.

* Going over the changes one last time.

* Fix rebase errors.

* More migration from bare statics to static inlines.
2024-05-07 23:35:28 +02:00
Phil Ashby
24e9b50779 Do not attempt case-sensitive processing on Windows 2024-05-06 19:08:31 +01:00
Phil Ashby
583aac6471 Movies now load from case-insensitive file names 2024-05-06 18:49:39 +01:00
Phil Ashby
b5ba5f2a91 Support case-insensitive loading of HOG files 2024-05-06 17:52:45 +01:00
Ryan C. Gordon
6c8977caf0
Heavy patching for compiler warnings.
The vast majority of this is fixing up `char *` that should be `const char *`
but a handful of other fixes, like potential buffer overflows that GCC
noticed, etc, were applied as well.

This removes `-Wno-write-strings` from CMakeLists.txt, as it is no longer
necessary, as there is no longer a flood of compiler warning spam when
building.

This does not fix all compiler warnings; there are still a handful, and they
are legitimate, but they can be dealt with in a future commit.
2024-04-29 00:18:56 -04:00
Chris Sarbora
e6ba1906c9
Remove MacOS (Classic) code and all references (1/3)
The MACINTOSH define refers to MacOS Classic (not OS X) which we do not
plan to support. Rather than carry the cruft forever, let's delete it.

NOTE: legacy/ is unused but we're keeping it around, so MACINTOSH uses
there are left alone.

Process used for this commit:
```
git rm -r mac
find . \( -iname "*.c" -or -iname "*.cpp" -or -iname "*.h" \) \
  -exec unifdef -UMACINTOSH -o {} {} \;
git restore legacy
git add .
```

Test Plan:
On Mac, build both Debug and Release
```
cmake --build --preset mac --config Debug
cmake --build --preset mac --config Release
```
2024-04-27 07:38:14 -07:00
Azamat H. Hackimov
4826c37a69 Add BUILD_INTERFACE to cfile module
Other modules that depends on it, can reuse includes on linking.
There some files formally not belonging any packages (lib directory), as workaround there temporary include_directories(cfile) on root of project. After migrating all modules this can be removed.
2024-04-24 00:41:02 +03:00
Azamat H. Hackimov
63b4284658 Fix float reading in cfile
Whoah, big surprise float_t and double_t may have different length. Still waiting float32_t and float64_t types from C++23...
2024-04-24 00:40:41 +03:00