From d37fb6880986c70a13eb0597d378ba872700e7a6 Mon Sep 17 00:00:00 2001 From: Anders Jenbo Date: Mon, 1 Mar 2021 13:25:19 +0100 Subject: [PATCH] Make header checks portable --- CMakeLists.txt | 2 +- Source/engine.h | 1 + Source/loadsave.cpp | 16 ++++++++-------- Source/mainmenu.cpp | 5 +---- Source/mpqapi.cpp | 6 +++--- Source/multi.cpp | 11 +++-------- Source/multi.h | 2 +- defs.h | 2 +- 8 files changed, 19 insertions(+), 26 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index d142c4ca9..ad0ba7861 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -626,7 +626,7 @@ if(NOT CMAKE_CXX_COMPILER_ID MATCHES "MSVC") target_compile_options(devilution PUBLIC $<${DEBUG_GENEX}:-fno-omit-frame-pointer>) # Warnings for devilution - target_compile_options(devilution PRIVATE -Wno-multichar -Wno-int-to-pointer-cast) + target_compile_options(devilution PRIVATE -Wno-int-to-pointer-cast) # Warnings for devilutionX target_compile_options(${BIN_TARGET} PRIVATE -Wall -Wextra -Wno-unused-parameter -Wno-format-security) diff --git a/Source/engine.h b/Source/engine.h index ee6e20c20..0d455037d 100644 --- a/Source/engine.h +++ b/Source/engine.h @@ -33,6 +33,7 @@ inline BYTE *CelGetFrameStart(BYTE *pCelBuff, int nCel) } #define LOAD_LE32(b) (((DWORD)(b)[3] << 24) | ((DWORD)(b)[2] << 16) | ((DWORD)(b)[1] << 8) | (DWORD)(b)[0]) +#define LOAD_BE32(b) (((DWORD)(b)[0] << 24) | ((DWORD)(b)[1] << 16) | ((DWORD)(b)[2] << 8) | (DWORD)(b)[3]) inline BYTE *CelGetFrame(BYTE *pCelBuff, int nCel, int *nDataSize) { DWORD nCellStart; diff --git a/Source/loadsave.cpp b/Source/loadsave.cpp index 57741ff77..395d7c4f3 100644 --- a/Source/loadsave.cpp +++ b/Source/loadsave.cpp @@ -748,14 +748,14 @@ int RemapItemIdxToDiablo(int i) bool IsHeaderValid(int magicNumber) { gbIsHellfireSaveGame = false; - if (magicNumber == 'SHAR') { + if (magicNumber == LOAD_BE32("SHAR")) { return true; - } else if (magicNumber == 'SHLF') { + } else if (magicNumber == LOAD_BE32("SHLF")) { gbIsHellfireSaveGame = true; return true; - } else if (!gbIsSpawn && magicNumber == 'RETL') { + } else if (!gbIsSpawn && magicNumber == LOAD_BE32("RETL")) { return true; - } else if (!gbIsSpawn && magicNumber == 'HELF') { + } else if (!gbIsSpawn && magicNumber == LOAD_BE32("HELF")) { gbIsHellfireSaveGame = true; return true; } @@ -1642,13 +1642,13 @@ void SaveGame() tbuff = SaveBuff; if (gbIsSpawn && !gbIsHellfire) - ISave('SHAR'); + ISave(LOAD_BE32("SHAR")); else if (gbIsSpawn && gbIsHellfire) - ISave('SHLF'); + ISave(LOAD_BE32("SHLF")); else if (!gbIsSpawn && gbIsHellfire) - ISave('HELF'); + ISave(LOAD_BE32("HELF")); else if (!gbIsSpawn && !gbIsHellfire) - ISave('RETL'); + ISave(LOAD_BE32("RETL")); else app_fatal("Invalid game state"); diff --git a/Source/mainmenu.cpp b/Source/mainmenu.cpp index d9ee6c5e5..37b5dc148 100644 --- a/Source/mainmenu.cpp +++ b/Source/mainmenu.cpp @@ -126,10 +126,7 @@ BOOL mainmenu_select_hero_dialog( pfile_create_player_description(cdesc, cdlen); if (multi) { - if (mode == 'BNET') - *multi = hero_is_created || !plr[myplr].pBattleNet; - else - *multi = hero_is_created; + *multi = hero_is_created; } if (cname && clen) SStrCopy(cname, gszHero, clen); diff --git a/Source/mpqapi.cpp b/Source/mpqapi.cpp index 8f4b1fca2..33c1ab422 100644 --- a/Source/mpqapi.cpp +++ b/Source/mpqapi.cpp @@ -262,7 +262,7 @@ private: _FILEHEADER fhdr; memset(&fhdr, 0, sizeof(fhdr)); - fhdr.signature = SDL_SwapLE32('\x1AQPM'); + fhdr.signature = LOAD_LE32("MPQ\x1A"); fhdr.headersize = SDL_SwapLE32(32); fhdr.filesize = SDL_SwapLE32(static_cast(size)); fhdr.version = SDL_SwapLE16(0); @@ -312,7 +312,7 @@ void ByteSwapHdr(_FILEHEADER *hdr) void InitDefaultMpqHeader(Archive *archive, _FILEHEADER *hdr) { std::memset(hdr, 0, sizeof(*hdr)); - hdr->signature = '\x1AQPM'; + hdr->signature = LOAD_LE32("MPQ\x1A"); hdr->headersize = 32; hdr->sectorsizeid = 3; hdr->version = 0; @@ -322,7 +322,7 @@ void InitDefaultMpqHeader(Archive *archive, _FILEHEADER *hdr) bool IsValidMPQHeader(const Archive &archive, _FILEHEADER *hdr) { - return hdr->signature == '\x1AQPM' + return hdr->signature == LOAD_LE32("MPQ\x1A") && hdr->headersize == 32 && hdr->version <= 0 && hdr->sectorsizeid == 3 diff --git a/Source/multi.cpp b/Source/multi.cpp index ceea0c8fe..69df91fe7 100644 --- a/Source/multi.cpp +++ b/Source/multi.cpp @@ -105,7 +105,7 @@ static BYTE *multi_recv_packet(TBuffer *pBuf, BYTE *body, DWORD *size) static void NetRecvPlrData(TPkt *pkt) { - pkt->hdr.wCheck = 'ip'; + pkt->hdr.wCheck = LOAD_BE32("\0\0ip"); pkt->hdr.px = plr[myplr]._px; pkt->hdr.py = plr[myplr]._py; pkt->hdr.targx = plr[myplr]._ptargx; @@ -457,7 +457,7 @@ void multi_process_network_packets() continue; if (dwID >= MAX_PLRS) continue; - if (pkt->wCheck != 'ip') + if (pkt->wCheck != LOAD_BE32("\0\0ip")) continue; if (pkt->wLen != dwMsgSize) continue; @@ -522,7 +522,7 @@ void multi_send_zero_packet(int pnum, BYTE bCmd, BYTE *pbSrc, DWORD dwLen) dwOffset = 0; while (dwLen != 0) { - pkt.hdr.wCheck = 'ip'; + pkt.hdr.wCheck = LOAD_BE32("\0\0ip"); pkt.hdr.px = 0; pkt.hdr.py = 0; pkt.hdr.targx = 0; @@ -851,8 +851,6 @@ BOOL multi_init_multi(_SNETPROGRAMDATA *client_info, _SNETPLAYERDATA *user_info, && (!first || SErrGetLastError() != STORM_ERROR_REQUIRES_UPGRADE || !multi_upgrade(pfExitProgram))) { return FALSE; } - if (type == 'BNET') - plr[0].pBattleNet = 1; } multi_event_handler(TRUE); @@ -870,9 +868,6 @@ BOOL multi_init_multi(_SNETPROGRAMDATA *client_info, _SNETPLAYERDATA *user_info, pfile_read_player_from_save(); - if (type == 'BNET') - plr[myplr].pBattleNet = 1; - return TRUE; } } diff --git a/Source/multi.h b/Source/multi.h index 7e90c245f..b1c5412d5 100644 --- a/Source/multi.h +++ b/Source/multi.h @@ -15,7 +15,7 @@ extern "C" { typedef struct GameData { Sint32 size; Sint32 dwSeed; - Sint32 programid; + Uint32 programid; Uint8 versionMajor; Uint8 versionMinor; Uint8 versionPatch; diff --git a/defs.h b/defs.h index 73765e92b..88d1b2224 100644 --- a/defs.h +++ b/defs.h @@ -92,7 +92,7 @@ #define PMSG_COUNT 8 -#define GAME_ID (gbIsHellfire ? (gbIsSpawn ? 'HSHR' : 'HRTL') : (gbIsSpawn ? 'DSHR' : 'DRTL')) +#define GAME_ID (gbIsHellfire ? (gbIsSpawn ? LOAD_BE32("HSHR") : LOAD_BE32("HRTL")) : (gbIsSpawn ? LOAD_BE32("DSHR") : LOAD_BE32("DRTL"))) // Diablo uses a 256 color palette // Entry 0-127 (0x00-0x7F) are level specific