From 4a287e7f7943dd83e4c797f7bb6b1534abb22f99 Mon Sep 17 00:00:00 2001 From: Azi Hassan Date: Sun, 6 Oct 2024 20:45:03 +0100 Subject: [PATCH] Reformat code to match .clang-format soecs --- Source/DiabloUI/mainmenu.cpp | 4 +- Source/controls/devices/joystick.cpp | 30 ++++---- Source/diablo.cpp | 5 +- Source/init.cpp | 6 +- Source/loadsave.cpp | 16 ++--- Source/main.cpp | 7 +- Source/pfile.cpp | 95 ++++++++++++-------------- Source/pfile.h | 2 +- Source/utils/file_util.cpp | 29 ++++---- Source/utils/sdl2_to_1_2_backports.cpp | 2 +- Source/utils/sdl_compat.h | 2 +- 11 files changed, 93 insertions(+), 105 deletions(-) diff --git a/Source/DiabloUI/mainmenu.cpp b/Source/DiabloUI/mainmenu.cpp index ba240932266..5b1449b7320 100644 --- a/Source/DiabloUI/mainmenu.cpp +++ b/Source/DiabloUI/mainmenu.cpp @@ -36,8 +36,8 @@ void MainmenuEsc() void MainmenuLoad(const char *name) { #ifndef __DREAMCAST__ - //single player save files are too big for the VMU - //todo reactivate when SD card saving is implemented + // single player save files are too big for the VMU + // todo reactivate when SD card saving is implemented vecMenuItems.push_back(std::make_unique(_("Single Player"), MAINMENU_SINGLE_PLAYER)); #endif vecMenuItems.push_back(std::make_unique(_("Multi Player"), MAINMENU_MULTIPLAYER)); diff --git a/Source/controls/devices/joystick.cpp b/Source/controls/devices/joystick.cpp index f1ac60bcfc4..af62cb395ea 100644 --- a/Source/controls/devices/joystick.cpp +++ b/Source/controls/devices/joystick.cpp @@ -104,18 +104,18 @@ StaticVector Joystick::ToControllerButtonEvents(const case SDL_JOYAXISMOTION: case SDL_JOYBALLMOTION: #ifdef __DREAMCAST__ - if(event.jaxis.axis == 3) { - Log("BUTTON_LEFTSHOULDER detected"); - Log("event.jbutton.button = {}", event.jbutton.button); - Log("event.jbutton.state == SDL_RELEASED = {}", event.jbutton.state == SDL_RELEASED); - return { ControllerButtonEvent { ControllerButton_BUTTON_LEFTSHOULDER, event.jaxis.value < 255 } }; - } - if(event.jaxis.axis == 2) { - Log("BUTTON_RIGHTSHOULDER detected"); - Log("event.jbutton.button = {}", event.jbutton.button); - Log("event.jbutton.state == SDL_RELEASED = {}", event.jbutton.state == SDL_RELEASED); - return { ControllerButtonEvent { ControllerButton_BUTTON_RIGHTSHOULDER, event.jaxis.value < 255 } }; - } + if (event.jaxis.axis == 3) { + Log("BUTTON_LEFTSHOULDER detected"); + Log("event.jbutton.button = {}", event.jbutton.button); + Log("event.jbutton.state == SDL_RELEASED = {}", event.jbutton.state == SDL_RELEASED); + return { ControllerButtonEvent { ControllerButton_BUTTON_LEFTSHOULDER, event.jaxis.value < 255 } }; + } + if (event.jaxis.axis == 2) { + Log("BUTTON_RIGHTSHOULDER detected"); + Log("event.jbutton.button = {}", event.jbutton.button); + Log("event.jbutton.state == SDL_RELEASED = {}", event.jbutton.state == SDL_RELEASED); + return { ControllerButtonEvent { ControllerButton_BUTTON_RIGHTSHOULDER, event.jaxis.value < 255 } }; + } #endif // ProcessAxisMotion() requires a ControllerButtonEvent parameter // so provide one here using ControllerButton_NONE @@ -322,11 +322,11 @@ bool Joystick::ProcessAxisMotion(const SDL_Event &event) Log("event.jbutton.state == SDL_RELEASED = {}", event.jbutton.state == SDL_RELEASED); switch (event.jaxis.axis) { - case 0: //horizontal + case 0: // horizontal leftStickXUnscaled = event.jaxis.value; leftStickNeedsScaling = true; return true; - case 1: //vertical + case 1: // vertical leftStickYUnscaled = event.jaxis.value; leftStickNeedsScaling = true; return true; @@ -334,7 +334,7 @@ bool Joystick::ProcessAxisMotion(const SDL_Event &event) return false; } } -#else //!ifdef __DREAMCAST__ +#else //! ifdef __DREAMCAST__ bool Joystick::ProcessAxisMotion(const SDL_Event &event) { if (event.type != SDL_JOYAXISMOTION) diff --git a/Source/diablo.cpp b/Source/diablo.cpp index a80500982ac..8de62493e08 100644 --- a/Source/diablo.cpp +++ b/Source/diablo.cpp @@ -787,12 +787,11 @@ void GameEventHandler(const SDL_Event &event, uint16_t modState) #endif default: if (IsCustomEvent(event.type)) { - if (gbIsMultiplayer) - { + if (gbIsMultiplayer) { Log("IsCustomEvent({}) = true", event.type); Log("pfile_write_hero"); pfile_write_hero(); - } + } nthread_ignore_mutex(true); PaletteFadeOut(8); sound_stop(); diff --git a/Source/init.cpp b/Source/init.cpp index 9196a280a44..c922016ecd7 100644 --- a/Source/init.cpp +++ b/Source/init.cpp @@ -197,10 +197,10 @@ bool CheckExtraFontsVersion(AssetRef &&ref) bool AreExtraFontsOutOfDate(const std::string &path) { #ifdef __DREAMCAST__ - //handle ISO 9660 trailing period - const std::string versionPath = path + "fonts" DIRECTORY_SEPARATOR_STR "VERSION."; + // handle ISO 9660 trailing period + const std::string versionPath = path + "fonts" DIRECTORY_SEPARATOR_STR "VERSION."; #else - const std::string versionPath = path + "fonts" DIRECTORY_SEPARATOR_STR "VERSION"; + const std::string versionPath = path + "fonts" DIRECTORY_SEPARATOR_STR "VERSION"; #endif if (versionPath.size() + 1 > AssetRef::PathBufSize) diff --git a/Source/loadsave.cpp b/Source/loadsave.cpp index 95a7f898843..ce4f7fb7b8d 100644 --- a/Source/loadsave.cpp +++ b/Source/loadsave.cpp @@ -226,7 +226,7 @@ class SaveHelper { ~SaveHelper() { - //const auto encodedLen = m_cur_; + // const auto encodedLen = m_cur_; const auto encodedLen = codec_get_encoded_len(m_cur_); const char *const password = pfile_get_password(); Log("codec_encode(m_buffer_.get(), {}, {})", m_cur_, encodedLen); @@ -2235,7 +2235,7 @@ size_t HotkeysSize(size_t nHotkeys = NumHotkeys) void LoadHotkeys() { - //hotkeys => htks to get around VMU filename size limits + // hotkeys => htks to get around VMU filename size limits LoadHelper file(OpenSaveArchive(gSaveNumber), "htks"); if (!file.IsValid()) return; @@ -2278,7 +2278,7 @@ void LoadHotkeys() void SaveHotkeys(SaveWriter &saveWriter, const Player &player) { - //hotkeys => htks to get around VMU filename size limits + // hotkeys => htks to get around VMU filename size limits SaveHelper file(saveWriter, "htks", HotkeysSize()); // Write the number of spell hotkeys @@ -2299,7 +2299,7 @@ void SaveHotkeys(SaveWriter &saveWriter, const Player &player) void LoadHeroItems(Player &player) { - //heroitems => hitms to get around VMU filename size limits + // heroitems => hitms to get around VMU filename size limits LoadHelper file(OpenSaveArchive(gSaveNumber), "hitms"); if (!file.IsValid()) return; @@ -2585,12 +2585,12 @@ void LoadGame(bool firstflag) gbIsHellfireSaveGame = gbIsHellfire; } -//todo restore saving of inventory body +// todo restore saving of inventory body void SaveHeroItems(SaveWriter &saveWriter, Player &player) { - size_t itemCount = static_cast(NUM_INVLOC) + InventoryGridCells + MaxBeltItems; //7 + 40 + 8 = 55 - //heroitems => hitms to get around VMU filename size limits - SaveHelper file(saveWriter, "hitms", itemCount * (gbIsHellfire ? HellfireItemSaveSize : DiabloItemSaveSize) + sizeof(uint8_t)); //55 * 368 + 1 = 20241 bytes + size_t itemCount = static_cast(NUM_INVLOC) + InventoryGridCells + MaxBeltItems; // 7 + 40 + 8 = 55 + // heroitems => hitms to get around VMU filename size limits + SaveHelper file(saveWriter, "hitms", itemCount * (gbIsHellfire ? HellfireItemSaveSize : DiabloItemSaveSize) + sizeof(uint8_t)); // 55 * 368 + 1 = 20241 bytes file.WriteLE(gbIsHellfire ? 1 : 0); diff --git a/Source/main.cpp b/Source/main.cpp index a248f4bde02..9fedd1b8c41 100644 --- a/Source/main.cpp +++ b/Source/main.cpp @@ -23,9 +23,10 @@ #include "diablo.h" #ifdef __DREAMCAST__ -//fchmod fails to link on the dreamcast, this stub is provided as a workaround -extern "C" int fchmod(int fd, mode_t mode) { - return 0; +// fchmod fails to link on the dreamcast, this stub is provided as a workaround +extern "C" int fchmod(int fd, mode_t mode) +{ + return 0; } #endif diff --git a/Source/pfile.cpp b/Source/pfile.cpp index c45d72a982c..97dc14c52d8 100644 --- a/Source/pfile.cpp +++ b/Source/pfile.cpp @@ -33,10 +33,10 @@ #include "utils/utf8.hpp" #ifdef __DREAMCAST__ -#include +#include #include +#include #include -#include #endif #ifdef UNPACKED_SAVES @@ -54,19 +54,19 @@ namespace devilution { bool gbValidSaveFile; - -void listdir(const char *dir, int depth) { +void listdir(const char *dir, int depth) +{ file_t d = fs_open(dir, O_RDONLY | O_DIR); dirent_t *entry; printf("============ %s ============\n", dir); - while(NULL != (entry = fs_readdir(d))) { + while (NULL != (entry = fs_readdir(d))) { char absolutePath[1024]; strcpy(absolutePath, dir); strcat(absolutePath, "/"); strcat(absolutePath, entry->name); bool isDir = entry->size == -1; printf("[%s]\t%.2f kB\t%s\n", isDir ? "DIR" : "FIL", entry->size / 1024.0, entry->name); - if(isDir) { + if (isDir) { printf("absolutePath = %s, depth = %d\n", absolutePath, depth); listdir(absolutePath, depth + 1); } @@ -81,7 +81,7 @@ char hero_names[MAX_CHARACTERS][PlayerNameLength]; std::string GetSavePath(uint32_t saveNum, std::string_view savePrefix = {}) { - //shorter names to get around VMU filename size limits + // shorter names to get around VMU filename size limits return StrCat(paths::PrefPath(), savePrefix, gbIsSpawn ? (gbIsMultiplayer ? "M" : "S") @@ -89,12 +89,12 @@ std::string GetSavePath(uint32_t saveNum, std::string_view savePrefix = {}) saveNum, #ifdef UNPACKED_SAVES #ifdef __DREAMCAST__ - //flatten directory structure for easier fs_ramdisk_* usage - //for example, /ram/spawn_sv/hero would become /ram/spawn_sv_hero + // flatten directory structure for easier fs_ramdisk_* usage + // for example, /ram/spawn_sv/hero would become /ram/spawn_sv_hero - gbIsHellfire ? "_hsv" DIRECTORY_SEPARATOR_STR : "_sv_" + gbIsHellfire ? "_hsv" DIRECTORY_SEPARATOR_STR : "_sv_" #else - gbIsHellfire ? "_hsv" DIRECTORY_SEPARATOR_STR : "_sv" DIRECTORY_SEPARATOR_STR + gbIsHellfire ? "_hsv" DIRECTORY_SEPARATOR_STR : "_sv" DIRECTORY_SEPARATOR_STR #endif #else gbIsHellfire ? ".hsv" : ".sv" @@ -108,7 +108,7 @@ std::string GetStashSavePath() gbIsSpawn ? "stash_spawn" : "stash", #ifdef UNPACKED_SAVES #ifdef __DREAMCAST__ - //same as above + // same as above gbIsHellfire ? "_hsv" DIRECTORY_SEPARATOR_STR : "_sv_" #else gbIsHellfire ? "_hsv" DIRECTORY_SEPARATOR_STR : "_sv" DIRECTORY_SEPARATOR_STR @@ -187,10 +187,10 @@ bool ReadHero(SaveReader &archive, PlayerPack *pPack) } Log("{} == sizeof(*pPack) ({}) = {}", read, sizeof(*pPack), read == sizeof(*pPack)); Log("Read player {}", pPack->pName); - //Log("\tpHPBase = {}", pPack->pHPBase); + // Log("\tpHPBase = {}", pPack->pHPBase); listdir("/ram", 0); - listdir("/vmu/a1", 0); + listdir("/vmu/a1", 0); return ret; } @@ -204,7 +204,7 @@ void EncodeHero(SaveWriter &saveWriter, const PlayerPack *pack) memcpy(packed.get(), pack, sizeof(*pack)); codec_encode(packed.get(), sizeof(*pack), packedLen, pfile_get_password()); Log("Saving player {}", pack->pName); - //Log("\tpHPBase = {}", pack->pHPBase); + // Log("\tpHPBase = {}", pack->pHPBase); bool result = saveWriter.WriteFile("hero", packed.get(), packedLen /* sizeof(*pack) */); Log("saveWriter.WriteFile(\"hero\", packed.get(), {}) = {}", packedLen, result); } @@ -292,8 +292,8 @@ std::optional CreateSaveReader(std::string &&path) #ifdef UNPACKED_SAVES #ifdef __DREAMCAST__ Log("\tAttempting to load save file {}", path); - //no notion of directories in ramdisk, so /ram/spawn_0_sv/ doesn't exist - //instead, we check for /ram/spawn_0_sv_hero which was previously created + // no notion of directories in ramdisk, so /ram/spawn_0_sv/ doesn't exist + // instead, we check for /ram/spawn_0_sv_hero which was previously created std::string heroFile = path + "hero"; if (!FileExists(heroFile)) { Log("\tFailed ):"); @@ -613,16 +613,14 @@ std::unique_ptr SaveReader::ReadFile(const char *filename, std::siz Log("path = \"{}\"", path); size_t size = 0; uint8 *contents; - if(fs_load(path.c_str(), &contents) == -1) - { + if (fs_load(path.c_str(), &contents) == -1) { error = 1; LogError("fs_load(\"{}\", &contents) = -1", path); app_fatal("SaveReader::ReadFile " + path + " KO"); return nullptr; } vmu_pkg_t package; - if(vmu_pkg_parse(contents, &package) < 0) - { + if (vmu_pkg_parse(contents, &package) < 0) { error = 1; free(contents); LogError("vmu_pkg_parse = -1"); @@ -634,7 +632,7 @@ std::unique_ptr SaveReader::ReadFile(const char *filename, std::siz std::unique_ptr result; result.reset(new std::byte[fileSize]); memcpy(result.get(), package.data, fileSize); - //free(package.data); + // free(package.data); free(contents); return result; } @@ -670,10 +668,9 @@ bool SaveWriter::WriteFile(const char *filename, const std::byte *data, size_t s const std::string path = dir_ + filename; Log("dir_ = {}", dir_); Log("path = {}", path); - const char* baseName = basename(path.c_str()); - //vmu code - if(dir_.starts_with("/vmu")) - { + const char *baseName = basename(path.c_str()); + // vmu code + if (dir_.starts_with("/vmu")) { vmu_pkg_t package; strcpy(package.app_id, "DevilutionX"); strncpy(package.desc_short, filename, 20); @@ -687,16 +684,14 @@ bool SaveWriter::WriteFile(const char *filename, const std::byte *data, size_t s uint8 *contents; size_t packageSize; - if(vmu_pkg_build(&package, &contents, &packageSize) < 0) - { + if (vmu_pkg_build(&package, &contents, &packageSize) < 0) { delete[] package.data; LogError("vmu_pkg_build failed"); app_fatal("vmu_pkg_build failed"); return false; } - FILE* file = OpenFile(path.c_str(), "wb"); - if(file == nullptr) - { + FILE *file = OpenFile(path.c_str(), "wb"); + if (file == nullptr) { delete[] package.data; free(contents); LogError("fopen(\"{}\", \"wb\") = nullptr", path); @@ -704,8 +699,7 @@ bool SaveWriter::WriteFile(const char *filename, const std::byte *data, size_t s return false; } size_t written = std::fwrite(contents, sizeof(uint8), packageSize, file); - if(written != packageSize) - { + if (written != packageSize) { delete[] package.data; free(contents); std::fclose(file); @@ -713,8 +707,7 @@ bool SaveWriter::WriteFile(const char *filename, const std::byte *data, size_t s app_fatal("vmu fwrite call failed"); return false; } - if(std::fclose(file) != 0) - { + if (std::fclose(file) != 0) { delete[] package.data; free(contents); LogError("fclose(file) = 0"); @@ -727,23 +720,21 @@ bool SaveWriter::WriteFile(const char *filename, const std::byte *data, size_t s return true; } - //ramdisk code + // ramdisk code bool exists = FileExists(baseName); - if(exists) - { + if (exists) { Log("{} exists, removing it", path); void *toFree; size_t ignore; int detach_result = fs_ramdisk_detach(baseName, &toFree, &ignore); free(toFree); Log("fs_ramdisk_detach result = {}", detach_result); - if(detach_result == -1) - { + if (detach_result == -1) { return false; } } Log("\tAllocating {} bytes for path {}", size, baseName); - void* buffer = malloc(size); + void *buffer = malloc(size); memcpy(buffer, data, size); Log("\tMallocation succeeded ? {}", buffer != NULL); int attach_result = fs_ramdisk_attach(baseName, buffer, size); @@ -793,7 +784,7 @@ bool SaveWriter::WriteFile(const char *filename, const std::byte *data, size_t s std::fclose(file); return true; } -#endif //def __DREAMCAST__ +#endif // def __DREAMCAST__ void SaveWriter::RemoveHashEntries(bool (*fnGetName)(uint8_t, char *)) { char pszFileName[MaxMpqPathSize]; @@ -803,7 +794,7 @@ void SaveWriter::RemoveHashEntries(bool (*fnGetName)(uint8_t, char *)) RemoveHashEntry(pszFileName); } } -#endif //def UNPACKED_SAVES +#endif // def UNPACKED_SAVES std::optional OpenSaveArchive(uint32_t saveNum) { @@ -836,12 +827,11 @@ std::unique_ptr ReadArchive(SaveReader &archive, const char *pszNam app_fatal("decodedLength = 0"); return nullptr; } - if(strcmp(pszName, "hero") == 0) - { + if (strcmp(pszName, "hero") == 0) { PlayerPack pPack; memcpy(&pPack, result.get(), decodedLength); Log("ReadArchive player {}", pPack.pName); - //Log("\tpHPBase = {}", pPack.pHPBase); + // Log("\tpHPBase = {}", pPack.pHPBase); } Log("ReadArchive 2"); @@ -918,7 +908,7 @@ bool pfile_ui_set_hero_infos(bool (*uiAddHeroInfo)(_uiheroinfo *)) if (ReadHero(*archive, &pkplr)) { Log("ReadHero OK"); Log("Player {}", pkplr.pName); - //Log("Player {}, HP = {}", pkplr.pName, pkplr.pHPBase); + // Log("Player {}, HP = {}", pkplr.pName, pkplr.pHPBase); _uiheroinfo uihero; uihero.saveNumber = i; strcpy(hero_names[i], pkplr.pName); @@ -936,8 +926,7 @@ bool pfile_ui_set_hero_infos(bool (*uiAddHeroInfo)(_uiheroinfo *)) Game2UiPlayer(player, &uihero, hasSaveGame); uiAddHeroInfo(&uihero); - } - else { + } else { Log("ReadHero(*archive, &pkplr) failed"); app_fatal("ReadHero(*archive, &pkplr) failed"); } @@ -1015,12 +1004,12 @@ void pfile_read_player_from_save(uint32_t saveNum, Player &player) std::optional archive = OpenSaveArchive(saveNum); if (!archive) { listdir("/ram", 0); - listdir("/vmu/a1", 0); + listdir("/vmu/a1", 0); app_fatal(_("Unable to open archive")); } if (!ReadHero(*archive, &pkplr)) { listdir("/ram", 0); - listdir("/vmu/a1", 0); + listdir("/vmu/a1", 0); app_fatal(_("Unable to load character")); } @@ -1066,8 +1055,8 @@ void pfile_update(bool forceSave) return; Uint32 tick = SDL_GetTicks(); - //600000 instead of 60000 - //60000 ms is too frequent for the VMU, the game hangs too often and too long + // 600000 instead of 60000 + // 60000 ms is too frequent for the VMU, the game hangs too often and too long if (!forceSave && tick - prevTick <= 600000) return; diff --git a/Source/pfile.h b/Source/pfile.h index e96205e7c43..6643be31ec4 100644 --- a/Source/pfile.h +++ b/Source/pfile.h @@ -19,7 +19,7 @@ namespace devilution { -#define MAX_CHARACTERS 1 //todo restore me to 99 +#define MAX_CHARACTERS 1 // todo restore me to 99 extern bool gbValidSaveFile; diff --git a/Source/utils/file_util.cpp b/Source/utils/file_util.cpp index dfd128d3190..ac83269b509 100644 --- a/Source/utils/file_util.cpp +++ b/Source/utils/file_util.cpp @@ -107,14 +107,14 @@ bool FileExists(const char *path) } return true; #elif defined(__DREAMCAST__) - //ramdisk access doesn't work with SDL_RWFromFile or std::filesystem::exists + // ramdisk access doesn't work with SDL_RWFromFile or std::filesystem::exists int file = fs_open(path, O_RDONLY); - if(file != -1) { + if (file != -1) { fs_close(file); return true; } file = fs_open(path, O_RDONLY | O_DIR); - if(file != -1) { + if (file != -1) { fs_close(file); return true; } @@ -317,19 +317,18 @@ void RecursivelyCreateDir(const char *path) #ifdef __DREAMCAST__ bool TruncateFile(const char *path, off_t size) { - Log("TruncateFile(\"{}\", {})", path, size); - void *contents; - size_t read = fs_load(path, &contents); - if(read == -1) - { - return false; - } + Log("TruncateFile(\"{}\", {})", path, size); + void *contents; + size_t read = fs_load(path, &contents); + if (read == -1) { + return false; + } - fs_unlink(path); - file_t fh = fs_open(path, O_WRONLY); - int result = fs_write(fh, contents, size); - free(contents); - return result != -1; + fs_unlink(path); + file_t fh = fs_open(path, O_WRONLY); + int result = fs_write(fh, contents, size); + free(contents); + return result != -1; } #endif diff --git a/Source/utils/sdl2_to_1_2_backports.cpp b/Source/utils/sdl2_to_1_2_backports.cpp index 648278716ab..3a758c14792 100644 --- a/Source/utils/sdl2_to_1_2_backports.cpp +++ b/Source/utils/sdl2_to_1_2_backports.cpp @@ -883,7 +883,7 @@ char *SDL_GetPrefPath(const char *org, const char *app) return retval; #elif defined(__DREAMCAST__) retval = SDL_strdup("/vmu/a1/"); - return retval; + return retval; #endif if (!app) { diff --git a/Source/utils/sdl_compat.h b/Source/utils/sdl_compat.h index 56c1c6aebfa..7c5da623487 100644 --- a/Source/utils/sdl_compat.h +++ b/Source/utils/sdl_compat.h @@ -85,7 +85,7 @@ inline int SDLC_SetSurfaceAndPaletteColors(SDL_Surface *surface, SDL_Palette *pa // In SDL1, the surface always has its own distinct palette, so we need to // update it as well. return SDL_SetPalette(surface, SDL_LOGPAL, colors, firstcolor, ncolors) - 1; -#endif //defined(__DREAMCAST__) +#endif // defined(__DREAMCAST__) #else // !USE_SDL1 if (SDL_SetPaletteColors(palette, colors, firstcolor, ncolors) < 0)