102 Patches: Detours to the Rescue
C reference for DttR maintainers and modders.
Loading...
Searching...
No Matches
paths.c
Go to the documentation of this file.
1#include <dttr_loader_paths.h>
2
3#include <sds.h>
4#include <xxhash.h>
5
6#include <stdint.h>
7#include <stdio.h>
8#include <string.h>
9
10static const wchar_t *const GAME_SUBPATHS[] = {
11 L"pcdogs.exe",
12 L"Setup\\102Dalms\\pcdogs.exe",
13};
14
15static const wchar_t ISO_SUFFIX[] = L".iso";
16static const char *const ISO_CACHE_PATH = "DetoursToTheRescue\\cache\\iso";
17static const char *const ISO_GAME_ROOT = "Setup/102Dalms";
18static const char *const ISO_GAME_EXE_PATH = "Setup/102Dalms/pcdogs.exe";
19static const char *const ISO_GAME_PKG_PATH = "Setup/102Dalms/pcdogs.pkg";
20static const char *const ISO_GAME_DATA_PATH = "Setup/102Dalms/data";
21
22static wchar_t ascii_lower_w(wchar_t ch) {
23 if (ch >= L'A' && ch <= L'Z') {
24 return (wchar_t)(ch - L'A' + L'a');
25 }
26
27 return ch;
28}
29
30static bool has_suffix_w(const wchar_t *path, const wchar_t *suffix) {
31 const size_t path_len = wcslen(path);
32 const size_t suffix_len = wcslen(suffix);
33 if (path_len < suffix_len) {
34 return false;
35 }
36
37 const wchar_t *candidate = path + path_len - suffix_len;
38 for (size_t i = 0; i < suffix_len; i++) {
39 if (ascii_lower_w(candidate[i]) != ascii_lower_w(suffix[i])) {
40 return false;
41 }
42 }
43
44 return true;
45}
46
47bool DTTR_LoaderPath_IsISOW(const wchar_t *path) {
48 return path && has_suffix_w(path, ISO_SUFFIX);
49}
50
52 return sizeof(GAME_SUBPATHS) / sizeof(GAME_SUBPATHS[0]);
53}
54
55const wchar_t *DTTR_Loader_GameSubpathAt(size_t index) {
56 return index < DTTR_Loader_GameSubpathCount() ? GAME_SUBPATHS[index] : NULL;
57}
58
60 return ISO_GAME_ROOT;
61}
62
64 return ISO_GAME_EXE_PATH;
65}
66
68 return ISO_GAME_PKG_PATH;
69}
70
72 return ISO_GAME_DATA_PATH;
73}
74
75static uint64_t hash_path(const char *path) {
76 sds normalized = sdsnew(path);
77 if (!normalized) {
78 return 0;
79 }
80
81 sdstolower(normalized);
82 sdsmapchars(normalized, "/", "\\", 1);
83
84 const XXH64_hash_t hash = XXH3_64bits(normalized, sdslen(normalized));
85 sdsfree(normalized);
86 return hash;
87}
88
89static bool path_needs_separator(const char *path) {
90 const size_t len = strlen(path);
91 return len > 0 && path[len - 1] != '\\' && path[len - 1] != '/';
92}
93
95 const char *cache_base_dir,
96 const char *iso_path,
97 char *out_path,
98 size_t out_path_size
99) {
100 if (!cache_base_dir || !cache_base_dir[0] || !iso_path || !iso_path[0] || !out_path
101 || out_path_size == 0) {
102 return false;
103 }
104
105 const uint64_t hash = hash_path(iso_path);
106 const bool needs_separator = path_needs_separator(cache_base_dir);
107
108 const int written = snprintf(
109 out_path,
110 out_path_size,
111 "%s%s%s\\%016llx",
112 cache_base_dir,
113 needs_separator ? "\\" : "",
115 (unsigned long long)hash
116 );
117 return written > 0 && (size_t)written < out_path_size;
118}
DTTR_Graphics_COM_DirectDrawSurface7 DWORD flags void NULL
const char * DTTR_LoaderISO_GameEXEPath()
Definition paths.c:63
size_t DTTR_Loader_GameSubpathCount()
Definition paths.c:51
static bool has_suffix_w(const wchar_t *path, const wchar_t *suffix)
Definition paths.c:30
static const char *const ISO_CACHE_PATH
Definition paths.c:16
const char * DTTR_LoaderISO_GameDataPath()
Definition paths.c:71
static uint64_t hash_path(const char *path)
Definition paths.c:75
static const char *const ISO_GAME_ROOT
Definition paths.c:17
bool DTTR_LoaderISO_CacheRootForPath(const char *cache_base_dir, const char *iso_path, char *out_path, size_t out_path_size)
Definition paths.c:94
const wchar_t * DTTR_Loader_GameSubpathAt(size_t index)
Definition paths.c:55
static const char *const ISO_GAME_EXE_PATH
Definition paths.c:18
static wchar_t ascii_lower_w(wchar_t ch)
Definition paths.c:22
const char * DTTR_LoaderISO_GameRoot()
Definition paths.c:59
static const char *const ISO_GAME_DATA_PATH
Definition paths.c:20
bool DTTR_LoaderPath_IsISOW(const wchar_t *path)
Definition paths.c:47
static const wchar_t ISO_SUFFIX[]
Definition paths.c:15
static const wchar_t *const GAME_SUBPATHS[]
Definition paths.c:10
static bool path_needs_separator(const char *path)
Definition paths.c:89
const char * DTTR_LoaderISO_GamePkgPath()
Definition paths.c:67
static const char *const ISO_GAME_PKG_PATH
Definition paths.c:19