Skip to content

Commit 0f52aae

Browse files
committed
Added tracked stat attribute and added more comments.
1 parent 5aa295e commit 0f52aae

File tree

4 files changed

+133
-16
lines changed

4 files changed

+133
-16
lines changed

Runtime/DevConsole.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -384,11 +384,12 @@ public static void ClearConsole()
384384
/// <summary>
385385
/// Set a tracked developer console stat that can be displayed on-screen.
386386
/// </summary>
387-
/// <param name="name"></param>
388-
/// <param name="func"></param>
389-
public static void SetTrackedStat(string name, Func<object> func)
387+
/// <param name="name">Identifier.</param>
388+
/// <param name="func">Lambda function returning the stat's result that is displayed.</param>
389+
/// <param name="startEnabled">Whether the stat should start enabled.</param>
390+
public static void SetTrackedStat(string name, Func<object> func, bool startEnabled = true)
390391
{
391-
_console.SetTrackedStat(name, func);
392+
_console.SetTrackedStat(name, func, startEnabled);
392393
}
393394

394395
/// <summary>

Runtime/DevConsoleMono.cs

Lines changed: 80 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1159,7 +1159,7 @@ private void Awake()
11591159
LoadPreferences();
11601160
InitBuiltInCommands();
11611161
InitBuiltInParsers();
1162-
InitAttributeCommands();
1162+
InitAttributes();
11631163

11641164
// Enable the console by default if in editor or a development build
11651165
if (Debug.isDebugBuild)
@@ -2509,6 +2509,22 @@ void logChildren(GameObject obj, int tabAmount)
25092509

25102510
#region Stat display commands
25112511

2512+
AddCommand(Command.Create(
2513+
"stats_help",
2514+
"",
2515+
"Display information about the developer console stats feature",
2516+
() =>
2517+
{
2518+
DevConsole.LogSeperator("Developer console tracked stats display");
2519+
DevConsole.Log("When enabled, stats such as variables can be displayed on-screen during gameplay.");
2520+
DevConsole.Log("There are three ways to add tracked stats:");
2521+
DevConsole.Log($"1. Using commands ({GetCommand("stats_set").GetFormattedName()}) to add a stat that is evaluated as a C# expression;");
2522+
DevConsole.Log($"2. Declaring a <i>{typeof(DevConsoleTrackedStatAttribute).Name}</i> attribute on a declared static field or property; or");
2523+
DevConsole.Log($"3. Calling <i>DevConsole.SetTrackedStat()</i> to add a stat this is retrieved via a provided lambda function.");
2524+
DevConsole.LogSeperator();
2525+
}
2526+
));
2527+
25122528
AddCommand(Command.Create(
25132529
"stats_list",
25142530
"",
@@ -2521,7 +2537,7 @@ void logChildren(GameObject obj, int tabAmount)
25212537
return;
25222538
}
25232539

2524-
LogCollection(_stats, x => $"{x.Key}: {x.Value} ({x.Value.Desc}){(_hiddenStats.Contains(x.Key) ? " [Disabled]" : "")}");
2540+
LogCollection(_stats, x => $"{x.Key}: {x.Value} ({x.Value.Desc}){(_hiddenStats.Contains(x.Key) ? " [Disabled]" : "")}.");
25252541
}
25262542
));
25272543

@@ -2779,9 +2795,9 @@ private void InitBuiltInParsers()
27792795
}
27802796

27812797
/// <summary>
2782-
/// Search for, and add all the attribute commands.
2798+
/// Search for, and add all the attribute commands & other custom attributes.
27832799
/// </summary>
2784-
private void InitAttributeCommands()
2800+
private void InitAttributes()
27852801
{
27862802
// https://github.com/yasirkula/UnityIngameDebugConsole/blob/master/Plugins/IngameDebugConsole/Scripts/DebugLogConsole.cs
27872803
// Implementation of finding attributes sourced from yasirkula's code
@@ -2846,6 +2862,38 @@ private void InitAttributeCommands()
28462862
}
28472863
}
28482864
}
2865+
2866+
foreach (FieldInfo field in type.GetFields(BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.DeclaredOnly))
2867+
{
2868+
DevConsoleTrackedStatAttribute attribute = field.GetCustomAttribute<DevConsoleTrackedStatAttribute>();
2869+
if (attribute == null)
2870+
{
2871+
continue;
2872+
}
2873+
2874+
string name = $"var_{field.Name}";
2875+
_stats[name] = new ReflectedStat(field);
2876+
if (!attribute.StartEnabled)
2877+
{
2878+
_hiddenStats.Add(name);
2879+
}
2880+
}
2881+
2882+
foreach (PropertyInfo property in type.GetProperties(BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.DeclaredOnly))
2883+
{
2884+
DevConsoleTrackedStatAttribute attribute = property.GetCustomAttribute<DevConsoleTrackedStatAttribute>();
2885+
if (attribute == null)
2886+
{
2887+
continue;
2888+
}
2889+
2890+
string name = $"var_{property.Name}";
2891+
_stats[name] = new ReflectedStat(property);
2892+
if (!attribute.StartEnabled)
2893+
{
2894+
_hiddenStats.Add(name);
2895+
}
2896+
}
28492897
}
28502898
}
28512899
catch (NotSupportedException) { }
@@ -3399,7 +3447,7 @@ private void SavePreferences()
33993447
DevConsoleData.SetObject(PrefIncludedUsings, _includedUsings);
34003448
DevConsoleData.SetObject(PrefShowStats, _isDisplayingStats);
34013449
DevConsoleData.SetObject(PrefStats, _stats.Where(x => x.Value is EvaluatedStat).ToDictionary(x => x.Key, x => ((EvaluatedStat)x.Value).Expression));
3402-
DevConsoleData.SetObject(PrefHiddenStats, _hiddenStats);
3450+
DevConsoleData.SetObject(PrefHiddenStats, new HashSet<string>(_hiddenStats.Where(x => _stats.Keys.Contains(x))));
34033451

