From c1e3ab59aa9dde547dd2087cf9382ee7bc996eb1 Mon Sep 17 00:00:00 2001 From: DaymareOn Date: Tue, 8 Mar 2022 00:23:06 +0100 Subject: [PATCH] fix!: stop corrupting saves (#19) * fix!: stop corrupting saves - This was seen only on AE. Our code for saving DHDT data through SKSE serialization for AE was faulty. We don't yet understand why, so our solution is to implement our own saves for DHDT. - No save files for non-DHDT users Co-authored-by: SesamePaste233 <61174335+SesamePaste233@users.noreply.github.com> --- hdtSMP64/main.cpp | 38 +++++++++++++++++++++++++++----------- 1 file changed, 27 insertions(+), 11 deletions(-) diff --git a/hdtSMP64/main.cpp b/hdtSMP64/main.cpp index 8a678e7..cdd847b 100644 --- a/hdtSMP64/main.cpp +++ b/hdtSMP64/main.cpp @@ -592,6 +592,8 @@ extern "C" { mm->MenuOpenCloseEventDispatcher()->AddEventSink(&hdt::g_freezeEventHandler); hdt::checkOldPlugins(); hdt::loadConfig(); + +// I think we only have _DEBUG now... #ifdef DEBUG hdt::g_armorAttachEventDispatcher.addListener(&hdt::g_eventDebugLogger); GetEventDispatcherList()->unk1B8.AddEventSink(&hdt::g_eventDebugLogger); @@ -599,19 +601,33 @@ extern "C" { #endif } - }); - } - - const SKSESerializationInterface* srlz_intfc = reinterpret_cast(skse->QueryInterface(kInterface_Serialization)); - if (srlz_intfc) { - //Initialize all Serializer Module - hdt::Override::OverrideManager::GetSingleton(); - - srlz_intfc->SetUniqueID(hdt::g_PluginHandle, 'FHDT'); + // If we receive a SaveGame message, we serialize our data and save it in our dedicated save files. + if (msg && msg->type == SKSEMessagingInterface::kMessage_SaveGame) + { + auto data = hdt::Override::OverrideManager::GetSingleton()->Serialize(); + if (!data.str().empty()) { + std::string save_name = reinterpret_cast(msg->data); + std::ofstream ofs(OVERRIDE_SAVE_PATH + save_name + ".dhdt", std::ios::out); + if(ofs && ofs.is_open()) + ofs << data.str(); + } + } - srlz_intfc->SetSaveCallback(hdt::g_PluginHandle, hdt::SerializerBase::Save); + // If we receive a PreLoadGame message, we take our data in our dedicated save files and deserialize it. + if (msg && msg->type == SKSEMessagingInterface::kMessage_PreLoadGame) + { + std::string save_name = reinterpret_cast(msg->data); + save_name = save_name.substr(0, save_name.find_last_of(".")); - srlz_intfc->SetLoadCallback(hdt::g_PluginHandle, hdt::SerializerBase::Load); + std::ifstream ifs(OVERRIDE_SAVE_PATH + save_name + ".dhdt", std::ios::in); + if (ifs && ifs.is_open()) + { + std::stringstream data; + data << ifs.rdbuf(); + hdt::Override::OverrideManager::GetSingleton()->Deserialize(data); + } + } + }); } ObScriptCommand* hijackedCommand = nullptr;