diff --git a/mod.json b/mod.json index 4db390e..9964f3d 100644 --- a/mod.json +++ b/mod.json @@ -29,7 +29,7 @@ "type": "bool", "default": false }, - "saveSongOnExit": { + "saveSongOnGameClose": { "name": "Remember Last Menu Loop", "description": "Original idea by HexCodesGMD.\n\nIf enabled, this mod's most recently chosen menu loop will be the first loop you hear the next time you open Geometry Dash.", "type": "bool", diff --git a/src/GameManager.cpp b/src/GameManager.cpp index 781d562..93455eb 100644 --- a/src/GameManager.cpp +++ b/src/GameManager.cpp @@ -1,4 +1,5 @@ #include "SongManager.hpp" +#include "Utils.hpp" #include using namespace geode::prelude; @@ -10,4 +11,11 @@ class $modify(MenuLoopGMHook, GameManager) { gd::string getMenuMusicFile() { return m_fields->songManager.getCurrentSong(); } + void encodeDataTo(DS_Dictionary* p0) { + std::string currentSong = m_fields->songManager.getCurrentSong(); + log::debug("Game close detected. Most recent songManager song: {}", currentSong); + Mod::get()->setSavedValue("lastMenuLoop", currentSong); + + GameManager::encodeDataTo(p0); + } }; \ No newline at end of file diff --git a/src/SongManager.cpp b/src/SongManager.cpp index 3f3b067..b5b807d 100644 --- a/src/SongManager.cpp +++ b/src/SongManager.cpp @@ -35,6 +35,10 @@ std::string SongManager::getCurrentSong() { return m_currentSong; } +void SongManager::setCurrentSongToSavedSong() { + m_currentSong = geode::Mod::get()->getSavedValue("lastMenuLoop"); +} + bool SongManager::isOriginalMenuLoop() { return m_isMenuLoop; } @@ -54,3 +58,11 @@ void SongManager::update(float dt) { if (Utils::getBool("enableNotification")) Utils::generateNotification(); } + +void SongManager::setCalledOnce(bool value) { + m_calledOnce = value; +} + +bool SongManager::getCalledOnce() { + return m_calledOnce; +} diff --git a/src/SongManager.hpp b/src/SongManager.hpp index 8bc4c61..a5817d9 100644 --- a/src/SongManager.hpp +++ b/src/SongManager.hpp @@ -13,7 +13,10 @@ class SongManager { void pickRandomSong(); bool isOriginalMenuLoop(); std::string getCurrentSong(); + void setCurrentSongToSavedSong(); void update(float); + bool getCalledOnce(); + void setCalledOnce(bool); private: SongManager(); @@ -21,4 +24,5 @@ class SongManager { std::vector m_songs; std::string m_currentSong; bool m_isMenuLoop; + bool m_calledOnce = false; }; diff --git a/src/Utils.cpp b/src/Utils.cpp index b4d391e..6511dc4 100644 --- a/src/Utils.cpp +++ b/src/Utils.cpp @@ -55,10 +55,15 @@ void Utils::playlistModeNewSong() { return Utils::setNewSong(); } geode::log::info("attempting to hijack menuloop channel to use playlist mode"); - FMODAudioEngine::sharedEngine()->m_backgroundMusicChannel->stop(); + auto fmod = FMODAudioEngine::sharedEngine(); + fmod->m_backgroundMusicChannel->stop(); SongManager::get().pickRandomSong(); geode::log::info("is it over?"); - FMODAudioEngine::get()->playMusic(SongManager::get().getCurrentSong(), true, 1.0f, 1); + if (SongManager::get().getCalledOnce()) fmod->playMusic(SongManager::get().getCurrentSong(), true, 1.0f, 1); + else { + fmod->playMusic(geode::Mod::get()->getSavedValue("lastMenuLoop"), true, 1.0f, 1); + SongManager::get().setCalledOnce(true); + } } // create notif card stuff diff --git a/src/main.cpp b/src/main.cpp index cccf797..203fe63 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -73,7 +73,13 @@ void populateVector(bool customSongs) { $on_mod(Loaded) { populateVector(Utils::getBool("useCustomSongs")); - songManager.pickRandomSong(); + std::string lastMenuLoop = Mod::get()->getSavedValue("lastMenuLoop"); + bool saveSongOnGameClose = Utils::getBool("saveSongOnGameClose"); + bool loopExists = std::filesystem::exists(lastMenuLoop); + log::debug("\n=== 'REMEMBER LAST MENU LOOP' DEBUG INFO ===\nlast menu loop: {}\n'saveSongOnGameClose' setting: {}\nloopExists: {}", lastMenuLoop, saveSongOnGameClose, loopExists); + if (!lastMenuLoop.empty() && Utils::isSupportedExtension(lastMenuLoop) && loopExists && saveSongOnGameClose) { + songManager.setCurrentSongToSavedSong(); + } else songManager.pickRandomSong(); if (!std::filesystem::exists(configDir / R"(store_your_disabled_menuloops_here)")) { std::filesystem::create_directory(configDir / R"(store_your_disabled_menuloops_here)");