Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Re-Implement Song Deletion #1288

Merged
merged 7 commits into from
Mar 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Themes/_fallback/metrics.ini
Original file line number Diff line number Diff line change
Expand Up @@ -2215,7 +2215,7 @@ LineGO="lua,GlobalOffsetSeconds()"
Fallback="ScreenOptionsServiceChild"
NextScreen="ScreenOptionsService"
PrevScreen="ScreenOptionsService"
LineNames="3,4,SI,SM,HN,SSR,14,30,OsuLifts,ReplayMods,PR,PackProgress,MinidumpUpload"
LineNames="3,4,SI,SM,HN,SSR,14,30,OsuLifts,ReplayMods,PR,PackProgress,MinidumpUpload,AllowSongDeletion"
#LineScore="lua,UserPrefScoringMode()"
Line3="lua,JudgeDifficulty()"
Line4="conf,LifeDifficulty"
Expand All @@ -2225,6 +2225,7 @@ LineHN="conf,MinTNSToHideNotes"
LineSSR="conf,SortBySSRNormPercent"
Line14="conf,EasterEggs"
Line30="conf,FastLoad"
LineAllowSongDeletion="conf,AllowSongDeletion"
#Line31="conf,FastLoadAdditionalSongs"
LinePR="conf,EnablePitchRates"
LineOsuLifts="conf,LiftsOnOsuHolds"
Expand Down
2 changes: 2 additions & 0 deletions src/Etterna/Screen/Options/ScreenOptionsMasterPrefs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -827,6 +827,8 @@ InitializeConfOptions()
ADD(ConfOption("PackProgressInWheel", MovePref<bool>, "Off", "On"));
ADD(ConfOption("EnableMinidumpUpload", MovePref<bool>, "Off", "On"));

ADD(ConfOption("AllowSongDeletion", MovePref<bool>, "Off", "On"));

