/* (triforce) Sharing a save from the System tab, after NX2navi's menu rows (its docs/launcher.md, "Sharing a save"): Export save writes the save as a zip into the player's Downloads folder, Export save for a bug report adds the logs, the settings and the freeze report to it, and Import a save takes such a zip (Triforce's or NX2navi's) from the game folder's save-import folder and leaves its save to wait for the next start, where it becomes the save with the one it replaces kept first (host/save_tx_share.h, host/nn_fs.c). No game file goes into a zip. The rows are the renderer's (gpu/vk/vk_menu.cpp), the work is done here, on one thread of this file's own, so the game and its picture go on meanwhile: the thread asks the menu ten times a second what was chosen (rx_option HOS_OPT_SHARE_ASK), does it, and tells the row what to show under its help (HOS_OPT_SHARE_TEXT a byte at a time, then HOS_OPT_SHARE_SAID). It starts with the game's first save mount (nn_fs.c). The bug capture (host/bug_capture.c) is written on the same thread, and its save is this file's export. The save in a zip is the one the next start would load, read with no Commit under way (hos_fs_save_read): the files of one commit, never the working copy the game is writing. With HOS_SAVE_TX=0 the game writes its save in place and nothing is exported: a copy could catch a file half written. HOS_SAVE_EXPORT_DIR=: where the zips go, in place of the Downloads folder (a test's, or a player's own). */ #include "hos_paths.h" /* (first: hos_game_path_a, and windows.h on Windows) */ #include #include #include #include #include #include "../gpu/vk/vk_api.h" #include "save_pack.h" #ifdef _WIN32 #define SEP "\\" #else #define SEP "/" #endif #define SHARE_PATH 1024 #define LOG_MAX (16u << 20) /* (of a log in a bug report: more than that is a trace run's, cut in its middle) */ void hos_log(const char *fmt, ...); void hos_log_flush(void); int rx_option(int id, int otherwise); void rx_option_set(int id, int value); const char *hos_version(void); /* hos_main.c */ /* nn_fs.c */ int hos_fs_save_read(int (*fn)(void *arg, const char *rel, const void *data, size_t n), void *arg); int hos_fs_save_import_stage(int n, const char *const *rels, const uint8_t *const *data, const size_t *sizes); int hos_fs_save_import_waits(void); int hos_fs_save_import_cancel(void); int hos_fs_save_import_note(char *out, size_t cap); /* bug_capture.c */ int hos_bug_capture_ask(const char *trigger); void hos_bug_capture_key(void); void hos_bug_capture_tick(void); void hos_bug_capture_work(void); /* The game folder (the log, the settings and the saves are in it: hos_paths.h), with no separator at its end. 0: unknown */ int hos_share_game_dir(char *out, size_t cap) { char exe[MAX_PATH]; DWORD n = hos_game_path_a(exe, MAX_PATH); if (!n || n >= MAX_PATH) return 0; while (n && exe[n - 1] != '\\' && exe[n - 1] != '/') n--; if (n < 2) return 0; exe[n - 1] = 0; return snprintf(out, cap, "%s", exe) < (int)cap; } static int is_dir(const char *path) { const DWORD at = GetFileAttributesA(path); return at != INVALID_FILE_ATTRIBUTES && (at & FILE_ATTRIBUTE_DIRECTORY); } /* The player's Downloads folder. 0: not known, or not there */ static int downloads_dir(char *out, size_t cap) { const char *forced = getenv("HOS_SAVE_EXPORT_DIR"); if (forced && *forced) return snprintf(out, cap, "%s", forced) < (int)cap; #ifdef _WIN32 /* (Windows' known folder: a player may have moved Downloads to another drive. shell32 and ole32 are looked up here, so the game links neither) */ static const struct { uint32_t a; uint16_t b, c; uint8_t d[8]; } downloads = { /* FOLDERID_Downloads */ 0x374DE290, 0x123F, 0x4565, {0x91, 0x64, 0x39, 0xC4, 0x92, 0x5E, 0x46, 0x7B}}; typedef HRESULT (WINAPI *known_fn)(const void *, DWORD, HANDLE, wchar_t **); typedef void (WINAPI *free_fn)(void *); HMODULE shell = LoadLibraryExA("shell32.dll", NULL, LOAD_LIBRARY_SEARCH_SYSTEM32); HMODULE ole = LoadLibraryExA("ole32.dll", NULL, LOAD_LIBRARY_SEARCH_SYSTEM32); known_fn known = shell ? (known_fn)(void *)GetProcAddress(shell, "SHGetKnownFolderPath") : NULL; free_fn co_free = ole ? (free_fn)(void *)GetProcAddress(ole, "CoTaskMemFree") : NULL; wchar_t *wide = NULL; int ok = 0; if (known && co_free && known(&downloads, 0, NULL, &wide) >= 0 && wide) ok = WideCharToMultiByte(CP_UTF8, 0, wide, -1, out, (int)cap, NULL, NULL) > 0; if (wide && co_free) co_free(wide); return ok && is_dir(out); #else const char *home = getenv("HOME"); return home && home[0] == '/' && snprintf(out, cap, "%s/Downloads", home) < (int)cap && is_dir(out); #endif } /* A file whole: malloc'd, *n its size, NULL when it is not there. One longer than max keeps its first sixteenth and its end, with a line between them that says what was left out (a log: its start names the build and the GPU, its end has what happened) */ static uint8_t *read_file(const char *path, size_t max, size_t *n) { FILE *f = fopen(path, "rb"); if (!f) return NULL; _fseeki64(f, 0, SEEK_END); const long long size = _ftelli64(f); uint8_t *data = size >= 0 ? (uint8_t *)malloc((size_t)(size > (long long)max ? max : size) + 96) : NULL; size_t got = 0; if (data && size <= (long long)max) { _fseeki64(f, 0, SEEK_SET); got = fread(data, 1, (size_t)size, f); } else if (data) { const size_t head = max / 16, tail = max - head; _fseeki64(f, 0, SEEK_SET); got = fread(data, 1, head, f); got += (size_t)snprintf((char *)data + got, 96, "\n[... %lld bytes left out here ...]\n", size - (long long)max); _fseeki64(f, size - (long long)tail, SEEK_SET); got += fread(data + got, 1, tail, f); } fclose(f); *n = got; return data; } /* ---- the save's files, kept while nn_fs.c holds its lock and packed after ---- */ typedef struct { char rel[104]; uint8_t *data; size_t n; } SaveFile; typedef struct { SaveFile *f; int count, cap, left_out; } SaveFiles; static int keep_file(void *arg, const char *rel, const void *data, size_t n) { SaveFiles *s = (SaveFiles *)arg; if (!save_pack_safe_rel(rel)) { /* (not a name a save's file has: something of the player's own in the folder) */ s->left_out++; return 1; } if (s->count == s->cap) { const int cap = s->cap ? s->cap * 2 : 32; SaveFile *f = (SaveFile *)realloc(s->f, (size_t)cap * sizeof *f); if (!f) return 0; s->f = f, s->cap = cap; } SaveFile *f = &s->f[s->count]; f->data = (uint8_t *)malloc(n ? n : 1); if (!f->data) return 0; memcpy(f->data, data, n); f->n = n; snprintf(f->rel, sizeof f->rel, "%s", rel); s->count++; return 1; } static void files_free(SaveFiles *s) { for (int i = 0; i < s->count; i++) free(s->f[i].data); free(s->f); memset(s, 0, sizeof *s); } /* The zip's bytes written as .part and renamed when whole: nothing of that name is half a zip. 1: written */ static int write_whole(const char *path, const uint8_t *data, size_t n) { char part[SHARE_PATH + 8]; snprintf(part, sizeof part, "%s.part", path); FILE *f = fopen(part, "wb"); if (!f) return 0; int ok = fwrite(data, 1, n, f) == n; if (fflush(f)) ok = 0; if (fclose(f)) ok = 0; if (ok && !MoveFileExA(part, path, 0)) ok = 0; if (!ok) DeleteFileA(part); return ok; } /* The save the next start would load as Triforce-save--