Skip to content

Commit

Permalink
remember last song
Browse files Browse the repository at this point in the history
Signed-off-by: Erymanthus[#5074] | (u/)RayDeeUx <[email protected]>
  • Loading branch information
RayDeeUx committed Sep 4, 2024
1 parent 72acfbe commit 9ba2846
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 4 deletions.
2 changes: 1 addition & 1 deletion mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"type": "bool",
"default": false
},
"saveSongOnExit": {
"saveSongOnGameClose": {
"name": "Remember Last Menu Loop",
"description": "<cl>Original idea by HexCodesGMD.</c>\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",
Expand Down
8 changes: 8 additions & 0 deletions src/GameManager.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "SongManager.hpp"
#include "Utils.hpp"
#include <Geode/modify/GameManager.hpp>

using namespace geode::prelude;
Expand All @@ -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<std::string>("lastMenuLoop", currentSong);

GameManager::encodeDataTo(p0);
}
};
12 changes: 12 additions & 0 deletions src/SongManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ std::string SongManager::getCurrentSong() {
return m_currentSong;
}

void SongManager::setCurrentSongToSavedSong() {
m_currentSong = geode::Mod::get()->getSavedValue<std::string>("lastMenuLoop");
}

bool SongManager::isOriginalMenuLoop() {
return m_isMenuLoop;
}
Expand All @@ -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;
}
4 changes: 4 additions & 0 deletions src/SongManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,16 @@ class SongManager {
void pickRandomSong();
bool isOriginalMenuLoop();
std::string getCurrentSong();
void setCurrentSongToSavedSong();
void update(float);
bool getCalledOnce();
void setCalledOnce(bool);

private:
SongManager();
SongManager(const SongManager &) = delete;
std::vector<std::string> m_songs;
std::string m_currentSong;
bool m_isMenuLoop;
bool m_calledOnce = false;
};
9 changes: 7 additions & 2 deletions src/Utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string>("lastMenuLoop"), true, 1.0f, 1);
SongManager::get().setCalledOnce(true);
}
}

// create notif card stuff
Expand Down
8 changes: 7 additions & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,13 @@ void populateVector(bool customSongs) {
$on_mod(Loaded) {
populateVector(Utils::getBool("useCustomSongs"));

songManager.pickRandomSong();
std::string lastMenuLoop = Mod::get()->getSavedValue<std::string>("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)");
Expand Down

0 comments on commit 9ba2846

Please sign in to comment.