|
| 1 | +#include "asset_path.h" |
| 2 | + |
| 3 | +#include <stdio.h> |
| 4 | +#include <string.h> |
| 5 | +#include <stdlib.h> |
| 6 | +#include <sys/stat.h> |
| 7 | + |
| 8 | +#ifdef _WIN32 |
| 9 | +#include <direct.h> |
| 10 | +#define mkdir_p(path) _mkdir(path) |
| 11 | +#else |
| 12 | +#include <unistd.h> |
| 13 | +#define mkdir_p(path) mkdir(path, 0755) |
| 14 | +#endif |
| 15 | + |
| 16 | +// Cached base directories (set once in asset_path_init) |
| 17 | +static char s_user_data[512]; // user-writable data dir |
| 18 | +static char s_install_data[512]; // system install dir (compile-time) |
| 19 | + |
| 20 | +static int file_exists(const char *path) { |
| 21 | +#ifdef _WIN32 |
| 22 | + struct _stat st; |
| 23 | + return _stat(path, &st) == 0; |
| 24 | +#else |
| 25 | + return access(path, R_OK) == 0; |
| 26 | +#endif |
| 27 | +} |
| 28 | + |
| 29 | +// Recursively create directories for a file path |
| 30 | +static void mkdirs_for_file(const char *filepath) { |
| 31 | + char tmp[512]; |
| 32 | + snprintf(tmp, sizeof(tmp), "%s", filepath); |
| 33 | + |
| 34 | + // Find last separator |
| 35 | + char *last_sep = strrchr(tmp, '/'); |
| 36 | +#ifdef _WIN32 |
| 37 | + char *last_bsep = strrchr(tmp, '\\'); |
| 38 | + if (last_bsep > last_sep) last_sep = last_bsep; |
| 39 | +#endif |
| 40 | + if (!last_sep) return; |
| 41 | + *last_sep = '\0'; |
| 42 | + |
| 43 | + // Create each directory component |
| 44 | + for (char *p = tmp + 1; *p; p++) { |
| 45 | + if (*p == '/' |
| 46 | +#ifdef _WIN32 |
| 47 | + || *p == '\\' |
| 48 | +#endif |
| 49 | + ) { |
| 50 | + *p = '\0'; |
| 51 | + mkdir_p(tmp); |
| 52 | + *p = '/'; |
| 53 | + } |
| 54 | + } |
| 55 | + mkdir_p(tmp); |
| 56 | +} |
| 57 | + |
| 58 | +void asset_path_init(void) { |
| 59 | + s_user_data[0] = '\0'; |
| 60 | + s_install_data[0] = '\0'; |
| 61 | + |
| 62 | +#ifdef __APPLE__ |
| 63 | + const char *home = getenv("HOME"); |
| 64 | + if (home) { |
| 65 | + snprintf(s_user_data, sizeof(s_user_data), |
| 66 | + "%s/Library/Application Support/mavsim-viewer", home); |
| 67 | + } |
| 68 | +#elif !defined(_WIN32) |
| 69 | + // Linux: XDG_DATA_HOME or ~/.local/share |
| 70 | + const char *xdg = getenv("XDG_DATA_HOME"); |
| 71 | + if (xdg && xdg[0]) { |
| 72 | + snprintf(s_user_data, sizeof(s_user_data), |
| 73 | + "%s/mavsim-viewer", xdg); |
| 74 | + } else { |
| 75 | + const char *home = getenv("HOME"); |
| 76 | + if (home) { |
| 77 | + snprintf(s_user_data, sizeof(s_user_data), |
| 78 | + "%s/.local/share/mavsim-viewer", home); |
| 79 | + } |
| 80 | + } |
| 81 | +#endif |
| 82 | + |
| 83 | + // Compile-time install prefix (set by CMake) |
| 84 | +#ifdef MAVSIM_INSTALL_DATADIR |
| 85 | + snprintf(s_install_data, sizeof(s_install_data), |
| 86 | + "%s", MAVSIM_INSTALL_DATADIR); |
| 87 | +#endif |
| 88 | +} |
| 89 | + |
| 90 | +void asset_path(const char *subpath, char *out, size_t out_size) { |
| 91 | + char candidate[512]; |
| 92 | + |
| 93 | + // 1. User data directory |
| 94 | + if (s_user_data[0]) { |
| 95 | + snprintf(candidate, sizeof(candidate), "%s/%s", s_user_data, subpath); |
| 96 | + if (file_exists(candidate)) { |
| 97 | + snprintf(out, out_size, "%s", candidate); |
| 98 | + return; |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + // 2. System install directory |
| 103 | + if (s_install_data[0]) { |
| 104 | + snprintf(candidate, sizeof(candidate), "%s/%s", s_install_data, subpath); |
| 105 | + if (file_exists(candidate)) { |
| 106 | + snprintf(out, out_size, "%s", candidate); |
| 107 | + return; |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + // 3. Relative path (dev/build fallback) |
| 112 | + snprintf(out, out_size, "%s", subpath); |
| 113 | +} |
| 114 | + |
| 115 | +void asset_write_path(const char *subpath, char *out, size_t out_size) { |
| 116 | + if (s_user_data[0]) { |
| 117 | + snprintf(out, out_size, "%s/%s", s_user_data, subpath); |
| 118 | + mkdirs_for_file(out); |
| 119 | + } else { |
| 120 | + // Fallback to relative path |
| 121 | + snprintf(out, out_size, "%s", subpath); |
| 122 | + } |
| 123 | +} |
0 commit comments