Skip to content

Commit

Permalink
Major refactor and optimizations (#3)
Browse files Browse the repository at this point in the history
* Refactored code for better readability. Added fps calculation based on detected refresh rate for determining the graph draw speed.

* Cleaned up the color selection logic. Removed unnecessary variables.

* Don't mix variable names, especially when you're up late putzing around....

* Added comments.

* Moved to C# 9 to be able to pass pointers to functions.

* First pass at optimizing the color selection.

* Cleaned up the color logic and removed combinations that aren't possible.

* Removed the creation of an unnecessary Createcircle object. Flipped a loop to a do/while to prevent a pointless initial check. Fixed a color missing from the energy mode.

* Removed from the Energy mode color that didn't conform to the README.

* Bumped version.

* Update AssemblyInfo to match version.

* Add a delay to the destruction of the panel to pause the garbage collection event. Without the slight delay on levels with 1,000s of notes, there is a noticeable pause after a level completes and goes back to the main menu.

* Fixed incorrect first param

* Minor nit. Inlined the colorOverride and sideColor lines.

* Fixed config conversion, credits adjustments

* Updated for Beat Saber 1.20.0

* Updated version

* Fixed side colors to be constant-ish, fixed issues when no data

Making side colors constant fixes an issue when carrying over config files.
When no data is available in either graph, the graph now just shows text saying "No data available."

Co-authored-by: MCJack123 <[email protected]>
  • Loading branch information
SHv2 and MCJack123 authored Apr 7, 2022
1 parent 1afe8ba commit e2013a3
Show file tree
Hide file tree
Showing 10 changed files with 366 additions and 302 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2021 JackMacWindows
Copyright (c) 2021-2022 JackMacWindows

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
5 changes: 5 additions & 0 deletions PerformanceMeter.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<RootNamespace>PerformanceMeter</RootNamespace>
<AssemblyName>PerformanceMeter</AssemblyName>
<TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion>
<LangVersion>9.0</LangVersion>
<FileAlignment>512</FileAlignment>
<DebugType>portable</DebugType>
<BeatSaberDir>$(ProjectDir)Refs</BeatSaberDir>
Expand All @@ -35,6 +36,7 @@
</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
<PropertyGroup Condition="$(DefineConstants.Contains('CIBuild')) OR '$(NCrunch)' == '1'">
<DisableCopyToPlugins>True</DisableCopyToPlugins>
Expand All @@ -43,6 +45,9 @@
<DisableCopyToPlugins>True</DisableCopyToPlugins>
<DisableZipRelease>True</DisableZipRelease>
</PropertyGroup>
<PropertyGroup>
<StartupObject />
</PropertyGroup>
<ItemGroup>
<Reference Include="BeatmapCore, Version=0.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<Private>False</Private>
Expand Down
312 changes: 189 additions & 123 deletions PerformanceMeterController.cs

Large diffs are not rendered by default.

10 changes: 6 additions & 4 deletions Plugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* This file defines the entry points of PerformanceMeter.
*
* This code is licensed under the MIT license.
* Copyright (c) 2021 JackMacWindows.
* Copyright (c) 2021-2022 JackMacWindows.
*/

using IPA;
Expand Down Expand Up @@ -39,7 +39,7 @@ public void Init(IPALogger logger, IPA.Config.Config conf) {
public void OnApplicationStart() {
Logger.log.Debug("OnApplicationStart");
new GameObject("PerformanceMeterController").AddComponent<PerformanceMeterController>();
BSEvents.gameSceneActive += GameSceneActive;
BSEvents.gameSceneLoaded += GameSceneActive;
SceneManager.activeSceneChanged += ActiveSceneChanged;
BSMLSettings.instance.AddSettingsMenu("PerformanceMeter", "PerformanceMeter.Settings", Settings.instance);
}
Expand All @@ -52,11 +52,13 @@ public void OnApplicationQuit() {
}

void GameSceneActive() {
if (PluginConfig.Instance.enabled) PerformanceMeterController.instance.GetControllers();
if (PluginConfig.Instance.enabled)
PerformanceMeterController.instance.GetControllers();
}

void ActiveSceneChanged(Scene oldScene, Scene newScene) {
if (PluginConfig.Instance.enabled && newScene.name == "MainMenu") PerformanceMeterController.instance.ShowResults();
if (PluginConfig.Instance.enabled && newScene.name == "MainMenu")
PerformanceMeterController.instance.ShowResults();
}
}

Expand Down
61 changes: 51 additions & 10 deletions PluginConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,71 @@
* This file defines the configuration of PerformanceMeter.
*
* This code is licensed under the MIT license.
* Copyright (c) 2021 JackMacWindows.
* Copyright (c) 2021-2022 JackMacWindows.
*/

using System.Runtime.CompilerServices;
using IPA.Config.Data;
using IPA.Config.Stores;
using IPA.Config.Stores.Attributes;
using IPA.Config.Stores.Converters;
using UnityEngine;

[assembly: InternalsVisibleTo(GeneratedStore.AssemblyVisibilityTarget)]
namespace PerformanceMeter {
internal class IntColorConverter : ValueConverter<Color> {
public override Color FromValue(Value value, object parent) {
if (!(value is Integer)) throw new System.ArgumentException("Input value is not an integer");
long num = (value as Integer).Value;
return new Color(((num >> 16) & 0xFF) / 255.0f, ((num >> 8) & 0xFF) / 255.0f, (num & 0xFF) / 255.0f);
}

public override Value ToValue(Color obj, object parent) {
return new Integer((long)(obj.r * 255) << 16 | (long)(obj.g * 255) << 8 | (long)(obj.b * 255));
}
}

public class PluginConfig {
public static PluginConfig Instance { get; set; }
public virtual bool enabled { get; set; } = true;
public virtual int mode { get; set; } = (int)MeasurementMode.Energy;
public virtual int side { get; set; } = (int)MeasurementSide.Both;
public virtual int secondaryMode { get; set; } = (int)MeasurementMode.None;
public virtual int secondarySide { get; set; } = (int)MeasurementSide.Both;
[UseConverter(typeof(NumericEnumConverter<MeasurementMode>))]
public virtual MeasurementMode mode { get; set; } = MeasurementMode.Energy;
[UseConverter(typeof(NumericEnumConverter<MeasurementSide>))]
public virtual MeasurementSide side { get; set; } = MeasurementSide.Both;
[UseConverter(typeof(IntColorConverter))]
public Color sideColor {
get {
return side switch {
MeasurementSide.Left => Color.red,
MeasurementSide.Right => Color.blue,
MeasurementSide.Both => Color.white,
_ => Color.white,
};
}
}
[UseConverter(typeof(NumericEnumConverter<MeasurementMode>))]
public virtual MeasurementMode secondaryMode { get; set; } = MeasurementMode.None;
[UseConverter(typeof(NumericEnumConverter<MeasurementSide>))]
public virtual MeasurementSide secondarySide { get; set; } = MeasurementSide.Both;
[UseConverter(typeof(IntColorConverter))]
public Color secondarySideColor {
get {
return secondarySide switch {
MeasurementSide.Left => Color.red,
MeasurementSide.Right => Color.blue,
MeasurementSide.Both => Color.white,
_ => Color.white,
};
}
}
public virtual bool showMisses { get; set; } = false;
public virtual float animationDuration { get; set; } = 3.0f;
public virtual int color { get; set; } = 0xFF0000;
public virtual int secondaryColor { get; set; } = 0x0000FF;
[UseConverter(typeof(IntColorConverter))]
public virtual Color color { get; set; } = new Color(1f, 0f, 0f);
[UseConverter(typeof(IntColorConverter))]
public virtual Color secondaryColor { get; set; } = new Color(0f, 0f, 1f);
public virtual bool overrideColor { get; set; } = false;
public virtual bool overrideSecondaryColor { get; set; } = false;
internal MeasurementMode GetMode(bool sec) { return (MeasurementMode)(sec ? secondaryMode : mode); }
internal MeasurementSide GetSide(bool sec) { return (MeasurementSide)(sec ? secondarySide : side); }
internal UnityEngine.Color GetColor(bool sec) { int c = sec ? secondaryColor : color; return new UnityEngine.Color((float)((c >> 16) & 0xFF) / 255f, (float)((c >> 8) & 0xFF) / 255f, (float)(c & 0xFF) / 255f); }

public enum MeasurementMode {
Energy,
Expand Down
6 changes: 3 additions & 3 deletions Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("PerformanceMeter")]
[assembly: AssemblyCopyright("Copyright © 2021 JackMacWindows")]
[assembly: AssemblyCopyright("Copyright © 2021-2022 JackMacWindows")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

Expand All @@ -32,5 +32,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.3.2")]
[assembly: AssemblyFileVersion("1.3.2")]
[assembly: AssemblyVersion("1.4.0")]
[assembly: AssemblyFileVersion("1.4.0")]
11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ A Beat Saber mod to show a graph of your energy bar, percentage level, or cut va
![Image](screenshot.png)

## Requirements
* Beat Saber 1.19.0 or compatible
* BSIPA 4.2.1
* Beat Saber Utils 1.11.1
* BeatSaberMarkupLanguage 1.6.0
* Beat Saber 1.20.0 or compatible
* BSIPA 4.2.2
* Beat Saber Utils 1.12.1
* BeatSaberMarkupLanguage 1.6.3

## Installation
Simply drop the latest PerformanceMeter.dll plugin file into your Plugins folder, inside the main Beat Saber installation directory.
Expand Down Expand Up @@ -127,5 +127,8 @@ These set the color for each graph. Colors are 24-bit hexadecimal colors (e.g. `
#### `override[Secondary]Color`
These toggle whether the override color is enabled for each graph. If set to `true`, the color in `[secondary]Color` will be used; otherwise the default color will be used.

## Special Thanks
Thanks to @SHv2 for rewriting a significant chunk of the code to improve style and performance.

## License
PerformanceMeter is licensed under the MIT license. See LICENSE for more info.
47 changes: 14 additions & 33 deletions Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@
* This file defines the settings panel controller.
*
* This code is licensed under the MIT license.
* Copyright (c) 2021 JackMacWindows.
* Copyright (c) 2021-2022 JackMacWindows.
*/

using BeatSaberMarkupLanguage.Attributes;
using System.Collections.Generic;
using UnityEngine;
using System.Linq;


Expand Down Expand Up @@ -50,10 +51,10 @@ internal class Settings : PersistentSingleton<Settings> {
public bool overrideSecondaryColor = PluginConfig.Instance.overrideSecondaryColor;

[UIValue("color")]
public UnityEngine.Color color = PluginConfig.Instance.GetColor(false);
public UnityEngine.Color color = PluginConfig.Instance.color;

[UIValue("secondaryColor")]
public UnityEngine.Color secondaryColor = PluginConfig.Instance.GetColor(true);
public UnityEngine.Color secondaryColor = PluginConfig.Instance.secondaryColor;

[UIAction("#apply")]
public void OnApply() {
Expand All @@ -62,40 +63,20 @@ public void OnApply() {
PluginConfig.Instance.animationDuration = animationDuration;
PluginConfig.Instance.overrideColor = overrideColor;
PluginConfig.Instance.overrideSecondaryColor = overrideSecondaryColor;
PluginConfig.Instance.color = ((int)(color.r * 255) << 16) | ((int)(color.g * 255) << 8) | (int)(color.b * 255);
PluginConfig.Instance.secondaryColor = ((int)(secondaryColor.r * 255) << 16) | ((int)(secondaryColor.g * 255) << 8) | (int)(secondaryColor.b * 255);
int ok = 0;
for (int i = 0; i < modeOptions.Count; i++) {
if (modeOptions[i] as string == listChoice) {
PluginConfig.Instance.mode = i >= (int)PluginConfig.MeasurementMode.None ? i + 1 : i;
break;
}
}
for (int i = 0; i < secondaryModeOptions.Count; i++) {
if (secondaryModeOptions[i] as string == secondaryListChoice) {
PluginConfig.Instance.secondaryMode = i;
break;
}
}
for (int i = 0; i < sideOptions.Count; i++) {
if (sideOptions[i] as string == sideChoice) {
PluginConfig.Instance.side = i;
ok++;
}
if (sideOptions[i] as string == secondarySideChoice) {
PluginConfig.Instance.secondarySide = i;
ok++;
}
if (ok >= 2) break;
}
PluginConfig.Instance.color = color;
PluginConfig.Instance.secondaryColor = secondaryColor;
PluginConfig.Instance.mode = (PluginConfig.MeasurementMode)modeOptions.FindIndex(a => a.ToString() == listChoice);
PluginConfig.Instance.secondaryMode = (PluginConfig.MeasurementMode)secondaryModeOptions.FindIndex(a => a.ToString() == secondaryListChoice);
PluginConfig.Instance.side = (PluginConfig.MeasurementSide)sideOptions.FindIndex(a => a.ToString() == sideChoice);
PluginConfig.Instance.secondarySide = (PluginConfig.MeasurementSide)sideOptions.FindIndex(a => a.ToString() == secondarySideChoice);
PluginConfig.Instance.Changed();
}

Settings() {
listChoice = modeOptions[PluginConfig.Instance.mode] as string;
sideChoice = sideOptions[PluginConfig.Instance.side] as string;
secondaryListChoice = secondaryModeOptions[PluginConfig.Instance.secondaryMode] as string;
secondarySideChoice = sideOptions[PluginConfig.Instance.secondarySide] as string;
listChoice = modeOptions[(int)PluginConfig.Instance.mode] as string;
sideChoice = sideOptions[(int)PluginConfig.Instance.side] as string;
secondaryListChoice = secondaryModeOptions[(int)PluginConfig.Instance.secondaryMode] as string;
secondarySideChoice = sideOptions[(int)PluginConfig.Instance.secondarySide] as string;
}
}
}
Loading

0 comments on commit e2013a3

Please sign in to comment.