Skip to content

Commit

Permalink
Refactoring & Cleanup of ViewController References
Browse files Browse the repository at this point in the history
  • Loading branch information
Tatenshi committed Aug 11, 2022
1 parent 0c0e1e6 commit c99c919
Show file tree
Hide file tree
Showing 10 changed files with 118 additions and 92 deletions.
22 changes: 22 additions & 0 deletions include/Impl/RandomSongImpl.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include "beatsaber-hook/shared/utils/typedefs-wrappers.hpp"

#include "GlobalNamespace/LevelCollectionNavigationController.hpp"
#include "GlobalNamespace/LevelSelectionNavigationController.hpp"
#include "GlobalNamespace/LevelFilteringNavigationController.hpp"
#include "GlobalNamespace/AnnotatedBeatmapLevelCollectionsViewController.hpp"
#include "GlobalNamespace/IBeatmapLevelCollection.hpp"
#include "GlobalNamespace/IBeatmapLevelPack.hpp"
#include "GlobalNamespace/IPreviewBeatmapLevel.hpp"
#include "GlobalNamespace/IAnnotatedBeatmapLevelCollection.hpp"
#include "GlobalNamespace/LevelSearchViewController.hpp"
#include "GlobalNamespace/LevelSearchViewController_BeatmapLevelPackCollection.hpp"

#include "logger.hpp"

namespace RandomSongImpl {

extern SafePtr<GlobalNamespace::LevelCollectionNavigationController> levelCollectionNavigationController;
extern SafePtr<GlobalNamespace::LevelFilteringNavigationController> filteringNavigationController;

void selectRandomSong();
}
File renamed without changes.
7 changes: 5 additions & 2 deletions include/SettingsVC.hpp → include/Settings/SettingsVC.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#pragma once

#include "HMUI/ViewController.hpp"
#include "questui/shared/BeatSaberUI.hpp"
#include "HMUI/Touchable.hpp"

#include "Settings/config.hpp"
#include "logger.hpp"

void DidActivate(HMUI::ViewController* self, bool firstActivation, bool addedToHierarchy, bool screenSystemEnabling);
File renamed without changes.
14 changes: 14 additions & 0 deletions include/logger.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#pragma once

#include "beatsaber-hook/shared/utils/logging.hpp"
#include "beatsaber-hook/shared/config/config-utils.hpp"

// Include the modloader header, which allows us to tell the modloader which mod this is, and the version etc.
#include "modloader/shared/modloader.hpp"

// Static Variables
extern ModInfo modInfo; // Stores the ID and version of our mod, and is sent to the modloader upon startup

// Define these functions here so that we can easily read configuration and log information from other files
Configuration& getConfig();
Logger& getLogger();
35 changes: 5 additions & 30 deletions include/main.hpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
#pragma once

// Include the modloader header, which allows us to tell the modloader which mod this is, and the version etc.
#include "modloader/shared/modloader.hpp"

// beatsaber-hook is a modding framework that lets us call functions and fetch field values from in the game
// It also allows creating objects, configuration, and importantly, hooking methods to modify their values
#include "beatsaber-hook/shared/utils/logging.hpp"
Expand All @@ -15,23 +12,12 @@
#include "GlobalNamespace/GameplaySetupViewController.hpp"
#include "GlobalNamespace/SoloFreePlayFlowCoordinator.hpp"
#include "GlobalNamespace/PartyFreePlayFlowCoordinator.hpp"
#include "GlobalNamespace/LevelCollectionNavigationController.hpp"
#include "GlobalNamespace/LevelSelectionNavigationController.hpp"
#include "GlobalNamespace/LevelFilteringNavigationController.hpp"
#include "GlobalNamespace/AnnotatedBeatmapLevelCollectionsViewController.hpp"
#include "GlobalNamespace/IBeatmapLevelCollection.hpp"
#include "GlobalNamespace/IBeatmapLevelPack.hpp"
#include "GlobalNamespace/IPreviewBeatmapLevel.hpp"
#include "GlobalNamespace/IAnnotatedBeatmapLevelCollection.hpp"
#include "GlobalNamespace/LevelSearchViewController.hpp"
#include "GlobalNamespace/LevelSearchViewController_BeatmapLevelPackCollection.hpp"
#include "GlobalNamespace/OculusVRHelper.hpp"
#include "GlobalNamespace/VRController.hpp"
#include "GlobalNamespace/OVRInput.hpp"
#include "GlobalNamespace/OVRInput_Button.hpp"
#include "HMUI/CurvedTextMeshPro.hpp"
#include "HMUI/ViewController_AnimationDirection.hpp"
#include "HMUI/Touchable.hpp"
#include "config-utils/shared/config-utils.hpp"
#include "UnityEngine/UI/Button.hpp"
#include "UnityEngine/Vector2.hpp"
Expand All @@ -43,21 +29,10 @@
#include <random>

