-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrophy.html
More file actions
362 lines (332 loc) · 13.7 KB
/
Copy pathtrophy.html
File metadata and controls
362 lines (332 loc) · 13.7 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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Trophy Surfer</title>
<style>
:root { --pad: 10px; }
body { font-family: system-ui, Segoe UI, Roboto, Arial, sans-serif; margin: 0; color: #111; }
header { position: sticky; top: 0; background: #f7f7fb; border-bottom: 1px solid #e8e8ef; padding: var(--pad); display: grid; gap: 8px; }
header .row { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; }
button, input[type="file"]::file-selector-button { cursor: pointer; border: 1px solid #d0d0da; background: #fff; padding: 6px 10px; border-radius: 6px; }
button[disabled] { opacity: .6; cursor: not-allowed; }
input[type="search"] { border: 1px solid #d0d0da; padding: 6px 10px; border-radius: 6px; min-width: 260px; }
main { padding: var(--pad); }
table { border-collapse: collapse; width: 100%; min-width: 820px; }
thead th { position: sticky; top: 104px; background: #fff; border-bottom: 2px solid #eee; padding: 8px; text-align: left; font-weight: 600; }
tbody td { border-bottom: 1px solid #f0f0f5; padding: 8px; }
tbody tr:nth-child(even) { background: #fafafe; }
.muted { color: #666; }
.pill { display: inline-block; background: #eef2ff; color: #334155; border: 1px solid #dbeafe; padding: 2px 8px; border-radius: 999px; font-size: 12px; }
.controls { display: flex; gap: 8px; flex-wrap: wrap; }
.right { margin-left: auto; }
.spacer { flex: 1 1 auto; }
</style>
</head>
<body>
<header>
<div class="row">
<strong>Trophy Surfer</strong>
<span class="muted">— Web <code>(RR)Settings.pul</code> Editor</span>
</div>
<div class="row">
<label for="pulFile"><strong>Load Settings (.pul):</strong></label>
<input id="pulFile" type="file" accept=".pul" />
<button id="saveBtn" disabled>Download Edited .pul</button>
<input id="search" type="search" placeholder="Search tracks by name or ID" disabled />
<span class="spacer"></span>
<button id="btnRR" title="Load FolderToTrackName.txt from server">Load Retro Rewind translations</button>
<button id="btnCustom">Load custom translations…</button>
<input id="transFile" type="file" accept=".txt" style="display:none" />
<span class="muted" id="status">No translations loaded.</span>
<span class="pill" id="countsPill" hidden></span>
</div>
<div class="row controls">
<span><strong>Column actions:</strong></span>
<button id="col1All" disabled>Set All 150cc</button>
<button id="col2All" disabled>Set All 200cc</button>
<button id="col3All" disabled>Set All 150cc Feather</button>
<button id="col4All" disabled>Set All 200cc Feather</button>
<button id="colClear" class="right" disabled>Clear All</button>
</div>
</header>
<main>
<table id="tracksTable">
<thead>
<tr>
<th style="width:160px">Track ID</th>
<th>Track Name</th>
<th style="width:140px">150cc</th>
<th style="width:140px">200cc</th>
<th style="width:160px">150cc Feather</th>
<th style="width:160px">200cc Feather</th>
</tr>
</thead>
<tbody></tbody>
</table>
</main>
<script>
// === Constants ===
const SIG_TROP = new Uint8Array([0x54, 0x52, 0x4F, 0x50]); // "TROP"
const FOOTER = new Uint8Array([0x47, 0x50, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01]); // 47 50 00 00 00 00 00 01
const COUNT_OFFS = [0x0C, 0x0E, 0x10, 0x12]; // big-endian 2-byte counts
const FIRST_BLOCK = 0x14; // start of 8-byte entries (ID[4], STATUS[4])
// === State ===
let translations = {}; // ID (uppercase) -> "Name (ID)"
let bytes = null; // Uint8Array of the loaded .pul file
let headerIdx = -1; // index of TROP header
let entries = []; // {pos, idHex, flags[4], checks[4], row, name}
let currentFileNameBase = 'settings';
// === Elements ===
const elStatus = document.getElementById('status');
const elCounts = document.getElementById('countsPill');
const elPulFile = document.getElementById('pulFile');
const elSaveBtn = document.getElementById('saveBtn');
const elTBody = document.querySelector('#tracksTable tbody');
const elSearch = document.getElementById('search');
const elBtnRR = document.getElementById('btnRR');
const elBtnCustom = document.getElementById('btnCustom');
const elTransFile = document.getElementById('transFile');
const colBtns = [
document.getElementById('col1All'),
document.getElementById('col2All'),
document.getElementById('col3All'),
document.getElementById('col4All'),
];
const clearBtn = document.getElementById('colClear');
// === UI wiring ===
elPulFile.addEventListener('change', onPulChosen);
elSaveBtn.addEventListener('click', onSave);
colBtns.forEach((btn, i) => btn.addEventListener('click', () => setAllInColumn(i, true)));
clearBtn.addEventListener('click', () => setAllInColumn(null, false));
elSearch.addEventListener('input', onSearch);
elBtnRR.addEventListener('click', loadRRTranslations);
elBtnCustom.addEventListener('click', () => elTransFile.click());
elTransFile.addEventListener('change', onCustomTransChosen);
// NOTE: No automatic translation loading occurs. Use the buttons above.
function parseTranslations(text) {
const map = {};
const lines = text.split(/\r?\n/);
for (const raw of lines) {
const line = raw.trim();
if (!line) continue;
const eq = line.indexOf('=');
if (eq === -1) continue;
const namePart = line.slice(0, eq);
const idPart = line.slice(eq + 1);
// Strip \c{...} sequences, control chars, and collapse whitespace
const name = namePart
.replace(/\\c\{.*?\}/g, '')
.replace(/[\u0000-\u001F\u007F]/g, ' ')
.replace(/\s+/g, ' ')
.trim();
const id = idPart.trim().toUpperCase();
if (id) map[id] = `${name} (${id})`;
}
return map;
}
function onPulChosen(evt) {
const f = evt.target.files && evt.target.files[0];
if (!f) return;
currentFileNameBase = (f.name || 'settings').replace(/\.[^.]+$/, '') || 'settings';
const reader = new FileReader();
reader.onload = () => {
bytes = new Uint8Array(reader.result);
headerIdx = findSignature(bytes, SIG_TROP);
if (headerIdx === -1) {
alert('TROP header not found in this file.');
return;
}
entries = parseEntries(bytes, headerIdx);
render(entries);
elSaveBtn.disabled = false;
colBtns.forEach(b => b.disabled = false);
clearBtn.disabled = false;
elSearch.disabled = false;
// If translations already loaded, apply them
if (Object.keys(translations).length) applyTranslations('current');
};
reader.readAsArrayBuffer(f);
}
function findSignature(arr, sig) {
outer: for (let i = 0; i <= arr.length - sig.length; i++) {
for (let j = 0; j < sig.length; j++) {
if (arr[i + j] !== sig[j]) continue outer;
}
return i;
}
return -1;
}
function parseEntries(arr, tropIdx) {
const out = [];
let pos = tropIdx + FIRST_BLOCK;
while (pos + 8 <= arr.length) {
// detect footer
let isFooter = true;
for (let k = 0; k < 8; k++) {
if (arr[pos + k] !== FOOTER[k]) { isFooter = false; break; }
}
if (isFooter) break;
const idBytes = arr.slice(pos, pos + 4);
const idHex = toHex(idBytes);
const statusBytes = arr.slice(pos + 4, pos + 8);
const flags = [...statusBytes].map(b => b !== 0);
const name = translations[idHex] || idHex;
out.push({ pos, idHex, flags, checks: [], name, row: null });
pos += 8;
}
return out;
}
function toHex(u8) {
let s = '';
for (let i = 0; i < u8.length; i++) s += u8[i].toString(16).padStart(2, '0');
return s.toUpperCase();
}
function render(list) {
elTBody.innerHTML = '';
let unrecognized = 0;
for (const ent of list) {
const tr = document.createElement('tr');
ent.row = tr;
const tdId = document.createElement('td');
tdId.textContent = ent.idHex;
tr.appendChild(tdId);
const tdName = document.createElement('td');
ent.name = translations[ent.idHex] || ent.idHex;
if (!translations[ent.idHex]) unrecognized++;
tdName.textContent = ent.name;
tdName.dataset.kind = 'nameCell';
tr.appendChild(tdName);
for (let i = 0; i < 4; i++) {
const td = document.createElement('td');
const cb = document.createElement('input');
cb.type = 'checkbox';
cb.checked = !!ent.flags[i];
cb.addEventListener('change', () => {
ent.flags[i] = cb.checked;
updateCountsPill();
});
ent.checks[i] = cb;
td.appendChild(cb);
tr.appendChild(td);
}
elTBody.appendChild(tr);
}
elStatus.textContent = `Loaded ${list.length} tracks. Unrecognized: ${unrecognized}.`;
updateCountsPill();
if (elSearch.value) onSearch();
}
function onSearch() {
const q = elSearch.value.trim().toLowerCase();
for (const ent of entries) {
if (!q) {
ent.row.style.display = '';
continue;
}
const hay = (ent.idHex + ' ' + (ent.name || '')).toLowerCase();
ent.row.style.display = hay.includes(q) ? '' : 'none';
}
}
function setAllInColumn(colIndex, value) {
for (const ent of entries) {
if (colIndex === null) {
for (let i = 0; i < 4; i++) {
ent.flags[i] = false;
if (ent.checks[i]) ent.checks[i].checked = false;
}
} else {
ent.flags[colIndex] = !!value;
if (ent.checks[colIndex]) ent.checks[colIndex].checked = !!value;
}
}
updateCountsPill();
}
function currentCounts() {
const counts = [0,0,0,0];
for (const ent of entries) for (let i=0;i<4;i++) if (ent.flags[i]) counts[i]++;
return counts;
}
function updateCountsPill() {
const c = currentCounts();
elCounts.textContent = `150cc ${c[0]} · 200cc ${c[1]} · 150cc F ${c[2]} · 200cc F ${c[3]}`;
elCounts.hidden = false;
}
function onSave() {
if (!bytes || headerIdx < 0 || !entries.length) return;
// Write per-track flags back (do not alter size)
for (const ent of entries) {
for (let i = 0; i < 4; i++) {
bytes[ent.pos + 4 + i] = ent.flags[i] ? 0x01 : 0x00;
}
}
// Recalculate and write trophy totals (2 bytes, big-endian)
const counts = currentCounts();
for (let i = 0; i < 4; i++) {
const off = headerIdx + COUNT_OFFS[i];
bytes[off] = (counts[i] >> 8) & 0xff;
bytes[off + 1] = counts[i] & 0xff;
}
// Emit new file with identical length
const blob = new Blob([bytes], { type: 'application/octet-stream' });
const a = document.createElement('a');
a.href = URL.createObjectURL(blob);
a.download = `${currentFileNameBase}_edited.pul`;
document.body.appendChild(a);
a.click();
setTimeout(() => { URL.revokeObjectURL(a.href); a.remove(); }, 0);
}
// === Translation loading helpers ===
function setTranslations(map, sourceLabel) {
translations = map || {};
const count = Object.keys(translations).length;
elStatus.textContent = count ? `Translations loaded (${count}) from ${sourceLabel}.` : 'No translations loaded.';
if (entries.length) applyTranslations(sourceLabel);
}
function applyTranslations(sourceLabel) {
let unrec = 0;
for (const ent of entries) {
ent.name = translations[ent.idHex] || ent.idHex;
if (!translations[ent.idHex]) unrec++;
const nameCell = ent.row.querySelector('[data-kind="nameCell"]');
if (nameCell) nameCell.textContent = ent.name;
}
elStatus.textContent = `${elStatus.textContent} Unrecognized: ${unrec}.`;
if (elSearch.value) onSearch();
// Warn the user if the loaded translations do not cover all track IDs present
if (entries.length && unrec > 0) {
alert(
`⚠️ Some tracks in this settings file were not recognized (missing in translations).\n\n` +
`This likely means your settings file is OUTDATED. To update it cleanly:\n` +
`1) In-game, change one of your settings to something else.\n` +
`2) Drive a quick Time Trials race.\n` +
`3) Re-export/reload the settings file here.\n\n` +
`Continuing without reloading/refreshing the file may reset your settings or corrupt the file entirely, which can cause the game to crash.`
);
}
}
async function loadRRTranslations() {
try {
const r = await fetch('FolderToTrackName.txt', { cache: 'no-store' });
if (!r.ok) throw new Error('HTTP ' + r.status);
const text = await r.text();
const map = parseTranslations(text);
setTranslations(map, 'server file');
} catch (e) {
alert('Could not load FolderToTrackName.txt from server.');
}
}
function onCustomTransChosen(evt) {
const f = evt.target.files && evt.target.files[0];
if (!f) return;
const reader = new FileReader();
reader.onload = () => {
const map = parseTranslations(reader.result || '');
setTranslations(map, 'custom file');
elTransFile.value = '';
};
reader.readAsText(f);
}
</script>
</body>
</html>