Skip to content

Commit

Permalink
Merge pull request #3 from qe201020335/zenject
Browse files Browse the repository at this point in the history
Use Zenject
  • Loading branch information
qe201020335 authored Jul 15, 2023
2 parents 6dd3f4d + b9020cc commit 93854c1
Show file tree
Hide file tree
Showing 25 changed files with 1,226 additions and 867 deletions.
64 changes: 0 additions & 64 deletions SongPlayHistory/BeatSaberUI.cs

This file was deleted.

2 changes: 1 addition & 1 deletion SongPlayHistory/Configuration/PluginConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
{
public class PluginConfig
{
public static PluginConfig Instance { get; set; }
public static PluginConfig Instance { get; set; } = null!;

public bool ShowFailed { get; set; } = true;
public bool AverageAccuracy { get; set; } = true;
Expand Down
29 changes: 29 additions & 0 deletions SongPlayHistory/Installers/AppInstaller.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System.Linq;
using IPA.Loader;
using SiraUtil.Logging;
using SongPlayHistory.VoteTracker;
using Zenject;

namespace SongPlayHistory.Installers
{
public class AppInstaller: Installer<AppInstaller>
{

public override void InstallBindings()
{
Container.BindInterfacesAndSelfTo<RecordsManager>().AsSingle();
var bsVoting = PluginManager.GetPluginFromId(Plugin.BeatSaverVotingId) != null;

if (bsVoting)
{
Plugin.Log.Info("BeatSaverVoting is installed! Binding BeatSaverVotingTracker.");
Container.BindInterfacesTo<BeatSaverVotingTracker>().AsSingle();
}
else
{
Plugin.Log.Info("BeatSaverVoting is NOT installed! Binding InternalVoteTracker.");
Container.BindInterfacesTo<InternalVoteTracker>().AsSingle();
}
}
}
}
15 changes: 15 additions & 0 deletions SongPlayHistory/Installers/MenuInstaller.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using SongPlayHistory.UI;
using SongPlayHistory.VoteTracker;
using Zenject;

namespace SongPlayHistory.Installers
{
public class MenuInstaller: Installer<MenuInstaller>
{
public override void InstallBindings()
{
Container.BindInterfacesTo<SPHUI>().AsSingle();
Container.BindInterfacesAndSelfTo<InMenuVoteTrackingHelper>().AsSingle();
}
}
}
14 changes: 14 additions & 0 deletions SongPlayHistory/Installers/ScoreTrackerInstaller.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using SongPlayHistory.Utils;
using Zenject;

namespace SongPlayHistory.Installers
{
public class ScoreTrackerInstaller : Installer<ScoreTrackerInstaller>
{
public override void InstallBindings()
{
Plugin.Log.Warn("Binding ScoreTracker");
Container.BindInterfacesTo<ScoreTracker>().AsSingle().NonLazy();
}
}
}
109 changes: 109 additions & 0 deletions SongPlayHistory/Model/Record.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using Newtonsoft.Json;

namespace SongPlayHistory.Model
{
internal class Record
{
public long Date = 0L;
public int ModifiedScore = 0;
public int RawScore = 0;
public int LastNote = 0;
public int Param = 0;
[JsonProperty(DefaultValueHandling = DefaultValueHandling.Ignore)]
[DefaultValue(null)]
public int? MaxRawScore = null;
[JsonIgnore]
public int? CalculatedMaxRawScore = null; // only save in memory
public string ToShortString()
{
return $"ModifiedScore {ModifiedScore}, RawScore {RawScore}, LastNote {LastNote}, MaxRawScore {MaxRawScore ?? CalculatedMaxRawScore}";
}
}

[Flags]
internal enum Param
{
None = 0,
BatteryEnergy = 1 << 0,
NoFail = 1 << 1,
InstaFail = 1 << 2,
NoObstacles = 1 << 3,
NoBombs = 1 << 4,
FastNotes = 1 << 5,
StrictAngles = 1 << 6,
DisappearingArrows = 1 << 7,
FasterSong = 1 << 8,
SlowerSong = 1 << 9,
NoArrows = 1 << 10,
GhostNotes = 1 << 11,
SuperFastSong = 1 << 12,
SmallCubes = 1 << 13,
ProMode = 1 << 14,
// where is 1 << 15 (0x8000) ??
SubmissionDisabled = 1 << 16,
Multiplayer = 1 << 17,
}

internal static class ParamHelper
{
internal static Param ModsToParam(GameplayModifiers mods, bool softFailed)
{
Param param = Param.None;
param |= mods.energyType == GameplayModifiers.EnergyType.Battery ? Param.BatteryEnergy : 0;
param |= mods.noFailOn0Energy && softFailed ? Param.NoFail : 0;
param |= mods.instaFail ? Param.InstaFail : 0;
param |= mods.enabledObstacleType == GameplayModifiers.EnabledObstacleType.NoObstacles ? Param.NoObstacles : 0;
param |= mods.noBombs ? Param.NoBombs : 0;
param |= mods.fastNotes ? Param.FastNotes : 0;
param |= mods.strictAngles ? Param.StrictAngles : 0;
param |= mods.disappearingArrows ? Param.DisappearingArrows : 0;
param |= mods.songSpeed == GameplayModifiers.SongSpeed.SuperFast ? Param.SuperFastSong : 0;
param |= mods.songSpeed == GameplayModifiers.SongSpeed.Faster ? Param.FasterSong : 0;
param |= mods.songSpeed == GameplayModifiers.SongSpeed.Slower ? Param.SlowerSong : 0;
param |= mods.noArrows ? Param.NoArrows : 0;
param |= mods.ghostNotes ? Param.GhostNotes : 0;
param |= mods.smallCubes ? Param.SmallCubes : 0;
param |= mods.proMode ? Param.ProMode : 0;
return param;
}

internal static string ToParamString(this Param param)
{
if (param == Param.None)
{
return "";
}

var mods = new List<string>(10); // an init capacity of 10 should be plenty in most cases

if (param.HasFlag(Param.Multiplayer)) mods.Add("MULTI");
if (param.HasFlag(Param.BatteryEnergy)) mods.Add("BE");
if (param.HasFlag(Param.NoFail)) mods.Add("NF");
if (param.HasFlag(Param.InstaFail)) mods.Add("IF");
if (param.HasFlag(Param.NoObstacles)) mods.Add("NO");
if (param.HasFlag(Param.NoBombs)) mods.Add("NB");
if (param.HasFlag(Param.FastNotes)) mods.Add("FN");
if (param.HasFlag(Param.StrictAngles)) mods.Add("SA");
if (param.HasFlag(Param.DisappearingArrows)) mods.Add("DA");
if (param.HasFlag(Param.SuperFastSong)) mods.Add("SFS");
if (param.HasFlag(Param.FasterSong)) mods.Add("FS");
if (param.HasFlag(Param.SlowerSong)) mods.Add("SS");
if (param.HasFlag(Param.NoArrows)) mods.Add("NA");
if (param.HasFlag(Param.GhostNotes)) mods.Add("GN");
if (param.HasFlag(Param.SmallCubes)) mods.Add("SN");
if (param.HasFlag(Param.ProMode)) mods.Add("PRO");
if (param.HasFlag(Param.SubmissionDisabled)) mods.Add("??");
if (mods.Count > 4)
{
mods = mods.Take(3).ToList(); // Truncate
mods.Add("..");
}

return string.Join(",", mods);
}
}
}
21 changes: 21 additions & 0 deletions SongPlayHistory/Model/UserVote.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;

namespace SongPlayHistory.Model
{
internal struct UserVote
{
[JsonProperty("hash")]
internal string Hash;

[JsonProperty("voteType")]
[JsonConverter(typeof(StringEnumConverter))]
internal VoteType VoteType;
}

public enum VoteType
{
Upvote,
Downvote
}
}
36 changes: 36 additions & 0 deletions SongPlayHistory/Patches/DiTailsVotePatch.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System;
using System.Reflection;
using HarmonyLib;
using IPA.Loader;
using SongPlayHistory.Model;
using SongPlayHistory.VoteTracker;

namespace SongPlayHistory.Patches
{
[HarmonyPatch]
internal class DiTailsVotePatch
{
private static readonly MethodBase? DiTailsVote = AccessTools.Method("DiTails.UI.DetailViewHost:Vote", new[] { typeof(bool) });

[HarmonyTargetMethod]
private static MethodBase CalculateMethod()
{
return DiTailsVote!;
}

[HarmonyPrepare]
private static bool Prepare()
{
return DiTailsVote != null;
}

[HarmonyPrefix]
public static void Prefix(bool upvote, IDifficultyBeatmap? ____activeBeatmap)
{
if (____activeBeatmap == null) return;
var vote = upvote ? VoteType.Upvote : VoteType.Downvote;
Plugin.Log.Debug($"DiTails voted {vote} to {____activeBeatmap.level.levelID}");
InMenuVoteTrackingHelper.Instance?.Vote(____activeBeatmap.level, vote);
}
}
}
Loading

0 comments on commit 93854c1

Please sign in to comment.