Files
Triforce-Source/host/hos_update.c
ruckus7382sfgsgdf 4fa4f9188b Triforce v0.8.0
Evil HD (SMAA Ultra, 16x filtering, full textures, ambient occlusion with bounce light, sunlight shafts, foliage drawn 4x as far, the remastered texture pack's loader and the launcher's download of it), FSR 3.1 frame generation for every GPU, Intel XeSS, FSR 4 model choice, ReShade presets, Auto (this PC), HUD placement, the options menu in 13 languages, a second input for every control, hold to toggle, DualSense speaker and haptics, NFC from the player's own dumps, the above-60 fps game fixes, the Debug tab's flight and Inventory fixes, the launcher's full settings, and over 250 fixes.
2026-11-25 10:21:32 -06:00

362 lines
18 KiB
C

/* (hookshot) The update offer: at start, the latest release on GitHub is asked for (a few seconds at most, nothing when offline), and
when it is not the release this game folder was built with, the player is asked whether to update. Yes starts Triforce Setup
that made the folder (triforce_version.txt, written by nx2/make_game.py) with --update: it downloads the new release and builds the
folder again from the game files the folder keeps, and the game closes. A folder with no triforce_version.txt (built from the
repository, or before updates came in) is never asked about. HOOKSHOT_NO_UPDATE_CHECK=1 (triforce_env.txt) turns it off */
#if defined(_WIN32) || defined(__linux__)
#include <ctype.h>
#include <string.h>
/* a release tag as it may be shown and passed on to the Builder: letters, digits and . _ + - (from the network, or a
file of the game folder) */
static int safe_tag(const char *s) {
size_t n = strlen(s);
if (!n || n > 80) return 0;
for (size_t i = 0; i < n; i++)
if (!isalnum((unsigned char)s[i]) && !strchr("._+-", s[i])) return 0;
return 1;
}
/* ((Triforce, 11-24, after Source A's) Whether release a is newer than release b: "v0.7.2" > "v0.7.1", and a release after
its own prereleases ("v0.7.1" > "v0.7.1-test3"). A folder built by a test build of the next release, before it is out, is
newer than the latest release: "Update" would build the older one over it. Tags that are not v<numbers> compare as not newer */
static int newer_version(const char *a, const char *b) {
if (*a == 'v' || *a == 'V') a++;
if (*b == 'v' || *b == 'V') b++;
if (!isdigit((unsigned char)*a) || !isdigit((unsigned char)*b)) return 0;
for (;;) {
unsigned long x = 0, y = 0;
while (isdigit((unsigned char)*a)) x = x * 10 + (unsigned long)(*a++ - '0');
while (isdigit((unsigned char)*b)) y = y * 10 + (unsigned long)(*b++ - '0');
if (x != y) return x > y;
const int ma = *a == '.' && isdigit((unsigned char)a[1]), mb = *b == '.' && isdigit((unsigned char)b[1]);
if (ma && mb) { a++, b++; continue; }
if (ma != mb) return ma; /* (v0.7.1.1 after v0.7.1) */
return !*a && *b; /* (the same numbers: a release after a prerelease of it, nothing else newer) */
}
}
#endif
#ifdef _WIN32
#include "hos_paths.h"
#include <windows.h>
#include <winhttp.h>
#include <shellapi.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* (11-13: Triforce's own home, as setup/Setup.cs Program.ReleaseApi: the release list of triforce/downloads on its Forgejo,
asked on the main domain. Up to v0.3.5: api.github.com, /repos/ruckus7382sfgsgdf/CBeta0.2/releases?per_page=1. The Linux
lines below ask the same list) */
#define UPDATE_HOST L"triforceproject.com"
#define UPDATE_PATH L"/api/v1/repos/triforce/downloads/releases?limit=1" /* (the newest, pre-releases too) */
#define RELEASES_PAGE L"https://triforceproject.com"
/* (a line "name=value" of a small text file, in UTF-8: its value, trimmed) */
static int read_value(const wchar_t *path, const char *name, char *out, size_t cap) {
FILE *f = _wfopen(path, L"rb");
if (!f) return 0;
char line[1024];
const size_t n = strlen(name);
int found = 0;
while (!found && fgets(line, sizeof line, f)) {
char *s = line;
if ((unsigned char)s[0] == 0xEF && (unsigned char)s[1] == 0xBB && (unsigned char)s[2] == 0xBF) s += 3;
if (strncmp(s, name, n) || s[n] != '=') continue;
s += n + 1;
size_t len = strlen(s);
while (len && (s[len - 1] == 10 || s[len - 1] == 13 || s[len - 1] == ' ' || s[len - 1] == 9)) s[--len] = 0;
if (len && len < cap) {
memcpy(out, s, len + 1);
found = 1;
}
}
fclose(f);
return found;
}
/* (the latest release's tag, from the release list (Forgejo's, GitHub's before): "tag_name":"...") */
static int latest_tag(char *tag, size_t cap) {
int ok = 0;
HINTERNET s = WinHttpOpen(L"Triforce-update-check", WINHTTP_ACCESS_TYPE_DEFAULT_PROXY, WINHTTP_NO_PROXY_NAME, WINHTTP_NO_PROXY_BYPASS, 0);
if (!s) return 0;
WinHttpSetTimeouts(s, 2500, 2500, 2500, 3000);
HINTERNET c = WinHttpConnect(s, UPDATE_HOST, INTERNET_DEFAULT_HTTPS_PORT, 0);
HINTERNET r = c ? WinHttpOpenRequest(c, L"GET", UPDATE_PATH, NULL, WINHTTP_NO_REFERER, WINHTTP_DEFAULT_ACCEPT_TYPES, WINHTTP_FLAG_SECURE) : NULL;
if (r && WinHttpSendRequest(r, L"Accept: application/vnd.github+json\r\n", (DWORD)-1, WINHTTP_NO_REQUEST_DATA, 0, 0, 0) &&
WinHttpReceiveResponse(r, NULL)) {
DWORD status = 0, size = sizeof status;
WinHttpQueryHeaders(r, WINHTTP_QUERY_STATUS_CODE | WINHTTP_QUERY_FLAG_NUMBER, WINHTTP_HEADER_NAME_BY_INDEX, &status, &size, WINHTTP_NO_HEADER_INDEX);
if (status == 200) {
const size_t max = 512 << 10;
char *body = (char *)malloc(max + 1);
size_t have = 0;
DWORD got = 0;
while (body && have < max && WinHttpReadData(r, body + have, (DWORD)(max - have), &got) && got) have += got;
if (body) {
body[have] = 0;
const char *k = strstr(body, "\"tag_name\"");
if (k && (k = strchr(k + 10, '"'))) {
const char *e = strchr(++k, '"');
if (e && e > k && (size_t)(e - k) < cap) {
memcpy(tag, k, (size_t)(e - k));
tag[e - k] = 0;
ok = safe_tag(tag); /* (hookshot: it goes on the Builder's command line) */
}
}
free(body);
}
}
}
if (r) WinHttpCloseHandle(r);
if (c) WinHttpCloseHandle(c);
WinHttpCloseHandle(s);
return ok;
}
void hos_update_check(void) {
if (getenv("HOOKSHOT_NO_UPDATE_CHECK") && strcmp(getenv("HOOKSHOT_NO_UPDATE_CHECK"), "0")) return;
wchar_t dir[MAX_PATH], file[MAX_PATH + 64];
DWORD n = hos_game_path_w(dir, MAX_PATH);
if (!n || n >= MAX_PATH) return;
while (n && dir[n - 1] != L'\\' && dir[n - 1] != L'/') n--;
if (!n) return;
dir[n - 1] = 0;
char have[128], builder[MAX_PATH * 3], skipped[128] = "", tag[128];
_snwprintf(file, MAX_PATH + 64, L"%ls\\triforce_version.txt", dir);
if (!read_value(file, "version", have, sizeof have) || !safe_tag(have)) return;
_snwprintf(file, MAX_PATH + 64, L"%ls\\user\\triforce_update_skip.txt", dir);
read_value(file, "skip", skipped, sizeof skipped);
if (!latest_tag(tag, sizeof tag)) {
fprintf(stderr, "update: the latest release could not be asked for (offline?)\n");
return;
}
fprintf(stderr, "update: this folder is %s, the latest release is %s\n", have, tag);
if (!strcmp(tag, have) || !strcmp(tag, skipped)) return;
if (newer_version(have, tag)) return; /* ((Triforce, 11-24) a test build of the next release: not offered the older one */
wchar_t wtag[128], whave[128], wbuilder[MAX_PATH], text[2048];
MultiByteToWideChar(CP_UTF8, 0, tag, -1, wtag, 128);
MultiByteToWideChar(CP_UTF8, 0, have, -1, whave, 128);
_snwprintf(file, MAX_PATH + 64, L"%ls\\triforce_version.txt", dir);
const int has_builder = read_value(file, "builder", builder, sizeof builder) &&
MultiByteToWideChar(CP_UTF8, 0, builder, -1, wbuilder, MAX_PATH) > 0 &&
GetFileAttributesW(wbuilder) != INVALID_FILE_ATTRIBUTES;
if (has_builder)
_snwprintf(text, 2048,
L"A new version of Triforce is out: %ls (this game is %ls).\n\n"
L"Update now? The game closes, and Triforce Setup downloads the new version and builds this game folder again from the "
L"game files it keeps. Only what changed is compiled again, so it usually takes a few minutes. Your saves and settings stay.\n\n"
L"Yes: update now\nNo: not now (asked again next time)\nCancel: skip this version",
wtag, whave);
else
_snwprintf(text, 2048,
L"A new version of Triforce is out: %ls (this game is %ls).\n\n"
L"The Triforce Setup that made this folder was not found, so it can't update by itself. Open the download page?\n\n"
L"Yes: open the page\nNo: not now (asked again next time)\nCancel: skip this version",
wtag, whave);
const int answer = MessageBoxW(NULL, text, L"Triforce update", MB_YESNOCANCEL | MB_ICONINFORMATION | MB_TOPMOST | MB_SETFOREGROUND);
if (answer == IDCANCEL) {
wchar_t user[MAX_PATH + 16];
_snwprintf(user, MAX_PATH + 16, L"%ls\\user", dir);
CreateDirectoryW(user, NULL);
_snwprintf(file, MAX_PATH + 64, L"%ls\\user\\triforce_update_skip.txt", dir);
FILE *f = _wfopen(file, L"wb");
if (f) fprintf(f, "skip=%s\n", tag), fclose(f);
return;
}
if (answer != IDYES) return;
if (!has_builder) {
ShellExecuteW(NULL, L"open", RELEASES_PAGE, NULL, NULL, SW_SHOWNORMAL);
return;
}
/* (the Python builder: builder= is its pythonw.exe, and builder_args= what goes before --update, its start script) */
char bargs[1024] = "";
wchar_t wbargs[1024] = L"";
_snwprintf(file, MAX_PATH + 64, L"%ls\\triforce_version.txt", dir);
if (read_value(file, "builder_args", bargs, sizeof bargs)) MultiByteToWideChar(CP_UTF8, 0, bargs, -1, wbargs, 1024);
wchar_t args[MAX_PATH + 1280], wdir[MAX_PATH];
_snwprintf(args, MAX_PATH + 1280, L"%ls%ls--update \"%ls\" --tag \"%ls\"", wbargs, wbargs[0] ? L" " : L"", dir, wtag);
wcsncpy(wdir, wbuilder, MAX_PATH);
wdir[MAX_PATH - 1] = 0;
wchar_t *slash = wcsrchr(wdir, L'\\');
if (slash) *slash = 0;
if ((INT_PTR)ShellExecuteW(NULL, L"open", wbuilder, args, slash ? wdir : NULL, SW_SHOWNORMAL) > 32) {
void hos_log_flush(void);
hos_log_flush(); /* (the lines logged before, queued: host/hos_thread.c log_queue) */
fprintf(stderr, "update: Triforce Setup started to update to %s; the game closes\n", tag);
fflush(stderr);
{ void hos_crash_clean_end(void); hos_crash_clean_end(); } /* ((Triforce) a clean end: host/hos_crash_record.c */
ExitProcess(0);
}
MessageBoxW(NULL, L"Triforce Setup could not be started. The game starts as it is.", L"Triforce update", MB_OK | MB_ICONWARNING | MB_TOPMOST);
}
#elif defined(__linux__)
#include "hos_paths.h"
extern const char *hos_user_dir; /* (host/nn_fs.c: the saves' folder, set by hos_main.c before hos_update_check) */
#include <ctype.h>
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
/* (the same offer as Windows, when the player started bin/hookshot itself. The launcher sets HOOKSHOT_NO_UPDATE_CHECK
and asks in its own window. curl and zenity do the network and the question; without either, the game just starts) */
static int read_value(const char *path, const char *name, char *out, size_t cap) {
FILE *f = fopen(path, "rb");
if (!f) return 0;
char line[1024];
const size_t n = strlen(name);
int found = 0;
while (!found && fgets(line, sizeof line, f)) {
char *s = line;
if (strncmp(s, name, n) || s[n] != '=') continue;
s += n + 1;
size_t len = strlen(s);
while (len && (s[len - 1] == '\n' || s[len - 1] == '\r' || s[len - 1] == ' ')) s[--len] = 0;
if (len && len < cap) memcpy(out, s, len + 1), found = 1;
}
fclose(f);
return found;
}
static int pipe_tag(const char *cmd, char *tag, size_t cap) {
FILE *p = popen(cmd, "r");
if (!p) return 0;
char buf[8192];
size_t n = fread(buf, 1, sizeof buf - 1, p);
pclose(p);
buf[n] = 0;
const char *k = strstr(buf, "\"tag_name\"");
if (k && (k = strchr(k + 10, '"'))) {
const char *e = strchr(++k, '"');
if (e && e > k && (size_t)(e - k) < cap) {
memcpy(tag, k, (size_t)(e - k));
tag[e - k] = 0;
return safe_tag(tag);
}
}
if (!strncmp(buf, "tag ", 4)) {
const char *s = buf + 4;
size_t len = strcspn(s, "\r\n");
if (len && len < cap) {
memcpy(tag, s, len);
tag[len] = 0;
return safe_tag(tag);
}
}
return 0;
}
static int latest_tag(char *tag, size_t cap) {
/* (11-13: Triforce's own home, as on Windows above: the newest release of triforce/downloads, pre-releases too. The list
of one release starts {"id":..,"tag_name":"..": pipe_tag finds the tag in its first bytes. The second address is the
same list asked of the Forgejo itself, should the main domain not pass it on. Up to v0.3.5 this asked GitHub's
/releases/latest, which skips pre-releases, and every Triforce release is one: no Linux game was ever told of an update) */
if (pipe_tag("curl -fsSL --max-time 4 --retry 2 -A Triforce-update-check -H \"Accept: application/json\" "
"\"https://triforceproject.com/api/v1/repos/triforce/downloads/releases?limit=1\" 2>/dev/null", tag, cap))
return 1;
return pipe_tag("curl -fsSL --max-time 8 --retry 2 -A Triforce-update-check -H \"Accept: application/json\" "
"\"https://git.triforceproject.com/api/v1/repos/triforce/downloads/releases?limit=1\" 2>/dev/null", tag, cap);
}
/* the builder an update installed (builder/linux/linux_update.py): <data>/triforce/builder/versions/<current>/AppRun. The
builder= line names the AppImage that built the folder, which the player may have deleted since */
static int installed_builder(char *out, size_t cap) {
const char *home = getenv("HOOKSHOT_BUILDER_HOME"), *data = getenv("XDG_DATA_HOME"), *user = getenv("HOME");
char root[MAX_PATH * 2], path[MAX_PATH * 2 + 64], tag[128];
if (home && *home) snprintf(root, sizeof root, "%s/triforce/builder", home);
else if (data && *data) snprintf(root, sizeof root, "%s/triforce/builder", data);
else if (user && *user) snprintf(root, sizeof root, "%s/.local/share/triforce/builder", user);
else return 0;
snprintf(path, sizeof path, "%s/current", root);
FILE *f = fopen(path, "rb");
if (!f) return 0;
const int got = fgets(tag, sizeof tag, f) != NULL;
fclose(f);
if (!got) return 0;
tag[strcspn(tag, "\r\n ")] = 0;
if (!safe_tag(tag)) return 0;
if (snprintf(out, cap, "%s/versions/%s/AppRun", root, tag) >= (int)cap) return 0;
return access(out, X_OK) == 0;
}
void hos_update_check(void) {
const char *off = getenv("HOOKSHOT_NO_UPDATE_CHECK");
if (off && strcmp(off, "0")) return;
char path[MAX_PATH + 64], have[128], builder[MAX_PATH * 2], skipped[128] = "", tag[128];
DWORD n = hos_game_path_a(path, MAX_PATH);
if (!n || n >= MAX_PATH) return;
while (n && path[n - 1] != '/') n--;
if (!n) return;
path[n] = 0;
char dir[MAX_PATH];
memcpy(dir, path, n);
dir[n] = 0;
snprintf(path, sizeof path, "%striforce_version.txt", dir);
if (!read_value(path, "version", have, sizeof have) || !safe_tag(have)) return;
/* ((Triforce, 11-24, after Source A's) "Skip this version" is kept with the saves, where they are (hos_main.c sets
hos_user_dir first: $XDG_DATA_HOME/triforce/user, or --user's folder). In user/ beside the game, where Linux keeps no
saves any more, the next start's move of an old user/ folder (hos_main.c saves_dir) took the file for a save, moved it
and wrote saves-moved.txt, and the start asked again; skipped again, the file was in both places, which the move reads
as saves it cannot move: the game then ran on the empty user/ beside it and showed no saves */
char skip[MAX_PATH + 64];
if (hos_user_dir) snprintf(skip, sizeof skip, "%s/triforce_update_skip.txt", hos_user_dir);
else snprintf(skip, sizeof skip, "%suser/triforce_update_skip.txt", dir);
read_value(skip, "skip", skipped, sizeof skipped);
if (!latest_tag(tag, sizeof tag)) {
fprintf(stderr, "update: the latest release could not be asked for (offline?)\n");
return;
}
fprintf(stderr, "update: this folder is %s, the latest release is %s\n", have, tag);
if (!strcmp(tag, have) || !strcmp(tag, skipped)) return;
if (newer_version(have, tag)) return; /* ((Triforce, 11-24) a test build of the next release: not offered the older one */
snprintf(path, sizeof path, "%striforce_version.txt", dir);
const int has_builder = installed_builder(builder, sizeof builder) ||
(read_value(path, "builder", builder, sizeof builder) && access(builder, X_OK) == 0);
char text[1600], cmd[4096];
snprintf(text, sizeof text,
has_builder
? "A new version of Triforce is out: %s (this game is %s). Update now? The game closes, and Triforce Setup downloads only what changed and builds this folder again. Your saves and settings stay."
: "A new version of Triforce is out: %s (this game is %s). The builder that made this folder was not found. Open the download page?",
tag, have);
snprintf(cmd, sizeof cmd, "zenity --list --title=Triforce --hide-header --column= --text='%s' 'Update now' 'Not now' 'Skip this version'", text);
FILE *pick = popen(cmd, "r");
if (!pick) return;
char answer[64] = "";
if (!fgets(answer, sizeof answer, pick)) answer[0] = 0;
pclose(pick);
if (!strncmp(answer, "Skip", 4)) {
if (hos_user_dir) mkdir(hos_user_dir, 0755);
else snprintf(path, sizeof path, "%suser", dir), mkdir(path, 0755);
FILE *f = fopen(skip, "wb");
if (f) fprintf(f, "skip=%s\n", tag), fclose(f);
return;
}
if (strncmp(answer, "Update", 6)) return;
if (!has_builder) {
if (fork() == 0) {
execlp("xdg-open", "xdg-open", "https://triforceproject.com", (char *)0);
_exit(127);
}
return;
}
pid_t pid = fork();
if (pid == 0) {
execl(builder, builder, "--update", dir, "--tag", tag, (char *)0);
_exit(127);
}
if (pid > 0) {
fprintf(stderr, "update: Triforce Setup started to update to %s; the game closes\n", tag);
fflush(stderr);
_exit(0);
}
}
#else
void hos_update_check(void) {}
#endif