// Icons
#include "icons.hpp"
#include "config.hpp"
#include "SettingsVC.hpp"

// Define these functions here so that we can easily read configuration and log information from other files
Configuration& getConfig();
Logger& getLogger();
#include "Impl/icons.hpp"
#include "Impl/RandomSongImpl.hpp"
#include "Settings/config.hpp"
#include "Settings/SettingsVC.hpp"

// Static Variables
static ModInfo modInfo; // Stores the ID and version of our mod, and is sent to the modloader upon startup

// TODO Look into deleting pointers when exiting instead of SafePtr, to avoid Memory Leak
static SafePtr<GlobalNamespace::LevelCollectionNavigationController> levelCollectionNavigationController;
static SafePtr<GlobalNamespace::LevelFilteringNavigationController> filteringNavigationController;
static UnityEngine::UI::Button *button;
static bool pressedEventAllreadyRun = false;

void selectRandomSong();
static bool pressedEventAllreadyRun = false;
44 changes: 44 additions & 0 deletions src/Impl/RandomSongImpl.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#include "Impl/RandomSongImpl.hpp"

namespace RandomSongImpl {

SafePtr<GlobalNamespace::LevelCollectionNavigationController> levelCollectionNavigationController;
SafePtr<GlobalNamespace::LevelFilteringNavigationController> filteringNavigationController;

void selectRandomSong()
{
// Only do Things, if we also have the necessary References
if (levelCollectionNavigationController && filteringNavigationController)
{
// Get Array of all beatmaps that the user currently sees
ArrayW<GlobalNamespace::IPreviewBeatmapLevel *> allmapsArray;

auto *levelPack = levelCollectionNavigationController->levelPack;
if (levelPack)
{
allmapsArray = il2cpp_utils::cast<GlobalNamespace::IAnnotatedBeatmapLevelCollection>(levelPack)->get_beatmapLevelCollection()->get_beatmapLevels();
getLogger().info("Acquired Maps using CollectionNavigationView");
}
else
{
allmapsArray = filteringNavigationController->levelSearchViewController->beatmapLevelPackCollection->get_beatmapLevelCollection()->get_beatmapLevels();
getLogger().info("Acquired Maps using SearchView");
}

// Calculate Upper Bound for rand
int max = allmapsArray->Length();
getLogger().info("Acquired BeatMapCount");

if (max > 0)
{
// Select a random level from 0 to max (exclusive)
levelCollectionNavigationController->SelectLevel(allmapsArray->get(rand() % max));
getLogger().info("Selected level");
}
else
{
getLogger().info("No level found to select");
}
}
}
}
4 changes: 2 additions & 2 deletions src/SettingsVC.cpp → src/Settings/SettingsVC.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "main.hpp"
#include "Settings/SettingsVC.hpp"

