Skip to content

Commit

Permalink
Moved attributes to own namespace, fix bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
Caeden117 committed Jan 11, 2020
1 parent ccae92b commit cb271ad
Show file tree
Hide file tree
Showing 15 changed files with 125 additions and 51 deletions.
16 changes: 10 additions & 6 deletions Counters+/Counters+.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,6 @@
<Reference Include="0Harmony.1.2.0.1">
<HintPath>$(BeatSaberDir)\Libs\0Harmony.1.2.0.1.dll</HintPath>
</Reference>
<Reference Include="MainAssembly">
<HintPath>$(BeatSaberDir)\Beat Saber_Data\Managed\MainAssembly.dll</HintPath>
</Reference>
<Reference Include="HMUI">
<HintPath>$(BeatSaberDir)\Beat Saber_Data\Managed\HMUI.dll</HintPath>
</Reference>
Expand All @@ -64,6 +61,10 @@
<SpecificVersion>False</SpecificVersion>
<HintPath>$(BeatSaberDir)\Beat Saber_Data\Managed\IPA.Loader.dll</HintPath>
</Reference>
<Reference Include="MainAssembly, Version=0.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>D:\Oculus\Software\Software\hyperbolic-magnetism-beat-saber\Beat Saber_Data\Managed\MainAssembly.dll</HintPath>
</Reference>
<Reference Include="SemVer.1.2.0.0">
<HintPath>$(BeatSaberDir)\Libs\SemVer.1.2.0.0.dll</HintPath>
</Reference>
Expand Down Expand Up @@ -107,11 +108,14 @@
<Compile Include="Counters\FailCounter.cs" />
<Compile Include="Counters\NotesLeftCounter.cs" />
<Compile Include="Counters\Spinometer.cs" />
<Compile Include="Custom\Attributes\CounterDestroyAttribute.cs" />
<Compile Include="Custom\Attributes\CounterStartAttribute.cs" />
<Compile Include="Custom\Attributes\CounterUpdateAttribute.cs" />
<Compile Include="Custom\CustomCounters.cs" />
<Compile Include="Custom\CustomCounterTemplate.cs" />
<Compile Include="Custom\DisplayNameAttribute.cs" />
<Compile Include="Custom\FormattedTextAttribute.cs" />
<Compile Include="Custom\RefreshCounterAttribute.cs" />
<Compile Include="Custom\Attributes\DisplayNameAttribute.cs" />
<Compile Include="Custom\Attributes\FormattedTextAttribute.cs" />
<Compile Include="Custom\Attributes\RefreshCounterAttribute.cs" />
<Compile Include="Harmony\ScoreCounterHook.cs" />
<Compile Include="UI\AdvancedCounterSettings.cs" />
<Compile Include="UI\CounterWarning.cs" />
Expand Down
3 changes: 2 additions & 1 deletion Counters+/Counters/ScoreCounter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ private void CreateText()
PointsText.rectTransform.position = new Vector3(-3.2f,
0.35f + (settings.Mode == ICounterMode.LeavePoints ? 7.8f : 0), 7);
}
GetComponent<ImmediateRankUIPanel>().Start(); //BS way of getting Harmony patch to function but "if it works its not stupid" ~Caeden117
//BS way of getting Harmony patch to function but "if it works its not stupid" ~Caeden117
GetComponent<ImmediateRankUIPanel>().Start();
}
}
}
12 changes: 12 additions & 0 deletions Counters+/Custom/Attributes/CounterDestroyAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;

namespace CountersPlus.Custom.Attributes
{
/// <summary>
/// Triggered when the counter is destroyed. Used for unsubscribing from events and resetting variables to defaults.
/// </summary>
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
public class CounterDestroyAttribute : Attribute
{
}
}
12 changes: 12 additions & 0 deletions Counters+/Custom/Attributes/CounterStartAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;

namespace CountersPlus.Custom.Attributes
{
/// <summary>
/// Triggered when the counter has started. Used for finding Unity objects and attaching delegates to events.
/// </summary>
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
public class CounterStartAttribute : Attribute
{
}
}
12 changes: 12 additions & 0 deletions Counters+/Custom/Attributes/CounterUpdateAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;

