Skip to content

Commit

Permalink
Use Zenject whenever possible
Browse files Browse the repository at this point in the history
  • Loading branch information
Meivyn committed Jun 20, 2024
1 parent 54f345d commit 72a32e4
Show file tree
Hide file tree
Showing 11 changed files with 92 additions and 42 deletions.
14 changes: 9 additions & 5 deletions PlaylistManager/Plugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@
using IPA.Config.Stores;
using PlaylistManager.Installers;
using SiraUtil.Zenject;
using System.Reflection;
using IPA.Loader;
using IPALogger = IPA.Logging.Logger;

namespace PlaylistManager
{
[Plugin(RuntimeOptions.DynamicInit)]
public class Plugin
{
private readonly PluginMetadata _metadata;
private readonly Harmony _harmony;

internal static Plugin Instance { get; private set; }
internal static IPALogger Log { get; private set; }

Expand All @@ -24,11 +27,12 @@ public class Plugin
/// [Init] methods that use a Constructor or called before regular methods like InitWithConfig.
/// Only use [Init] with one Constructor.
/// </summary>
public Plugin(IPALogger logger, Zenjector zenjector)
public Plugin(IPALogger logger, PluginMetadata metadata, Zenjector zenjector)
{
Instance = this;
Log = logger;
harmony = new Harmony(HarmonyId);
_metadata = metadata;
_harmony = new Harmony(HarmonyId);
zenjector.UseMetadataBinder<Plugin>();
zenjector.UseHttpService();
zenjector.UseSiraSync(SiraUtil.Web.SiraSync.SiraSyncServiceType.GitHub, "rithik-b");
Expand All @@ -49,13 +53,13 @@ public void InitWithConfig(Config conf)
[OnEnable]
public void OnEnable()
{
harmony.PatchAll(Assembly.GetExecutingAssembly());
_harmony.PatchAll(_metadata.Assembly);
}

[OnDisable]
public void OnDisable()
{
harmony.UnpatchSelf();
_harmony.UnpatchSelf();
}
}
}
12 changes: 8 additions & 4 deletions PlaylistManager/UI/RefreshButtonUI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,23 @@ namespace PlaylistManager.UI
{
public class RefreshButtonUI : IInitializable, IDisposable
{
private readonly Loader _loader;
private readonly ProgressBar _progressBar;
private readonly MenuButtons _menuButtons;

private MenuButton refreshButton;

private RefreshButtonUI(ProgressBar progressBar)
private RefreshButtonUI(Loader loader, ProgressBar progressBar, MenuButtons menuButtons)
{
_loader = loader;
_progressBar = progressBar;
_menuButtons = menuButtons;
}

public void Initialize()
{
refreshButton = new MenuButton("Refresh Playlists", "Refresh Songs & Playlists", RefreshButtonPressed);
MenuButtons.instance.RegisterButton(refreshButton);
_menuButtons.RegisterButton(refreshButton);
Loader.SongsLoadedEvent += SongsLoaded;
}

Expand All @@ -34,15 +38,15 @@ private void SongsLoaded(Loader _, System.Collections.Concurrent.ConcurrentDicti

public void Dispose()
{
MenuButtons.instance.UnregisterButton(refreshButton);
_menuButtons.UnregisterButton(refreshButton);
Loader.SongsLoadedEvent -= SongsLoaded;
}

private void RefreshButtonPressed()
{
if (!Loader.AreSongsLoading)
{
Loader.Instance.RefreshSongs(fullRefresh: false);
_loader.RefreshSongs(fullRefresh: false);
}
}
}
Expand Down
11 changes: 8 additions & 3 deletions PlaylistManager/UI/ViewControllers/AddPlaylistModalController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,17 @@
using System.IO;
using System.ComponentModel;
using System.Collections.Generic;
using PlaylistManager.Types;
using IPA.Loader;
using SiraUtil.Zenject;

namespace PlaylistManager.UI
{
public class AddPlaylistModalController : INotifyPropertyChanged
{
private readonly StandardLevelDetailViewController standardLevelDetailViewController;
private readonly PopupModalsController popupModalsController;
private readonly PluginMetadata pluginMetadata;
private readonly BSMLParser bsmlParser;

private BeatSaberPlaylistsLib.PlaylistManager parentManager;
private List<BeatSaberPlaylistsLib.PlaylistManager> childManagers;
Expand Down Expand Up @@ -56,10 +59,12 @@ public class AddPlaylistModalController : INotifyPropertyChanged
[UIParams]
private readonly BSMLParserParams parserParams;

public AddPlaylistModalController(StandardLevelDetailViewController standardLevelDetailViewController, PopupModalsController popupModalsController)
public AddPlaylistModalController(StandardLevelDetailViewController standardLevelDetailViewController, PopupModalsController popupModalsController, UBinder<Plugin, PluginMetadata> pluginMetadata, BSMLParser bsmlParser)
{
this.standardLevelDetailViewController = standardLevelDetailViewController;
this.popupModalsController = popupModalsController;
this.pluginMetadata = pluginMetadata.Value;
this.bsmlParser = bsmlParser;
folderIcon = BeatSaberMarkupLanguage.Utilities.FindSpriteInAssembly("PlaylistManager.Icons.FolderIcon.png");
parsed = false;
}
Expand All @@ -68,7 +73,7 @@ private void Parse()
{
if (!parsed)
{
BSMLParser.instance.Parse(BeatSaberMarkupLanguage.Utilities.GetResourceContent(Assembly.GetExecutingAssembly(), "PlaylistManager.UI.Views.AddPlaylistModal.bsml"), standardLevelDetailViewController.transform.Find("LevelDetail").gameObject, this);
bsmlParser.Parse(BeatSaberMarkupLanguage.Utilities.GetResourceContent(pluginMetadata.Assembly, "PlaylistManager.UI.Views.AddPlaylistModal.bsml"), standardLevelDetailViewController._standardLevelDetailView.gameObject, this);
modalPosition = modalTransform.localPosition;
createModalPosition = createModalTransform.localPosition;
}
Expand Down
13 changes: 9 additions & 4 deletions PlaylistManager/UI/ViewControllers/FoldersViewController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Reflection;
using IPA.Loader;
using PlaylistManager.Types;
using SiraUtil.Zenject;
using UnityEngine;
using UnityEngine.UI;
using Zenject;
Expand All @@ -26,7 +27,9 @@ public class FoldersViewController : IInitializable, IDisposable, INotifyPropert
private readonly LevelSelectionNavigationController levelSelectionNavigationController;
private readonly PopupModalsController popupModalsController;
private readonly HoverHintController hoverHintController;
private BeatmapLevelsModel beatmapLevelsModel;
private readonly BeatmapLevelsModel beatmapLevelsModel;
private readonly PluginMetadata pluginMetadata;
private readonly BSMLParser bsmlParser;

private FloatingScreen floatingScreen;
private readonly Sprite levelPacksSprite;
Expand Down Expand Up @@ -71,14 +74,16 @@ private set

public FoldersViewController(AnnotatedBeatmapLevelCollectionsViewController annotatedBeatmapLevelCollectionsViewController, MainFlowCoordinator mainFlowCoordinator,
LevelSelectionNavigationController levelSelectionNavigationController, PopupModalsController popupModalsController, HoverHintController hoverHintController,
BeatmapLevelsModel beatmapLevelsModel)
BeatmapLevelsModel beatmapLevelsModel, UBinder<Plugin, PluginMetadata> pluginMetadata, BSMLParser bsmlParser)
{
this.annotatedBeatmapLevelCollectionsViewController = annotatedBeatmapLevelCollectionsViewController;
this.mainFlowCoordinator = mainFlowCoordinator;
this.levelSelectionNavigationController = levelSelectionNavigationController;
this.popupModalsController = popupModalsController;
this.hoverHintController = hoverHintController;
this.beatmapLevelsModel = beatmapLevelsModel;
this.pluginMetadata = pluginMetadata.Value;
this.bsmlParser = bsmlParser;

levelPacksSprite = BeatSaberMarkupLanguage.Utilities.FindSpriteInAssembly("PlaylistManager.Icons.LevelPacks.png");
customPacksSprite = BeatSaberMarkupLanguage.Utilities.FindSpriteInAssembly("PlaylistManager.Icons.CustomPacks.png");
Expand All @@ -97,7 +102,7 @@ public void Initialize()
transform.eulerAngles = new Vector3(60, 0, 0);
transform.localScale = new Vector3(0.03f, 0.03f, 0.03f);

BSMLParser.instance.Parse(BeatSaberMarkupLanguage.Utilities.GetResourceContent(Assembly.GetExecutingAssembly(), "PlaylistManager.UI.Views.FoldersView.bsml"), floatingScreen.gameObject, this);
bsmlParser.Parse(BeatSaberMarkupLanguage.Utilities.GetResourceContent(pluginMetadata.Assembly, "PlaylistManager.UI.Views.FoldersView.bsml"), floatingScreen.gameObject, this);
LevelFilteringNavigationController_ShowPacksInChildController.AllPacksViewSelectedEvent += LevelFilteringNavigationController_ShowPacksInChildController_AllPacksViewSelectedEvent;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using IPA.Loader;
using SiraUtil.Zenject;
using UnityEngine;
using static BeatSaberMarkupLanguage.Components.CustomListTableData;

Expand All @@ -20,6 +21,8 @@ public class ImageSelectionModalController : NotifiableBase
{
private readonly LevelPackDetailViewController levelPackDetailViewController;
private readonly PopupModalsController popupModalsController;
private readonly PluginMetadata pluginMetadata;
private readonly BSMLParser bsmlParser;

private readonly string IMAGES_PATH = Path.Combine(PlaylistLibUtils.playlistManager.PlaylistPath, "CoverImages");
private readonly Sprite playlistManagerIcon;
Expand All @@ -43,10 +46,12 @@ public class ImageSelectionModalController : NotifiableBase
[UIParams]
private readonly BSMLParserParams parserParams;

public ImageSelectionModalController(LevelPackDetailViewController levelPackDetailViewController, PopupModalsController popupModalsController)
public ImageSelectionModalController(LevelPackDetailViewController levelPackDetailViewController, PopupModalsController popupModalsController, UBinder<Plugin, PluginMetadata> pluginMetadata, BSMLParser bsmlParser)
{
this.levelPackDetailViewController = levelPackDetailViewController;
this.popupModalsController = popupModalsController;
this.pluginMetadata = pluginMetadata.Value;
this.bsmlParser = bsmlParser;

// Have to do this in case directory perms are not given
try
Expand All @@ -68,7 +73,7 @@ private void Parse()
{
if (!parsed)
{
BSMLParser.instance.Parse(BeatSaberMarkupLanguage.Utilities.GetResourceContent(Assembly.GetExecutingAssembly(), "PlaylistManager.UI.Views.ImageSelectionModal.bsml"), levelPackDetailViewController.transform.Find("Detail").gameObject, this);
bsmlParser.Parse(BeatSaberMarkupLanguage.Utilities.GetResourceContent(pluginMetadata.Assembly, "PlaylistManager.UI.Views.ImageSelectionModal.bsml"), levelPackDetailViewController._detailWrapper.gameObject, this);
modalPosition = modalTransform.position;
}
modalTransform.position = modalPosition;
Expand Down Expand Up @@ -175,7 +180,7 @@ private void ChangeImage()
}
else if (selectedIndex == 1)
{
using (var imageStream = Assembly.GetExecutingAssembly().GetManifestResourceStream("PlaylistManager.Icons.DefaultIcon.png"))
using (var imageStream = pluginMetadata.Assembly.GetManifestResourceStream("PlaylistManager.Icons.DefaultIcon.png"))
{
var imageBytes = new byte[imageStream.Length];
imageStream.Read(imageBytes, 0, (int)imageStream.Length);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
using System.ComponentModel;
using PlaylistManager.Utilities;
using System;
using IPA.Loader;
using PlaylistManager.Configuration;
using SiraUtil.Zenject;

namespace PlaylistManager.UI
{
Expand All @@ -20,6 +22,8 @@ public class LevelDetailButtonsViewController : IInitializable, IDisposable, IPr
private readonly AddPlaylistModalController addPlaylistController;
private readonly PopupModalsController popupModalsController;
private readonly DifficultyHighlighter difficultyHighlighter;
private readonly PluginMetadata pluginMetadata;
private readonly BSMLParser bsmlParser;

public event PropertyChangedEventHandler PropertyChanged;
private BeatmapLevel selectedBeatmapLevel;
Expand All @@ -33,19 +37,21 @@ public class LevelDetailButtonsViewController : IInitializable, IDisposable, IPr
private RectTransform rootTransform;

public LevelDetailButtonsViewController(StandardLevelDetailViewController standardLevelDetailViewController, LevelCollectionViewController levelCollectionViewController, LevelCollectionNavigationController levelCollectionNavigationController,
AddPlaylistModalController addPlaylistController, PopupModalsController popupModalsController, DifficultyHighlighter difficultyHighlighter)
AddPlaylistModalController addPlaylistController, PopupModalsController popupModalsController, DifficultyHighlighter difficultyHighlighter, UBinder<Plugin, PluginMetadata> pluginMetadata, BSMLParser bsmlParser)
{
this.standardLevelDetailViewController = standardLevelDetailViewController;
levelCollectionTableView = levelCollectionViewController._levelCollectionTableView;
this.levelCollectionNavigationController = levelCollectionNavigationController;
this.addPlaylistController = addPlaylistController;
this.popupModalsController = popupModalsController;
this.difficultyHighlighter = difficultyHighlighter;
this.pluginMetadata = pluginMetadata.Value;
this.bsmlParser = bsmlParser;
}

public void Initialize()
{
BSMLParser.instance.Parse(BeatSaberMarkupLanguage.Utilities.GetResourceContent(Assembly.GetExecutingAssembly(), "PlaylistManager.UI.Views.LevelDetailButtonsView.bsml"), standardLevelDetailViewController.transform.Find("LevelDetail").gameObject, this);
bsmlParser.Parse(BeatSaberMarkupLanguage.Utilities.GetResourceContent(pluginMetadata.Assembly, "PlaylistManager.UI.Views.LevelDetailButtonsView.bsml"), standardLevelDetailViewController._standardLevelDetailView.gameObject, this);
rootTransform.transform.localScale *= 0.7f;
AddActive = false;
difficultyHighlighter.selectedDifficultyChanged += DifficultyHighlighter_selectedDifficultyChanged;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using IPA.Loader;
using PlaylistManager.Downloaders;
using SiraUtil.Zenject;
using UnityEngine;
using Zenject;

Expand All @@ -27,7 +28,9 @@ public class PlaylistDetailViewButtonsController : IInitializable, IDisposable,
private readonly LevelPackDetailViewController levelPackDetailViewController;
private readonly PopupModalsController popupModalsController;
private readonly PlaylistDetailsViewController playlistDetailsViewController;
private AnnotatedBeatmapLevelCollectionsViewController annotatedBeatmapLevelCollectionsViewController;
private readonly AnnotatedBeatmapLevelCollectionsViewController annotatedBeatmapLevelCollectionsViewController;
private readonly PluginMetadata pluginMetadata;
private readonly BSMLParser bsmlParser;

private IPlaylist selectedPlaylist;
private BeatSaberPlaylistsLib.PlaylistManager parentManager;
Expand All @@ -44,19 +47,21 @@ public class PlaylistDetailViewButtonsController : IInitializable, IDisposable,
private readonly Transform syncButtonTransform;

internal PlaylistDetailViewButtonsController(IHttpService siraHttpService, PlaylistSequentialDownloader playlistDownloader, LevelPackDetailViewController levelPackDetailViewController,
PopupModalsController popupModalsController, PlaylistDetailsViewController playlistDetailsViewController, AnnotatedBeatmapLevelCollectionsViewController annotatedBeatmapLevelCollectionsViewController)
PopupModalsController popupModalsController, PlaylistDetailsViewController playlistDetailsViewController, AnnotatedBeatmapLevelCollectionsViewController annotatedBeatmapLevelCollectionsViewController, UBinder<Plugin, PluginMetadata> pluginMetadata, BSMLParser bsmlParser)
{
this.siraHttpService = siraHttpService;
this.playlistDownloader = playlistDownloader;
this.levelPackDetailViewController = levelPackDetailViewController;
this.popupModalsController = popupModalsController;
this.playlistDetailsViewController = playlistDetailsViewController;
this.annotatedBeatmapLevelCollectionsViewController = annotatedBeatmapLevelCollectionsViewController;
this.pluginMetadata = pluginMetadata.Value;
this.bsmlParser = bsmlParser;
}

public void Initialize()
{
BSMLParser.instance.Parse(BeatSaberMarkupLanguage.Utilities.GetResourceContent(Assembly.GetExecutingAssembly(), "PlaylistManager.UI.Views.PlaylistDetailViewButtons.bsml"), levelPackDetailViewController.transform.Find("Detail").gameObject, this);
bsmlParser.Parse(BeatSaberMarkupLanguage.Utilities.GetResourceContent(pluginMetadata.Assembly, "PlaylistManager.UI.Views.PlaylistDetailViewButtons.bsml"), levelPackDetailViewController._detailWrapper.gameObject, this);
syncButtonTransform.transform.localScale *= 0.6f;
syncButtonTransform.gameObject.SetActive(false);
rootTransform.gameObject.SetActive(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
using System;
using System.ComponentModel;
using System.IO;
using System.Reflection;
using IPA.Loader;
using SiraUtil.Zenject;
using UnityEngine;
using Zenject;

Expand All @@ -22,6 +23,8 @@ public class PlaylistDetailsViewController : IInitializable, IDisposable, ILevel
private readonly LevelPackDetailViewController levelPackDetailViewController;
private readonly ImageSelectionModalController imageSelectionModalController;
private readonly PopupModalsController popupModalsController;
private readonly PluginMetadata pluginMetadata;
private readonly BSMLParser bsmlParser;

private bool parsed;
private IPlaylist selectedPlaylist;
Expand Down Expand Up @@ -49,11 +52,13 @@ public class PlaylistDetailsViewController : IInitializable, IDisposable, ILevel
private readonly BSMLParserParams parserParams;

public PlaylistDetailsViewController(LevelPackDetailViewController levelPackDetailViewController, ImageSelectionModalController imageSelectionModalController,
PopupModalsController popupModalsController)
PopupModalsController popupModalsController, UBinder<Plugin, PluginMetadata> pluginMetadata, BSMLParser bsmlParser)
{
this.levelPackDetailViewController = levelPackDetailViewController;
this.imageSelectionModalController = imageSelectionModalController;
this.popupModalsController = popupModalsController;
this.pluginMetadata = pluginMetadata.Value;
this.bsmlParser = bsmlParser;
parsed = false;
}

Expand All @@ -76,7 +81,7 @@ private void Parse()
{
if (!parsed)
{
BSMLParser.instance.Parse(BeatSaberMarkupLanguage.Utilities.GetResourceContent(Assembly.GetExecutingAssembly(), "PlaylistManager.UI.Views.PlaylistDetailsView.bsml"), levelPackDetailViewController.transform.Find("Detail").gameObject, this);
bsmlParser.Parse(BeatSaberMarkupLanguage.Utilities.GetResourceContent(pluginMetadata.Assembly, "PlaylistManager.UI.Views.PlaylistDetailsView.bsml"), levelPackDetailViewController._detailWrapper.gameObject, this);
modalPosition = modalTransform.position;
}
modalTransform.position = modalPosition;
Expand Down
Loading

0 comments on commit 72a32e4

Please sign in to comment.