std::vector<std::string> buttonOptions = {
"None",
Expand All @@ -24,7 +24,7 @@ void DidActivate(HMUI::ViewController* self, bool firstActivation, bool addedToH
}

// Create actual Dropdown
QuestUI::BeatSaberUI::CreateDropdown(container->get_transform(), "ControllerButton Binding", "None", buttonOptionsWrapper, [](auto value) {
QuestUI::BeatSaberUI::CreateDropdown(container->get_transform(), "ControllerButton Binding", buttonOptionsWrapper[getModConfig().UseButton.GetValue()], buttonOptionsWrapper, [](auto value) {

// Find Index of selected Element
int index = std::find(buttonOptions.begin(), buttonOptions.end(), value) - buttonOptions.begin();
Expand Down
18 changes: 18 additions & 0 deletions src/logger.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include "logger.hpp"

ModInfo modInfo;

// Loads the config from disk using our modInfo, then returns it for use
Configuration &getConfig()
{
static Configuration config(modInfo);
config.Load();
return config;
}

// Returns a logger, useful for printing debug messages
Logger &getLogger()
{
static Logger *logger = new Logger(modInfo);
return *logger;
}
66 changes: 8 additions & 58 deletions src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,20 +1,5 @@
#include "main.hpp"

// Loads the config from disk using our modInfo, then returns it for use
Configuration &getConfig()
{
static Configuration config(modInfo);
config.Load();
return config;
}

// Returns a logger, useful for printing debug messages
Logger &getLogger()
{
static Logger *logger = new Logger(modInfo);
return *logger;
}

MAKE_HOOK_MATCH(PresentFlowCoordinatorHook, &HMUI::FlowCoordinator::PresentFlowCoordinator, void, HMUI::FlowCoordinator *self, HMUI::FlowCoordinator *flowCoordinator, System::Action *finishedCallback, HMUI::ViewController::AnimationDirection animationDirection, bool immediately, bool replaceTopViewController)
{
// Base Call
Expand All @@ -25,8 +10,8 @@ MAKE_HOOK_MATCH(PresentFlowCoordinatorHook, &HMUI::FlowCoordinator::PresentFlowC
{
// Save Controller for later use in button onClick
auto *LevelSelectionFlowCoordinatorInstance = (GlobalNamespace::LevelSelectionFlowCoordinator *)flowCoordinator;
levelCollectionNavigationController.emplace(LevelSelectionFlowCoordinatorInstance->levelSelectionNavigationController->levelCollectionNavigationController);
filteringNavigationController.emplace(LevelSelectionFlowCoordinatorInstance->levelSelectionNavigationController->levelFilteringNavigationController);
RandomSongImpl::levelCollectionNavigationController.emplace(LevelSelectionFlowCoordinatorInstance->levelSelectionNavigationController->levelCollectionNavigationController);
RandomSongImpl::filteringNavigationController.emplace(LevelSelectionFlowCoordinatorInstance->levelSelectionNavigationController->levelFilteringNavigationController);

if(button) {
button->get_gameObject()->set_active(true);
Expand All @@ -45,6 +30,10 @@ MAKE_HOOK_MATCH(PresentFlowCoordinatorHook, &HMUI::FlowCoordinator::PresentFlowC
else {
getLogger().info("Wanted to disable Random Song Button, but button was null");
}

// Either way we dont want to hold the old NavigationController references, so that they can be freed
RandomSongImpl::levelCollectionNavigationController.emplace(nullptr);
RandomSongImpl::filteringNavigationController.emplace(nullptr);
}
}

Expand All @@ -56,9 +45,7 @@ MAKE_HOOK_MATCH(GamePlaySetUpHook, &GlobalNamespace::GameplaySetupViewController
if (firstActivation)
{
// Create Randomselection Button
button = QuestUI::BeatSaberUI::CreateUIButton(self->get_transform(), "", UnityEngine::Vector2(56.0f, 0), UnityEngine::Vector2(11.0f, 11.0f), []() {
selectRandomSong();
});
button = QuestUI::BeatSaberUI::CreateUIButton(self->get_transform(), "", UnityEngine::Vector2(56.0f, 0), UnityEngine::Vector2(11.0f, 11.0f), RandomSongImpl::selectRandomSong);

// Set Icon of the Button and scale it to fit the Button
auto *image = QuestUI::BeatSaberUI::CreateImage(button->get_transform(), QuestUI::BeatSaberUI::Base64ToSprite(diceIcon));
Expand Down Expand Up @@ -86,7 +73,7 @@ MAKE_HOOK_MATCH(FixedUpdateHook, &GlobalNamespace::OculusVRHelper::FixedUpdate,
bool buttonPressed = GlobalNamespace::OVRInput::Get(useButtonValue, controllerIndex);
if(buttonPressed){
if(!pressedEventAllreadyRun) {
selectRandomSong();
RandomSongImpl::selectRandomSong();
pressedEventAllreadyRun = true;
}
}
Expand All @@ -96,43 +83,6 @@ MAKE_HOOK_MATCH(FixedUpdateHook, &GlobalNamespace::OculusVRHelper::FixedUpdate,
}
}

// TODO Extract to own Class & File
void selectRandomSong() {
// Only do Things, if we also have the necessary References
if (levelCollectionNavigationController && filteringNavigationController)
{
// Get Array of all beatmaps that the user currently sees
ArrayW<GlobalNamespace::IPreviewBeatmapLevel *> allmapsArray;

auto *levelPack = levelCollectionNavigationController->levelPack;
if (levelPack)
{
allmapsArray = il2cpp_utils::cast<GlobalNamespace::IAnnotatedBeatmapLevelCollection>(levelPack)->get_beatmapLevelCollection()->get_beatmapLevels();
getLogger().info("Acquired Maps using CollectionNavigationView");
}
else
{
allmapsArray = filteringNavigationController->levelSearchViewController->beatmapLevelPackCollection->get_beatmapLevelCollection()->get_beatmapLevels();
getLogger().info("Acquired Maps using SearchView");
}

// Calculate Upper Bound for rand
int max = allmapsArray->Length();
getLogger().info("Acquired BeatMapCount");

if (max > 0)
{
// Select a random level from 0 to max (exclusive)
levelCollectionNavigationController->SelectLevel(allmapsArray->get(rand() % max));
getLogger().info("Selected level");
}
else
{
getLogger().info("No level found to select");
}
}
}

// Define Config
DEFINE_CONFIG(ModConfig);

Expand Down

0 comments on commit c99c919

Please sign in to comment.