Skip to content

Commit

Permalink
Fix level id issues
Browse files Browse the repository at this point in the history
  • Loading branch information
Metalit committed Apr 28, 2024
1 parent 495ccc8 commit dbac4ea
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 11 deletions.
1 change: 1 addition & 0 deletions shared/Utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

namespace PlaylistCore {
namespace Utils {
bool CaseInsensitiveEquals(std::string const& a, std::string const& b);

GlobalNamespace::BeatmapLevel* GetLevelByID(std::string id);

Expand Down
12 changes: 12 additions & 0 deletions src/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,14 @@ MAKE_HOOK_MATCH(LevelCollectionNavigationController_DidActivate, &LevelCollectio
}
}

MAKE_HOOK(abort_hook, nullptr, void) {
auto logger = Paper::ConstLoggerContext("abort_hook");
logger.info("abort called");
logger.Backtrace(40);

abort_hook();
}

extern "C" void setup(CModInfo* info) {
info->id = "PlaylistCore";
info->version = VERSION;
Expand Down Expand Up @@ -264,6 +272,10 @@ extern "C" void late_load() {
auto managerCInfo = managerModInfo.to_c();
hasManager = modloader_require_mod(&managerCInfo, CMatchType::MatchType_IdOnly);

auto libc = dlopen("libc.so", RTLD_NOW);
auto abrt = dlsym(libc, "abort");
INSTALL_HOOK_DIRECT(logger, abort_hook, abrt);

INSTALL_HOOK_ORIG(logger, LevelCollectionViewController_SetData);
INSTALL_HOOK(logger, AnnotatedBeatmapLevelCollectionsGridView_OnEnable);
INSTALL_HOOK(logger, AnnotatedBeatmapLevelCollectionsGridViewAnimator_AnimateOpen);
Expand Down
14 changes: 6 additions & 8 deletions src/PlaylistCore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -331,13 +331,14 @@ namespace PlaylistCore {
// add all songs to the playlist object
std::vector<BeatmapLevel*> foundSongs;
for(auto itr = songs.begin(); itr != songs.end(); itr++) {
LOWER(itr->LevelID);
if(levelIds.contains(itr->LevelID))
itr = songs.erase(itr);
else {
levelIds.insert(itr->LevelID);
if(auto search = Utils::GetLevelByID(itr->LevelID))
foundSongs.emplace_back(search);
else
LOG_ERROR("level id {} not found", itr->LevelID);
}
}
// save removed duplicates
Expand Down Expand Up @@ -575,13 +576,12 @@ namespace PlaylistCore {
int songsMissing = 0;
for(auto& song : playlist->playlistJSON.Songs) {
std::string& id = song.LevelID;
LOWER(id);
bool hasSong = false;
// search in songs in playlist instead of all songs
// we need to treat the list as an array because it is initialized as an array elsewhere
ArrayW<BeatmapLevel*> levelList(playlist->playlistCS->beatmapLevels);
for(int i = 0; i < levelList.size(); i++) {
if(id == levelList[i]->levelID) {
if(CaseInsensitiveEquals(id, levelList[i]->levelID)) {
hasSong = true;
break;
}
Expand Down Expand Up @@ -672,8 +672,7 @@ namespace PlaylistCore {
// find song by id and remove
for(auto itr = json.Songs.begin(); itr != json.Songs.end(); ++itr) {
auto& song = *itr;
LOWER(song.LevelID);
if(song.LevelID == level->levelID) {
if(CaseInsensitiveEquals(song.LevelID, level->levelID)) {
json.Songs.erase(itr);
// only erase
break;
Expand Down Expand Up @@ -730,10 +729,9 @@ namespace PlaylistCore {
int replacedLevelIndex = -1;
for(int i = 0; i < songs.size(); i++) {
auto& song = songs[i];
LOWER(song.LevelID);
if(song.LevelID == level->levelID)
if(CaseInsensitiveEquals(song.LevelID, levelList[i]->levelID))
removeIndex = i;
if(song.LevelID == replacedLevelID)
if(CaseInsensitiveEquals(song.LevelID, replacedLevelID))
replacedLevelIndex = i;
if (removeIndex >= 0 && replacedLevelIndex >= 0)
break;
Expand Down
16 changes: 13 additions & 3 deletions src/Utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include "ResettableStaticPtr.hpp"

#include "songcore/shared/SongCore.hpp"
#include "bsml/shared/Helpers/getters.hpp"
#include "songcore/shared/SongLoader/RuntimeSongLoader.hpp"

#include "UnityEngine/ImageConversion.hpp"
#include "UnityEngine/GameObject.hpp"
Expand Down Expand Up @@ -35,11 +35,21 @@ namespace PlaylistCore {
// desired image size
const int imageSize = 512;

bool CaseInsensitiveEquals(std::string const& a, std::string const& b) {
if(a.size() != b.size())
return false;
for(int i = 0; i < a.size(); i++) {
if(tolower(a[i]) != tolower(b[i]))
return false;
}
return true;
}

GlobalNamespace::BeatmapLevel* GetLevelByID(std::string id) {
if(auto search = SongCore::API::Loading::GetLevelByLevelID(id))
return search;
else if(auto levels = BSML::Helpers::GetMainFlowCoordinator()->_beatmapLevelsModel)
return levels->GetBeatmapLevel(id);
else if(auto instance = SongCore::SongLoader::RuntimeSongLoader::get_instance())
return (*il2cpp_utils::GetFieldValue<GlobalNamespace::BeatmapLevelsModel*>(instance, "_beatmapLevelsModel"))->GetBeatmapLevel(id);
return nullptr;
}

Expand Down

0 comments on commit dbac4ea

Please sign in to comment.