Fix RGBLINK object type detection

This commit is contained in:
Rangi
2025-11-18 22:01:43 -05:00
parent 3553c9c4da
commit a3d3e1525a

View File

@@ -420,19 +420,15 @@ void obj_ReadFile(std::string const &filePath, size_t fileID) {
} }
Defer closeFile{[&] { fclose(file); }}; Defer closeFile{[&] { fclose(file); }};
// First, check if the object is a RGBDS object or a SDCC one. If the first byte is 'R', // First, check if the object is a RGBDS object, a SDCC one, or neither.
// we'll assume it's a RGBDS object file, and otherwise, that it's a SDCC object file. // A single `ungetc` is guaranteed to work.
int c = getc(file); switch (ungetc(getc(file), file)) {
ungetc(c, file); // Guaranteed to work
switch (c) {
case EOF: case EOF:
fatal("File \"%s\" is empty!", fileName); fatal("File \"%s\" is empty!", fileName);
case 'R': case 'X':
break; case 'D':
case 'Q': {
default:
// This is (probably) a SDCC object file, defer the rest of detection to it. // This is (probably) a SDCC object file, defer the rest of detection to it.
// Since SDCC does not provide line info, everything will be reported as coming from the // Since SDCC does not provide line info, everything will be reported as coming from the
// object file. It's better than nothing. // object file. It's better than nothing.
@@ -450,11 +446,16 @@ void obj_ReadFile(std::string const &filePath, size_t fileID) {
return; return;
} }
// Begin by reading the magic bytes case 'R':
int matchedElems; // Check the magic byte signature for a RGB object file.
if (char magic[literal_strlen(RGBDS_OBJECT_VERSION_STRING)];
fread(magic, 1, sizeof(magic), file) == sizeof(magic)
&& !memcmp(magic, RGBDS_OBJECT_VERSION_STRING, sizeof(magic))) {
break;
}
[[fallthrough]];
if (fscanf(file, RGBDS_OBJECT_VERSION_STRING "%n", &matchedElems) == 1 default:
&& matchedElems != literal_strlen(RGBDS_OBJECT_VERSION_STRING)) {
fatal("%s: Not a RGBDS object file", fileName); fatal("%s: Not a RGBDS object file", fileName);
} }