Skip to content

Commit

Permalink
Add attributes to make custom counter creation easier
Browse files Browse the repository at this point in the history
  • Loading branch information
Caeden117 committed Jan 11, 2020
1 parent e55f515 commit ccae92b
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 11 deletions.
3 changes: 3 additions & 0 deletions Counters+/Counters+.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,9 @@
<Compile Include="Counters\Spinometer.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="Harmony\ScoreCounterHook.cs" />
<Compile Include="UI\AdvancedCounterSettings.cs" />
<Compile Include="UI\CounterWarning.cs" />
Expand Down
7 changes: 3 additions & 4 deletions Counters+/CountersController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,10 @@ public static void OnLoad()
/// <typeparam name="T">ConfigModel to use for settings.</typeparam>
/// <typeparam name="R">MonoBehaviour to attach to the new GameObject.</typeparam>
/// <param name="settings">ConfigModel settings reference.</param>
internal static void LoadCounter<T, R>(T settings, Type counterInstance = null) where T : ConfigModel where R : Counter<T>
internal static void LoadCounter<T, R>(T settings) where T : ConfigModel where R : Counter<T>
{
if (counterInstance is null) counterInstance = typeof(R);
if (!settings.Enabled || GameObject.Find($"Counters+ | {settings.DisplayName} Counter")) return;
R counter = new GameObject($"Counters+ | {settings.DisplayName} Counter").AddComponent(counterInstance) as R;
R counter = new GameObject($"Counters+ | {settings.DisplayName} Counter").AddComponent(typeof(R)) as R;
counter.settings = settings;
Plugin.Log($"Loaded Counter: {settings.DisplayName}");
LoadedCounters.Add(counter.gameObject);
Expand Down Expand Up @@ -90,7 +89,7 @@ public static void LoadCounters()
foreach (CustomCounter potential in CustomCounterCreator.LoadedCustomCounters)
{
if (potential.TemplateCounter != null)
LoadCounter<CustomConfigModel, CustomCounterTemplate>(potential.ConfigModel, potential.TemplateCounter);
LoadCounter<CustomConfigModel, CustomCounterTemplate>(potential.ConfigModel);
else
LoadCounter<CustomConfigModel, CustomCounterHook>(potential.ConfigModel);
}
Expand Down
50 changes: 45 additions & 5 deletions Counters+/Custom/CustomCounterTemplate.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,56 @@
using CountersPlus.Counters;
using TMPro;
using UnityEngine;
using System;
using System.Reflection;

namespace CountersPlus.Custom
{
public abstract class CustomCounterTemplate : Counter<CustomConfigModel>
public class CustomCounterTemplate : Counter<CustomConfigModel>
{
public abstract string DisplayName { get; }
public abstract string FormattedText { get; }
private PropertyInfo displayName;
private PropertyInfo formattedText;
private FieldInfo refreshCounter;
private object host;

private Action counterRefreshedEvent;

private TMP_Text label;
private TMP_Text counter;

internal override void Counter_Start()
{
foreach (PropertyInfo propertyInfo in settings.CustomCounter.TemplateCounter.GetType().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))
{
RefreshCounterAttribute refresh = fieldInfo.GetCustomAttribute(typeof(RefreshCounterAttribute), true) as RefreshCounterAttribute;
if (refresh != null)
{
if (refreshCounter.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);
}
refreshCounter = fieldInfo;
fieldInfo.SetValue(host, counterRefreshedEvent);
}
}
counterRefreshedEvent += RefreshCounter;
}

internal override void Counter_Destroy()
{
counterRefreshedEvent -= RefreshCounter;
}

internal override void Init(CountersData data)
{
Vector3 position = CountersController.DeterminePosition(gameObject, settings.Position, settings.Distance);
Expand All @@ -33,8 +72,9 @@ internal override void Init(CountersData data)

public void RefreshCounter()
{
label.text = DisplayName;
counter.text = FormattedText;
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();
}
}
}
4 changes: 2 additions & 2 deletions Counters+/Custom/CustomCounters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,9 @@ public class CustomCounter
/// </summary>
public Type CustomSettingsHandler;
/// <summary>
/// A template counter that inherits from <see cref="CustomCounterTemplate"/>.
/// An instance to a class that contains various attributes for a Counters+ template counter.
/// </summary>
public Type TemplateCounter;
public object TemplateCounter;

/// <summary>
/// Local name of the mod from BSIPA or IPA.
Expand Down
9 changes: 9 additions & 0 deletions Counters+/Custom/DisplayNameAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System;

namespace CountersPlus.Custom
{
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
public class DisplayNameAttribute : Attribute
{
}
}
9 changes: 9 additions & 0 deletions Counters+/Custom/FormattedTextAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System;

namespace CountersPlus.Custom
{
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
public class FormattedTextAttribute : Attribute
{
}
}
9 changes: 9 additions & 0 deletions Counters+/Custom/RefreshCounterAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System;

namespace CountersPlus.Custom
{
[AttributeUsage(AttributeTargets.Field, AllowMultiple = false, Inherited = true)]
public class RefreshCounterAttribute : Attribute
{
}
}

0 comments on commit ccae92b

Please sign in to comment.