Repository navigation
Expand file tree
/
Copy pathupdate.c
More file actions
222 lines (213 loc) · 11.2 KB
/
Copy pathupdate.c
File metadata and controls
222 lines (213 loc) · 11.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
// Self-update from the Steam Workshop copy. Steam keeps steamapps\workshop\content\712100\<item>\dinput8.dll
// up to date, but players forget to run install.bat again. At startup this compares the version marker in that file
// with ours; if it is higher, the new file replaces the one in the game folder for the next launch (a loaded DLL can
// be renamed: ours becomes dinput8.dll.old, deleted on the next start). It never downgrades, ignores files without
// the marker, uses no network, and on any failure keeps the current DLL and says so in the log.
#include <windows.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "version.h"
// The Workshop item id, set once the item exists; empty = no self-update (the check logs that and stops).
#define WORKSHOP_ITEM L"3828420829"
static const char MARKER[] = "ThronesCPUPerfFix-version:";
#define STR_(x) #x
#define STR(x) STR_(x)
__attribute__((used)) static const char g_marker[] = "ThronesCPUPerfFix-version:" STR(MOD_VERSION_NUM);
// The version marker in a file, or 0 (missing, unreadable, not a PE file, or no marker).
static int marker_in(const char *buf, size_t n) {
if (n < 0x400 || buf[0] != 'M' || buf[1] != 'Z') return 0;
for (size_t i = 0; i + sizeof MARKER <= n; i++) // room for the prefix and at least one digit
if (buf[i] == 'T' && memcmp(buf + i, MARKER, sizeof MARKER - 1) == 0) {
int v = 0;
for (size_t j = i + sizeof MARKER - 1; j < n && buf[j] >= '0' && buf[j] <= '9' && v < 100000; j++)
v = v * 10 + (buf[j] - '0');
if (v) return v; // the bare prefix (MARKER itself) is in the file too; skip it
}
return 0;
}
static int file_version(const wchar_t *path) {
enum { CAP = 2 << 20 };
FILE *f = _wfopen(path, L"rb");
if (!f) return 0;
char *buf = HeapAlloc(GetProcessHeap(), 0, CAP); // only while reading (was a static 2 MB kept all session; Attila's ultrareview)
size_t n = buf ? fread(buf, 1, CAP, f) : 0;
fclose(f);
int v = buf ? marker_in(buf, n) : 0;
if (buf) HeapFree(GetProcessHeap(), 0, buf);
return v;
}
// 1 if the file is at least as long as its own PE header says (the end of its last section): a Workshop download Steam
// has not finished passes the version marker, which sits early in the file (Attila review E4, AGENT_LOG 219 there)
static int pe_complete(const wchar_t *path) {
HANDLE f = CreateFileW(path, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
if (f == INVALID_HANDLE_VALUE) return 0;
static unsigned char h[4096];
DWORD n = 0, size = GetFileSize(f, NULL);
BOOL ok = ReadFile(f, h, sizeof h, &n, NULL);
CloseHandle(f);
if (!ok || n < 0x40 || h[0] != 'M' || h[1] != 'Z') return 0;
DWORD pe = *(DWORD *)(h + 0x3C);
if (pe > n - sizeof(IMAGE_NT_HEADERS32)) return 0;
IMAGE_NT_HEADERS32 *nt = (IMAGE_NT_HEADERS32 *)(h + pe);
IMAGE_SECTION_HEADER *sec = IMAGE_FIRST_SECTION(nt);
DWORD end = 0;
for (unsigned i = 0; i < nt->FileHeader.NumberOfSections; i++) {
if ((unsigned char *)(sec + i + 1) > h + n) return 0;
if (sec[i].PointerToRawData + sec[i].SizeOfRawData > end) end = sec[i].PointerToRawData + sec[i].SizeOfRawData;
}
return end && size != INVALID_FILE_SIZE && size >= end;
}
// self = this DLL's path, ws = the Workshop copy. Returns 1 if the game folder now holds the newer version.
int update_from(const wchar_t *self, const wchar_t *ws, int mine, void (*log)(const char *fmt, ...)) {
wchar_t old_[MAX_PATH], new_[MAX_PATH];
if (wcslen(self) + 5 >= MAX_PATH) return 0;
swprintf(old_, MAX_PATH, L"%ls.old", self);
swprintf(new_, MAX_PATH, L"%ls.new", self);
DeleteFileW(old_); // left by the previous update; not loaded any more
if (GetFileAttributesW(ws) == INVALID_FILE_ATTRIBUTES) return 0; // not subscribed on the Workshop
int theirs = file_version(ws);
if (theirs <= mine) return 0;
if (!pe_complete(ws)) {
log("update: the Workshop has version %d, but its file is not complete yet (Steam still downloading?) - next start", theirs);
return 0;
}
if (!CopyFileW(ws, new_, FALSE) || file_version(new_) != theirs || !pe_complete(new_)) {
log("update: the Workshop has version %d, but copying it failed (error %lu) - run install.bat in the Workshop "
"folder to update", theirs, GetLastError());
DeleteFileW(new_);
return 0;
}
if (!MoveFileExW(self, old_, MOVEFILE_REPLACE_EXISTING)) {
log("update: the Workshop has version %d, but this DLL could not be renamed (error %lu) - run install.bat",
theirs, GetLastError());
DeleteFileW(new_);
return 0;
}
if (!MoveFileExW(new_, self, MOVEFILE_REPLACE_EXISTING)) {
DWORD e = GetLastError();
MoveFileExW(old_, self, MOVEFILE_REPLACE_EXISTING); // put ours back
DeleteFileW(new_);
log("update: installing version %d failed (error %lu) - run install.bat", theirs, e);
return 0;
}
log("update: installed version %d from the Steam Workshop; it takes effect the next time the game starts", theirs);
return 1;
}
// The DLSS add-on (Upscaling and Framegen for Thrones of Britannia) is copied into <game>\FrameBridge by its install.bat,
// which also writes FrameBridge\source.txt: the Workshop item it was run from ("workshop_item=<id>"). Only that item is
// followed (no other Workshop item can put files in the game folder, and no id has to be built in before the add-on is
// uploaded).
// Its version is the number in FrameBridge\version.txt ("addon_version=N", written by tools/pack_framebridge.sh; none =
// 0). If the item's number is higher than the installed one, all its files are copied over, version.txt last, so a
// failed update is tried again next start. Never downgrades; files only in the game folder (settings.txt, source.txt,
// the helper's logs) are left alone. As Attila's and Rome 2's.
static int addon_version(const wchar_t *dir) {
wchar_t p[MAX_PATH];
char buf[64] = {0};
if (swprintf(p, MAX_PATH, L"%ls\\version.txt", dir) < 0) return 0;
FILE *f = _wfopen(p, L"rb");
if (!f) return 0;
fread(buf, 1, sizeof buf - 1, f);
fclose(f);
const char *v = strstr(buf, "addon_version=");
return v ? atoi(v + 14) : 0;
}
// the Workshop item id install.bat wrote, digits only, or 0
static int addon_item(const wchar_t *game_fb, wchar_t *id, int n) {
wchar_t p[MAX_PATH];
char buf[64] = {0};
if (swprintf(p, MAX_PATH, L"%ls\\source.txt", game_fb) < 0) return 0;
FILE *f = _wfopen(p, L"rb");
if (!f) return 0;
fread(buf, 1, sizeof buf - 1, f);
fclose(f);
const char *v = strstr(buf, "workshop_item=");
if (!v) return 0;
v += 14;
int k = 0;
while (v[k] >= '0' && v[k] <= '9' && k < n - 1 && k < 20) id[k] = (wchar_t)v[k], k++;
id[k] = 0;
return k > 0 && (!v[k] || strchr("\r\n \t", v[k]));
}
static int sync_dir(const wchar_t *dst, const wchar_t *src, int *copied) {
wchar_t pat[MAX_PATH], s[MAX_PATH], d[MAX_PATH];
if (swprintf(pat, MAX_PATH, L"%ls\\*", src) < 0) return 1;
WIN32_FIND_DATAW fd;
HANDLE h = FindFirstFileW(pat, &fd);
if (h == INVALID_HANDLE_VALUE) return 1;
int failed = 0;
do {
if (!wcscmp(fd.cFileName, L".") || !wcscmp(fd.cFileName, L"..") || !_wcsicmp(fd.cFileName, L"version.txt")) continue;
if (swprintf(s, MAX_PATH, L"%ls\\%ls", src, fd.cFileName) < 0 || swprintf(d, MAX_PATH, L"%ls\\%ls", dst, fd.cFileName) < 0) {
failed++;
continue;
}
if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
CreateDirectoryW(d, NULL);
failed += sync_dir(d, s, copied);
continue;
}
if (CopyFileW(s, d, FALSE)) (*copied)++;
else failed++;
} while (FindNextFileW(h, &fd));
FindClose(h);
return failed;
}
// game_fb = <game>\FrameBridge, ws_fb = the add-on's Workshop FrameBridge folder. Returns the number of files copied.
int addon_update_from(const wchar_t *game_fb, const wchar_t *ws_fb, void (*log)(const char *fmt, ...)) {
if (GetFileAttributesW(game_fb) == INVALID_FILE_ATTRIBUTES) return 0; // add-on not installed: never install it
if (GetFileAttributesW(ws_fb) == INVALID_FILE_ATTRIBUTES) return 0; // unsubscribed since
int theirs = addon_version(ws_fb), mine = addon_version(game_fb);
if (theirs <= mine) return 0;
wchar_t s[MAX_PATH], d[MAX_PATH];
int copied = 0, failed = sync_dir(game_fb, ws_fb, &copied);
if (!failed && (swprintf(s, MAX_PATH, L"%ls\\version.txt", ws_fb) < 0 || swprintf(d, MAX_PATH, L"%ls\\version.txt", game_fb) < 0 ||
!CopyFileW(s, d, FALSE)))
failed++;
if (failed)
log("update: DLSS add-on version %d from the Steam Workshop: %d file(s) could not be copied into FrameBridge - run the "
"add-on's install.bat", theirs, failed);
else
log("update: DLSS add-on version %d installed from the Steam Workshop (was %d, %d files)", theirs, mine, copied + 1);
return failed ? 0 : copied + 1;
}
// The game folder is steamapps\common\<game>; the Workshop copies live in the same library's steamapps\workshop.
void update_check(HMODULE self, void (*log)(const char *fmt, ...)) {
wchar_t me[MAX_PATH], ws[MAX_PATH];
DWORD n = GetModuleFileNameW(self, me, MAX_PATH);
if (n == 0 || n >= MAX_PATH) return;
// Windows reports the name as the game imports it ("DINPUT8.dll"); the update would be saved under that casing
wchar_t *fn = wcsrchr(me, L'\\');
if (!fn || fn + sizeof L"update_off.txt" / sizeof(wchar_t) >= me + MAX_PATH) return; // the longest name written at fn + 1
// update_off.txt in the game folder: nothing is taken from the Workshop (a dev or other build installed by hand
// stays, whatever version the Workshop copies have; Attila AGENT_LOG 120)
wcscpy(fn + 1, L"update_off.txt");
if (GetFileAttributesW(me) != INVALID_FILE_ATTRIBUTES) {
log("update: update_off.txt found - no updates from the Steam Workshop (this DLL and the add-on stay as installed)");
return;
}
wcscpy(fn + 1, L"dinput8.dll");
wcscpy(ws, me);
for (int up = 0; up < 3; up++) { // strip dinput8.dll, <game>, common
wchar_t *s = wcsrchr(ws, L'\\');
if (!s) return;
*s = 0;
}
if (wcslen(ws) + 64 >= MAX_PATH) return;
wchar_t game_fb[MAX_PATH], ws_fb[MAX_PATH], id[24];
wcscpy(game_fb, me);
wcscpy(wcsrchr(game_fb, L'\\') + 1, L"FrameBridge");
int addon = addon_item(game_fb, id, 24);
if (addon) swprintf(ws_fb, MAX_PATH, L"%ls\\workshop\\content\\712100\\%ls\\FrameBridge", ws, id);
if (!WORKSHOP_ITEM[0]) log("update: no Steam Workshop item set in this build - self-update off");
else {
wcscat(ws, L"\\workshop\\content\\712100\\" WORKSHOP_ITEM L"\\dinput8.dll");
// Our version is read from our own file, the same way as the Workshop copy's, so the comparison can never
// disagree with the marker and re-install the same file on every start.
int mine = file_version(me);
// a new DLL first: the add-on's new files may need it, so they follow on the next start, when it runs
if (update_from(me, ws, mine ? mine : MOD_VERSION_NUM, log)) return;
}
if (addon) addon_update_from(game_fb, ws_fb, log);
}