|
| 1 | +/* XQF - Quake server browser and launcher |
| 2 | +What is not explicitly stated to be covered by another license is |
| 3 | +distributed under the CC0 1.0 license. |
| 4 | +See: https://creativecommons.org/public-domain/cc0/ */ |
| 5 | + |
| 6 | +/* XQF CC0 Source Code. |
| 7 | +Copyright (C) 2025 XQF Team - https://xqf.github.io |
| 8 | +No rights reserved. */ |
| 9 | + |
| 10 | +#if defined(USE_RELATIVE_PREFIX) |
| 11 | +#include <algorithm> |
| 12 | +#include <memory> |
| 13 | +#include <string> |
| 14 | + |
| 15 | +#include <cstring> |
| 16 | +#include <unistd.h> |
| 17 | + |
| 18 | +#include "system.h" |
| 19 | +#endif |
| 20 | + |
| 21 | +#if defined(USE_RELATIVE_PREFIX) |
| 22 | +char xqf_PACKAGE_DATA_DIR[ XQF_MAX_PATH ] = {}; |
| 23 | +char xqf_LOCALEDIR[ XQF_MAX_PATH ] = {}; |
| 24 | +#else |
| 25 | +const char* xqf_PACKAGE_DATA_DIR = PACKAGE_DATA_DIR; |
| 26 | +const char* xqf_LOCALEDIR = LOCALEDIR; |
| 27 | +#endif |
| 28 | + |
| 29 | +#if defined(USE_RELATIVE_PREFIX) |
| 30 | +namespace FS { |
| 31 | + std::string DefaultLibPath(); |
| 32 | +} |
| 33 | + |
| 34 | +void setDefaultDirs() |
| 35 | +{ |
| 36 | +#ifdef _WIN32 |
| 37 | + std::string sep = "\\"; |
| 38 | +#else |
| 39 | + std::string sep = "/"; |
| 40 | +#endif |
| 41 | + |
| 42 | + std::string exe_dir = FS::DefaultLibPath(); |
| 43 | +#if defined(USE_FHS) |
| 44 | + std::string share_dir = exe_dir + sep + ".." + sep + "share"; |
| 45 | +#else |
| 46 | + std::string share_dir = exe_dir + sep + "share"; |
| 47 | +#endif |
| 48 | + std::string data_dir = share_dir + sep + "xqf"; |
| 49 | + std::string locale_dir = share_dir + sep + "locale"; |
| 50 | + |
| 51 | + strncpy(xqf_PACKAGE_DATA_DIR, data_dir.c_str(), std::min(data_dir.size(), size_t(XQF_MAX_PATH))); |
| 52 | + strncpy(xqf_LOCALEDIR, locale_dir.c_str(), std::min(data_dir.size(), size_t(XQF_MAX_PATH))); |
| 53 | +} |
| 54 | + |
| 55 | +// The following is copied from https://github.com/DaemonEngine/Daemon |
| 56 | + |
| 57 | +/* |
| 58 | +=========================================================================== |
| 59 | +Daemon BSD Source Code |
| 60 | +Copyright (c) 2013-2025, Daemon Developers |
| 61 | +All rights reserved. |
| 62 | +
|
| 63 | +Redistribution and use in source and binary forms, with or without |
| 64 | +modification, are permitted provided that the following conditions are met: |
| 65 | + * Redistributions of source code must retain the above copyright |
| 66 | + notice, this list of conditions and the following disclaimer. |
| 67 | + * Redistributions in binary form must reproduce the above copyright |
| 68 | + notice, this list of conditions and the following disclaimer in the |
| 69 | + documentation and/or other materials provided with the distribution. |
| 70 | + * Neither the name of the Daemon developers nor the |
| 71 | + names of its contributors may be used to endorse or promote products |
| 72 | + derived from this software without specific prior written permission. |
| 73 | +
|
| 74 | +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
| 75 | +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 76 | +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 77 | +DISCLAIMED. IN NO EVENT SHALL DAEMON DEVELOPERS BE LIABLE FOR ANY |
| 78 | +DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 79 | +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 80 | +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 81 | +ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 82 | +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 83 | +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 84 | +=========================================================================== |
| 85 | +*/ |
| 86 | + |
| 87 | +#ifdef _WIN32 |
| 88 | +namespace Str |
| 89 | +{ |
| 90 | +// Copied from Daemon/src/common/String.h |
| 91 | +template<typename T> class BasicStringRef { |
| 92 | + public: |
| 93 | + const T* begin() const |
| 94 | + { |
| 95 | + return ptr; |
| 96 | + } |
| 97 | + const T* end() const |
| 98 | + { |
| 99 | + return ptr + len; |
| 100 | + } |
| 101 | + private: |
| 102 | + const T* ptr; |
| 103 | + size_t len; |
| 104 | + }; |
| 105 | +} |
| 106 | + |
| 107 | +// Copied from Daemon/src/common/String.cpp |
| 108 | +std::string UTF16To8(Str::BasicStringRef<wchar_t> str) |
| 109 | +{ |
| 110 | + std::string out; |
| 111 | + auto it = str.begin(); |
| 112 | + auto end = str.end(); |
| 113 | + |
| 114 | + while (it != end) { |
| 115 | + uint16_t ch = *it++; |
| 116 | + if (ch >= UNICODE_SURROGATE_HEAD_START && ch <= UNICODE_SURROGATE_HEAD_END) { |
| 117 | + if (it == end) |
| 118 | + out.append(UNICODE_REPLACEMENT_CHAR_UTF8); |
| 119 | + else { |
| 120 | + uint16_t tail = *it++; |
| 121 | + if (tail >= UNICODE_SURROGATE_TAIL_START && tail <= UNICODE_SURROGATE_TAIL_END) |
| 122 | + UTF8_Append(out, ((ch & UNICODE_SURROGATE_MASK) << 10) | (tail & UNICODE_SURROGATE_MASK)); |
| 123 | + else |
| 124 | + out.append(UNICODE_REPLACEMENT_CHAR_UTF8); |
| 125 | + } |
| 126 | + } else if (ch >= UNICODE_SURROGATE_TAIL_START && ch <= UNICODE_SURROGATE_TAIL_END) |
| 127 | + out.append(UNICODE_REPLACEMENT_CHAR_UTF8); |
| 128 | + else |
| 129 | + UTF8_Append(out, ch); |
| 130 | + } |
| 131 | + return out; |
| 132 | +} |
| 133 | +} |
| 134 | +#endif |
| 135 | + |
| 136 | +namespace FS |
| 137 | +{ |
| 138 | +// Copied from Daemon/src/common/FileSystem.cpp |
| 139 | +std::string DefaultLibPath() |
| 140 | +{ |
| 141 | +#ifdef _WIN32 |
| 142 | + wchar_t buffer[MAX_PATH]; |
| 143 | + DWORD len = GetModuleFileNameW(nullptr, buffer, MAX_PATH); |
| 144 | + if (len == 0 || len >= MAX_PATH) |
| 145 | + return ""; |
| 146 | + |
| 147 | + wchar_t* p = wcsrchr(buffer, L'\\'); |
| 148 | + if (!p) |
| 149 | + return empty_string; |
| 150 | + *p = L'\0'; |
| 151 | + |
| 152 | + return Str::UTF16To8(buffer); |
| 153 | +#elif defined(__linux__) || defined(__FreeBSD__) |
| 154 | + ssize_t len = 64; |
| 155 | + while (true) { |
| 156 | + std::unique_ptr<char[]> out(new char[len]); |
| 157 | +#if defined(__linux__) |
| 158 | + const char* proc_file = "/proc/self/exe"; |
| 159 | +#elif defined(__FreeBSD__) |
| 160 | + const char* proc_file = "/proc/curproc/file"; |
| 161 | +#endif |
| 162 | + ssize_t result = readlink(proc_file, out.get(), len); |
| 163 | + if (result == -1) |
| 164 | + return ""; |
| 165 | + if (result < len) { |
| 166 | + out[result] = '\0'; |
| 167 | + char* p = strrchr(out.get(), '/'); |
| 168 | + if (!p) |
| 169 | + return ""; |
| 170 | + *p = '\0'; |
| 171 | + return out.get(); |
| 172 | + } |
| 173 | + len *= 2; |
| 174 | + } |
| 175 | +#elif defined(__APPLE__) |
| 176 | + uint32_t bufsize = 0; |
| 177 | + _NSGetExecutablePath(nullptr, &bufsize); |
| 178 | + |
| 179 | + std::unique_ptr<char[]> out(new char[bufsize]); |
| 180 | + _NSGetExecutablePath(out.get(), &bufsize); |
| 181 | + |
| 182 | + char* p = strrchr(out.get(), '/'); |
| 183 | + if (!p) |
| 184 | + return ""; |
| 185 | + *p = '\0'; |
| 186 | + |
| 187 | + return out.get(); |
| 188 | +#endif |
| 189 | +} |
| 190 | +} |
| 191 | +#endif // USE_RELATIVE_PREFIX |
0 commit comments