diff --git a/src/console_cmds.cpp b/src/console_cmds.cpp index 296bbd019b..cae08abe2e 100644 --- a/src/console_cmds.cpp +++ b/src/console_cmds.cpp @@ -2459,7 +2459,7 @@ DEF_CONSOLE_CMD(ConNewGRFProfile) bool active = selected && profiler->active; TextColour tc = active ? TC_LIGHT_BLUE : selected ? TC_GREEN : CC_INFO; const char *statustext = active ? " (active)" : selected ? " (selected)" : ""; - IConsolePrint(tc, "{}: [{:08X}] {}{}", i, BSWAP32(grf->grfid), grf->filename, statustext); + IConsolePrint(tc, "{}: [{:08X}] {}{}", i, std::byteswap(grf->grfid), grf->filename, statustext); i++; } return true; @@ -2475,7 +2475,7 @@ DEF_CONSOLE_CMD(ConNewGRFProfile) } GRFFile *grf = files[grfnum - 1]; if (std::any_of(_newgrf_profilers.begin(), _newgrf_profilers.end(), [&](NewGRFProfiler &pr) { return pr.grffile == grf; })) { - IConsolePrint(CC_WARNING, "GRF number {} [{:08X}] is already selected for profiling.", grfnum, BSWAP32(grf->grfid)); + IConsolePrint(CC_WARNING, "GRF number {} [{:08X}] is already selected for profiling.", grfnum, std::byteswap(grf->grfid)); continue; } _newgrf_profilers.emplace_back(grf); @@ -2511,7 +2511,7 @@ DEF_CONSOLE_CMD(ConNewGRFProfile) started++; if (!grfids.empty()) grfids += ", "; - fmt::format_to(std::back_inserter(grfids), "[{:08X}]", BSWAP32(pr.grffile->grfid)); + fmt::format_to(std::back_inserter(grfids), "[{:08X}]", std::byteswap(pr.grffile->grfid)); } } if (started > 0) { @@ -2601,7 +2601,7 @@ static std::string FormatLabel(uint32_t label) return fmt::format("{:c}{:c}{:c}{:c}", GB(label, 24, 8), GB(label, 16, 8), GB(label, 8, 8), GB(label, 0, 8)); } - return fmt::format("{:08X}", BSWAP32(label)); + return fmt::format("{:08X}", std::byteswap(label)); } static void ConDumpRoadTypes() @@ -2632,12 +2632,12 @@ static void ConDumpRoadTypes() HasBit(rti->flags, ROTF_NO_HOUSES) ? 'X' : '-', HasBit(rti->flags, ROTF_HIDDEN) ? 'h' : '-', HasBit(rti->flags, ROTF_TOWN_BUILD) ? 'T' : '-', - BSWAP32(grfid), + std::byteswap(grfid), GetStringPtr(rti->strings.name) ); } for (const auto &grf : grfs) { - IConsolePrint(CC_DEFAULT, " GRF: {:08X} = {}", BSWAP32(grf.first), grf.second->filename); + IConsolePrint(CC_DEFAULT, " GRF: {:08X} = {}", std::byteswap(grf.first), grf.second->filename); } } @@ -2670,12 +2670,12 @@ static void ConDumpRailTypes() HasBit(rti->flags, RTF_NO_SPRITE_COMBINE) ? 's' : '-', HasBit(rti->flags, RTF_ALLOW_90DEG) ? 'a' : '-', HasBit(rti->flags, RTF_DISALLOW_90DEG) ? 'd' : '-', - BSWAP32(grfid), + std::byteswap(grfid), GetStringPtr(rti->strings.name) ); } for (const auto &grf : grfs) { - IConsolePrint(CC_DEFAULT, " GRF: {:08X} = {}", BSWAP32(grf.first), grf.second->filename); + IConsolePrint(CC_DEFAULT, " GRF: {:08X} = {}", std::byteswap(grf.first), grf.second->filename); } } @@ -2729,12 +2729,12 @@ static void ConDumpCargoTypes() (spec->classes & CC_POTABLE) != 0 ? 'e' : '-', (spec->classes & CC_NON_POTABLE) != 0 ? 'i' : '-', (spec->classes & CC_SPECIAL) != 0 ? 'S' : '-', - BSWAP32(grfid), + std::byteswap(grfid), GetStringPtr(spec->name) ); } for (const auto &grf : grfs) { - IConsolePrint(CC_DEFAULT, " GRF: {:08X} = {}", BSWAP32(grf.first), grf.second->filename); + IConsolePrint(CC_DEFAULT, " GRF: {:08X} = {}", std::byteswap(grf.first), grf.second->filename); } } diff --git a/src/core/bitmath_func.hpp b/src/core/bitmath_func.hpp index 25dffa6c33..befcb1d45c 100644 --- a/src/core/bitmath_func.hpp +++ b/src/core/bitmath_func.hpp @@ -360,7 +360,4 @@ namespace std { } } -constexpr uint32_t BSWAP32(uint32_t x) { return std::byteswap(x); } -constexpr uint16_t BSWAP16(uint16_t x) { return std::byteswap(x); } - #endif /* BITMATH_FUNC_HPP */ diff --git a/src/core/endian_func.hpp b/src/core/endian_func.hpp index b6cdebda16..780fab85da 100644 --- a/src/core/endian_func.hpp +++ b/src/core/endian_func.hpp @@ -20,17 +20,17 @@ # define TO_BE16(x) (x) # define TO_BE32(x) (x) # define TO_BE32X(x) (x) -# define FROM_LE16(x) BSWAP16(x) -# define FROM_LE32(x) BSWAP32(x) -# define TO_LE16(x) BSWAP16(x) -# define TO_LE32(x) BSWAP32(x) -# define TO_LE32X(x) BSWAP32(x) +# define FROM_LE16(x) std::byteswap(x) +# define FROM_LE32(x) std::byteswap(x) +# define TO_LE16(x) std::byteswap(x) +# define TO_LE32(x) std::byteswap(x) +# define TO_LE32X(x) std::byteswap(x) #else -# define FROM_BE16(x) BSWAP16(x) -# define FROM_BE32(x) BSWAP32(x) -# define TO_BE16(x) BSWAP16(x) -# define TO_BE32(x) BSWAP32(x) -# define TO_BE32X(x) BSWAP32(x) +# define FROM_BE16(x) std::byteswap(x) +# define FROM_BE32(x) std::byteswap(x) +# define TO_BE16(x) std::byteswap(x) +# define TO_BE32(x) std::byteswap(x) +# define TO_BE32X(x) std::byteswap(x) # define FROM_LE16(x) (x) # define FROM_LE32(x) (x) # define TO_LE16(x) (x) diff --git a/src/gamelog.cpp b/src/gamelog.cpp index a6c630b85e..c73028b9aa 100644 --- a/src/gamelog.cpp +++ b/src/gamelog.cpp @@ -109,9 +109,9 @@ void Gamelog::Reset() static void AddGrfInfo(std::back_insert_iterator &output_iterator, uint32_t grfid, const MD5Hash *md5sum, const GRFConfig *gc) { if (md5sum != nullptr) { - fmt::format_to(output_iterator, "GRF ID {:08X}, checksum {}", BSWAP32(grfid), FormatArrayAsHex(*md5sum)); + fmt::format_to(output_iterator, "GRF ID {:08X}, checksum {}", std::byteswap(grfid), FormatArrayAsHex(*md5sum)); } else { - fmt::format_to(output_iterator, "GRF ID {:08X}", BSWAP32(grfid)); + fmt::format_to(output_iterator, "GRF ID {:08X}", std::byteswap(grfid)); } if (gc != nullptr) { @@ -284,7 +284,7 @@ void Gamelog::Print(std::function proc) /* The order of NewGRFs got changed, which might cause some other NewGRFs to behave differently. */ auto gm = grf_names.find(this->grfid); fmt::format_to(output_iterator, "GRF order changed: {:08X} moved {} places {}", - BSWAP32(this->grfid), abs(this->offset), this->offset >= 0 ? "down" : "up" ); + std::byteswap(this->grfid), abs(this->offset), this->offset >= 0 ? "down" : "up" ); AddGrfInfo(output_iterator, this->grfid, nullptr, gm != grf_names.end() ? gm->second.gc : nullptr); if (gm == grf_names.end()) fmt::format_to(output_iterator, ". Gamelog inconsistency: GrfID was never added!"); } @@ -295,7 +295,7 @@ void Gamelog::Print(std::function proc) auto gm = grf_names.find(this->grfid); assert(this->bug == GBUG_VEH_LENGTH); - fmt::format_to(output_iterator, "Rail vehicle changes length outside a depot: GRF ID {:08X}, internal ID 0x{:X}", BSWAP32(this->grfid), this->data); + fmt::format_to(output_iterator, "Rail vehicle changes length outside a depot: GRF ID {:08X}, internal ID 0x{:X}", std::byteswap(this->grfid), this->data); AddGrfInfo(output_iterator, this->grfid, nullptr, gm != grf_names.end() ? gm->second.gc : nullptr); if (gm == grf_names.end()) fmt::format_to(output_iterator, ". Gamelog inconsistency: GrfID was never added!"); } diff --git a/src/network/core/tcp_content.cpp b/src/network/core/tcp_content.cpp index 4c22c8e47c..2c8b48f695 100644 --- a/src/network/core/tcp_content.cpp +++ b/src/network/core/tcp_content.cpp @@ -70,7 +70,7 @@ std::optional ContentInfo::GetTextfile(TextfileType type) const tmp = Game::GetScannerLibrary()->FindMainScript(this, true); break; case CONTENT_TYPE_NEWGRF: { - const GRFConfig *gc = FindGRFConfig(BSWAP32(this->unique_id), FGCM_EXACT, &this->md5sum); + const GRFConfig *gc = FindGRFConfig(std::byteswap(this->unique_id), FGCM_EXACT, &this->md5sum); tmp = gc != nullptr ? gc->filename.c_str() : nullptr; break; } diff --git a/src/network/network_client.cpp b/src/network/network_client.cpp index edebe47615..8686ddfa00 100644 --- a/src/network/network_client.cpp +++ b/src/network/network_client.cpp @@ -654,7 +654,7 @@ NetworkRecvStatus ClientNetworkGameSocketHandler::Receive_SERVER_CHECK_NEWGRFS(P const GRFConfig *f = FindGRFConfig(c.grfid, FGCM_EXACT, &c.md5sum); if (f == nullptr) { /* We do not know this GRF, bail out of initialization */ - Debug(grf, 0, "NewGRF {:08X} not found; checksum {}", BSWAP32(c.grfid), FormatArrayAsHex(c.md5sum)); + Debug(grf, 0, "NewGRF {:08X} not found; checksum {}", std::byteswap(c.grfid), FormatArrayAsHex(c.md5sum)); ret = NETWORK_RECV_STATUS_NEWGRF_MISMATCH; } } diff --git a/src/network/network_content.cpp b/src/network/network_content.cpp index 560824e8b2..1191ef22a3 100644 --- a/src/network/network_content.cpp +++ b/src/network/network_content.cpp @@ -38,7 +38,7 @@ ClientNetworkContentSocketHandler _network_content_client; /** Wrapper function for the HasProc */ static bool HasGRFConfig(const ContentInfo *ci, bool md5sum) { - return FindGRFConfig(BSWAP32(ci->unique_id), md5sum ? FGCM_EXACT : FGCM_ANY, md5sum ? &ci->md5sum : nullptr) != nullptr; + return FindGRFConfig(std::byteswap(ci->unique_id), md5sum ? FGCM_EXACT : FGCM_ANY, md5sum ? &ci->md5sum : nullptr) != nullptr; } /** diff --git a/src/newgrf.cpp b/src/newgrf.cpp index 293a4df4e8..c62b304cd2 100644 --- a/src/newgrf.cpp +++ b/src/newgrf.cpp @@ -595,10 +595,10 @@ static void SetNewGRFOverride(uint32_t source_grfid, uint32_t target_grfid) { if (target_grfid == 0) { _grf_id_overrides.erase(source_grfid); - GrfMsg(5, "SetNewGRFOverride: Removed override of 0x{:X}", BSWAP32(source_grfid)); + GrfMsg(5, "SetNewGRFOverride: Removed override of 0x{:X}", std::byteswap(source_grfid)); } else { _grf_id_overrides[source_grfid] = target_grfid; - GrfMsg(5, "SetNewGRFOverride: Added override of 0x{:X} to 0x{:X}", BSWAP32(source_grfid), BSWAP32(target_grfid)); + GrfMsg(5, "SetNewGRFOverride: Added override of 0x{:X} to 0x{:X}", std::byteswap(source_grfid), std::byteswap(target_grfid)); } } @@ -636,9 +636,9 @@ static Engine *GetNewEngine(const GRFFile *file, VehicleType type, uint16_t inte scope_grfid = it->second; const GRFFile *grf_match = GetFileByGRFID(scope_grfid); if (grf_match == nullptr) { - GrfMsg(5, "Tried mapping from GRFID {:x} to {:x} but target is not loaded", BSWAP32(file->grfid), BSWAP32(scope_grfid)); + GrfMsg(5, "Tried mapping from GRFID {:x} to {:x} but target is not loaded", std::byteswap(file->grfid), std::byteswap(scope_grfid)); } else { - GrfMsg(5, "Mapping from GRFID {:x} to {:x}", BSWAP32(file->grfid), BSWAP32(scope_grfid)); + GrfMsg(5, "Mapping from GRFID {:x} to {:x}", std::byteswap(file->grfid), std::byteswap(scope_grfid)); } } @@ -662,7 +662,7 @@ static Engine *GetNewEngine(const GRFFile *file, VehicleType type, uint16_t inte if (!e->grf_prop.HasGrfFile()) { e->grf_prop.grfid = file->grfid; e->grf_prop.grffile = file; - GrfMsg(5, "Replaced engine at index {} for GRFID {:x}, type {}, index {}", e->index, BSWAP32(file->grfid), type, internal_id); + GrfMsg(5, "Replaced engine at index {} for GRFID {:x}, type {}, index {}", e->index, std::byteswap(file->grfid), type, internal_id); } return e; @@ -693,7 +693,7 @@ static Engine *GetNewEngine(const GRFFile *file, VehicleType type, uint16_t inte _gted[e->index].railtypelabel = GetRailTypeInfo(e->u.rail.railtype)->label; } - GrfMsg(5, "Created new engine at index {} for GRFID {:x}, type {}, index {}", e->index, BSWAP32(file->grfid), type, internal_id); + GrfMsg(5, "Created new engine at index {} for GRFID {:x}, type {}, index {}", e->index, std::byteswap(file->grfid), type, internal_id); return e; } @@ -1960,7 +1960,7 @@ static ChangeInfoResult StationChangeInfo(uint first, uint last, int prop, ByteR /* Swap classid because we read it in BE meaning WAYP or DFLT */ uint32_t classid = buf.ReadDWord(); - statspec->class_index = StationClass::Allocate(BSWAP32(classid)); + statspec->class_index = StationClass::Allocate(std::byteswap(classid)); break; } @@ -2716,13 +2716,13 @@ static ChangeInfoResult LoadTranslationTable(uint first, uint last, ByteReader & translation_table.clear(); translation_table.reserve(last); for (uint id = first; id < last; ++id) { - translation_table.push_back(T(BSWAP32(buf.ReadDWord()))); + translation_table.push_back(T(std::byteswap(buf.ReadDWord()))); } GRFFile *grf_override = GetCurrentGRFOverride(); if (grf_override != nullptr) { /* GRF override is present, copy the translation table to the overridden GRF as well. */ - GrfMsg(1, "LoadTranslationTable: Copying {} translation table to override GRFID '{}'", name, BSWAP32(grf_override->grfid)); + GrfMsg(1, "LoadTranslationTable: Copying {} translation table to override GRFID '{}'", name, std::byteswap(grf_override->grfid)); std::vector &override_table = gettable(*grf_override); override_table = translation_table; } @@ -3141,7 +3141,7 @@ static ChangeInfoResult CargoChangeInfo(uint first, uint last, int prop, ByteRea break; case 0x17: // Cargo label - cs->label = CargoLabel{BSWAP32(buf.ReadDWord())}; + cs->label = CargoLabel{std::byteswap(buf.ReadDWord())}; BuildCargoLabelMap(); break; @@ -4170,7 +4170,7 @@ static ChangeInfoResult ObjectChangeInfo(uint first, uint last, int prop, ByteRe /* Swap classid because we read it in BE. */ uint32_t classid = buf.ReadDWord(); - spec->class_index = ObjectClass::Allocate(BSWAP32(classid)); + spec->class_index = ObjectClass::Allocate(std::byteswap(classid)); break; } @@ -4326,7 +4326,7 @@ static ChangeInfoResult RailTypeChangeInfo(uint first, uint last, int prop, Byte int n = buf.ReadByte(); for (int j = 0; j != n; j++) { RailTypeLabel label = buf.ReadDWord(); - RailType resolved_rt = GetRailTypeByLabel(BSWAP32(label), false); + RailType resolved_rt = GetRailTypeByLabel(std::byteswap(label), false); if (resolved_rt != INVALID_RAILTYPE) { switch (prop) { case 0x0F: SetBit(rti->powered_railtypes, resolved_rt); [[fallthrough]]; // Powered implies compatible. @@ -4413,7 +4413,7 @@ static ChangeInfoResult RailTypeReserveInfo(uint first, uint last, int prop, Byt case 0x08: // Label of rail type { RailTypeLabel rtl = buf.ReadDWord(); - rtl = BSWAP32(rtl); + rtl = std::byteswap(rtl); RailType rt = GetRailTypeByLabel(rtl, false); if (rt == INVALID_RAILTYPE) { @@ -4441,7 +4441,7 @@ static ChangeInfoResult RailTypeReserveInfo(uint first, uint last, int prop, Byt if (_cur.grffile->railtype_map[id] != INVALID_RAILTYPE) { int n = buf.ReadByte(); for (int j = 0; j != n; j++) { - _railtypes[_cur.grffile->railtype_map[id]].alternate_labels.push_back(BSWAP32(buf.ReadDWord())); + _railtypes[_cur.grffile->railtype_map[id]].alternate_labels.push_back(std::byteswap(buf.ReadDWord())); } break; } @@ -4538,7 +4538,7 @@ static ChangeInfoResult RoadTypeChangeInfo(uint first, uint last, int prop, Byte int n = buf.ReadByte(); for (int j = 0; j != n; j++) { RoadTypeLabel label = buf.ReadDWord(); - RoadType resolved_rt = GetRoadTypeByLabel(BSWAP32(label), false); + RoadType resolved_rt = GetRoadTypeByLabel(std::byteswap(label), false); if (resolved_rt != INVALID_ROADTYPE) { switch (prop) { case 0x0F: @@ -4629,7 +4629,7 @@ static ChangeInfoResult RoadTypeReserveInfo(uint first, uint last, int prop, Byt switch (prop) { case 0x08: { // Label of road type RoadTypeLabel rtl = buf.ReadDWord(); - rtl = BSWAP32(rtl); + rtl = std::byteswap(rtl); RoadType rt = GetRoadTypeByLabel(rtl, false); if (rt == INVALID_ROADTYPE) { @@ -4659,7 +4659,7 @@ static ChangeInfoResult RoadTypeReserveInfo(uint first, uint last, int prop, Byt if (type_map[id] != INVALID_ROADTYPE) { int n = buf.ReadByte(); for (int j = 0; j != n; j++) { - _roadtypes[type_map[id]].alternate_labels.push_back(BSWAP32(buf.ReadDWord())); + _roadtypes[type_map[id]].alternate_labels.push_back(std::byteswap(buf.ReadDWord())); } break; } @@ -4854,7 +4854,7 @@ static ChangeInfoResult RoadStopChangeInfo(uint first, uint last, int prop, Byte } uint32_t classid = buf.ReadDWord(); - rs->class_index = RoadStopClass::Allocate(BSWAP32(classid)); + rs->class_index = RoadStopClass::Allocate(std::byteswap(classid)); break; } @@ -6913,31 +6913,31 @@ static void SkipIf(ByteReader &buf) if (condtype >= 0x0B) { /* Tests that ignore 'param' */ switch (condtype) { - case 0x0B: result = !IsValidCargoType(GetCargoTypeByLabel(CargoLabel(BSWAP32(cond_val)))); + case 0x0B: result = !IsValidCargoType(GetCargoTypeByLabel(CargoLabel(std::byteswap(cond_val)))); break; - case 0x0C: result = IsValidCargoType(GetCargoTypeByLabel(CargoLabel(BSWAP32(cond_val)))); + case 0x0C: result = IsValidCargoType(GetCargoTypeByLabel(CargoLabel(std::byteswap(cond_val)))); break; - case 0x0D: result = GetRailTypeByLabel(BSWAP32(cond_val)) == INVALID_RAILTYPE; + case 0x0D: result = GetRailTypeByLabel(std::byteswap(cond_val)) == INVALID_RAILTYPE; break; - case 0x0E: result = GetRailTypeByLabel(BSWAP32(cond_val)) != INVALID_RAILTYPE; + case 0x0E: result = GetRailTypeByLabel(std::byteswap(cond_val)) != INVALID_RAILTYPE; break; case 0x0F: { - RoadType rt = GetRoadTypeByLabel(BSWAP32(cond_val)); + RoadType rt = GetRoadTypeByLabel(std::byteswap(cond_val)); result = rt == INVALID_ROADTYPE || !RoadTypeIsRoad(rt); break; } case 0x10: { - RoadType rt = GetRoadTypeByLabel(BSWAP32(cond_val)); + RoadType rt = GetRoadTypeByLabel(std::byteswap(cond_val)); result = rt != INVALID_ROADTYPE && RoadTypeIsRoad(rt); break; } case 0x11: { - RoadType rt = GetRoadTypeByLabel(BSWAP32(cond_val)); + RoadType rt = GetRoadTypeByLabel(std::byteswap(cond_val)); result = rt == INVALID_ROADTYPE || !RoadTypeIsTram(rt); break; } case 0x12: { - RoadType rt = GetRoadTypeByLabel(BSWAP32(cond_val)); + RoadType rt = GetRoadTypeByLabel(std::byteswap(cond_val)); result = rt != INVALID_ROADTYPE && RoadTypeIsTram(rt); break; } @@ -6954,7 +6954,7 @@ static void SkipIf(ByteReader &buf) } if (condtype != 10 && c == nullptr) { - GrfMsg(7, "SkipIf: GRFID 0x{:08X} unknown, skipping test", BSWAP32(cond_val)); + GrfMsg(7, "SkipIf: GRFID 0x{:08X} unknown, skipping test", std::byteswap(cond_val)); return; } @@ -7061,7 +7061,7 @@ static void ScanInfo(ByteReader &buf) if (grf_version < 2 || grf_version > 8) { SetBit(_cur.grfconfig->flags, GCF_INVALID); - Debug(grf, 0, "{}: NewGRF \"{}\" (GRFID {:08X}) uses GRF version {}, which is incompatible with this version of OpenTTD.", _cur.grfconfig->filename, StrMakeValid(name), BSWAP32(grfid), grf_version); + Debug(grf, 0, "{}: NewGRF \"{}\" (GRFID {:08X}) uses GRF version {}, which is incompatible with this version of OpenTTD.", _cur.grfconfig->filename, StrMakeValid(name), std::byteswap(grfid), grf_version); } /* GRF IDs starting with 0xFF are reserved for internal TTDPatch use */ @@ -7098,7 +7098,7 @@ static void GRFInfo(ByteReader &buf) } if (_cur.grffile->grfid != grfid) { - Debug(grf, 0, "GRFInfo: GRFID {:08X} in FILESCAN stage does not match GRFID {:08X} in INIT/RESERVE/ACTIVATION stage", BSWAP32(_cur.grffile->grfid), BSWAP32(grfid)); + Debug(grf, 0, "GRFInfo: GRFID {:08X} in FILESCAN stage does not match GRFID {:08X} in INIT/RESERVE/ACTIVATION stage", std::byteswap(_cur.grffile->grfid), std::byteswap(grfid)); _cur.grffile->grfid = grfid; } @@ -7106,7 +7106,7 @@ static void GRFInfo(ByteReader &buf) _cur.grfconfig->status = _cur.stage < GLS_RESERVE ? GCS_INITIALISED : GCS_ACTIVATED; /* Do swap the GRFID for displaying purposes since people expect that */ - Debug(grf, 1, "GRFInfo: Loaded GRFv{} set {:08X} - {} (palette: {}, version: {})", version, BSWAP32(grfid), StrMakeValid(name), (_cur.grfconfig->palette & GRFP_USE_MASK) ? "Windows" : "DOS", _cur.grfconfig->version); + Debug(grf, 1, "GRFInfo: Loaded GRFv{} set {:08X} - {} (palette: {}, version: {})", version, std::byteswap(grfid), StrMakeValid(name), (_cur.grfconfig->palette & GRFP_USE_MASK) ? "Windows" : "DOS", _cur.grfconfig->version); } /** @@ -8120,7 +8120,7 @@ static void TranslateGRFStrings(ByteReader &buf) uint32_t grfid = buf.ReadDWord(); const GRFConfig *c = GetGRFConfig(grfid); if (c == nullptr || (c->status != GCS_INITIALISED && c->status != GCS_ACTIVATED)) { - GrfMsg(7, "TranslateGRFStrings: GRFID 0x{:08X} unknown, skipping action 13", BSWAP32(grfid)); + GrfMsg(7, "TranslateGRFStrings: GRFID 0x{:08X} unknown, skipping action 13", std::byteswap(grfid)); return; } @@ -8564,7 +8564,7 @@ static bool HandleNode(uint8_t type, uint32_t id, ByteReader &buf, std::span default_grf_overrides[] = { - { BSWAP32(0x44442202), BSWAP32(0x44440111) }, // UKRS addons modifies UKRS - { BSWAP32(0x6D620402), BSWAP32(0x6D620401) }, // DBSetXL ECS extension modifies DBSetXL - { BSWAP32(0x4D656f20), BSWAP32(0x4D656F17) }, // LV4cut modifies LV4 + { std::byteswap(0x44442202), std::byteswap(0x44440111) }, // UKRS addons modifies UKRS + { std::byteswap(0x6D620402), std::byteswap(0x6D620401) }, // DBSetXL ECS extension modifies DBSetXL + { std::byteswap(0x4D656f20), std::byteswap(0x4D656F17) }, // LV4cut modifies LV4 }; for (const auto &grf_override : default_grf_overrides) { SetNewGRFOverride(grf_override.first, grf_override.second); diff --git a/src/newgrf_config.cpp b/src/newgrf_config.cpp index 88683a0bb1..18c0e93e8f 100644 --- a/src/newgrf_config.cpp +++ b/src/newgrf_config.cpp @@ -455,7 +455,7 @@ GRFListCompatibility IsGoodGRFConfigList(GRFConfigList &grfconfig) * same grfid, as it most likely is compatible */ f = FindGRFConfig(c->ident.grfid, FGCM_COMPATIBLE, nullptr, c->version); if (f != nullptr) { - Debug(grf, 1, "NewGRF {:08X} ({}) not found; checksum {}. Compatibility mode on", BSWAP32(c->ident.grfid), c->filename, FormatArrayAsHex(c->ident.md5sum)); + Debug(grf, 1, "NewGRF {:08X} ({}) not found; checksum {}. Compatibility mode on", std::byteswap(c->ident.grfid), c->filename, FormatArrayAsHex(c->ident.md5sum)); if (!HasBit(c->flags, GCF_COMPATIBLE)) { /* Preserve original_md5sum after it has been assigned */ SetBit(c->flags, GCF_COMPATIBLE); @@ -468,13 +468,13 @@ GRFListCompatibility IsGoodGRFConfigList(GRFConfigList &grfconfig) } /* No compatible grf was found, mark it as disabled */ - Debug(grf, 0, "NewGRF {:08X} ({}) not found; checksum {}", BSWAP32(c->ident.grfid), c->filename, FormatArrayAsHex(c->ident.md5sum)); + Debug(grf, 0, "NewGRF {:08X} ({}) not found; checksum {}", std::byteswap(c->ident.grfid), c->filename, FormatArrayAsHex(c->ident.md5sum)); c->status = GCS_NOT_FOUND; res = GLC_NOT_FOUND; } else { compatible_grf: - Debug(grf, 1, "Loading GRF {:08X} from {}", BSWAP32(f->ident.grfid), f->filename); + Debug(grf, 1, "Loading GRF {:08X} from {}", std::byteswap(f->ident.grfid), f->filename); /* The filename could be the filename as in the savegame. As we need * to load the GRF here, we need the correct filename, so overwrite that * in any case and set the name and info when it is not set already. diff --git a/src/newgrf_debug_gui.cpp b/src/newgrf_debug_gui.cpp index 5f586a5eca..c608c8ad9c 100644 --- a/src/newgrf_debug_gui.cpp +++ b/src/newgrf_debug_gui.cpp @@ -464,7 +464,7 @@ struct NewGRFInspectWindow : Window { auto psa = nih->GetPSA(index, this->caller_grfid); if (!psa.empty()) { if (nih->PSAWithParameter()) { - this->DrawString(r, i++, fmt::format("Persistent storage [{:08X}]:", BSWAP32(this->caller_grfid))); + this->DrawString(r, i++, fmt::format("Persistent storage [{:08X}]:", std::byteswap(this->caller_grfid))); } else { this->DrawString(r, i++, "Persistent storage:"); } diff --git a/src/newgrf_gui.cpp b/src/newgrf_gui.cpp index 51f406a2f2..77afeb08ab 100644 --- a/src/newgrf_gui.cpp +++ b/src/newgrf_gui.cpp @@ -90,7 +90,7 @@ static void ShowNewGRFInfo(const GRFConfig &c, const Rect &r, bool show_params) } /* Prepare and draw GRF ID */ - SetDParamStr(0, fmt::format("{:08X}", BSWAP32(c.ident.grfid))); + SetDParamStr(0, fmt::format("{:08X}", std::byteswap(c.ident.grfid))); tr.top = DrawStringMultiLine(tr, STR_NEWGRF_SETTINGS_GRF_ID); if ((_settings_client.gui.newgrf_developer_tools || _settings_client.gui.newgrf_show_old_versions) && c.version != 0) { @@ -1572,7 +1572,7 @@ void ShowMissingContentWindow(const GRFConfigList &list) ci->type = CONTENT_TYPE_NEWGRF; ci->state = ContentInfo::DOES_NOT_EXIST; ci->name = c->GetName(); - ci->unique_id = BSWAP32(c->ident.grfid); + ci->unique_id = std::byteswap(c->ident.grfid); ci->md5sum = HasBit(c->flags, GCF_COMPATIBLE) ? c->original_md5sum : c->ident.md5sum; cv.push_back(ci); } diff --git a/src/newgrf_profiling.cpp b/src/newgrf_profiling.cpp index 0b1464a1d0..7b589d7f67 100644 --- a/src/newgrf_profiling.cpp +++ b/src/newgrf_profiling.cpp @@ -95,14 +95,14 @@ uint32_t NewGRFProfiler::Finish() if (!this->active) return 0; if (this->calls.empty()) { - IConsolePrint(CC_DEBUG, "Finished profile of NewGRF [{:08X}], no events collected, not writing a file.", BSWAP32(this->grffile->grfid)); + IConsolePrint(CC_DEBUG, "Finished profile of NewGRF [{:08X}], no events collected, not writing a file.", std::byteswap(this->grffile->grfid)); this->Abort(); return 0; } std::string filename = this->GetOutputFilename(); - IConsolePrint(CC_DEBUG, "Finished profile of NewGRF [{:08X}], writing {} events to '{}'.", BSWAP32(this->grffile->grfid), this->calls.size(), filename); + IConsolePrint(CC_DEBUG, "Finished profile of NewGRF [{:08X}], writing {} events to '{}'.", std::byteswap(this->grffile->grfid), this->calls.size(), filename); uint32_t total_microseconds = 0; @@ -134,7 +134,7 @@ void NewGRFProfiler::Abort() */ std::string NewGRFProfiler::GetOutputFilename() const { - return fmt::format("{}grfprofile-{:%Y%m%d-%H%M}-{:08X}.csv", FiosGetScreenshotDir(), fmt::localtime(time(nullptr)), BSWAP32(this->grffile->grfid)); + return fmt::format("{}grfprofile-{:%Y%m%d-%H%M}-{:08X}.csv", FiosGetScreenshotDir(), fmt::localtime(time(nullptr)), std::byteswap(this->grffile->grfid)); } /* static */ uint32_t NewGRFProfiler::FinishAll() diff --git a/src/newgrf_storage.cpp b/src/newgrf_storage.cpp index ff309a66df..ae90bfd555 100644 --- a/src/newgrf_storage.cpp +++ b/src/newgrf_storage.cpp @@ -91,7 +91,7 @@ void AddChangedPersistentStorage(BasePersistentStorageArray *storage) /* Discard all temporary changes */ for (auto &it : *_changed_storage_arrays) { - Debug(desync, 2, "warning: discarding persistent storage changes: Feature {}, GrfID {:08X}, Tile {}", it->feature, BSWAP32(it->grfid), it->tile); + Debug(desync, 2, "warning: discarding persistent storage changes: Feature {}, GrfID {:08X}, Tile {}", it->feature, std::byteswap(it->grfid), it->tile); it->ClearChanges(); } _changed_storage_arrays->clear(); diff --git a/src/openttd.cpp b/src/openttd.cpp index b47696151d..803cfbdf70 100644 --- a/src/openttd.cpp +++ b/src/openttd.cpp @@ -243,7 +243,7 @@ static void WriteSavegameInfo(const std::string &name) message += "NewGRFs:\n"; if (_load_check_data.HasNewGrfs()) { for (GRFConfig *c = _load_check_data.grfconfig; c != nullptr; c = c->next) { - fmt::format_to(std::back_inserter(message), "{:08X} {} {}\n", BSWAP32(c->ident.grfid), + fmt::format_to(std::back_inserter(message), "{:08X} {} {}\n", std::byteswap(c->ident.grfid), FormatArrayAsHex(HasBit(c->flags, GCF_COMPATIBLE) ? c->original_md5sum : c->ident.md5sum), c->filename); } } diff --git a/src/picker_gui.cpp b/src/picker_gui.cpp index 6867c03714..6b14974a27 100644 --- a/src/picker_gui.cpp +++ b/src/picker_gui.cpp @@ -97,7 +97,7 @@ static void PickerSaveConfig(IniFile &ini, const PickerCallbacks &callbacks) group.Clear(); for (const PickerItem &item : callbacks.saved) { - std::string key = fmt::format("{:08X}|{}", BSWAP32(item.grfid), item.local_id); + std::string key = fmt::format("{:08X}|{}", std::byteswap(item.grfid), item.local_id); group.CreateItem(key); } } diff --git a/src/saveload/afterload.cpp b/src/saveload/afterload.cpp index 3c23b1e5d1..8355362ff1 100644 --- a/src/saveload/afterload.cpp +++ b/src/saveload/afterload.cpp @@ -391,11 +391,11 @@ static void CDECL HandleSavegameLoadCrash(int signum) if (HasBit(c->flags, GCF_COMPATIBLE)) { const GRFIdentifier &replaced = _gamelog.GetOverriddenIdentifier(*c); fmt::format_to(std::back_inserter(message), "NewGRF {:08X} (checksum {}) not found.\n Loaded NewGRF \"{}\" (checksum {}) with same GRF ID instead.\n", - BSWAP32(c->ident.grfid), FormatArrayAsHex(c->original_md5sum), c->filename, FormatArrayAsHex(replaced.md5sum)); + std::byteswap(c->ident.grfid), FormatArrayAsHex(c->original_md5sum), c->filename, FormatArrayAsHex(replaced.md5sum)); } if (c->status == GCS_NOT_FOUND) { fmt::format_to(std::back_inserter(message), "NewGRF {:08X} ({}) not found; checksum {}.\n", - BSWAP32(c->ident.grfid), c->filename, FormatArrayAsHex(c->ident.md5sum)); + std::byteswap(c->ident.grfid), c->filename, FormatArrayAsHex(c->ident.md5sum)); } } } else { diff --git a/src/saveload/oldloader_sl.cpp b/src/saveload/oldloader_sl.cpp index ee884e4a5b..1c5aa8be16 100644 --- a/src/saveload/oldloader_sl.cpp +++ b/src/saveload/oldloader_sl.cpp @@ -1564,7 +1564,7 @@ static bool LoadTTDPatchExtraChunks(LoadgameState *ls, int) c->ident.grfid = grfid; AppendToGRFConfigList(_grfconfig, c); - Debug(oldloader, 3, "TTDPatch game using GRF file with GRFID {:08X}", BSWAP32(c->ident.grfid)); + Debug(oldloader, 3, "TTDPatch game using GRF file with GRFID {:08X}", std::byteswap(c->ident.grfid)); } len -= 5; } diff --git a/src/saveload/saveload.cpp b/src/saveload/saveload.cpp index bcea36362a..6f64b632da 100644 --- a/src/saveload/saveload.cpp +++ b/src/saveload/saveload.cpp @@ -998,7 +998,7 @@ static void SlCopyInternal(void *object, size_t length, VarType conv) /* used for conversion of Money 32bit->64bit */ if (conv == (SLE_FILE_I32 | SLE_VAR_I64)) { for (uint i = 0; i < length; i++) { - ((int64_t*)object)[i] = (int32_t)BSWAP32(SlReadUint32()); + ((int64_t*)object)[i] = (int32_t)std::byteswap(SlReadUint32()); } return; } diff --git a/src/screenshot.cpp b/src/screenshot.cpp index ac12dd2ea2..f48838b167 100644 --- a/src/screenshot.cpp +++ b/src/screenshot.cpp @@ -308,7 +308,7 @@ static bool MakePNGImage(const char *name, ScreenshotCallback *callb, void *user fmt::format_to(std::back_inserter(message), "Graphics set: {} ({})\n", BaseGraphics::GetUsedSet()->name, BaseGraphics::GetUsedSet()->version); message += "NewGRFs:\n"; for (const GRFConfig *c = _game_mode == GM_MENU ? nullptr : _grfconfig; c != nullptr; c = c->next) { - fmt::format_to(std::back_inserter(message), "{:08X} {} {}\n", BSWAP32(c->ident.grfid), FormatArrayAsHex(c->ident.md5sum), c->filename); + fmt::format_to(std::back_inserter(message), "{:08X} {} {}\n", std::byteswap(c->ident.grfid), FormatArrayAsHex(c->ident.md5sum), c->filename); } message += "\nCompanies:\n"; for (const Company *c : Company::Iterate()) { diff --git a/src/script/api/script_industrytype.cpp b/src/script/api/script_industrytype.cpp index 64e969aefc..964b7e61a4 100644 --- a/src/script/api/script_industrytype.cpp +++ b/src/script/api/script_industrytype.cpp @@ -162,6 +162,6 @@ { EnforcePrecondition(IT_INVALID, IsInsideBS(grf_local_id, 0x00, NUM_INDUSTRYTYPES_PER_GRF)); - grfid = BSWAP32(GB(grfid, 0, 32)); // Match people's expectations. + grfid = std::byteswap(GB(grfid, 0, 32)); // Match people's expectations. return _industry_mngr.GetID(grf_local_id, grfid); } diff --git a/src/script/api/script_newgrf.cpp b/src/script/api/script_newgrf.cpp index 884918c549..7548081036 100644 --- a/src/script/api/script_newgrf.cpp +++ b/src/script/api/script_newgrf.cpp @@ -19,14 +19,14 @@ ScriptNewGRFList::ScriptNewGRFList() { for (auto c = _grfconfig; c != nullptr; c = c->next) { if (!HasBit(c->flags, GCF_STATIC)) { - this->AddItem(BSWAP32(c->ident.grfid)); + this->AddItem(std::byteswap(c->ident.grfid)); } } } /* static */ bool ScriptNewGRF::IsLoaded(SQInteger grfid) { - grfid = BSWAP32(GB(grfid, 0, 32)); // Match people's expectations. + grfid = std::byteswap(GB(grfid, 0, 32)); // Match people's expectations. for (auto c = _grfconfig; c != nullptr; c = c->next) { if (!HasBit(c->flags, GCF_STATIC) && c->ident.grfid == grfid) { @@ -39,7 +39,7 @@ ScriptNewGRFList::ScriptNewGRFList() /* static */ SQInteger ScriptNewGRF::GetVersion(SQInteger grfid) { - grfid = BSWAP32(GB(grfid, 0, 32)); // Match people's expectations. + grfid = std::byteswap(GB(grfid, 0, 32)); // Match people's expectations. for (auto c = _grfconfig; c != nullptr; c = c->next) { if (!HasBit(c->flags, GCF_STATIC) && c->ident.grfid == grfid) { @@ -52,7 +52,7 @@ ScriptNewGRFList::ScriptNewGRFList() /* static */ std::optional ScriptNewGRF::GetName(SQInteger grfid) { - grfid = BSWAP32(GB(grfid, 0, 32)); // Match people's expectations. + grfid = std::byteswap(GB(grfid, 0, 32)); // Match people's expectations. for (auto c = _grfconfig; c != nullptr; c = c->next) { if (!HasBit(c->flags, GCF_STATIC) && c->ident.grfid == grfid) { diff --git a/src/script/api/script_objecttype.cpp b/src/script/api/script_objecttype.cpp index 7e036a3e3d..002da3cef9 100644 --- a/src/script/api/script_objecttype.cpp +++ b/src/script/api/script_objecttype.cpp @@ -51,6 +51,6 @@ { EnforcePrecondition(INVALID_OBJECT_TYPE, IsInsideBS(grf_local_id, 0x00, NUM_OBJECTS_PER_GRF)); - grfid = BSWAP32(GB(grfid, 0, 32)); // Match people's expectations. + grfid = std::byteswap(GB(grfid, 0, 32)); // Match people's expectations. return _object_mngr.GetID(grf_local_id, grfid); } diff --git a/src/settings.cpp b/src/settings.cpp index 573bb89132..b3430b62eb 100644 --- a/src/settings.cpp +++ b/src/settings.cpp @@ -1025,7 +1025,7 @@ static void GraphicsSetLoadConfig(IniFile &ini) if (const IniItem *item = group->GetItem("name"); item != nullptr && item->value) BaseGraphics::ini_data.name = *item->value; if (const IniItem *item = group->GetItem("shortname"); item != nullptr && item->value && item->value->size() == 8) { - BaseGraphics::ini_data.shortname = BSWAP32(std::strtoul(item->value->c_str(), nullptr, 16)); + BaseGraphics::ini_data.shortname = std::byteswap(std::strtoul(item->value->c_str(), nullptr, 16)); } if (const IniItem *item = group->GetItem("extra_version"); item != nullptr && item->value) BaseGraphics::ini_data.extra_version = std::strtoul(item->value->c_str(), nullptr, 10); @@ -1237,7 +1237,7 @@ static void GraphicsSetSaveConfig(IniFile &ini) group.Clear(); group.GetOrCreateItem("name").SetValue(used_set->name); - group.GetOrCreateItem("shortname").SetValue(fmt::format("{:08X}", BSWAP32(used_set->shortname))); + group.GetOrCreateItem("shortname").SetValue(fmt::format("{:08X}", std::byteswap(used_set->shortname))); const GRFConfig *extra_cfg = used_set->GetExtraConfig(); if (extra_cfg != nullptr && !extra_cfg->param.empty()) { @@ -1254,7 +1254,7 @@ static void GRFSaveConfig(IniFile &ini, const char *grpname, const GRFConfig *li const GRFConfig *c; for (c = list; c != nullptr; c = c->next) { - std::string key = fmt::format("{:08X}|{}|{}", BSWAP32(c->ident.grfid), + std::string key = fmt::format("{:08X}|{}|{}", std::byteswap(c->ident.grfid), FormatArrayAsHex(c->ident.md5sum), c->filename); group.GetOrCreateItem(key).SetValue(GRFBuildParamList(*c)); } diff --git a/src/soundloader_wav.cpp b/src/soundloader_wav.cpp index fc93249f31..1f968ab884 100644 --- a/src/soundloader_wav.cpp +++ b/src/soundloader_wav.cpp @@ -28,16 +28,16 @@ public: RandomAccessFile &file = *sound.file; /* Check RIFF/WAVE header. */ - if (file.ReadDword() != BSWAP32('RIFF')) return false; + if (file.ReadDword() != std::byteswap('RIFF')) return false; file.ReadDword(); // Skip data size - if (file.ReadDword() != BSWAP32('WAVE')) return false; + if (file.ReadDword() != std::byteswap('WAVE')) return false; /* Read riff tags */ for (;;) { uint32_t tag = file.ReadDword(); uint32_t size = file.ReadDword(); - if (tag == BSWAP32('fmt ')) { + if (tag == std::byteswap('fmt ')) { uint16_t format = file.ReadWord(); if (format != 1) return false; // File must be uncompressed PCM @@ -55,7 +55,7 @@ public: /* We've read 16 bytes of this chunk, we can skip anything extra. */ size -= 16; - } else if (tag == BSWAP32('data')) { + } else if (tag == std::byteswap('data')) { uint align = sound.channels * sound.bits_per_sample / 8; if (Align(size, align) != size) return false; // Ensure length is aligned correctly for channels and BPS. diff --git a/src/survey.cpp b/src/survey.cpp index bd5003c838..ea87304859 100644 --- a/src/survey.cpp +++ b/src/survey.cpp @@ -355,7 +355,7 @@ void SurveyTimers(nlohmann::json &survey) void SurveyGrfs(nlohmann::json &survey) { for (GRFConfig *c = _grfconfig; c != nullptr; c = c->next) { - auto grfid = fmt::format("{:08x}", BSWAP32(c->ident.grfid)); + auto grfid = fmt::format("{:08x}", std::byteswap(c->ident.grfid)); auto &grf = survey[grfid]; grf["md5sum"] = FormatArrayAsHex(c->ident.md5sum);