-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.js
More file actions
603 lines (525 loc) · 29 KB
/
server.js
File metadata and controls
603 lines (525 loc) · 29 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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
const express = require('express');
const path = require('path');
const minecraftData = require('minecraft-data');
const semver = require('semver');
const app = express();
const port = 3000;
// --- Caching ---
// Simple cache to avoid reloading minecraft-data repeatedly for the same version
const mcDataCache = {};
function getMcData(version) {
if (mcDataCache[version]) {
return mcDataCache[version];
}
try {
console.log(`[Cache] Loading minecraft-data for version: ${version}`);
const data = minecraftData(version);
if (data) {
mcDataCache[version] = data; // Cache only if loading succeeds
console.log(`[Cache] Successfully loaded and cached data for ${version}`);
} else {
console.error(`[Cache] minecraftData(${version}) returned null/undefined.`);
}
return data;
} catch (err) {
console.error(`[Cache] Error loading minecraft-data for version ${version}:`, err);
// Don't cache errors, let it retry next time if needed
return null; // Indicate failure
}
}
// Serve static files from the 'public' directory
app.use(express.static(path.join(__dirname, 'public')));
// --- Helper Functions ---
// Fetches supported PC versions within the specified range.
// Uses minecraftData.supportedVersions directly as per api.md.
function getSupportedVersions(start = "1.0.0", end = "99.99.99") { // Default to a wide range if none provided
// Make all versions X.Y.Z SemVer compliant
let versionsSemVerCompliant = minecraftData.supportedVersions.pc.map(function (ver) {
// example: 1.7 -> 1.7.0
if (ver.split(".").length < 3 && ver.split(".").length > 1) ver = ver + ".0";
return ver;
});
// Remove 0.30c.0 and snapshots
versionsSemVerCompliant = versionsSemVerCompliant.filter(elem => elem !== "0.30c.0" && elem.includes(".") && !elem.includes("-"));
// Filter and sort versions semantically
const filteredVersions = versionsSemVerCompliant
.filter(v => {
try {
// Use coerce for robustness against minor version format variations
const sv = semver.coerce(start)?.version || start;
const ev = semver.coerce(end)?.version || end;
const vv = semver.coerce(v)?.version || v;
// Ensure valid versions before comparison
if (!sv || !ev || !vv) return false;
// Use valid() to double check coercion didn't fail weirdly
if (!semver.valid(sv) || !semver.valid(ev) || !semver.valid(vv)) return false;
return semver.gte(vv, sv) && semver.lte(vv, ev);
} catch (e) {
console.warn(`Could not compare version: ${v} against range ${start}-${end}`, e);
return false;
}
})
.sort(semver.compare); // Sort chronologically
// Return them to the "Minecraft" notation without the trailing .0 if needed
const finalVersions = filteredVersions.map(function(version) {
// Handle cases like '1.7' which became '1.7.0'
const originalMcDataVersion = minecraftData.supportedVersions.pc.find(ov => ov + ".0" === version);
if (originalMcDataVersion) return originalMcDataVersion;
// Handle cases like '1.16.2' which were already fine
if (minecraftData.supportedVersions.pc.includes(version)) return version;
// Fallback (should ideally not be needed if logic above is sound)
return version.endsWith(".0") ? version.slice(0, -2) : version;
});
return finalVersions;
}
// Compares JSON-compatible values reasonably well.
function deepCompare(val1, val2) {
// Strict equality check first
if (val1 === val2) return true;
// Check for null/undefined mismatches
if (val1 == null || val2 == null) return false;
// If types are different, they aren't equal
if (typeof val1 !== typeof val2) return false;
// Handle simple types quickly
if (typeof val1 !== 'object') {
return false; // Already failed strict equality
}
// Handle Arrays
if (Array.isArray(val1)) {
if (!Array.isArray(val2) || val1.length !== val2.length) return false;
for (let i = 0; i < val1.length; i++) {
if (!deepCompare(val1[i], val2[i])) return false;
}
return true;
}
// Handle Objects (plain objects)
const keys1 = Object.keys(val1);
const keys2 = Object.keys(val2);
if (keys1.length !== keys2.length) return false;
for (const key of keys1) {
// Check if key exists in both and values are deeply equal
if (!Object.prototype.hasOwnProperty.call(val2, key) || !deepCompare(val1[key], val2[key])) {
return false;
}
}
return true;
}
// Helper function to compare fields within container structures
function compareContainerFields(fields1, fields2) {
// Ensure both are arrays of fields before proceeding
if (!Array.isArray(fields1) || !Array.isArray(fields2)) {
return { changed: [], added: [], removed: [], isDifferent: !deepCompare(fields1, fields2) };
}
const fieldsMap1 = new Map();
const fieldsMap2 = new Map();
const anonFields1 = [];
const anonFields2 = [];
// Populate maps/arrays for easier lookup
fields1.forEach((field, index) => {
if (field && field.name && !field.anon) { // Check field exists
fieldsMap1.set(field.name, field);
} else if (field) { // Handle anonymous/unnamed fields
anonFields1.push({ index, field });
}
});
fields2.forEach((field, index) => {
if (field && field.name && !field.anon) { // Check field exists
fieldsMap2.set(field.name, field);
} else if (field) { // Handle anonymous/unnamed fields
anonFields2.push({ index, field });
}
});
const changed = [];
// Fields in 2 but not in 1 are added
const added = [...fieldsMap2.keys()].filter(name => !fieldsMap1.has(name));
// Fields in 1 but not in 2 are removed
const removed = [...fieldsMap1.keys()].filter(name => !fieldsMap2.has(name));
// Check fields present in both for changes
for (const [name, field1] of fieldsMap1) {
if (fieldsMap2.has(name)) {
const field2 = fieldsMap2.get(name);
// Compare the 'type' definition using deepCompare for robustness
if (!deepCompare(field1.type, field2.type)) {
changed.push(name);
}
// Optionally add more checks: e.g., change in 'anon' status for a named field?
// if (!!field1.anon !== !!field2.anon) { /* mark as changed? */ }
}
}
// Basic comparison for anonymous fields by index (less reliable but best effort)
let anonChanged = false;
const maxAnonLength = Math.max(anonFields1.length, anonFields2.length);
for (let i = 0; i < maxAnonLength; i++) {
const anon1 = anonFields1[i];
const anon2 = anonFields2[i];
const anonName = `(Anonymous Index ${i})`; // Consistent identifier
if (anon1 && anon2) { // Both exist at this index
if (!deepCompare(anon1.field.type, anon2.field.type)) {
changed.push(anonName); // Mark anon changes by index
anonChanged = true;
}
} else {
// Different number of anonymous fields implies change
anonChanged = true;
if (!anon1 && anon2) added.push(anonName); // Exists in 2, not 1 -> added
if (anon1 && !anon2) removed.push(anonName); // Exists in 1, not 2 -> removed
}
}
// Overall difference detected?
const isDifferent = changed.length > 0 || added.length > 0 || removed.length > 0;
return { changed, added, removed, isDifferent };
}
/**
* Retrieves the ID (hex string) and structure definition for a specific packet.
*/
function getPacketInfo(mcData, state, direction, packetName) {
let packetId = null;
let structure = null;
let error = null;
try {
// 1. Validate mcData
if (!mcData || typeof mcData !== 'object') {
return { id: null, structure: null, error: "Invalid mcData object provided." };
}
// Use mcData.protocol structure (common in minecraft-data >= 1.8)
// Need to adjust path depending on mcData version/structure potentially
const protocolData = mcData.protocol || mcData; // Fallback for older mcData structures if needed
// 2. Validate state and direction existence
if (!protocolData[state]) {
return { id: null, structure: null, error: `State '${state}' not found in protocol data.` };
}
if (!protocolData[state][direction]) {
return { id: null, structure: null, error: `Direction '${direction}' not found for state '${state}'.` };
}
const directionData = protocolData[state][direction];
if (!directionData.types || !directionData.types.packet) {
// Some states might genuinely have no packets in a direction (e.g., handshake toClient)
// Check if the mapping itself is missing or just empty
if (directionData.types && !directionData.types.packet) {
return { id: null, structure: null, error: null }; // No packets defined, not an error per se
}
return { id: null, structure: null, error: `Packet type definitions missing for ${state}.${direction}` };
}
// 3. Find the main packet definition (which contains the mappings)
const packetDefinitionContainer = directionData.types.packet;
// Check for the typical ["container", [fields...]] structure
if (!Array.isArray(packetDefinitionContainer) || packetDefinitionContainer.length < 2 || packetDefinitionContainer[0] !== 'container' || !Array.isArray(packetDefinitionContainer[1])) {
return { id: null, structure: null, error: `Unexpected packet definition structure for ${state}.${direction}` };
}
const fields = packetDefinitionContainer[1];
// 4. Find the ID mapping
const nameFieldDefinition = fields.find(field => field.name === 'name');
if (!nameFieldDefinition || !Array.isArray(nameFieldDefinition.type) || nameFieldDefinition.type.length < 2 || nameFieldDefinition.type[0] !== 'mapper' || !nameFieldDefinition.type[1].mappings) {
// Attempt fallback: sometimes the structure is simpler like ['mapper', { mappings }] directly? (Less common now)
if (Array.isArray(packetDefinitionContainer) && packetDefinitionContainer[0] === 'mapper' && packetDefinitionContainer[1]?.mappings) {
// Handle simpler direct mapper case if necessary (though unlikely for top 'packet')
console.warn(`[getPacketInfo] Using direct mapper fallback for ${state}.${direction}`);
const idMappingsFallback = packetDefinitionContainer[1].mappings;
for (const id in idMappingsFallback) {
if (idMappingsFallback[id] === packetName) {
packetId = id;
break;
}
}
// If found, structure is likely just the packet ID itself (no params)
if (packetId) return { id: packetId, structure: 'void', error: null };
}
return { id: null, structure: null, error: `Could not find packet ID mappings ('name' field mapper) for ${state}.${direction}` };
}
const idMappings = nameFieldDefinition.type[1].mappings;
// 5. Find the packet ID by looking up the name
for (const id in idMappings) {
if (idMappings[id] === packetName) {
packetId = id;
break;
}
}
if (packetId === null) {
// Packet name exists in mapping? Check case sensitivity? No, just not found.
return { id: null, structure: null, error: `Packet name '${packetName}' not found in ID mappings for state '${state}', direction '${direction}'` };
}
// 6. Find the structure definition
const paramsFieldDefinition = fields.find(field => field.name === 'params');
if (!paramsFieldDefinition || !Array.isArray(paramsFieldDefinition.type) || paramsFieldDefinition.type.length < 2 || paramsFieldDefinition.type[0] !== 'switch' || !paramsFieldDefinition.type[1].fields) {
// It's possible a packet exists but has no parameters (e.g., bundle_delimiter)
// Check if the main container ONLY has the 'name' field
if (fields.length === 1 && fields[0]?.name === 'name') {
return { id: packetId, structure: 'void', error: null };
}
return { id: packetId, structure: null, error: `Could not find packet parameter switch ('params' field) for ${state}.${direction}` };
}
const paramMappings = paramsFieldDefinition.type[1].fields;
// Get the definition for this specific packet (can be type name string or inline definition)
const structureDefinition = paramMappings[packetName];
if (structureDefinition === undefined) {
// Packet is in ID map, but not in params switch? Should be unlikely if mcData is consistent.
return { id: packetId, structure: null, error: `Packet '${packetName}' found in ID map but not in parameter switch for ${state}.${direction}` };
} else if (structureDefinition === 'void') {
structure = 'void'; // Explicitly handle void
} else if (typeof structureDefinition === 'string') {
// It's a type name, look it up
const structureTypeName = structureDefinition;
structure = directionData.types[structureTypeName]; // Check local types first
if (structure === undefined) {
structure = mcData.types[structureTypeName]; // Fallback to global types
if (structure === undefined) {
return { id: packetId, structure: null, error: `Structure definition for type '${structureTypeName}' (packet '${packetName}') not found in ${state}.${direction}.types or global types.` };
}
}
} else {
// It's an inline definition (object or array)
structure = structureDefinition;
}
} catch (e) {
console.error(`Error in getPacketInfo (${state}/${direction}/${packetName}):`, e);
error = `An unexpected error occurred while getting packet info: ${e.message}`;
}
return { id: packetId, structure: structure, error: error };
}
// --- API Endpoints ---
app.get('/api/versions', (req, res) => {
try {
// Provide a default wide range to getSupportedVersions if needed
const versions = getSupportedVersions(); // Uses defaults inside
res.json(versions);
} catch (err) {
console.error("Error fetching supported versions:", err);
res.status(500).json({ error: 'Failed to load supported versions list' });
}
});
app.get('/api/packets', (req, res) => {
const { version, state, direction } = req.query;
console.log(`[/api/packets] Request received for: version=${version}, state=${state}, direction=${direction}`);
if (!version || !state || !direction) {
console.error('[/api/packets] Missing query parameters');
return res.status(400).json({ error: 'Missing query parameters: version, state, direction' });
}
try {
const mcData = getMcData(version); // Use cache/loader
if (!mcData) {
console.error(`[/api/packets] Failed to load mcData for version ${version} from cache/loader.`);
return res.status(404).json({ error: `Could not load data for Minecraft version ${version}. It might be unsupported or an error occurred.` });
}
const protocolData = mcData.protocol || mcData; // Adjust for potential structure differences
// **Robust checks for navigating the protocol structure**
if (!protocolData[state]) {
console.warn(`[/api/packets] State '${state}' not found for version ${version}`);
return res.json([]); // Return empty list if state doesn't exist
}
if (!protocolData[state][direction]) {
console.warn(`[/api/packets] Direction '${direction}' not found for state '${state}' in version ${version}`);
return res.json([]); // Return empty list
}
const directionData = protocolData[state][direction];
if (!directionData.types || !directionData.types.packet) {
console.warn(`[/api/packets] Packet type definitions missing for ${version}/${state}/${direction}`);
return res.json([]); // Return empty list
}
console.log(`[/api/packets] Protocol branch found for ${version}/${state}/${direction}`);
const mainPacketType = directionData.types.packet;
let packetNames = [];
let mapperFound = false;
// Check for ["container", [fields...]] structure
if (Array.isArray(mainPacketType) && mainPacketType.length === 2 && mainPacketType[0] === 'container' && Array.isArray(mainPacketType[1])) {
console.log("[/api/packets] Found main 'packet' type definition with expected container structure.");
const fieldDefinitions = mainPacketType[1];
// Try finding the 'name' mapper
const nameFieldDefinition = fieldDefinitions.find(field => field.name === 'name');
const nameMapper = nameFieldDefinition?.type?.[0] === 'mapper' ? nameFieldDefinition.type[1] : null;
if (nameMapper?.mappings) {
console.log("[/api/packets] Found mapper in 'name' field.");
packetNames = Object.values(nameMapper.mappings);
mapperFound = true;
} else {
console.log("[/api/packets] 'name' field mapper not found or invalid. Checking 'params' switch fallback.");
// Fallback: Use keys from the 'params' switch
const paramsFieldDefinition = fieldDefinitions.find(field => field.name === 'params');
const paramsSwitch = paramsFieldDefinition?.type?.[0] === 'switch' ? paramsFieldDefinition.type[1] : null;
if (paramsSwitch?.fields) {
console.log("[/api/packets] Fallback SUCCEEDED: Using keys from 'params' switch.");
packetNames = Object.keys(paramsSwitch.fields);
mapperFound = true;
} else {
console.log("[/api/packets] Fallback FAILED: 'params' switch not found or invalid.");
}
}
}
// Add fallback for direct mapper if container check fails (less common)
else if (Array.isArray(mainPacketType) && mainPacketType[0] === 'mapper' && mainPacketType[1]?.mappings) {
console.warn(`[/api/packets] Using direct mapper structure fallback for ${version}/${state}/${direction}`);
packetNames = Object.values(mainPacketType[1].mappings);
mapperFound = true;
}
else {
console.warn(`[/api/packets] Main 'packet' type definition for ${version}/${state}/${direction} does not match expected structures.`);
console.warn("[/api/packets] Actual structure:", JSON.stringify(mainPacketType));
}
if (!mapperFound && packetNames.length === 0) { // Ensure we don't log warning if fallback worked
console.warn(`[/api/packets] Could not extract packet names for ${version}/${state}/${direction}`);
} else {
console.log(`[/api/packets] Extracted ${packetNames.length} packet names.`);
}
res.json(packetNames.sort());
} catch (err) {
console.error(`[/api/packets] Error processing ${version}/${state}/${direction}:`, err);
// Check if the error is specifically about an unsupported version
if (err.message && err.message.toLowerCase().includes('unsupported minecraft version')) {
res.status(404).json({ error: `Minecraft version ${version} is not supported by the minecraft-data library.` });
} else {
res.status(500).json({ error: `Failed to load or process packet list for version ${version}: ${err.message || err}` });
}
}
});
app.get('/api/compare', (req, res) => {
const { startVersion, endVersion, state, direction, packetName } = req.query;
console.log(`[/api/compare] Request: ${startVersion} -> ${endVersion}, ${state}/${direction}, packet: ${packetName}`);
// --- Parameter Validation ---
if (!startVersion || !endVersion || !state || !direction || !packetName) {
console.error('[/api/compare] Missing query parameters');
return res.status(400).json({ error: 'Missing query parameters: startVersion, endVersion, state, direction, packetName' });
}
// --- Version Sanity Check ---
try {
const svCoerced = semver.coerce(startVersion);
const evCoerced = semver.coerce(endVersion);
if (!svCoerced || !evCoerced || !semver.valid(svCoerced) || !semver.valid(evCoerced)) {
console.error(`[/api/compare] Invalid version format: start=${startVersion}, end=${endVersion}`);
return res.status(400).json({ error: 'Invalid version format provided.' });
}
if (semver.compare(svCoerced.version, evCoerced.version) > 0) {
console.error(`[/api/compare] Start version ${startVersion} > end version ${endVersion}`);
return res.status(400).json({ error: 'Start version cannot be greater than end version.' });
}
} catch (e) {
console.error('[/api/compare] Error during version coercion/comparison:', e);
return res.status(400).json({ error: 'Error processing version format.' });
}
// --- Main Comparison Logic ---
try {
const versionsToCompare = getSupportedVersions(startVersion, endVersion);
console.log(`[/api/compare] Versions to compare after filtering: [${versionsToCompare.join(', ')}]`);
if (versionsToCompare.length === 0) {
console.log('[/api/compare] No supported versions found in the range.');
return res.json({ results: [], message: 'No supported versions found in the specified range.' });
}
let preliminaryGroups = [];
let currentGroup = null;
// --- Step 1: Grouping based on ID and Stringified Structure ---
for (const version of versionsToCompare) {
let mcDataForVersion = null;
let info = { id: null, structure: null, error: null };
let loadError = false;
try {
mcDataForVersion = getMcData(version);
if (!mcDataForVersion) {
console.warn(`[/api/compare] Failed to load mcData for ${version}, skipping.`);
info.error = `Failed to load data for version ${version}.`;
loadError = true;
} else {
info = getPacketInfo(mcDataForVersion, state, direction, packetName);
}
} catch (loadErr) {
console.error(`[/api/compare] Error loading data or getting packet info for ${version}:`, loadErr);
info.error = `Error processing version ${version}: ${loadErr.message}`;
loadError = true;
}
const currentIdStr = info.id !== null ? String(info.id) : 'undefined';
const currentStructure = info.structure; // Keep the object
const currentStructureStr = currentStructure !== null ? JSON.stringify(currentStructure) : 'undefined';
const packetMaybeExists = info.id !== null || currentStructure !== null || loadError; // Consider packet existing if data load failed
if (!currentGroup) {
if (packetMaybeExists && !loadError) {
currentGroup = {
startVersion: version, endVersion: version, id: info.id, structure: currentStructure,
idStr: currentIdStr, structureStr: currentStructureStr, error: info.error
};
}
} else {
const idMatches = currentGroup.idStr === currentIdStr;
const structureMatchesStr = currentGroup.structureStr === currentStructureStr;
const matchesGroup = idMatches && structureMatchesStr;
if (packetMaybeExists && matchesGroup && !loadError) {
currentGroup.endVersion = version;
if (!currentGroup.error && info.error) { currentGroup.error = info.error; }
} else {
preliminaryGroups.push({ ...currentGroup }); // Store completed group
if (packetMaybeExists && !loadError) {
currentGroup = { // Start new group
startVersion: version, endVersion: version, id: info.id, structure: currentStructure,
idStr: currentIdStr, structureStr: currentStructureStr, error: info.error
};
} else {
currentGroup = null; // Reset if packet not found/load error
}
}
}
}
if (currentGroup) {
preliminaryGroups.push({ ...currentGroup }); // Add the last group
}
// --- Step 2: Post-processing - Add Protocol Versions and Detailed Diffs ---
const finalResults = [];
for (let i = 0; i < preliminaryGroups.length; i++) {
const group = preliminaryGroups[i];
const previousGroup = i > 0 ? preliminaryGroups[i-1] : null;
// Add Protocol Versions
let startPVer = null, endPVer = null;
try {
const startMcData = getMcData(group.startVersion);
startPVer = startMcData?.version?.version ?? null;
if (group.startVersion === group.endVersion) { endPVer = startPVer; }
else { const endMcData = getMcData(group.endVersion); endPVer = endMcData?.version?.version ?? null; }
} catch (err) { console.error(`Error getting protocol version for group ${group.startVersion}-${group.endVersion}:`, err); }
// Calculate Diffs
const diff = {};
if (previousGroup) {
if (group.idStr !== previousGroup.idStr) diff.idChanged = true;
if (group.structureStr !== previousGroup.structureStr) {
diff.structureChanged = true;
// Perform detailed field comparison if applicable
if (Array.isArray(group.structure) && group.structure[0] === 'container' &&
Array.isArray(previousGroup.structure) && previousGroup.structure[0] === 'container')
{
const fieldComparison = compareContainerFields(previousGroup.structure[1], group.structure[1]);
if (fieldComparison.isDifferent) {
diff.fieldDiffs = {
changed: fieldComparison.changed,
added: fieldComparison.added,
removed: fieldComparison.removed // Keep removed for potential future use
};
}
} // else: structure changed but not container-to-container
}
// Compare error presence/content
const errorChanged = !!group.error !== !!previousGroup.error || (group.error && previousGroup.error && group.error !== previousGroup.error);
if (errorChanged) diff.errorChanged = true;
}
finalResults.push({
...group, // Spread original group data (id, structure object, start/end version etc.)
startProtocolVersion: startPVer,
endProtocolVersion: endPVer,
diff: Object.keys(diff).length > 0 ? diff : null // Add diff object only if there were changes
});
}
// --- End Post-processing ---
// Handle case where no packet was found
if (finalResults.length === 0) {
console.log(`[/api/compare] Packet "${packetName}" not found in any version in the range.`);
return res.json({ results: [], message: `Packet "${packetName}" was not found or data failed to load in any supported version within the range ${startVersion} - ${endVersion} for ${state}/${direction}.` });
}
console.log(`[/api/compare] Comparison finished. Found ${finalResults.length} groups.`);
res.json({ results: finalResults });
} catch (err) {
console.error('[/api/compare] Uncaught Comparison Error:', err);
res.status(500).json({ error: `An internal error occurred during comparison: ${err.message}` });
}
});
// --- Serve the main HTML page ---
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'index.html'));
});
app.listen(port, () => {
console.log(`Minecraft Protocol Diff Tool listening at http://localhost:${port}`);
// Pre-cache latest version on startup? Optional.
// getMcData(minecraftData.supportedVersions.pc[minecraftData.supportedVersions.pc.length - 1]);
});