Free fork of NotProton: Windows Steam games in Mac Steam, on CodeWeavers' Wine and Apple's Game Porting Toolkit
Swift apple-silicond3dmetaldxmtdxvkgame-porting-toolkitgptkmac-gamingmacosmetalfxrosetta-2steamsteam-playwinewinecx
#include "shortcuts.h"
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
#define ICON_LIMIT (16 * 1024 * 1024)
typedef struct {
unsigned char *bytes;
size_t length;
} blob_t;
static int directory_at(int parent, const char *path) {
return openat(parent, path, O_RDONLY | O_DIRECTORY | O_NOFOLLOW_ANY | O_CLOEXEC);
}
static int same_time(struct timespec a, struct timespec b) {
return a.tv_sec == b.tv_sec && a.tv_nsec == b.tv_nsec;
}
static int read_at(int parent, const char *name, size_t limit, blob_t *out) {
*out = (blob_t){0};
int fd = openat(parent, name, O_RDONLY | O_NONBLOCK | O_NOFOLLOW | O_CLOEXEC);
if (fd < 0) return 0;
struct stat before, after;
int ok = fstat(fd, &before) == 0 && S_ISREG(before.st_mode) && before.st_size > 0 &&
(uint64_t)before.st_size <= limit && (out->bytes = malloc((size_t)before.st_size));
size_t done = 0;
while (ok && done < (size_t)before.st_size) {
ssize_t n = read(fd, out->bytes + done, (size_t)before.st_size - done);
if (n < 0 && errno == EINTR) continue;
if (n <= 0) ok = 0;
else done += (size_t)n;
}
ok = ok && fstat(fd, &after) == 0 && before.st_size == after.st_size &&
same_time(before.st_mtimespec, after.st_mtimespec) &&
same_time(before.st_ctimespec, after.st_ctimespec);
close(fd);
if (ok) {
out->length = done;
} else {
free(out->bytes);
out->bytes = NULL;
}
return ok;
}
static int same_blob(const blob_t *a, const blob_t *b) {
return a->length == b->length && memcmp(a->bytes, b->bytes, a->length) == 0;
}
// Accepts only the run.sh Steam writes for this app, unedited.
static int is_shortcut(int app, const char *app_id) {
char want[128];
int n = snprintf(want, sizeof(want),
"#!/bin/bash\n# autogenerated file - do not edit\n\nopen steam://run/%s\n\n", app_id);
int macos = directory_at(app, "Contents/MacOS");
if (macos < 0) return 0;
blob_t data;
int ok = n < (int)sizeof(want) && read_at(macos, "run.sh", sizeof(want), &data);
close(macos);
if (!ok) return 0;
ok = data.length == (size_t)n && memcmp(data.bytes, want, data.length) == 0;
free(data.bytes);
return ok;
}
static int same_entry(int parent, const char *name, int fd) {
struct stat path, opened;
return fstatat(parent, name, &path, AT_SYMLINK_NOFOLLOW) == 0 && fstat(fd, &opened) == 0 &&
path.st_dev == opened.st_dev && path.st_ino == opened.st_ino;
}
static void replace_icon(int parent, const char *name, int app, int resources, const blob_t *icon) {
char temp[64];
snprintf(temp, sizeof(temp), ".bridgeplay-icon-%d-%08x", (int)getpid(), arc4random());
int fd = openat(resources, temp, O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW | O_CLOEXEC, 0644);
if (fd < 0) return;
size_t done = 0;
while (done < icon->length) {
ssize_t n = write(fd, icon->bytes + done, icon->length - done);
if (n < 0 && errno == EINTR) continue;
if (n <= 0) break;
done += (size_t)n;
}
int error = close(fd);
// Finder keeps showing a bundle's cached icon until the bundle's mtime changes.
if (done == icon->length && error == 0 && same_entry(parent, name, app) &&
renameat(resources, temp, resources, "shortcut.icns") == 0)
futimens(app, NULL);
unlinkat(resources, temp, 0);
}
static void update_folder(int home, const char *folder, const char *app_id,
const blob_t *icon, const blob_t *stock) {
int parent = directory_at(home, folder);
if (parent < 0) return;
DIR *dir = fdopendir(parent);
if (!dir) {
close(parent);
return;
}
struct dirent *entry;
while ((entry = readdir(dir))) {
size_t len = strlen(entry->d_name);
if (len <= 4 || strcmp(entry->d_name + len - 4, ".app") != 0) continue;
int app = directory_at(parent, entry->d_name);
if (app < 0) continue;
int resources = is_shortcut(app, app_id) ? directory_at(app, "Contents/Resources") : -1;
blob_t current;
if (resources >= 0 && read_at(resources, "shortcut.icns", ICON_LIMIT, ¤t)) {
if (same_blob(¤t, stock)) replace_icon(parent, entry->d_name, app, resources, icon);
free(current.bytes);
}
if (resources >= 0) close(resources);
close(app);
}
closedir(dir);
}
void np_shortcuts_icons(const char *home, const char *app_id, const char *icon_path, const char *stock_path) {
if (!home || home[0] != '/' || !app_id || !icon_path || !stock_path) return;
blob_t icon, stock;
read_at(AT_FDCWD, icon_path, ICON_LIMIT, &icon);
read_at(AT_FDCWD, stock_path, ICON_LIMIT, &stock);
int dir = open(home, O_RDONLY | O_DIRECTORY | O_CLOEXEC);
if (dir >= 0 && icon.bytes && stock.bytes && !same_blob(&icon, &stock)) {
update_folder(dir, "Applications", app_id, &icon, &stock);
update_folder(dir, "Desktop", app_id, &icon, &stock);
}
if (dir >= 0) close(dir);
free(icon.bytes);
free(stock.bytes);
}