Skip to content

Commit

Permalink
Merge pull request #9 from rithik-b/dev/save
Browse files Browse the repository at this point in the history
Saving last leaderboard
  • Loading branch information
rithik-b authored Sep 28, 2022
2 parents b9ba3be + 499dcaa commit df1da21
Show file tree
Hide file tree
Showing 10 changed files with 111 additions and 37 deletions.
20 changes: 20 additions & 0 deletions LeaderboardCore/Configuration/PluginConfig.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#nullable enable
using System;
using System.Runtime.CompilerServices;
using IPA.Config.Stores;

[assembly: InternalsVisibleTo(GeneratedStore.AssemblyVisibilityTarget)]
namespace LeaderboardCore.Configuration
{
internal class PluginConfig : IDisposable
{
public string? LastLeaderboard { get; set; } = null;

public virtual void Changed() { }

public void Dispose()
{
Changed();
}
}
}
9 changes: 9 additions & 0 deletions LeaderboardCore/Installers/LeaderboardCoreMenuInstaller.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using IPA.Loader;
using LeaderboardCore.AffinityPatches;
using LeaderboardCore.Configuration;
using LeaderboardCore.UI.ViewControllers;
using Zenject;
using LeaderboardCore.Managers;
Expand All @@ -9,8 +10,16 @@ namespace LeaderboardCore.Installers
{
internal class LeaderboardCoreMenuInstaller : Installer
{
private readonly PluginConfig pluginConfig;

public LeaderboardCoreMenuInstaller(PluginConfig pluginConfig)
{
this.pluginConfig = pluginConfig;
}

public override void InstallBindings()
{
Container.BindInterfacesAndSelfTo<PluginConfig>().FromInstance(pluginConfig);
Container.BindInterfacesAndSelfTo<LeaderboardNavigationButtonsController>().FromNewComponentAsViewController().AsSingle();
Container.BindInterfacesTo<LeaderboardCoreManager>().AsSingle();
Container.BindInterfacesAndSelfTo<CustomLeaderboardManager>().AsSingle();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace LeaderboardCore.Interfaces
{
internal interface INotifyCustomLeaderboardsChange
{
public void OnLeaderboardsChanged(List<CustomLeaderboard> customLeaderboards);
public void OnLeaderboardsChanged(IEnumerable<CustomLeaderboard> orderedCustomLeaderboards, Dictionary<string, CustomLeaderboard> customLeaderboardsById);

}
}
8 changes: 7 additions & 1 deletion LeaderboardCore/LeaderboardCore.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 14 additions & 6 deletions LeaderboardCore/Managers/CustomLeaderboardManager.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
using LeaderboardCore.Interfaces;
using LeaderboardCore.Models;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Reflection;

namespace LeaderboardCore.Managers
{
Expand All @@ -10,12 +13,12 @@ namespace LeaderboardCore.Managers
public class CustomLeaderboardManager
{
private readonly List<INotifyCustomLeaderboardsChange> notifyCustomLeaderboardsChanges;
private readonly List<CustomLeaderboard> customLeaderboards;
private readonly Dictionary<string, CustomLeaderboard> customLeaderboardsById;

internal CustomLeaderboardManager(List<INotifyCustomLeaderboardsChange> notifyCustomLeaderboardsChanges)
{
this.notifyCustomLeaderboardsChanges = notifyCustomLeaderboardsChanges;
customLeaderboards = new List<CustomLeaderboard>();
customLeaderboardsById = new Dictionary<string, CustomLeaderboard>();
}

/// <summary>
Expand All @@ -24,9 +27,14 @@ internal CustomLeaderboardManager(List<INotifyCustomLeaderboardsChange> notifyCu
/// </summary>
public void Register(CustomLeaderboard customLeaderboard)
{
if (!customLeaderboards.Contains(customLeaderboard))
if (customLeaderboard.pluginId == null)
{
customLeaderboards.Add(customLeaderboard);
customLeaderboard.pluginId = Assembly.GetCallingAssembly().GetName().Name;
}

if (!customLeaderboardsById.ContainsKey(customLeaderboard.LeaderboardId))
{
customLeaderboardsById[customLeaderboard.LeaderboardId] = customLeaderboard;
OnLeaderboardsChanged();
}
}
Expand All @@ -37,15 +45,15 @@ public void Register(CustomLeaderboard customLeaderboard)
/// </summary>
public void Unregister(CustomLeaderboard customLeaderboard)
{
customLeaderboards.Remove(customLeaderboard);
customLeaderboardsById.Remove(customLeaderboard.LeaderboardId);
OnLeaderboardsChanged();
}

private void OnLeaderboardsChanged()
{
foreach (var notifyCustomLeaderboardsChange in notifyCustomLeaderboardsChanges)
{
notifyCustomLeaderboardsChange.OnLeaderboardsChanged(customLeaderboards);
notifyCustomLeaderboardsChange.OnLeaderboardsChanged(customLeaderboardsById.Values, customLeaderboardsById);
}
}
}
Expand Down
10 changes: 10 additions & 0 deletions LeaderboardCore/Models/CustomLeaderboard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,16 @@ public abstract class CustomLeaderboard
/// The ViewController for the leaderboard itself.
/// </summary>
protected abstract ViewController leaderboardViewController { get; }

/// <summary>
/// The ID for the leaderboard.
/// Must be a unique string if the mod has multiple leaderboards.
/// </summary>
protected virtual string leaderboardId { get; } = "";

internal string pluginId;

internal string LeaderboardId => $"{pluginId}{leaderboardId}";

internal void Show(FloatingScreen panelScreen, Vector3 leaderboardPosition, PlatformLeaderboardViewController platformLeaderboardViewController)
{
Expand Down
3 changes: 2 additions & 1 deletion LeaderboardCore/Models/ScoreSaberCustomLeaderboard.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using LeaderboardCore.Interfaces;
using System.Reflection;
using LeaderboardCore.Interfaces;
using UnityEngine;
using Zenject;

Expand Down
7 changes: 5 additions & 2 deletions LeaderboardCore/Plugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
using SiraUtil.Zenject;
using System;
using System.Reflection;
using IPA.Config.Stores;
using IPA.Loader;
using LeaderboardCore.Configuration;
using IPALogger = IPA.Logging.Logger;
using IPAConfig = IPA.Config.Config;

namespace LeaderboardCore
{
Expand All @@ -27,11 +30,11 @@ public class Plugin
/// Only use [Init] with one Constructor.
/// </summary>
[Init]
public Plugin(IPALogger logger, Zenjector zenjector)
public Plugin(IPALogger logger, Zenjector zenjector, IPAConfig config)
{
Instance = this;
Log = logger;
zenjector.Install<LeaderboardCoreMenuInstaller>(Location.Menu);
zenjector.Install<LeaderboardCoreMenuInstaller>(Location.Menu, config.Generated<PluginConfig>());
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
using LeaderboardCore.Models;
using System;
using System.Collections.Generic;
using System.Reflection;
using LeaderboardCore.Configuration;
using UnityEngine;
using Zenject;

Expand All @@ -17,6 +19,9 @@ internal class LeaderboardNavigationButtonsController : BSMLAutomaticViewControl
{
[Inject]
private readonly PlatformLeaderboardViewController platformLeaderboardViewController = null!;

[Inject]
private readonly PluginConfig pluginConfig = null!;

[InjectOptional]
private readonly ScoreSaberCustomLeaderboard? scoreSaberCustomLeaderboard = null!;
Expand All @@ -30,10 +35,10 @@ internal class LeaderboardNavigationButtonsController : BSMLAutomaticViewControl
private bool leaderboardLoaded;
private IPreviewBeatmapLevel? selectedLevel;

private readonly List<CustomLeaderboard> customLeaderboards = new List<CustomLeaderboard>();
private readonly List<CustomLeaderboard> orderedCustomLeaderboards = new List<CustomLeaderboard>();
private readonly Dictionary<string, CustomLeaderboard> customLeaderboardsById = new Dictionary<string, CustomLeaderboard>();
private int currentIndex;
private CustomLeaderboard? lastLeaderboard;


public void Initialize()
{
buttonsFloatingScreen = FloatingScreen.CreateFloatingScreen(new Vector2(120f, 25f), false, Vector3.zero, Quaternion.identity);
Expand Down Expand Up @@ -102,7 +107,6 @@ public void OnLeaderboardSet(IDifficultyBeatmap difficultyBeatmap)
public void OnLeaderboardLoaded(bool loaded)
{
leaderboardLoaded = loaded;
var lastLeaderboardIndex = customLeaderboards.IndexOf(lastLeaderboard!);

if (!(selectedLevel is CustomPreviewBeatmapLevel))
{
Expand All @@ -111,11 +115,11 @@ public void OnLeaderboardLoaded(bool loaded)
else
{
// If not loaded or leaderboard removed
if (!loaded || (lastLeaderboard != null && lastLeaderboardIndex == -1 && currentIndex != 0))
if (!loaded || (pluginConfig.LastLeaderboard != null && !customLeaderboardsById.ContainsKey(pluginConfig.LastLeaderboard) && currentIndex != 0))
{
SwitchToDefault();
}
else if (lastLeaderboard != null && lastLeaderboardIndex != -1 && currentIndex == 0)
else if (customLeaderboardsById.ContainsKey(pluginConfig.LastLeaderboard ?? "") && currentIndex == 0)
{
SwitchToLastLeaderboard();
}
Expand All @@ -131,19 +135,22 @@ public void OnLeaderboardLoaded(bool loaded)

private void SwitchToDefault()
{
lastLeaderboard?.Hide(customPanelFloatingScreen);
if (customLeaderboardsById.TryGetValue(pluginConfig.LastLeaderboard ?? "", out var lastLeaderboard))
{
lastLeaderboard.Hide(customPanelFloatingScreen);
}
currentIndex = 0;
UnYeetDefault();
}

private void SwitchToLastLeaderboard()
{
if (lastLeaderboard != null)
if (customLeaderboardsById.TryGetValue(pluginConfig.LastLeaderboard ?? "", out var lastLeaderboard))
{
var lastLeaderboardIndex = customLeaderboards.IndexOf(lastLeaderboard);
var lastLeaderboardIndex = orderedCustomLeaderboards.IndexOf(lastLeaderboard);
lastLeaderboard.Show(customPanelFloatingScreen, containerPosition, platformLeaderboardViewController);
currentIndex = lastLeaderboardIndex + 1;
YeetDefault();
YeetDefault();
}
}

Expand All @@ -168,22 +175,27 @@ private void UnYeetDefault()
[UIAction("left-button-click")]
private void LeftButtonClick()
{
if (lastLeaderboard == null)
if (pluginConfig.LastLeaderboard == null)
{
return;
}

lastLeaderboard.Hide(customPanelFloatingScreen);
if (customLeaderboardsById.TryGetValue(pluginConfig.LastLeaderboard, out var outLastLeaderboard))
{
outLastLeaderboard.Hide(customPanelFloatingScreen);
}

currentIndex--;

if (currentIndex == 0)
{
UnYeetDefault();
lastLeaderboard = null;
pluginConfig.LastLeaderboard = null;
}
else
{
lastLeaderboard = customLeaderboards[currentIndex - 1];
var lastLeaderboard = orderedCustomLeaderboards[currentIndex - 1];
pluginConfig.LastLeaderboard = lastLeaderboard.LeaderboardId;
lastLeaderboard.Show(customPanelFloatingScreen, containerPosition, platformLeaderboardViewController);
}

Expand All @@ -198,31 +210,36 @@ private void RightButtonClick()
{
YeetDefault();
}
else
else if (customLeaderboardsById.TryGetValue(pluginConfig.LastLeaderboard ?? "", out var outLastLeaderboard))
{
lastLeaderboard?.Hide(customPanelFloatingScreen);
outLastLeaderboard?.Hide(customPanelFloatingScreen);
}

currentIndex++;
lastLeaderboard = customLeaderboards[currentIndex - 1];
var lastLeaderboard = orderedCustomLeaderboards[currentIndex - 1];
pluginConfig.LastLeaderboard = lastLeaderboard.LeaderboardId;
lastLeaderboard.Show(customPanelFloatingScreen, containerPosition, platformLeaderboardViewController);

NotifyPropertyChanged(nameof(LeftButtonActive));
NotifyPropertyChanged(nameof(RightButtonActive));
}

public void OnLeaderboardsChanged(List<CustomLeaderboard> customLeaderboards)
public void OnLeaderboardsChanged(IEnumerable<CustomLeaderboard> orderedCustomLeaderboards, Dictionary<string, CustomLeaderboard> customLeaderboardsById)
{
this.customLeaderboards.Clear();
this.customLeaderboards.AddRange(customLeaderboards);
this.orderedCustomLeaderboards.Clear();
this.orderedCustomLeaderboards.AddRange(orderedCustomLeaderboards);

var lastLeaderboardIndex = customLeaderboards.IndexOf(lastLeaderboard!);
this.customLeaderboardsById.Clear();
foreach (var customLeaderboard in customLeaderboardsById)
{
this.customLeaderboardsById[customLeaderboard.Key] = customLeaderboard.Value;
}

if (lastLeaderboard != null && lastLeaderboardIndex == -1 && currentIndex != 0)
if (pluginConfig.LastLeaderboard != null && !customLeaderboardsById.ContainsKey(pluginConfig.LastLeaderboard) && currentIndex != 0)
{
SwitchToDefault();
}
else if (lastLeaderboard != null && lastLeaderboardIndex != -1 && currentIndex == 0)
else if (customLeaderboardsById.ContainsKey(pluginConfig.LastLeaderboard ?? "") && currentIndex == 0)
{
SwitchToLastLeaderboard();
}
Expand All @@ -239,8 +256,8 @@ public void OnLeaderboardsChanged(List<CustomLeaderboard> customLeaderboards)
private bool LeftButtonActive => (currentIndex > 0 && (ShowDefaultLeaderboard || currentIndex > 1 )) && leaderboardLoaded && selectedLevel is CustomPreviewBeatmapLevel;

[UIValue("right-button-active")]
private bool RightButtonActive => currentIndex < customLeaderboards.Count && leaderboardLoaded && selectedLevel is CustomPreviewBeatmapLevel;
private bool RightButtonActive => currentIndex < orderedCustomLeaderboards.Count && leaderboardLoaded && selectedLevel is CustomPreviewBeatmapLevel;

private bool ShowDefaultLeaderboard => scoreSaberCustomLeaderboard != null || !(selectedLevel is CustomPreviewBeatmapLevel) || customLeaderboards.Count == 0;
private bool ShowDefaultLeaderboard => scoreSaberCustomLeaderboard != null || !(selectedLevel is CustomPreviewBeatmapLevel) || orderedCustomLeaderboards.Count == 0;
}
}
2 changes: 1 addition & 1 deletion LeaderboardCore/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"id": "LeaderboardCore",
"name": "LeaderboardCore",
"author": "PixelBoom & PulseLane",
"version": "1.2.3",
"version": "1.3.0",
"description": "A utility for custom leaderboards to attach themselves in Beat Saber.",
"gameVersion": "1.20.0",
"dependsOn": {
Expand Down

0 comments on commit df1da21

Please sign in to comment.