namespace CountersPlus.Custom.Attributes
{
/// <summary>
/// Similar to MonoBehaviour.Update, except with an Atribute instead of a class.
/// </summary>
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
public class CounterUpdateAttribute : Attribute
{
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
using System;

namespace CountersPlus.Custom
namespace CountersPlus.Custom.Attributes
{
/// <summary>
/// Display Name for the counter, that will go above whatever you are trying to show.
/// </summary>
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
public class DisplayNameAttribute : Attribute
{
Expand Down
13 changes: 13 additions & 0 deletions Counters+/Custom/Attributes/FormattedTextAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;

namespace CountersPlus.Custom.Attributes
{
/// <summary>
/// Formatted Text for your counter, that will appear underneath the display name.
/// More than likely you'd want to just call <see cref="int.ToString()"/>.
/// </summary>
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
public class FormattedTextAttribute : Attribute
{
}
}
13 changes: 13 additions & 0 deletions Counters+/Custom/Attributes/RefreshCounterAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;

namespace CountersPlus.Custom.Attributes
{
/// <summary>
/// Attach this to an <see cref="Action"/> and invoke it to cause the text of the counter to refresh, pulling data
/// from any <see cref="DisplayNameAttribute"/> and <see cref="FormattedTextAttribute"/>.
/// </summary>
[AttributeUsage(AttributeTargets.Field, AllowMultiple = false, Inherited = true)]
public class RefreshCounterAttribute : Attribute
{
}
}
39 changes: 31 additions & 8 deletions Counters+/Custom/CustomCounterTemplate.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using CountersPlus.Counters;
using CountersPlus.Custom.Attributes;
using TMPro;
using UnityEngine;
using System;
Expand All @@ -11,6 +12,11 @@ public class CustomCounterTemplate : Counter<CustomConfigModel>
private PropertyInfo displayName;
private PropertyInfo formattedText;
private FieldInfo refreshCounter;

private MethodInfo counterUpdate;
private MethodInfo counterStart;
private MethodInfo counterDestroy;

private object host;

private Action counterRefreshedEvent;
Expand All @@ -20,35 +26,48 @@ public class CustomCounterTemplate : Counter<CustomConfigModel>

internal override void Counter_Start()
{
foreach (PropertyInfo propertyInfo in settings.CustomCounter.TemplateCounter.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic))
counterRefreshedEvent += RefreshCounter;
host = settings.CustomCounter.TemplateCounter;
Type templateType = settings.CustomCounter.TemplateCounter.GetType();
foreach (PropertyInfo propertyInfo in templateType.GetProperties(BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic))
{
DisplayNameAttribute display = propertyInfo.GetCustomAttribute(typeof(DisplayNameAttribute), true) as DisplayNameAttribute;
FormattedTextAttribute text = propertyInfo.GetCustomAttribute(typeof(FormattedTextAttribute), true) as FormattedTextAttribute;
host = settings.CustomCounter.TemplateCounter;
if (display != null) displayName = propertyInfo;
if (text != null) formattedText = propertyInfo;
}
foreach (FieldInfo fieldInfo in settings.CustomCounter.TemplateCounter.GetType().GetFields(BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic))
foreach (FieldInfo fieldInfo in templateType.GetFields(BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic))
{
RefreshCounterAttribute refresh = fieldInfo.GetCustomAttribute(typeof(RefreshCounterAttribute), true) as RefreshCounterAttribute;
if (refresh != null)
{
if (refreshCounter.FieldType != typeof(Action))
if (fieldInfo.FieldType != typeof(Action))
{
Plugin.Log($"Custom Counter {settings.CustomCounter.Name} provides a RefreshCounterAttribute, however it is not an Action.",
LogInfo.Fatal, "Contact the creator of this Custom Counter, rather than to Counters+ itself");
Destroy(this);
return;
}
refreshCounter = fieldInfo;
fieldInfo.SetValue(host, counterRefreshedEvent);
}
}
counterRefreshedEvent += RefreshCounter;
foreach (MethodInfo methodInfo in templateType.GetMethods(BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic))
{
CounterUpdateAttribute update = methodInfo.GetCustomAttribute(typeof(CounterUpdateAttribute), true) as CounterUpdateAttribute;
CounterStartAttribute start = methodInfo.GetCustomAttribute(typeof(CounterStartAttribute), true) as CounterStartAttribute;
CounterDestroyAttribute destroy = methodInfo.GetCustomAttribute(typeof(CounterDestroyAttribute), true) as CounterDestroyAttribute;
if (update != null) counterUpdate = methodInfo;
if (start != null) counterStart = methodInfo;
if (destroy != null) counterDestroy = methodInfo;
}
counterStart?.Invoke(host, new object[] { });
}

internal override void Counter_Destroy()
{
counterRefreshedEvent -= RefreshCounter;
counterDestroy?.Invoke(host, new object[] { });
}

internal override void Init(CountersData data)
Expand All @@ -70,11 +89,15 @@ internal override void Init(CountersData data)
RefreshCounter();
}

private void Update()
{
counterUpdate?.Invoke(host, new object[] { });
}

public void RefreshCounter()
{
if (host is null || displayName is null || formattedText is null) return;
label.text = displayName.GetGetMethod(false).Invoke(host, new object[] { }).ToString();
counter.text = formattedText.GetGetMethod(false).Invoke(host, new object[] { }).ToString();
label.text = displayName?.GetGetMethod(false)?.Invoke(host, new object[] { })?.ToString();
counter.text = formattedText?.GetGetMethod(false)?.Invoke(host, new object[] { })?.ToString();
}
}
}
2 changes: 1 addition & 1 deletion Counters+/Custom/CustomCounters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public class CustomCounter
/// </summary>
public string CustomSettingsResource = null;
/// <summary>
/// The <see cref="Type"/> of a <see cref="MonoBehaviour"/> that will handle the file defined in <see cref="CustomSettingsResource"/>.
/// The <see cref="Type"/> of a <see cref="MonoBehaviour"/> that will handle the BSML file defined in <see cref="CustomSettingsResource"/>.
/// </summary>
public Type CustomSettingsHandler;
/// <summary>
Expand Down
9 changes: 0 additions & 9 deletions Counters+/Custom/FormattedTextAttribute.cs

