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 0179140
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 15 deletions.
17 changes: 16 additions & 1 deletion Source/loadsave.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2237,8 +2237,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 +2284,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 +2309,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 @@ -2587,12 +2599,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)); // 55 * 368 + 1 = 20241 bytes
#endif

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

Expand Down
36 changes: 22 additions & 14 deletions Source/pfile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ namespace devilution {

bool gbValidSaveFile;

#ifdef __DREAMCAST__
void listdir(const char *dir, int depth)
{
file_t d = fs_open(dir, O_RDONLY | O_DIR);
Expand All @@ -74,6 +75,7 @@ void listdir(const char *dir, int depth)
fs_close(d);
printf("============ %s ============\n\n\n", dir);
}
#endif
namespace {

/** List of character names for the character selection screen. */
Expand All @@ -84,13 +86,18 @@ std::string GetSavePath(uint32_t saveNum, std::string_view savePrefix = {})
// shorter names to get around VMU filename size limits
return StrCat(paths::PrefPath(), savePrefix,
gbIsSpawn
#ifdef __DREAMCAST__
? (gbIsMultiplayer ? "M" : "S")
: (gbIsMultiplayer ? "m" : "s"),
#else
? (gbIsMultiplayer ? "share_" : "spawn_")
: (gbIsMultiplayer ? "multi_" : "single_"),
#endif
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 VMU filesystem compatibility
// for example, /vmu/spawn_sv/hero would become /vmu/spawn_sv_hero

gbIsHellfire ? "_hsv" DIRECTORY_SEPARATOR_STR : "_sv_"
#else
Expand Down Expand Up @@ -189,8 +196,10 @@ bool ReadHero(SaveReader &archive, PlayerPack *pPack)
Log("Read player {}", pPack->pName);
// Log("\tpHPBase = {}", pPack->pHPBase);

#ifdef __DREAMCAST__
listdir("/ram", 0);
listdir("/vmu/a1", 0);
#endif
return ret;
}

Expand Down Expand Up @@ -292,8 +301,8 @@ std::optional<SaveReader> 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 vmu, so /vmu/spawn_0_sv/ doesn't exist
// instead, we check for /vmu/spawn_0_sv_hero which was previously created
std::string heroFile = path + "hero";
if (!FileExists(heroFile)) {
Log("\tFailed ):");
Expand Down Expand Up @@ -816,15 +825,13 @@ std::unique_ptr<std::byte[]> ReadArchive(SaveReader &archive, const char *pszNam
std::unique_ptr<std::byte[]> result = archive.ReadFile(pszName, length, error);
if (error != 0) {
Log("ReadArchive 0 error = {}", error);
app_fatal("ReadArchive 0 = " + error);
return nullptr;
}

Log("ReadArchive 1, length = {}", length);
std::size_t decodedLength = codec_decode(result.get(), length, pfile_get_password());
if (decodedLength == 0) {
Log("ReadArchive nullptr");
app_fatal("decodedLength = 0");
return nullptr;
}
if (strcmp(pszName, "hero") == 0) {
Expand Down Expand Up @@ -867,7 +874,6 @@ void pfile_write_hero_demo(int demo)

HeroCompareResult pfile_compare_hero_demo(int demo, bool logDetails)
{
Log("pfile_compare_hero_demo({}, {})", demo, logDetails);
std::string referenceSavePath = GetSavePath(gSaveNumber, StrCat("demo_", demo, "_reference_"));

if (!FileExists(referenceSavePath.c_str()))
Expand Down Expand Up @@ -906,13 +912,9 @@ bool pfile_ui_set_hero_infos(bool (*uiAddHeroInfo)(_uiheroinfo *))
if (archive) {
PlayerPack pkplr;
if (ReadHero(*archive, &pkplr)) {
Log("ReadHero OK");
Log("Player {}", pkplr.pName);
// Log("Player {}, HP = {}", pkplr.pName, pkplr.pHPBase);
_uiheroinfo uihero;
uihero.saveNumber = i;
strcpy(hero_names[i], pkplr.pName);
Log("hero_names[{}] = {}", i, pkplr.pName);
bool hasSaveGame = ArchiveContainsGame(*archive);
if (hasSaveGame)
pkplr.bIsHellfire = gbIsHellfireSaveGame ? 1 : 0;
Expand All @@ -926,9 +928,6 @@ bool pfile_ui_set_hero_infos(bool (*uiAddHeroInfo)(_uiheroinfo *))

Game2UiPlayer(player, &uihero, hasSaveGame);
uiAddHeroInfo(&uihero);
} else {
Log("ReadHero(*archive, &pkplr) failed");
app_fatal("ReadHero(*archive, &pkplr) failed");
}
}
}
Expand Down Expand Up @@ -1003,13 +1002,17 @@ void pfile_read_player_from_save(uint32_t saveNum, Player &player)
{
std::optional<SaveReader> archive = OpenSaveArchive(saveNum);
if (!archive) {
#ifdef __DREAMCAST__
listdir("/ram", 0);
listdir("/vmu/a1", 0);
#endif
app_fatal(_("Unable to open archive"));
}
if (!ReadHero(*archive, &pkplr)) {
#ifdef __DREAMCAST__
listdir("/ram", 0);
listdir("/vmu/a1", 0);
#endif
app_fatal(_("Unable to load character"));
}

Expand Down Expand Up @@ -1055,10 +1058,15 @@ void pfile_update(bool forceSave)
return;

Uint32 tick = SDL_GetTicks();
#ifdef __DREAMCAST__
// 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;
#else
if (!forceSave && tick - prevTick <= 60000)
return;
#endif

Log("pfile_update({})", forceSave);
prevTick = tick;
Expand Down
4 changes: 4 additions & 0 deletions Source/pfile.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@

namespace devilution {

#ifdef __DREAMCAST____
#define MAX_CHARACTERS 1 // todo restore me to 99
#else
#define MAX_CHARACTERS 99
#endif

extern bool gbValidSaveFile;

Expand Down
2 changes: 2 additions & 0 deletions Source/utils/file_util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ bool FileExists(const char *path)
return true;
#elif defined(__DREAMCAST__)
// ramdisk access doesn't work with SDL_RWFromFile or std::filesystem::exists
// todo check to see if this is needed with vmu fs
int file = fs_open(path, O_RDONLY);
if (file != -1) {
fs_close(file);
Expand Down Expand Up @@ -327,6 +328,7 @@ bool TruncateFile(const char *path, off_t size)
fs_unlink(path);
file_t fh = fs_open(path, O_WRONLY);
int result = fs_write(fh, contents, size);
fs_close(fh);
free(contents);
return result != -1;
}
Expand Down

0 comments on commit 0179140

Please sign in to comment.