Skip to content

Commit

Permalink
Wrap dreamcast-specific code if ifdef __DREAMCAST__
Browse files Browse the repository at this point in the history
  • Loading branch information
azihassan committed Oct 6, 2024
1 parent 65818af commit 7dbeed9
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 77 deletions.
7 changes: 1 addition & 6 deletions Source/diablo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -788,11 +788,8 @@ void GameEventHandler(const SDL_Event &event, uint16_t modState)
#endif
default:
if (IsCustomEvent(event.type)) {
if (gbIsMultiplayer) {
Log("IsCustomEvent({}) = true", event.type);
Log("pfile_write_hero");
if (gbIsMultiplayer)
pfile_write_hero();
}
nthread_ignore_mutex(true);
PaletteFadeOut(8);
sound_stop();
Expand Down Expand Up @@ -912,7 +909,6 @@ void RunGameLoop(interface_mode uMsg)
demo::NotifyGameLoopEnd();

if (gbIsMultiplayer) {
Log("gbRunGame = {}, pfile_write_hero(/*writeGameData=*/false)", gbRunGame);
pfile_write_hero(/*writeGameData=*/false);
sfile_write_stash();
}
Expand Down Expand Up @@ -2849,7 +2845,6 @@ void DisableInputEventHandler(const SDL_Event &event, uint16_t modState)

void LoadGameLevel(bool firstflag, lvl_entry lvldir)
{
Log("LoadGameLevel(firstflag = {}, lvldir = {})", firstflag, static_cast<uint8_t>(lvldir));
_music_id neededTrack = GetLevelMusic(leveltype);
ClearFloatingNumbers();

Expand Down
5 changes: 0 additions & 5 deletions Source/interfac.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ Cutscenes GetCutSceneFromLevelType(dungeon_type type)

Cutscenes PickCutscene(interface_mode uMsg)
{
Log("MyPlayer->plrlevel = {}", MyPlayer->plrlevel);
switch (uMsg) {
case WM_DIABLOADGAME:
case WM_DIABNEWGAME:
Expand Down Expand Up @@ -352,7 +351,6 @@ void ShowProgress(interface_mode uMsg)
case WM_DIABNEXTLVL:
IncProgress();
if (!gbIsMultiplayer) {
Log("pfile_save_level()");
pfile_save_level();
} else {
DeltaSaveLevel();
Expand All @@ -363,14 +361,12 @@ void ShowProgress(interface_mode uMsg)
currlevel = myPlayer.plrlevel;
leveltype = GetLevelType(currlevel);
IncProgress();
Log("LoadGameLevel(false, ENTRY_MAIN)");
LoadGameLevel(false, ENTRY_MAIN);
IncProgress();
break;
case WM_DIABPREVLVL:
IncProgress();
if (!gbIsMultiplayer) {
Log("pfile_save_level()");
pfile_save_level();
} else {
DeltaSaveLevel();
Expand All @@ -381,7 +377,6 @@ void ShowProgress(interface_mode uMsg)
leveltype = GetLevelType(currlevel);
assert(myPlayer.isOnActiveLevel());
IncProgress();
Log("LoadGameLevel(false, ENTRY_PREV)");
LoadGameLevel(false, ENTRY_PREV);
IncProgress();
break;
Expand Down
46 changes: 17 additions & 29 deletions Source/loadsave.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,6 @@ class SaveHelper {
, m_buffer_(new std::byte[codec_get_encoded_len(bufferLen)])
, m_capacity_(bufferLen)
{
Log("SaveHelper instantiated: this(mpqWriter, \"{}\", {})", szFileName, bufferLen);
Log("SaveHelper allocated byte array of {} bytes", codec_get_encoded_len(bufferLen));
}

bool IsValid(size_t len = 1)
Expand Down Expand Up @@ -226,12 +224,9 @@ class SaveHelper {

~SaveHelper()
{
// 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, password);
codec_encode(m_buffer_.get(), m_cur_, encodedLen, password);
Log("~SaveHelper WriteFile(\"{}\", m_buffer_.get(), {})", m_szFileName_, encodedLen);
m_mpqWriter.WriteFile(m_szFileName_, m_buffer_.get(), encodedLen);
}
};
Expand Down Expand Up @@ -1839,7 +1834,6 @@ void LoadLevelSeeds()

void SaveLevel(SaveWriter &saveWriter, LevelConversionData *levelConversionData)
{
Log("SaveLevel(SaveWriter &saveWriter, LevelConversionData *levelConversionData)");
Player &myPlayer = *MyPlayer;

DoUnVision(myPlayer.position.tile, myPlayer._pLightRad); // fix for vision staying on the level
Expand All @@ -1849,7 +1843,6 @@ void SaveLevel(SaveWriter &saveWriter, LevelConversionData *levelConversionData)

char szName[MaxMpqPathSize];
GetTempLevelNames(szName);
Log("SaveHelper file(saveWriter, \"{}\", 256 * 1024)", szName);
SaveHelper file(saveWriter, szName, 256 * 1024);

if (leveltype != DTYPE_TOWN) {
Expand Down Expand Up @@ -1920,18 +1913,11 @@ void SaveLevel(SaveWriter &saveWriter, LevelConversionData *levelConversionData)

void LoadLevel(LevelConversionData *levelConversionData)
{
Log("LoadLevel");
char szName[MaxMpqPathSize];
std::optional<SaveReader> archive = OpenSaveArchive(gSaveNumber);
Log("OpenSaveArchive({})", gSaveNumber);
GetTempLevelNames(szName);
Log("szName = '{}'", szName);
Log("!archive = {}", !archive);
Log("!archive->HasFile(\"{}\") = {}", szName, !archive->HasFile(szName));
if (!archive || !archive->HasFile(szName)) {
Log("{} not found in archive, calling GetPermLevelNames(\"{}\")", szName, szName);
if (!archive || !archive->HasFile(szName))
GetPermLevelNames(szName);
}
LoadHelper file(std::move(archive), szName);
if (!file.IsValid())
app_fatal(_("Unable to open save file archive"));
Expand Down Expand Up @@ -2237,8 +2223,12 @@ size_t HotkeysSize(size_t nHotkeys = NumHotkeys)

void LoadHotkeys()
{
#ifdef __DREAMCAST____
// hotkeys => htks to get around VMU filename size limits
LoadHelper file(OpenSaveArchive(gSaveNumber), "htks");
#else
LoadHelper file(OpenSaveArchive(gSaveNumber), "hotkeys");
#endif
if (!file.IsValid())
return;

Expand Down Expand Up @@ -2280,8 +2270,12 @@ void LoadHotkeys()

void SaveHotkeys(SaveWriter &saveWriter, const Player &player)
{
#ifdef __DREAMCAST____
// hotkeys => htks to get around VMU filename size limits
SaveHelper file(saveWriter, "htks", HotkeysSize());
#else
SaveHelper file(saveWriter, "hotkeys", HotkeysSize());
#endif

// Write the number of spell hotkeys
file.WriteLE<uint8_t>(static_cast<uint8_t>(NumHotkeys));
Expand All @@ -2301,8 +2295,12 @@ void SaveHotkeys(SaveWriter &saveWriter, const Player &player)

void LoadHeroItems(Player &player)
{
#ifdef __DREAMCAST____
// heroitems => hitms to get around VMU filename size limits
LoadHelper file(OpenSaveArchive(gSaveNumber), "hitms");
#else
LoadHelper file(OpenSaveArchive(gSaveNumber), "heroitems");
#endif
if (!file.IsValid())
return;

Expand Down Expand Up @@ -2368,7 +2366,6 @@ void RemoveEmptyInventory(Player &player)

void LoadGame(bool firstflag)
{
Log("LoadGame(firstflag = {})", firstflag);
FreeGameMem();

LoadHelper file(OpenSaveArchive(gSaveNumber), "game");
Expand All @@ -2389,7 +2386,6 @@ void LoadGame(bool firstflag)
giNumberOfSmithPremiumItems = 6;
}

Log("pfile_remove_temp_files");
pfile_remove_temp_files();

setlevel = file.NextBool8();
Expand Down Expand Up @@ -2419,16 +2415,13 @@ void LoadGame(bool firstflag)

Player &myPlayer = *MyPlayer;

Log("LoadPlayer(file, myPlayer);");
LoadPlayer(file, myPlayer);

if (sgGameInitInfo.nDifficulty < DIFF_NORMAL || sgGameInitInfo.nDifficulty > DIFF_HELL)
sgGameInitInfo.nDifficulty = DIFF_NORMAL;

Log("LoadQuests {}", giNumberQuests);
for (int i = 0; i < giNumberQuests; i++)
LoadQuest(&file, i);
Log("LoadPortals", MAXPORTAL);
for (int i = 0; i < MAXPORTAL; i++)
LoadPortal(&file, i);

Expand All @@ -2437,7 +2430,6 @@ void LoadGame(bool firstflag)
RemoveEmptyInventory(myPlayer);
}

Log("LoadGameLevel");
LoadGameLevel(firstflag, ENTRY_LOAD);
SetPlrAnims(myPlayer);
SyncPlrAnim(myPlayer);
Expand All @@ -2454,7 +2446,6 @@ void LoadGame(bool firstflag)
if (leveltype != DTYPE_TOWN) {
for (unsigned &monsterId : ActiveMonsters)
monsterId = file.NextBE<uint32_t>();
Log("LoadMonsters {}", ActiveMonsterCount);
for (size_t i = 0; i < ActiveMonsterCount; i++)
LoadMonster(&file, Monsters[ActiveMonsters[i]]);
for (size_t i = 0; i < ActiveMonsterCount; i++)
Expand All @@ -2463,7 +2454,6 @@ void LoadGame(bool firstflag)
file.Skip<int8_t>(MaxMissilesForSaveGame);
// Skip AvailableMissiles
file.Skip<int8_t>(MaxMissilesForSaveGame);
Log("LoadMissiles {}", tmpNummissiles);
for (int i = 0; i < tmpNummissiles; i++)
LoadMissile(&file);
// For petrified monsters, the data in missile.var1 must be used to
Expand Down Expand Up @@ -2587,12 +2577,15 @@ void LoadGame(bool firstflag)
gbIsHellfireSaveGame = gbIsHellfire;
}

// todo restore saving of inventory body
void SaveHeroItems(SaveWriter &saveWriter, Player &player)
{
size_t itemCount = static_cast<size_t>(NUM_INVLOC) + InventoryGridCells + MaxBeltItems; // 7 + 40 + 8 = 55
#ifdef __DREAMCAST____
// 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
#else
SaveHelper file(saveWriter, "heroitems", itemCount * (gbIsHellfire ? HellfireItemSaveSize : DiabloItemSaveSize) + sizeof(uint8_t));
#endif

file.WriteLE<uint8_t>(gbIsHellfire ? 1 : 0);

Expand Down Expand Up @@ -2641,23 +2634,20 @@ void SaveStash(SaveWriter &stashWriter)
}
};

Log("Saving {} pages of stash", pagesToSave.size());
// Current stash size is 100 pages. Will definitely fit in a 32 bit value.
file.WriteLE<uint32_t>(static_cast<uint32_t>(pagesToSave.size()));
for (const auto &page : pagesToSave) {
file.WriteLE<uint32_t>(page);
for (const auto &row : Stash.stashGrids[page]) {
for (uint16_t cell : row) {
file.WriteLE<uint16_t>(cell);
Log("\t\tSaving stash item {}", cell);
}
}
}

// 100 pages of 100 items is still only 10 000, as with the page count will definitely fit in 32 bits even in the worst case.
file.WriteLE<uint32_t>(static_cast<uint32_t>(Stash.stashList.size()));
for (const Item &item : Stash.stashList) {
Log("SaveItem(file, item)");
SaveItem(file, item);
}

Expand All @@ -2666,7 +2656,6 @@ void SaveStash(SaveWriter &stashWriter)

void SaveGameData(SaveWriter &saveWriter)
{
Log("SaveHelper file(saveWriter, \"game\", 320 * 1024)");
SaveHelper file(saveWriter, "game", 320 * 1024);

if (gbIsSpawn && !gbIsHellfire)
Expand Down Expand Up @@ -2832,7 +2821,6 @@ void SaveGameData(SaveWriter &saveWriter)

void SaveGame()
{
Log("SaveGame()");
gbValidSaveFile = true;
pfile_write_hero(/*writeGameData=*/true);
sfile_write_stash();
Expand Down
Loading

0 comments on commit 7dbeed9

Please sign in to comment.