This file was deleted.

9 changes: 0 additions & 9 deletions Counters+/Custom/RefreshCounterAttribute.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,13 @@ public override TableCell CellForIdx(TableView view, int row, CountersPlusHorizo
cell.reuseIdentifier = settings.ReuseIdentifier;
}
cell.showNewRibbon = false; //Dequeued cells will keep NEW ribbon value. Always change it to false.
TextMeshProUGUI packNameText = cell.GetPrivateField<TextMeshProUGUI>("_packNameText"); //Grab some TMP references.
TextMeshProUGUI packInfoText = cell.GetPrivateField<TextMeshProUGUI>("_infoText");
packInfoText.richText = true; //Enable rich text for info text. Optional, but I use it for Counters+.
UnityEngine.UI.Image packCoverImage = cell.GetPrivateField<UnityEngine.UI.Image>("_coverImage");
packCoverImage.mainTexture.wrapMode = TextureWrapMode.Clamp; //Fixes bordering on images (especially transparent ones)

SettingsInfo info = counterInfos[row];
packNameText.text = info.Name;
packInfoText.text = info.Description;
packInfoText.text = $"{info.Name}\n\n{info.Description}";
try
{
if (info.IsCustom)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,13 @@ public override TableCell CellForIdx(TableView view, int row, CountersPlusHorizo

private void SetCellInfo(ref LevelPackTableCell cell, string name, string info, string imageResource, bool ribbon = true)
{
TextMeshProUGUI packNameText = cell.GetPrivateField<TextMeshProUGUI>("_packNameText"); //Grab some TMP references.
TextMeshProUGUI packInfoText = cell.GetPrivateField<TextMeshProUGUI>("_infoText");
packInfoText.richText = true; //Enable rich text for info text. Optional, but I use it for Counters+.
UnityEngine.UI.Image packCoverImage = cell.GetPrivateField<UnityEngine.UI.Image>("_coverImage");
packCoverImage.mainTexture.wrapMode = TextureWrapMode.Clamp; //Fixes bordering on images
cell.showNewRibbon = ribbon;

packNameText.text = name;
packInfoText.text = info;
packInfoText.text = $"{name}\n\n{info}";
packCoverImage.sprite = Images.Images.LoadSprite(imageResource);
}

Expand Down
23 changes: 13 additions & 10 deletions Counters+/UI/ViewControllers/SettingsGroups/MainSettingsGroup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,32 +17,35 @@ public override TableCell CellForIdx(TableView view, int row, CountersPlusHorizo
cell.reuseIdentifier = settings.ReuseIdentifier;
}
cell.showNewRibbon = false; //Dequeued cells will keep NEW ribbon value. Always change it to false.
TextMeshProUGUI packNameText = cell.GetPrivateField<TextMeshProUGUI>("_packNameText"); //Grab some TMP references.
TextMeshProUGUI packInfoText = cell.GetPrivateField<TextMeshProUGUI>("_infoText");
packInfoText.richText = true; //Enable rich text for info text. Optional, but I use it for Counters+.
UnityEngine.UI.Image packCoverImage = cell.GetPrivateField<UnityEngine.UI.Image>("_coverImage");
packCoverImage.mainTexture.wrapMode = TextureWrapMode.Clamp; //Fixes bordering on images (especially transparent ones)
switch (row)
{
case 0:
packNameText.text = "Main Settings";
packInfoText.text = "Configure basic Counters+ settings.";
packCoverImage.sprite = Images.Images.LoadSprite("MainSettings");
SetText("Main Settings", "Configure basic Counters+ settings.", Images.Images.LoadSprite("MainSettings"),
ref packInfoText, ref packCoverImage);
break;
case 1:
packNameText.text = "Donators";
packInfoText.text = "See who supported me on Ko-fi!";
packCoverImage.sprite = Images.Images.LoadSprite("Donators");
SetText("Donators", "See who supported me on Ko-fi!", Images.Images.LoadSprite("Donators"),
ref packInfoText, ref packCoverImage);
break;
case 2:
packNameText.text = "Contributors";
packInfoText.text = "See who helped with Counters+!";
packCoverImage.sprite = Images.Images.LoadSprite("Contributors");
SetText("Contributors", "See who helped with Counters+!", Images.Images.LoadSprite("Contributors"),
ref packInfoText, ref packCoverImage);
break;
}
return cell;
}


private void SetText(string name, string desc, Sprite sprite, ref TextMeshProUGUI packInfo, ref UnityEngine.UI.Image packCover)
{
packInfo.text = $"{name}\n\n{desc}";
packCover.sprite = sprite;
}

public override int NumberOfCells() => 3;

public override float CellSize() => 49f;
Expand Down

0 comments on commit cb271ad

Please sign in to comment.