34043452
DevConsoleData.Save();
34053453
}
@@ -3479,7 +3527,8 @@ private void InitMonoEvaluator()
34793527
/// </summary>
34803528
/// <param name="name"></param>
34813529
/// <param name="func"></param>
3482-
internal void SetTrackedStat(string name, Func<object> func)
3530+
/// <param name="startEnabled"></param>
3531+
internal void SetTrackedStat(string name, Func<object> func, bool startEnabled)
34833532
{
34843533
if (string.IsNullOrEmpty(name))
34853534
{
@@ -3492,6 +3541,11 @@ internal void SetTrackedStat(string name, Func<object> func)
34923541
}
34933542

34943543
_stats[name] = new LambdaStat(func);
3544+
3545+
if (!startEnabled)
3546+
{
3547+
_hiddenStats.Add(name);
3548+
}
34953549
}
34963550

34973551
/// <summary>
@@ -3517,6 +3571,9 @@ internal bool RemoveTrackedStat(string name)
35173571

35183572
#region Nested types
35193573

3574+
/// <summary>
3575+
/// Abstract class for tracked developer console stats.
3576+
/// </summary>
35203577
private abstract class StatBase
35213578
{
35223579
#region Properties
@@ -3537,11 +3594,18 @@ private abstract class StatBase
35373594
/// <returns></returns>
35383595
public abstract object GetResult(Evaluator evaluator);
35393596

3597+
/// <summary>
3598+
/// String representation that is used when displaying a list of all the tracked developer console stats.
3599+
/// </summary>
3600+
/// <returns></returns>
35403601
public abstract override string ToString();
35413602

35423603
#endregion
35433604
}
35443605

3606+
/// <summary>
3607+
/// Stat that evaluates a C# expression to determine the result.
3608+
/// </summary>
35453609
private sealed class EvaluatedStat : StatBase
35463610
{
35473611
#region Constructors
@@ -3557,6 +3621,9 @@ public EvaluatedStat(string expression)
35573621

35583622
public override string Desc => "Evaluated";
35593623

3624+
/// <summary>
3625+
/// C# expression to be evaluated for the stat result.
3626+
/// </summary>
35603627
public string Expression { get; }
35613628

35623629
#endregion
@@ -3576,6 +3643,9 @@ public override string ToString()
35763643
#endregion
35773644
}
35783645

3646+
/// <summary>
3647+
/// Stat that uses a user-defined lambda to retrieve the result.
3648+
/// </summary>
35793649
private sealed class LambdaStat : StatBase
35803650
{
35813651
#region Fields
@@ -3614,6 +3684,9 @@ public override string ToString()
36143684
#endregion
36153685
}
36163686

3687+
/// <summary>
3688+
/// Stat that retrieves the result from a static field or property.
3689+
/// </summary>
36173690
private sealed class ReflectedStat : StatBase
36183691
{
36193692
#region Fields
@@ -3653,12 +3726,7 @@ public override object GetResult(Evaluator _ = null)
36533726

36543727
public override string ToString()
36553728
{
3656-
if (_field != null)
3657-
{
3658-
return $"{_field.Name} (field: {_field.FieldType.Name})";
3659-
}
3660-
3661-
return $"{_property.Name} (property: {_property.PropertyType.Name})";
3729+
return _field?.FieldType.Name ?? _property.PropertyType.Name;
36623730
}
36633731

36643732
#endregion
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// File: DevConsoleTrackedStatAttribute.cs
2+
// Purpose: Defines an attribute that is tracked by the developer console and displayed on-screen
3+
// Created by: DavidFDev
4+
5+
using System;
6+
7+
namespace DavidFDev.DevConsole
8+
{
9+
/// <summary>
10+
/// Declare a static field or property to be tracked as a developer console stat.
11+
/// </summary>
12+
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, Inherited = false, AllowMultiple = false)]
13+
public sealed class DevConsoleTrackedStatAttribute : Attribute
14+
{
15+
#region Constructors
16+
17+
/// <summary>
18+
/// Track a field or property and have it displayed on-screen.
19+
/// </summary>
20+
/// <param name="startEnabled">Whether to have the stat displayed by default.</param>
21+
public DevConsoleTrackedStatAttribute(bool startEnabled = true)
22+
{
23+
StartEnabled = startEnabled;
24+
}
25+
26+
#endregion
27+
28+
#region Properties
29+
30+
/// <summary>
31+
/// Whether the stat is displayed by default.
32+
/// </summary>
33+
public bool StartEnabled { get; }
34+
35+
#endregion
36+
}
37+
}

Runtime/DevConsoleTrackedStatAttribute.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)