// Machine options
ADD(ConfOption("TimingWindowScale",
TimingWindowScale,
Expand Down
66 changes: 62 additions & 4 deletions src/Etterna/Screen/Others/ScreenSelectMusic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@
#include "Etterna/Actor/Gameplay/Player.h"
#include "Etterna/Models/NoteData/NoteDataUtil.h"
#include "Etterna/Singletons/ReplayManager.h"
#include "Etterna/Screen/Others/ScreenPrompt.h"
#include <filesystem>

#include <Core/Platform/Platform.hpp>

#include <algorithm>

Expand All @@ -56,6 +60,7 @@ AutoScreenMessage(SM_SongChanged);
AutoScreenMessage(SM_SortOrderChanging);
AutoScreenMessage(SM_SortOrderChanged);
AutoScreenMessage(SM_BackFromPlayerOptions);
AutoScreenMessage(SM_ConfirmDeleteSong);
AutoScreenMessage(SM_BackFromNamePlaylist);
AutoScreenMessage(SM_BackFromCalcTestStuff);

Expand Down Expand Up @@ -387,10 +392,10 @@ void
ScreenSelectMusic::DifferentialReload()
{
// reload songs
SONGMAN->DifferentialReload();

SONGMAN->DifferentialReload();
if (IsTransitioning() || m_SelectionState == SelectionState_Finalized) {
return;
return;
}

const auto selSong = GAMESTATE->m_pCurSong;
Expand Down Expand Up @@ -473,6 +478,24 @@ ScreenSelectMusic::Input(const InputEventPlus& input)
m_MusicWheel.IsSettled() && input.type == IET_FIRST_PRESS) {
if (ReloadCurrentSong())
return true;
} else if (input.DeviceI.device == DEVICE_KEYBOARD && bHoldingCtrl &&
input.DeviceI.button == KEY_BACK &&
input.type == IET_FIRST_PRESS && m_MusicWheel.IsSettled()) {

// Keyboard shortcut to delete a song from disk (ctrl + backspace)
Song* songToDelete = m_MusicWheel.GetSelectedSong();

if (songToDelete && PREFSMAN->m_bAllowSongDeletion.Get()) {
m_pSongAwaitingDeletionConfirmation = songToDelete;

ScreenPrompt::Prompt(
SM_ConfirmDeleteSong,
ssprintf(PERMANENTLY_DELETE.GetValue(),
songToDelete->m_sMainTitle.c_str(),
songToDelete->GetSongDir().c_str()),
PROMPT_YES_NO);
return true;
}
} else if (holding_shift && bHoldingCtrl && c == 'P' &&
m_MusicWheel.IsSettled() && input.type == IET_FIRST_PRESS) {
if (ReloadCurrentPack())
Expand Down Expand Up @@ -978,7 +1001,16 @@ ScreenSelectMusic::HandleScreenMessage(const ScreenMessage& SM)
CodeDetector::RefreshCacheItems(CODES);
} else if (SM == SM_LoseFocus) {
CodeDetector::RefreshCacheItems(); // reset for other screens
} else if (SM == SM_BackFromCalcTestStuff) {
} else if (SM == SM_ConfirmDeleteSong) {
if (ScreenPrompt::s_LastAnswer == ANSWER_YES) {
OnConfirmSongDeletion();
} else {
// need to resume the song preview that was automatically paused
m_MusicWheel.ChangeMusic(0);
}
}
else if (SM == SM_BackFromCalcTestStuff)
{
auto ans = ScreenTextEntry::s_sLastAnswer;
std::vector<std::string> words;
std::istringstream iss(ans);
Expand Down Expand Up @@ -1103,6 +1135,32 @@ ScreenSelectMusic::HandleScreenMessage(const ScreenMessage& SM)
ScreenWithMenuElements::HandleScreenMessage(SM);
}

void
ScreenSelectMusic::OnConfirmSongDeletion()
{
Song* deletedSong = m_pSongAwaitingDeletionConfirmation;
if (!deletedSong) {
//Locator::getLogger()->warn("Attempted to delete a null song (ScreenSelectMusic::OnConfirmSongDeletion)");
return;
}

/* TODO: Make this platform independent */
const ghc::filesystem::path exeLocation = Core::Platform::getExecutableDirectory();
const std::string prefix = exeLocation.parent_path();
const std::filesystem::path songDir = std::filesystem::u8path(prefix + deletedSong->GetSongDir());


// flush the deleted song from any caches
SONGMAN->UnlistSong(deletedSong);
// refresh the song list
m_MusicWheel.ReloadSongList(false, "");
//Locator::getLogger()->trace("Deleting song: ", songDir.c_str());
// delete the song directory from disk

std::filesystem::remove_all(songDir);
m_pSongAwaitingDeletionConfirmation = NULL;
}

bool
ScreenSelectMusic::MenuStart(const InputEventPlus& input)
{
Expand Down
3 changes: 3 additions & 0 deletions src/Etterna/Screen/Others/ScreenSelectMusic.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ class ScreenSelectMusic : public ScreenWithMenuElements
void SwitchToPreferredDifficulty();
void AfterMusicChange();

Song* m_pSongAwaitingDeletionConfirmation;
void OnConfirmSongDeletion();

void CheckBackgroundRequests(bool bForce);
bool DetectCodes(const InputEventPlus& input);

Expand Down
1 change: 1 addition & 0 deletions src/Etterna/Singletons/PrefsManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ PrefsManager::PrefsManager()
, m_logging_level("LoggingLevel", 2)
, m_bEnableScoreboard("EnableScoreboard", true)
, m_bEnableCrashUpload("EnableMinidumpUpload", false)
, m_bAllowSongDeletion("AllowSongDeletion", false)
, m_bShowMinidumpUploadDialogue("ShowMinidumpUploadDialogue", true)

{
Expand Down
2 changes: 2 additions & 0 deletions src/Etterna/Singletons/PrefsManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,8 @@ class PrefsManager
Preference<bool> m_bEnableCrashUpload;
Preference<bool> m_bShowMinidumpUploadDialogue;

Preference<bool> m_bAllowSongDeletion;

void ReadPrefsFromIni(const IniFile& ini,
const std::string& sSection,
bool bIsStatic);
Expand Down
28 changes: 28 additions & 0 deletions src/Etterna/Singletons/SongManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -974,6 +974,11 @@ SongManager::FreeSongs()
m_pSongs.clear();
m_SongsByDir.clear();

// also free the songs that have been deleted from disk
for (unsigned i = 0; i < m_pDeletedSongs.size(); ++i)
SAFE_DELETE(m_pDeletedSongs[i]);
m_pDeletedSongs.clear();

m_mapSongGroupIndex.clear();
m_sSongGroupBannerPaths.clear();

Expand All @@ -984,6 +989,29 @@ SongManager::FreeSongs()
m_pPopularSongs.clear();
}

void
SongManager::UnlistSong(Song* song)
{
// cannot immediately free song data, as it is needed temporarily
// for smooth audio transitions, etc. Instead, remove it from the
// m_pSongs list and store it in a special place where it can safely
// be deleted later.
m_pDeletedSongs.emplace_back(song);

// remove all occurences of the song in each of our song vectors
vector<Song*>* songVectors[2] = { &m_pSongs,
&m_pPopularSongs};
for (int songVecIdx = 0; songVecIdx < 2; ++songVecIdx) {
vector<Song*>& v = *songVectors[songVecIdx];
for (size_t i = 0; i < v.size(); ++i) {
if (v[i] == song) {
v.erase(v.begin() + i);
--i;
}
}
}
}

auto
SongManager::IsGroupNeverCached(const std::string& group) const -> bool
{
Expand Down
6 changes: 6 additions & 0 deletions src/Etterna/Singletons/SongManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ class SongManager
void FreeSongs();
void Cleanup();

void UnlistSong(Song* song);


void Invalidate(const Song* pStaleSong);
static auto GetPlaylists() -> std::map<std::string, Playlist>&;
static void SaveEnabledSongsToPref();
Expand Down Expand Up @@ -179,6 +182,9 @@ class SongManager

std::set<std::string> m_GroupsToNeverCache;
/** @brief The most popular songs ranked by number of plays. */

std::vector<Song*> m_pDeletedSongs;

std::vector<Song*> m_pPopularSongs;

std::vector<std::string> m_sSongGroupNames;
Expand Down
Loading