Skip to content

Commit

Permalink
WIP Store save files in ramdisk
Browse files Browse the repository at this point in the history
  • Loading branch information
azihassan committed Aug 2, 2024
1 parent 0d332ce commit 0c20215
Show file tree
Hide file tree
Showing 15 changed files with 1,844 additions and 1,552 deletions.
2 changes: 1 addition & 1 deletion CMake/platforms/dreamcast.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ set(DEVILUTIONX_GAMEPAD_TYPE Nintendo)
set(NOSOUND ON)
set(DEVILUTIONX_STATIC_ZLIB ON)
set(UNPACKED_MPQS ON)
set(UNPACKED_SAVES OFF)
set(UNPACKED_SAVES ON)
set(DEVILUTIONX_SYSTEM_LIBFMT OFF)
set(DEVILUTIONX_STATIC_LUA ON)
#set(DCMAKE_BUILD_TYPE Debug)
Expand Down
6 changes: 5 additions & 1 deletion Source/codec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ void XorBlock(const uint32_t *shaResult, uint32_t *out)

std::size_t codec_decode(std::byte *pbSrcDst, std::size_t size, const char *pszPassword)
{
Log("codec_decode(pbSrcDst, {}, \"{}\")", size, pszPassword);
uint32_t buf[BlockSize];
uint32_t dst[SHA1HashSize];

Expand All @@ -112,17 +113,19 @@ std::size_t codec_decode(std::byte *pbSrcDst, std::size_t size, const char *pszP
memset(buf, 0, sizeof(buf));
const CodecSignature sig = GetCodecSignature(pbSrcDst);
if (sig.error > 0) {
Log("sig.error = {}", sig.error);
return 0;
}

SHA1Result(context, dst);
if (sig.checksum != dst[0]) {
LogError("Checksum mismatch signature={} vs calculated={}", sig.checksum, dst[0]);
Log("Checksum mismatch signature={} vs calculated={}", sig.checksum, dst[0]);
memset(dst, 0, sizeof(dst));
return 0;
}

size += sig.lastChunkSize - BlockSizeBytes;
Log("codec_decode OK");
return size;
}

Expand All @@ -135,6 +138,7 @@ std::size_t codec_get_encoded_len(std::size_t dwSrcBytes)

void codec_encode(std::byte *pbSrcDst, std::size_t size, std::size_t size64, const char *pszPassword)
{
Log("codec_encode(pbSrcDst, {}, {}, \"{}\")", size, size64, pszPassword);
uint32_t buf[BlockSize];
uint32_t tmp[SHA1HashSize];
uint32_t dst[SHA1HashSize];
Expand Down
1 change: 1 addition & 0 deletions Source/diablo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2888,6 +2888,7 @@ 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
10 changes: 5 additions & 5 deletions Source/engine/assets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ char *FindUnpackedMpqFile(char *relativePath)
return false;
path = relativePath - unpackedDir->size();
std::memcpy(path, unpackedDir->data(), unpackedDir->size());
Log("\t\tpath = {}", path);
//Log("\t\tpath = {}", path);
if (FileExists(path)) {
Log("\t\tFOUND!");
//Log("\t\tFOUND!");
return true;
}
path = nullptr;
Expand Down Expand Up @@ -78,7 +78,7 @@ bool FindMpqFile(std::string_view filename, MpqArchive **archive, uint32_t *file
#ifdef UNPACKED_MPQS
AssetRef FindAsset(std::string_view filename)
{
Log("UNPACKED_MPQS FindAsset {}", filename);
//Log("UNPACKED_MPQS FindAsset {}", filename);
AssetRef result;
if (filename.empty() || filename.back() == '\\')
return result;
Expand All @@ -93,7 +93,7 @@ AssetRef FindAsset(std::string_view filename)
std::replace(relativePath, pathEnd, '\\', '/');
#endif
// Absolute path:
Log("relativePath = {}", relativePath);
//Log("relativePath = {}", relativePath);
if (relativePath[0] == '/') {
if (FileExists(relativePath)) {
*BufCopy(result.path, std::string_view(relativePath, filename.size())) = '\0';
Expand All @@ -102,7 +102,7 @@ AssetRef FindAsset(std::string_view filename)
}

// Unpacked MPQ file:
Log("Calling FindUnpackedMpqFile(\"{}\")", relativePath);
//Log("Calling FindUnpackedMpqFile(\"{}\")", relativePath);
char *const unpackedMpqPath = FindUnpackedMpqFile(relativePath);
if (unpackedMpqPath != nullptr) {
*BufCopy(result.path, std::string_view(unpackedMpqPath, pathEnd - unpackedMpqPath)) = '\0';
Expand Down
4 changes: 2 additions & 2 deletions Source/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,9 @@ std::optional<std::string> FindUnpackedMpqData(const std::vector<std::string> &p
targetPath.clear();
targetPath.reserve(path.size() + mpqName.size() + 1);
targetPath.append(path).append(mpqName) += DirectorySeparator;
Log("Testing FindUnpackedMpqData {}", targetPath);
//Log("Testing FindUnpackedMpqData {}", targetPath);
if (FileExists(targetPath)) {
Log(" Found unpacked MPQ directory: {}", targetPath);
//Log(" Found unpacked MPQ directory: {}", targetPath);
return targetPath;
}
}
Expand Down
5 changes: 5 additions & 0 deletions Source/interfac.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ 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 @@ -351,6 +352,7 @@ void ShowProgress(interface_mode uMsg)
case WM_DIABNEXTLVL:
IncProgress();
if (!gbIsMultiplayer) {
Log("pfile_save_level()");
pfile_save_level();
} else {
DeltaSaveLevel();
Expand All @@ -361,12 +363,14 @@ 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 @@ -377,6 +381,7 @@ 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
29 changes: 28 additions & 1 deletion Source/loadsave.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,8 @@ 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 @@ -224,9 +226,12 @@ 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 @@ -1832,6 +1837,7 @@ 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 @@ -1841,6 +1847,7 @@ 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 @@ -1911,11 +1918,18 @@ 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);
if (!archive || !archive->HasFile(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);
GetPermLevelNames(szName);
}
LoadHelper file(std::move(archive), szName);
if (!file.IsValid())
app_fatal(_("Unable to open save file archive"));
Expand Down Expand Up @@ -2349,6 +2363,7 @@ void RemoveEmptyInventory(Player &player)

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

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

Log("pfile_remove_temp_files");
pfile_remove_temp_files();

setlevel = file.NextBool8();
Expand Down Expand Up @@ -2398,13 +2414,16 @@ 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 @@ -2413,6 +2432,7 @@ void LoadGame(bool firstflag)
RemoveEmptyInventory(myPlayer);
}

Log("LoadGameLevel");
LoadGameLevel(firstflag, ENTRY_LOAD);
SetPlrAnims(myPlayer);
SyncPlrAnim(myPlayer);
Expand All @@ -2429,6 +2449,7 @@ 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 @@ -2437,6 +2458,7 @@ 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 @@ -2612,20 +2634,23 @@ 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 @@ -2634,6 +2659,7 @@ 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 @@ -2799,6 +2825,7 @@ 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 0c20215

Please sign in to comment.