OpenTTD Source 20261112-master-gbcbec5e63f
fileio.cpp
Go to the documentation of this file.
1/*
2 * This file is part of OpenTTD.
3 * OpenTTD 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, version 2.
4 * OpenTTD 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.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
9
10#include "stdafx.h"
11#include "core/math_func.hpp"
13#include "fileio_func.h"
14#include "debug.h"
15#include "string_func.h"
16#include "tar_type.h"
17#ifdef _WIN32
18#include <windows.h>
19#elif defined(__HAIKU__)
20#include <Path.h>
21#include <storage/FindDirectory.h>
22#else
23#include <unistd.h>
24#include <pwd.h>
25#endif
26#include <sys/stat.h>
27#include <filesystem>
28
29#include "safeguards.h"
30
32static bool _do_scan_working_directory = true;
33
34extern std::string _config_file;
35extern std::string _highscore_file;
36
39 "",
40 "save" PATHSEP,
41 "save" PATHSEP "autosave" PATHSEP,
42 "scenario" PATHSEP,
43 "scenario" PATHSEP "heightmap" PATHSEP,
44 "gm" PATHSEP,
45 "data" PATHSEP,
46 "baseset" PATHSEP,
47 "newgrf" PATHSEP,
48 "lang" PATHSEP,
49 "ai" PATHSEP,
50 "ai" PATHSEP "library" PATHSEP,
51 "game" PATHSEP,
52 "game" PATHSEP "library" PATHSEP,
53 "screenshot" PATHSEP,
54 "social_integration" PATHSEP,
55 "docs" PATHSEP,
56};
57
65std::vector<Searchpath> _valid_searchpaths;
70
77{
78 return to_underlying(sp) < _searchpaths.size() && !_searchpaths[sp].empty();
79}
80
81static void FillValidSearchPaths(bool only_local_path)
82{
83 _valid_searchpaths.clear();
84
85 std::set<std::string> seen{};
88
89 if (only_local_path) {
90 switch (sp) {
91 case Searchpath::WorkingDir: // Can be influence by "-c" option.
92 case Searchpath::BinaryDir: // Most likely contains all the language files.
93 case Searchpath::AutodownloadDir: // Otherwise we cannot download in-game content.
94 break;
95
96 default:
97 continue;
98 }
99 }
100
101 if (IsValidSearchPath(sp)) {
102 if (seen.count(_searchpaths[sp]) != 0) continue;
103 seen.insert(_searchpaths[sp]);
104 _valid_searchpaths.emplace_back(sp);
105 }
106 }
107
108 /* The working-directory is special, as it is controlled by _do_scan_working_directory.
109 * Only add the search path if it isn't already in the set. To preserve the same order
110 * as the enum, insert it in the front. */
112 _valid_searchpaths.insert(_valid_searchpaths.begin(), Searchpath::WorkingDir);
113 }
114}
115
122bool FioCheckFileExists(std::string_view filename, Subdirectory subdir)
123{
124 auto f = FioFOpenFile(filename, "rb", subdir);
125 return f.has_value();
126}
127
133bool FileExists(std::string_view filename)
134{
135 std::error_code ec;
136 return std::filesystem::exists(OTTD2FS(filename), ec);
137}
138
145std::string FioFindFullPath(Subdirectory subdir, std::string_view filename)
146{
147 assert(subdir < Subdirectory::End);
148
149 for (Searchpath sp : _valid_searchpaths) {
150 std::string buf = FioGetDirectory(sp, subdir);
151 buf += filename;
152 if (FileExists(buf)) return buf;
153#if !defined(_WIN32)
154 /* Be, as opening files, aware that sometimes the filename
155 * might be in uppercase when it is in lowercase on the
156 * disk. Of course Windows doesn't care about casing. */
157 if (strtolower(buf, _searchpaths[sp].size() - 1) && FileExists(buf)) return buf;
158#endif
159 }
160
161 return {};
162}
163
164std::string FioGetDirectory(Searchpath sp, Subdirectory subdir)
165{
166 assert(subdir < Subdirectory::End);
167 assert(sp < Searchpath::End);
168
169 /* For official TTD directory, don't include the subdirectory. */
171
172 return fmt::format("{}{}", _searchpaths[sp], _subdirs[subdir]);
173}
174
175std::string FioFindDirectory(Subdirectory subdir)
176{
177 /* Find and return the first valid directory */
178 for (Searchpath sp : _valid_searchpaths) {
179 std::string ret = FioGetDirectory(sp, subdir);
180 if (FileExists(ret)) return ret;
181 }
182
183 /* Could not find the directory, fall back to a base path */
184 return _personal_dir;
185}
186
187static std::optional<FileHandle> FioFOpenFileSp(std::string_view filename, std::string_view mode, Searchpath sp, Subdirectory subdir, size_t *filesize)
188{
189#if defined(_WIN32)
190 /* fopen is implemented as a define with ellipses for
191 * Unicode support (prepend an L). As we are not sending
192 * a string, but a variable, it 'renames' the variable,
193 * so make that variable to makes it compile happily */
194 wchar_t Lmode[5];
195 MultiByteToWideChar(CP_ACP, 0, mode.data(), static_cast<int>(std::size(mode)), Lmode, static_cast<int>(std::size(Lmode)));
196#endif
197 std::string buf;
198
199 if (subdir == Subdirectory::None) {
200 buf = filename;
201 } else {
202 buf = fmt::format("{}{}", FioGetDirectory(sp, subdir), filename);
203 }
204
205 auto f = FileHandle::Open(buf, mode);
206#if !defined(_WIN32)
207 if (!f.has_value() && strtolower(buf, subdir == Subdirectory::None ? 0 : _searchpaths[sp].size() - 1) ) {
208 f = FileHandle::Open(buf, mode);
209 }
210#endif
211 if (f.has_value() && filesize != nullptr) {
212 /* Find the size of the file */
213 fseek(*f, 0, SEEK_END);
214 *filesize = ftell(*f);
215 fseek(*f, 0, SEEK_SET);
216 }
217 return f;
218}
219
227static std::optional<FileHandle> FioFOpenFileTar(const TarFileListEntry &entry, size_t *filesize)
228{
229 auto f = FileHandle::Open(entry.tar_filename, "rb");
230 if (!f.has_value()) return std::nullopt;
231
232 if (fseek(*f, entry.position, SEEK_SET) < 0) {
233 return std::nullopt;
234 }
235
236 if (filesize != nullptr) *filesize = entry.size;
237 return f;
238}
239
248std::optional<FileHandle> FioFOpenFile(std::string_view filename, std::string_view mode, Subdirectory subdir, size_t *filesize)
249{
250 std::optional<FileHandle> f = std::nullopt;
251 assert(subdir < Subdirectory::End || subdir == Subdirectory::None);
252
253 for (Searchpath sp : _valid_searchpaths) {
254 f = FioFOpenFileSp(filename, mode, sp, subdir, filesize);
255 if (f.has_value() || subdir == Subdirectory::None) break;
256 }
257
258 /* We can only use .tar in case of data-dir, and read-mode */
259 if (!f.has_value() && mode[0] == 'r' && subdir != Subdirectory::None) {
260 /* Filenames in tars are always forced to be lowercase */
261 std::string resolved_name{filename};
262 strtolower(resolved_name);
263
264 /* Resolve ".." */
265 std::istringstream ss(resolved_name);
266 std::vector<std::string> tokens;
267 for (std::string token; std::getline(ss, token, PATHSEPCHAR); /* nothing */) {
268 if (token == "..") {
269 if (tokens.size() < 2) return std::nullopt;
270 tokens.pop_back();
271 } else if (token == ".") {
272 /* Do nothing. "." means current folder, but you can create tar files with "." in the path.
273 * This confuses our file resolver. So, act like this folder doesn't exist. */
274 } else {
275 tokens.push_back(token);
276 }
277 }
278
279 resolved_name.clear();
280 bool first = true;
281 for (const std::string &token : tokens) {
282 if (!first) {
283 resolved_name += PATHSEP;
284 }
285 resolved_name += token;
286 first = false;
287 }
288
289 TarFileList::iterator it = _tar_filelist[subdir].find(resolved_name);
290 if (it != _tar_filelist[subdir].end()) {
291 f = FioFOpenFileTar(it->second, filesize);
292 }
293 }
294
295 /* Sometimes a full path is given. To support
296 * the 'subdirectory' must be 'removed'. */
297 if (!f.has_value() && subdir != Subdirectory::None) {
298 switch (subdir) {
300 f = FioFOpenFile(filename, mode, Subdirectory::OldGm, filesize);
301 if (f.has_value()) break;
302 [[fallthrough]];
304 f = FioFOpenFile(filename, mode, Subdirectory::OldData, filesize);
305 break;
306
307 default:
308 f = FioFOpenFile(filename, mode, Subdirectory::None, filesize);
309 break;
310 }
311 }
312
313 return f;
314}
315
321void FioCreateDirectory(const std::string &name)
322{
323 /* Ignore directory creation errors; they'll surface later on. */
324 std::error_code error_code;
325 std::filesystem::create_directories(OTTD2FS(name), error_code);
326}
327
333bool FioRemove(const std::string &filename)
334{
335 std::filesystem::path path = OTTD2FS(filename);
336 std::error_code error_code;
337 if (!std::filesystem::remove(path, error_code)) {
338 if (error_code) {
339 Debug(Facility::Misc, Severity::Critical, "Removing {} failed: {}", filename, error_code.message());
340 } else {
341 Debug(Facility::Misc, Severity::Critical, "Removing {} failed: file does not exist", filename);
342 }
343 return false;
344 }
345 return true;
346}
347
353void AppendPathSeparator(std::string &buf)
354{
355 if (buf.empty()) return;
356
357 if (buf.back() != PATHSEPCHAR) buf.push_back(PATHSEPCHAR);
358}
359
365static void SimplifyFileName(std::string &name)
366{
367 for (char &c : name) {
368 /* Force lowercase */
369 c = std::tolower(c);
370#if (PATHSEPCHAR != '/')
371 /* Tar-files always have '/' path-separator, but we want our PATHSEPCHAR */
372 if (c == '/') c = PATHSEPCHAR;
373#endif
374 }
375}
376
383{
384 _tar_filelist[sd].clear();
385 _tar_list[sd].clear();
386 uint num = this->Scan(".tar", sd, false);
387 if (sd == Subdirectory::Baseset || sd == Subdirectory::NewGrf) num += this->Scan(".tar", Subdirectory::OldData, false);
388 return num;
389}
390
396/* static */ uint TarScanner::DoScan(TarScanner::Modes modes)
397{
398 Debug(Facility::Misc, Severity::Warning, "Scanning for tars");
399 TarScanner fs;
400 uint num = 0;
401 if (modes.Test(TarScanner::Mode::Baseset)) {
402 num += fs.DoScan(Subdirectory::Baseset);
403 }
404 if (modes.Test(TarScanner::Mode::NewGRF)) {
405 num += fs.DoScan(Subdirectory::NewGrf);
406 }
407 if (modes.Test(TarScanner::Mode::AI)) {
408 num += fs.DoScan(Subdirectory::Ai);
410 }
411 if (modes.Test(TarScanner::Mode::Game)) {
412 num += fs.DoScan(Subdirectory::Gs);
414 }
415 if (modes.Test(TarScanner::Mode::Scenario)) {
418 }
419 Debug(Facility::Misc, Severity::Warning, "Scan complete, found {} files", num);
420 return num;
421}
422
429bool TarScanner::AddFile(Subdirectory sd, const std::string &filename)
430{
431 this->subdir = sd;
432 return this->AddFile(filename, 0);
433}
434
445static std::string ExtractString(std::span<char> buffer)
446{
447 return StrMakeValid(std::string_view(buffer.begin(), buffer.end()));
448}
449
450bool TarScanner::AddFile(const std::string &filename, size_t, [[maybe_unused]] const std::string &tar_filename)
451{
452 /* No tar within tar. */
453 assert(tar_filename.empty());
454
455 /* The TAR-header, repeated for every file */
456 struct TarHeader {
457 char name[100];
458 char mode[8];
459 char uid[8];
460 char gid[8];
461 char size[12];
462 char mtime[12];
463 char chksum[8];
464 char typeflag;
465 char linkname[100];
466 char magic[6];
467 char version[2];
468 char uname[32];
469 char gname[32];
470 char devmajor[8];
471 char devminor[8];
472 char prefix[155];
473
474 char unused[12];
475 };
476 static_assert(sizeof(TarHeader) == 512);
477
478 /* Check if we already seen this file */
479 TarList::iterator it = _tar_list[this->subdir].find(filename);
480 if (it != _tar_list[this->subdir].end()) return false;
481
482 auto of = FileHandle::Open(filename, "rb");
483 /* Although the file has been found there can be
484 * a number of reasons we cannot open the file.
485 * Most common case is when we simply have not
486 * been given read access. */
487 if (!of.has_value()) return false;
488 auto &f = *of;
489
490 _tar_list[this->subdir][filename] = std::string{};
491
492 std::string filename_base = FS2OTTD(std::filesystem::path(OTTD2FS(filename)).filename().native());
493 SimplifyFileName(filename_base);
494
495 TarHeader th;
496 size_t num = 0, pos = 0;
497
498 for (;;) { // Note: feof() always returns 'false' after 'fseek()'. Cool, isn't it?
499 size_t num_bytes_read = fread(&th, 1, sizeof(TarHeader), f);
500 if (num_bytes_read != sizeof(TarHeader)) break;
501 pos += num_bytes_read;
502
503 /* Check if we have the new tar-format (ustar) or the old one (a lot of zeros after 'link' field) */
504 auto last_of_th = &th.unused[std::size(th.unused)];
505 if (std::string_view{th.magic, 5} != "ustar" && std::any_of(th.magic, last_of_th, [](auto c) { return c != 0; })) {
506 /* If we have only zeros in the block, it can be an end-of-file indicator */
507 if (std::all_of(th.name, last_of_th, [](auto c) { return c == 0; })) continue;
508
509 Debug(Facility::Misc, Severity::Critical, "The file '{}' isn't a valid tar-file", filename);
510 return false;
511 }
512
513 std::string name;
514
515 /* The prefix contains the directory-name */
516 if (th.prefix[0] != '\0') {
517 name = ExtractString(th.prefix);
518 name += PATHSEP;
519 }
520
521 /* Copy the name of the file in a safe way at the end of 'name' */
522 name += ExtractString(th.name);
523
524 /* The size of the file, for some strange reason, this is stored as a string in octals. */
525 std::string size = ExtractString(th.size);
526 size_t skip = 0;
527 if (!size.empty()) {
528 auto value = ParseInteger<size_t>(size, 8);
529 if (!value.has_value()) {
530 Debug(Facility::Misc, Severity::Critical, "The file '{}' has an invalid size for '{}'", filename, name);
531 return false;
532 }
533 skip = *value;
534 }
535
536 switch (th.typeflag) {
537 case '\0':
538 case '0': { // regular file
539 if (name.empty()) break;
540
541 /* Store this entry in the list */
542 TarFileListEntry entry;
543 entry.tar_filename = filename;
544 entry.size = skip;
545 entry.position = pos;
546
547 /* Convert to lowercase and our PATHSEPCHAR */
548 SimplifyFileName(name);
549
550 Debug(Facility::Misc, Severity::Debug2, "Found file in tar: {} ({} bytes, {} offset)", name, skip, pos);
551 if (_tar_filelist[this->subdir].insert(TarFileList::value_type(filename_base + PATHSEPCHAR + name, entry)).second) num++;
552
553 break;
554 }
555
556 case '1': // hard links
557 case '2': { // symbolic links
558 std::string link = ExtractString(th.linkname);
559
560 Debug(Facility::Misc, Severity::Debug1, "Ignoring link in tar: {} -> {}", name, link);
561 break;
562 }
563
564 case '5': // directory
565 /* Convert to lowercase and our PATHSEPCHAR */
566 SimplifyFileName(name);
567
568 /* Store the first directory name we detect */
569 Debug(Facility::Misc, Severity::Debug2, "Found dir in tar: {}", name);
570 if (_tar_list[this->subdir][filename].empty()) _tar_list[this->subdir][filename] = std::move(name);
571 break;
572
573 default:
574 /* Ignore other types */
575 break;
576 }
577
578 /* Skip to the next block.. */
579 skip = Align(skip, 512);
580 if (fseek(f, skip, SEEK_CUR) < 0) {
581 Debug(Facility::Misc, Severity::Critical, "The file '{}' can't be read as a valid tar-file", filename);
582 return false;
583 }
584 pos += skip;
585 }
586
587 Debug(Facility::Misc, Severity::Info, "Found tar '{}' with {} new files", filename, num);
588
589 return true;
590}
591
599bool ExtractTar(const std::string &tar_filename, Subdirectory subdir)
600{
601 TarList::iterator it = _tar_list[subdir].find(tar_filename);
602 /* We don't know the file. */
603 if (it == _tar_list[subdir].end()) return false;
604
605 /* The file doesn't have a sub directory! */
606 if (it->second.empty()) {
607 Debug(Facility::Misc, Severity::Notice, "Extracting {} failed; archive rejected, the contents must be in a sub directory", tar_filename);
608 return false;
609 }
610
611 auto p = tar_filename.rfind(".tar");
612 /* The file's path does not have a ".tar"? */
613 if (p == std::string::npos) return false;
614
615 const std::string dirname = tar_filename.substr(0, p);
616 Debug(Facility::Misc, Severity::Trace2, "Extracting {} to directory {}", tar_filename, dirname);
617 FioCreateDirectory(dirname);
618
619 for (auto &it2 : _tar_filelist[subdir]) {
620 if (tar_filename != it2.second.tar_filename) continue;
621
622 /* it2.first is tarball + PATHSEPCHAR + name. */
623 std::string_view name = it2.first;
624 name.remove_prefix(name.find_last_of(PATHSEPCHAR) + 1);
625 std::string filename = fmt::format("{}{}{}", dirname, PATHSEP, name);
626
627 Debug(Facility::Misc, Severity::Trace3, " extracting {}", filename);
628
629 /* First open the file in the .tar. */
630 size_t to_copy = 0;
631 auto in = FioFOpenFileTar(it2.second, &to_copy);
632 if (!in.has_value()) {
633 Debug(Facility::Misc, Severity::Debug2, "Extracting {} failed; could not open {}", filename, tar_filename);
634 return false;
635 }
636
637 /* Now open the 'output' file. */
638 auto out = FileHandle::Open(filename, "wb");
639 if (!out.has_value()) {
640 Debug(Facility::Misc, Severity::Debug2, "Extracting {} failed; could not open {}", filename, filename);
641 return false;
642 }
643
644 /* Now read from the tar and write it into the file. */
645 char buffer[4096];
646 size_t read;
647 for (; to_copy != 0; to_copy -= read) {
648 size_t to_read = std::min(to_copy, lengthof(buffer));
649 read = fread(buffer, 1, to_read, *in);
650 if (read != to_read || fwrite(buffer, 1, read, *out) != read) break;
651 }
652
653 if (to_copy != 0) {
654 Debug(Facility::Misc, Severity::Debug2, "Extracting {} failed; still {} bytes to copy", filename, to_copy);
655 return false;
656 }
657 }
658
659 Debug(Facility::Misc, Severity::Trace3, " extraction successful");
660 return true;
661}
662
663#if defined(_WIN32)
669extern void DetermineBasePaths(std::string_view exe);
670
672char *getcwd(char *buf, size_t size);
673#else /* defined(_WIN32) */
674
683static bool ChangeWorkingDirectoryToExecutable(std::string_view exe)
684{
685 std::string path{exe};
686
687#ifdef WITH_COCOA
688 for (size_t pos = path.find_first_of('.'); pos != std::string::npos; pos = path.find_first_of('.', pos + 1)) {
689 if (StrEqualsIgnoreCase(path.substr(pos, 4), ".app")) {
690 path.erase(pos);
691 break;
692 }
693 }
694#endif /* WITH_COCOA */
695
696 size_t pos = path.find_last_of(PATHSEPCHAR);
697 if (pos == std::string::npos) return false;
698
699 path.erase(pos);
700
701 if (chdir(path.c_str()) != 0) {
702 Debug(Facility::Misc, Severity::Critical, "Directory with the binary does not exist?");
703 return false;
704 }
705
706 return true;
707}
708
720{
721 /* No working directory, so nothing to do. */
722 if (_searchpaths[Searchpath::WorkingDir].empty()) return false;
723
724 /* Working directory is root, so do nothing. */
725 if (_searchpaths[Searchpath::WorkingDir] == PATHSEP) return false;
726
727 /* No personal/home directory, so the working directory won't be that. */
728 if (_searchpaths[Searchpath::PersonalDir].empty()) return true;
729
730 std::string tmp = _searchpaths[Searchpath::WorkingDir] + PERSONAL_DIR;
732
734}
735
741static std::string GetHomeDir()
742{
743#ifdef __HAIKU__
744 BPath path;
745 find_directory(B_USER_SETTINGS_DIRECTORY, &path);
746 return std::string(path.Path());
747#else
748 auto home_env = GetEnv("HOME"); // Stack var, shouldn't be freed
749 if (home_env.has_value()) return std::string(*home_env);
750
751 const struct passwd *pw = getpwuid(getuid());
752 if (pw != nullptr) return std::string(pw->pw_dir);
753#endif
754 return {};
755}
756
761void DetermineBasePaths(std::string_view exe)
762{
763 std::string tmp;
764 const std::string homedir = GetHomeDir();
765#ifdef USE_XDG
766 if (auto xdg_data_home = GetEnv("XDG_DATA_HOME"); xdg_data_home.has_value()) {
767 tmp = *xdg_data_home;
768 tmp += PATHSEP;
769 tmp += PERSONAL_DIR[0] == '.' ? &PERSONAL_DIR[1] : PERSONAL_DIR;
771 _searchpaths[Searchpath::PersonalDirXdg] = tmp;
772
773 tmp += "content_download";
776 } else if (!homedir.empty()) {
777 tmp = homedir;
778 tmp += PATHSEP ".local" PATHSEP "share" PATHSEP;
779 tmp += PERSONAL_DIR[0] == '.' ? &PERSONAL_DIR[1] : PERSONAL_DIR;
781 _searchpaths[Searchpath::PersonalDirXdg] = tmp;
782
783 tmp += "content_download";
786 } else {
787 _searchpaths[Searchpath::PersonalDirXdg].clear();
789 }
790#endif
791
792#if !defined(WITH_PERSONAL_DIR)
794#else
795 if (!homedir.empty()) {
796 tmp = std::move(homedir);
797 tmp += PATHSEP;
798 tmp += PERSONAL_DIR;
801
802 tmp += "content_download";
805 } else {
808 }
809#endif
810
811#if defined(WITH_SHARED_DIR)
812 tmp = SHARED_DIR;
815#else
817#endif
818
819 char cwd[MAX_PATH];
820 if (getcwd(cwd, MAX_PATH) == nullptr) *cwd = '\0';
821
822 if (_config_file.empty()) {
823 /* Get the path to working directory of OpenTTD. */
824 tmp = cwd;
827
829 } else {
830 /* Use the folder of the config file as working directory. */
831 size_t end = _config_file.find_last_of(PATHSEPCHAR);
832 if (end == std::string::npos) {
833 /* _config_file is not in a folder, so use current directory. */
834 tmp = cwd;
835 } else {
836 tmp = FS2OTTD(std::filesystem::weakly_canonical(std::filesystem::path(OTTD2FS(_config_file))).parent_path().native());
837 }
840 }
841
842 /* Change the working directory to that one of the executable */
844 char buf[MAX_PATH];
845 if (getcwd(buf, lengthof(buf)) == nullptr) {
846 tmp.clear();
847 } else {
848 tmp = buf;
849 }
852 } else {
854 }
855
856 if (cwd[0] != '\0') {
857 /* Go back to the current working directory. */
858 if (chdir(cwd) != 0) {
859 Debug(Facility::Misc, Severity::Critical, "Failed to return to working directory!");
860 }
861 }
862
863#if !defined(GLOBAL_DATA_DIR)
865#else
866 tmp = GLOBAL_DATA_DIR;
869#endif
870#ifdef WITH_COCOA
871extern void CocoaSetApplicationBundleDir();
872 CocoaSetApplicationBundleDir();
873#else
875#endif
876
877 /* Look for Atari release of Transport Tycoon Deluxe for original data files */
878 std::string config_file_path;
879 const std::string atari_ini_filename = "Atari/Transport Tycoon Deluxe/installpath.ini";
880
882
883#ifdef WITH_COCOA
884extern std::string CocoaGetAppSupportDir();
885 config_file_path = CocoaGetAppSupportDir();
886
887 if (!config_file_path.empty()) {
888 AppendPathSeparator(config_file_path);
889 config_file_path += atari_ini_filename;
890 }
891#else
892 config_file_path = GetHomeDir();
893
894 if (!config_file_path.empty()) {
895 AppendPathSeparator(config_file_path);
896 config_file_path += ".local/share/";
897 config_file_path += atari_ini_filename;
898 }
899#endif
900
901 if (!config_file_path.empty()) {
902 size_t installpath_len;
903 std::unique_ptr<char[]> installpath = ReadFileToMem(config_file_path, installpath_len, MAX_PATH);
904
905 if (installpath != nullptr && installpath_len > 0) {
906 std::string ttd_path = installpath.get();
907 AppendPathSeparator(ttd_path);
908
909#ifdef WITH_COCOA
910 /* The path provided is to the TTD.app/Contents/MacOS folder */
911 ttd_path += "../Resources/";
912#endif
913
914 ttd_path += "CD";
915 AppendPathSeparator(ttd_path);
916
918 }
919 }
920}
921#endif /* defined(_WIN32) */
922
923std::string _personal_dir;
924
932void DeterminePaths(std::string_view exe, bool only_local_path)
933{
935 FillValidSearchPaths(only_local_path);
936
937#ifdef USE_XDG
938 std::string config_home;
939 std::string homedir = GetHomeDir();
940 if (auto xdg_config_home = GetEnv("XDG_CONFIG_HOME"); xdg_config_home.has_value()) {
941 config_home = *xdg_config_home;
942 config_home += PATHSEP;
943 config_home += PERSONAL_DIR[0] == '.' ? &PERSONAL_DIR[1] : PERSONAL_DIR;
944 } else if (!homedir.empty()) {
945 /* Defaults to ~/.config */
946 config_home = std::move(homedir);
947 config_home += PATHSEP ".config" PATHSEP;
948 config_home += PERSONAL_DIR[0] == '.' ? &PERSONAL_DIR[1] : PERSONAL_DIR;
949 }
950 AppendPathSeparator(config_home);
951#endif
952
953 for (Searchpath sp : _valid_searchpaths) {
955 Debug(Facility::Misc, Severity::Notice, "{} added as search path", _searchpaths[sp]);
956 }
957
958 std::string config_dir;
959 if (!_config_file.empty()) {
961 } else {
962 std::string personal_dir = FioFindFullPath(Subdirectory::Base, "openttd.cfg");
963 if (!personal_dir.empty()) {
964 auto end = personal_dir.find_last_of(PATHSEPCHAR);
965 if (end != std::string::npos) personal_dir.erase(end + 1);
966 config_dir = std::move(personal_dir);
967 } else {
968#ifdef USE_XDG
969 /* No previous configuration file found. Use the configuration folder from XDG. */
970 config_dir = config_home;
971#else
972 static const Searchpath new_openttd_cfg_order[] = {
974 };
975
976 config_dir.clear();
977 for (const auto &searchpath : new_openttd_cfg_order) {
978 if (IsValidSearchPath(searchpath)) {
979 config_dir = _searchpaths[searchpath];
980 break;
981 }
982 }
983#endif
984 }
985 _config_file = config_dir + "openttd.cfg";
986 }
987
988 Debug(Facility::Misc, Severity::Error, "{} found as config directory", config_dir);
989
990 _highscore_file = config_dir + "hs.dat";
991 extern std::string _hotkeys_file;
992 _hotkeys_file = config_dir + "hotkeys.cfg";
993 extern std::string _windows_file;
994 _windows_file = config_dir + "windows.cfg";
995 extern std::string _private_file;
996 _private_file = config_dir + "private.cfg";
997 extern std::string _secrets_file;
998 _secrets_file = config_dir + "secrets.cfg";
999 extern std::string _favs_file;
1000 _favs_file = config_dir + "favs.cfg";
1001
1002#ifdef USE_XDG
1003 if (config_dir == config_home) {
1004 /* We are using the XDG configuration home for the config file,
1005 * then store the rest in the XDG data home folder. */
1006 _personal_dir = _searchpaths[Searchpath::PersonalDirXdg];
1007 if (only_local_path) {
1008 /* In case of XDG and we only want local paths and we detected that
1009 * the user either manually indicated the XDG path or didn't use
1010 * "-c" option, we change the working-dir to the XDG personal-dir,
1011 * as this is most likely what the user is expecting. */
1012 _searchpaths[Searchpath::WorkingDir] = _searchpaths[Searchpath::PersonalDirXdg];
1013 }
1014 } else
1015#endif
1016 {
1017 _personal_dir = config_dir;
1018 }
1019
1020 /* Make the necessary folders */
1021 FioCreateDirectory(config_dir);
1022#if defined(WITH_PERSONAL_DIR)
1024#endif
1025
1026 Debug(Facility::Misc, Severity::Error, "{} found as personal directory", _personal_dir);
1027
1028 static const Subdirectory default_subdirs[] = {
1030 };
1031
1032 for (const auto &default_subdir : default_subdirs) {
1033 FioCreateDirectory(fmt::format("{}{}", _personal_dir, _subdirs[default_subdir]));
1034 }
1035
1036 /* If we have network we make a directory for the autodownloading of content */
1037 _searchpaths[Searchpath::AutodownloadDir] = _personal_dir + "content_download" PATHSEP;
1040 FillValidSearchPaths(only_local_path);
1041
1042 /* Create the directory for each of the types of content */
1044 for (const auto &subdir : subdirs) {
1045 FioCreateDirectory(FioGetDirectory(Searchpath::AutodownloadDir, subdir));
1046 }
1047
1048 extern std::string _log_file;
1049 _log_file = _personal_dir + "openttd.log";
1050}
1051
1056void SanitizeFilename(std::string &filename)
1057{
1058 for (auto &c : filename) {
1059 switch (c) {
1060 /* The following characters are not allowed in filenames
1061 * on at least one of the supported operating systems: */
1062 case ':': case '\\': case '*': case '?': case '/':
1063 case '<': case '>': case '|': case '"':
1064 c = '_';
1065 break;
1066 }
1067 }
1068}
1069
1078std::unique_ptr<char[]> ReadFileToMem(const std::string &filename, size_t &lenp, size_t maxsize)
1079{
1080 auto in = FileHandle::Open(filename, "rb");
1081 if (!in.has_value()) return nullptr;
1082
1083 fseek(*in, 0, SEEK_END);
1084 size_t len = ftell(*in);
1085 fseek(*in, 0, SEEK_SET);
1086 if (len > maxsize) return nullptr;
1087
1088 std::unique_ptr<char[]> mem = std::make_unique<char[]>(len + 1);
1089
1090 mem.get()[len] = 0;
1091 if (fread(mem.get(), len, 1, *in) != 1) return nullptr;
1092
1093 lenp = len;
1094 return mem;
1095}
1096
1103static bool MatchesExtension(std::string_view extension, const std::string &filename)
1104{
1105 if (extension.empty()) return true;
1106 if (filename.length() < extension.length()) return false;
1107
1108 std::string_view filename_sv = filename; // String view to avoid making another copy of the substring.
1109 return StrCompareIgnoreCase(extension, filename_sv.substr(filename_sv.length() - extension.length())) == 0;
1110}
1111
1122static uint ScanPath(FileScanner *fs, std::string_view extension, const std::filesystem::path &path, size_t basepath_length, bool recursive)
1123{
1124 uint num = 0;
1125
1126 std::error_code error_code;
1127 for (const auto &dir_entry : std::filesystem::directory_iterator(path, error_code)) {
1128 if (dir_entry.is_directory()) {
1129 if (!recursive) continue;
1130 num += ScanPath(fs, extension, dir_entry.path(), basepath_length, recursive);
1131 } else if (dir_entry.is_regular_file()) {
1132 std::string file = FS2OTTD(dir_entry.path().native());
1133 if (!MatchesExtension(extension, file)) continue;
1134 if (fs->AddFile(file, basepath_length, {})) num++;
1135 }
1136 }
1137 if (error_code) {
1138 Debug(Facility::Misc, Severity::Trace3, "Unable to read directory {}: {}", path.string(), error_code.message());
1139 }
1140
1141 return num;
1142}
1143
1151static uint ScanTar(FileScanner *fs, std::string_view extension, const TarFileList::value_type &tar)
1152{
1153 uint num = 0;
1154
1155 if (MatchesExtension(extension, tar.first) && fs->AddFile(tar.first, 0, tar.second.tar_filename)) num++;
1156
1157 return num;
1158}
1159
1169uint FileScanner::Scan(std::string_view extension, Subdirectory sd, bool tars, bool recursive)
1170{
1171 this->subdir = sd;
1172
1173 uint num = 0;
1174
1175 for (Searchpath sp : _valid_searchpaths) {
1176 /* Don't search in the working directory */
1177 if (sp == Searchpath::WorkingDir && !_do_scan_working_directory) continue;
1178
1179 std::string path = FioGetDirectory(sp, sd);
1180 num += ScanPath(this, extension, OTTD2FS(path), path.size(), recursive);
1181 }
1182
1183 if (tars && sd != Subdirectory::None) {
1184 for (const auto &tar : _tar_filelist[sd]) {
1185 num += ScanTar(this, extension, tar);
1186 }
1187 }
1188
1189 switch (sd) {
1191 num += this->Scan(extension, Subdirectory::OldGm, tars, recursive);
1192 [[fallthrough]];
1194 num += this->Scan(extension, Subdirectory::OldData, tars, recursive);
1195 break;
1196
1197 default: break;
1198 }
1199
1200 return num;
1201}
1202
1211uint FileScanner::Scan(std::string_view extension, const std::string &directory, bool recursive)
1212{
1213 std::string path(directory);
1214 AppendPathSeparator(path);
1215 return ScanPath(this, extension, OTTD2FS(path), path.size(), recursive);
1216}
1217
1218std::optional<FileHandle> FileHandle::Open(const std::string &filename, std::string_view mode)
1219{
1220#if defined(_WIN32)
1221 /* Windows also requires mode to be wchar_t. */
1222 auto f = _wfopen(OTTD2FS(filename).c_str(), OTTD2FS(mode).c_str());
1223#else
1224 auto f = fopen(filename.c_str(), std::string{mode}.c_str());
1225#endif /* _WIN32 */
1226
1227 if (f == nullptr) return std::nullopt;
1228 return FileHandle(f);
1229}
constexpr bool Test(Tvalue_type value) const
Test if the value-th bit is set.
Iterate a range of enum values.
static std::optional< FileHandle > Open(const std::string &filename, std::string_view mode)
Open an RAII file handle if possible.
Definition fileio.cpp:1218
Helper for scanning for files with a given name.
Definition fileio_func.h:37
uint Scan(std::string_view extension, Subdirectory sd, bool tars=true, bool recursive=true)
Scan for files with the given extension in the given search path.
Definition fileio.cpp:1169
virtual bool AddFile(const std::string &filename, size_t basepath_length, const std::string &tar_filename)=0
Add a file with the given filename.
Helper for scanning for files with tar as extension.
Definition fileio_func.h:59
@ AI
Scan for AIs and its libraries.
Definition fileio_func.h:66
@ Scenario
Scan for scenarios and heightmaps.
Definition fileio_func.h:67
@ Game
Scan for game scripts.
Definition fileio_func.h:68
@ Baseset
Scan for base sets.
Definition fileio_func.h:64
@ NewGRF
Scan for non-base sets.
Definition fileio_func.h:65
uint DoScan(Subdirectory sd)
Perform the scanning of a particular subdirectory.
Definition fileio.cpp:382
EnumBitSet< Mode, uint8_t > Modes
Bitset of Mode elements.
Definition fileio_func.h:72
bool AddFile(const std::string &filename, size_t basepath_length, const std::string &tar_filename={}) override
Add a file with the given filename.
Definition fileio.cpp:450
Functions related to debugging.
#define Debug(facility, severity, format_string,...)
Output a line of debugging information.
Definition debug.h:36
@ Misc
Misc message facility.
Definition debug_type.h:32
@ Warning
Warning, wrong but okay if you don't know.
Definition debug_type.h:17
@ Debug1
Debug #1 - High level debug messages.
Definition debug_type.h:20
@ Notice
Notice.
Definition debug_type.h:18
@ Critical
Critical, user should know about this.
Definition debug_type.h:15
@ Debug2
Debug #2 - Low level debug messages.
Definition debug_type.h:21
@ Info
Info, information you might care about.
Definition debug_type.h:19
@ Trace2
Trace information #2.
Definition debug_type.h:23
@ Error
Error, but we are recovering.
Definition debug_type.h:16
@ Trace3
Trace information #3.
Definition debug_type.h:24
std::string _log_file
Filename to reroute output of a forked OpenTTD to.
Definition dedicated.cpp:14
constexpr std::underlying_type_t< enum_type > to_underlying(enum_type e)
Implementation of std::to_underlying (from C++23).
Definition enum_type.hpp:21
EnumClassIndexContainer< std::array< T, to_underlying(N)>, Index > EnumIndexArray
A typedef for EnumClassIndexContainer using std::array as the backing container type.
static bool ChangeWorkingDirectoryToExecutable(std::string_view exe)
Changes the working directory to the path of the give executable.
Definition fileio.cpp:683
bool ExtractTar(const std::string &tar_filename, Subdirectory subdir)
Extract the tar with the given filename in the directory where the tar resides.
Definition fileio.cpp:599
void DeterminePaths(std::string_view exe, bool only_local_path)
Acquire the base paths (personal dir and game data dir), fill all other paths (save dir,...
Definition fileio.cpp:932
static bool IsValidSearchPath(Searchpath sp)
Checks whether the given search path is a valid search path.
Definition fileio.cpp:76
static const EnumIndexArray< std::string_view, Subdirectory, Subdirectory::End > _subdirs
Subdirectory names.
Definition fileio.cpp:38
bool FioRemove(const std::string &filename)
Remove a file.
Definition fileio.cpp:333
void SanitizeFilename(std::string &filename)
Sanitizes a filename, i.e.
Definition fileio.cpp:1056
static uint ScanPath(FileScanner *fs, std::string_view extension, const std::filesystem::path &path, size_t basepath_length, bool recursive)
Scan a single directory (and recursively its children) and add any graphics sets that are found.
Definition fileio.cpp:1122
void DetermineBasePaths(std::string_view exe)
Determine the base (personal dir and game data dir) paths.
Definition fileio.cpp:761
void AppendPathSeparator(std::string &buf)
Appends, if necessary, the path separator character to the end of the string.
Definition fileio.cpp:353
EnumIndexArray< TarFileList, Subdirectory, Subdirectory::End > _tar_filelist
List of files within tar files found in each subdirectory.
Definition fileio.cpp:69
std::optional< FileHandle > FioFOpenFile(std::string_view filename, std::string_view mode, Subdirectory subdir, size_t *filesize)
Opens a OpenTTD file somewhere in a personal or global directory.
Definition fileio.cpp:248
std::string _personal_dir
custom directory for personal settings, saves, newgrf, etc.
Definition fileio.cpp:923
static bool _do_scan_working_directory
Whether the working directory should be scanned.
Definition fileio.cpp:32
static std::string GetHomeDir()
Gets the home directory of the user.
Definition fileio.cpp:741
static void SimplifyFileName(std::string &name)
Simplify filenames from tars.
Definition fileio.cpp:365
static bool MatchesExtension(std::string_view extension, const std::string &filename)
Helper to see whether a given filename matches the extension.
Definition fileio.cpp:1103
EnumIndexArray< TarList, Subdirectory, Subdirectory::End > _tar_list
List of tar files found in each subdirectory.
Definition fileio.cpp:67
void FioCreateDirectory(const std::string &name)
Create a directory with the given name If the parent directory does not exist, it will try to create ...
Definition fileio.cpp:321
bool FileExists(std::string_view filename)
Test whether the given filename exists.
Definition fileio.cpp:133
static uint ScanTar(FileScanner *fs, std::string_view extension, const TarFileList::value_type &tar)
Scan the given tar and add graphics sets when it finds one.
Definition fileio.cpp:1151
bool FioCheckFileExists(std::string_view filename, Subdirectory subdir)
Check whether the given file exists.
Definition fileio.cpp:122
static std::optional< FileHandle > FioFOpenFileTar(const TarFileListEntry &entry, size_t *filesize)
Opens a file from inside a tar archive.
Definition fileio.cpp:227
static std::string ExtractString(std::span< char > buffer)
Helper to extract a string for the tar header.
Definition fileio.cpp:445
std::string _highscore_file
The file to store the highscore data in.
Definition highscore.cpp:24
bool DoScanWorkingDirectory()
Whether we should scan the working directory.
Definition fileio.cpp:719
std::string _config_file
Configuration file of OpenTTD.
Definition settings.cpp:64
EnumIndexArray< std::string, Searchpath, Searchpath::End > _searchpaths
The search paths OpenTTD could search through.
Definition fileio.cpp:64
std::string FioFindFullPath(Subdirectory subdir, std::string_view filename)
Find a path to the filename in one of the search directories.
Definition fileio.cpp:145
std::unique_ptr< char[]> ReadFileToMem(const std::string &filename, size_t &lenp, size_t maxsize)
Load a file into memory.
Definition fileio.cpp:1078
Functions for standard in/out file operations.
void AppendPathSeparator(std::string &buf)
Appends, if necessary, the path separator character to the end of the string.
Definition fileio.cpp:353
Searchpath
Types of searchpaths OpenTTD might use.
@ WorkingDir
Search in the working directory.
@ AutodownloadDir
Search within the autodownload directory.
@ ApplicationBundleDir
Search within the application bundle.
@ InstallationDir
Search in the installation directory.
@ End
End marker.
@ AutodownloadPersonalDir
Search within the autodownload directory located in the personal directory.
@ SharedDir
Search in the shared directory, like 'Shared Files' under Windows.
@ TransportTycoonDeluxeDir
Search within the Transport Tycoon Deluxe data directory (if installed).
@ BinaryDir
Search in the directory where the binary resides.
@ AutodownloadPersonalDirXdg
Search within the autodownload directory located in the personal directory (XDG variant).
@ PersonalDir
Search in the personal directory.
Subdirectory
The different kinds of subdirectories OpenTTD uses.
Definition fileio_type.h:87
@ Base
Base directory for all subdirectories.
Definition fileio_type.h:88
@ Autosave
Subdirectory of save for autosaves.
Definition fileio_type.h:90
@ NewGrf
Subdirectory for all NewGRFs.
Definition fileio_type.h:96
@ Screenshot
Subdirectory for all screenshots.
@ GsLibrary
Subdirectory for all GS libraries.
@ Scenario
Base directory for all scenarios.
Definition fileio_type.h:91
@ Heightmap
Subdirectory of scenario for heightmaps.
Definition fileio_type.h:92
@ None
A path without any base directory.
@ Ai
Subdirectory for all AI files.
Definition fileio_type.h:98
@ SocialIntegration
Subdirectory for all social integration plugins.
@ End
End marker.
@ Gs
Subdirectory for all game scripts.
@ Baseset
Subdirectory for all base data (base sets, intro game).
Definition fileio_type.h:95
@ Save
Base directory for all savegames.
Definition fileio_type.h:89
@ OldData
Old subdirectory for the data.
Definition fileio_type.h:94
@ AiLibrary
Subdirectory for all AI libraries.
Definition fileio_type.h:99
@ OldGm
Old subdirectory for the music.
Definition fileio_type.h:93
Integer math functions.
constexpr T Align(const T x, uint n)
Return the smallest multiple of n equal or greater than x.
Definition math_func.hpp:37
A number of safeguards to prevent using unsafe methods.
std::string _secrets_file
Secrets configuration file of OpenTTD.
Definition settings.cpp:66
std::string _favs_file
Picker favourites configuration file of OpenTTD.
Definition settings.cpp:67
std::string _private_file
Private configuration file of OpenTTD.
Definition settings.cpp:65
Definition of base types and functions in a cross-platform compatible way.
#define lengthof(array)
Return the length of an fixed size array.
Definition stdafx.h:261
std::optional< std::string_view > GetEnv(const char *variable)
Get the environment variable using std::getenv and when it is an empty string (or nullptr),...
Definition string.cpp:855
bool StrEqualsIgnoreCase(std::string_view str1, std::string_view str2)
Compares two string( view)s for equality, while ignoring the case of the characters.
Definition string.cpp:324
static void StrMakeValid(Builder &builder, StringConsumer &consumer, StringValidationSettings settings)
Copies the valid (UTF-8) characters from consumer to the builder.
Definition string.cpp:118
int StrCompareIgnoreCase(std::string_view str1, std::string_view str2)
Compares two string( view)s, while ignoring the case of the characters.
Definition string.cpp:311
Parse strings.
static std::optional< T > ParseInteger(std::string_view arg, int base=10, bool clamp=false)
Change a string into its number representation.
Functions related to low-level strings.
Structs, typedefs and macros used for TAR file handling.
std::wstring OTTD2FS(std::string_view name)
Convert from OpenTTD's encoding to a wide string.
Definition win32.cpp:389
std::string FS2OTTD(std::wstring_view name)
Convert to OpenTTD's encoding from a wide string.
Definition win32.cpp:373
std::string _windows_file
Config file to store WindowDesc.
Definition window.cpp:100