Skip to content

Commit 7a56d9a

Browse files
committed
Added command to change stats font size.
Changed warning text colour. Changed naming of stats created via the custom attribute. Prettied up stats_list output. Fixed exception thrown by using Mono on unsupported platform - now displays a notice in the console.
1 parent 6913cba commit 7a56d9a

File tree

3 files changed

+120
-27
lines changed

3 files changed

+120
-27
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

77
## [Unreleased]
8+
- Added notice in developer console when C# evaluation is not supported.
9+
- Added command to change stats font size.
10+
- Changed warning text colour.
11+
- Changed stats_list output to be prettier.
12+
- Changed naming of stats created via the custom attribute.
13+
- Fixed exception thrown by using Mono on unsupported platforms.
814

915
## [1.0.2] - 2021-10-30
1016
- Added user-defined developer console stats which are displayed on-screen.

Runtime/DevConsoleMono.cs

Lines changed: 98 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ internal sealed class DevConsoleMono : MonoBehaviour
4242
#region Static fields and constants
4343

4444
private const string ErrorColour = "#E99497";
45-
private const string WarningColour = "#B3E283";
45+
private const string WarningColour = "#DEBF1F";
4646
private const string SuccessColour = "#B3E283";
4747
private const string ClearLogText = "Type <b>devconsole</b> for instructions on how to use the developer console.";
4848
private const int MaximumTextVertices = 64000;
@@ -56,6 +56,8 @@ internal sealed class DevConsoleMono : MonoBehaviour
5656
private const int MaxCachedEnumTypes = 6;
5757
private const float FpsUpdateRate = 4f;
5858
private const float StatUpdateRate = 0.1f;
59+
private const int StatDefaultFontSize = 18;
60+
private const string MonoNotSupportedText = "C# expression evaluation is not supported on this platform.";
5961

6062
#region Input constants
6163

@@ -107,6 +109,7 @@ internal sealed class DevConsoleMono : MonoBehaviour
107109
private const string PrefShowStats = "DevConsole.displayStats";
108110
private const string PrefStats = "DevConsole.stats";
109111
private const string PrefHiddenStats = "DevConsole.hiddenStats";
112+
private const string PrefStatsFontSize = "DevConsole.statsFontSize";
110113

111114
#endregion
112115

@@ -377,7 +380,7 @@ internal sealed class DevConsoleMono : MonoBehaviour
377380
private readonly List<Type> _cacheEnumTypes = new List<Type>(MaxCachedEnumTypes);
378381

379382
/// <summary>
380-
/// Evaluator used by "cs_evaluate" and "cs_run" to execuet C# expressions or statements.
383+
/// Evaluator used by "cs_evaluate" and "cs_run" to execute C# expressions or statements.
381384
/// </summary>
382385
private Evaluator _monoEvaluator = null;
383386

@@ -409,6 +412,8 @@ internal sealed class DevConsoleMono : MonoBehaviour
409412
private HashSet<string> _hiddenStats = new HashSet<string>();
410413
private Dictionary<string, object> _cachedStats = new Dictionary<string, object>();
411414
private float _statUpdateTime;
415+
private int _statFontSize = StatDefaultFontSize;
416+
private int _oldStatFontSize = StatDefaultFontSize;
412417

413418
#endregion
414419

@@ -1175,6 +1180,11 @@ private void Awake()
11751180
ClearConsole();
11761181
CloseConsole();
11771182

1183+
if (_monoEvaluator == null)
1184+
{
1185+
LogWarning($"Some features may not be available: {MonoNotSupportedText}");
1186+
}
1187+
11781188
_init = false;
11791189
}
11801190

@@ -1406,17 +1416,19 @@ private void OnGUI()
14061416
GUI.contentColor = oldContentColour;
14071417
}
14081418

1409-
if (_isDisplayingStats && _monoEvaluator != null && _stats.Any())
1419+
if (_isDisplayingStats && _stats.Any())
14101420
{
1411-
if (_statStyle == null)
1421+
if (_statStyle == null || _statFontSize != _oldStatFontSize)
14121422
{
14131423
// Create the style
14141424
_statStyle = new GUIStyle(GUI.skin.box)
14151425
{
14161426
alignment = TextAnchor.MiddleCenter,
1417-
fontSize = 18,
1427+
fontSize = _statFontSize,
14181428
normal = { textColor = Color.white, background = Texture2D.whiteTexture }
14191429
};
1430+
1431+
_oldStatFontSize = _statFontSize;
14201432
}
14211433

14221434
// Initialise
@@ -1468,7 +1480,7 @@ private void OnGUI()
14681480

14691481
// Set content
14701482
string content = $"{stat.Key}: {result ?? "NULL"}";
1471-
GUI.contentColor = result == null ? Color.yellow : (result.Equals("ERROR") ? Color.red : Color.white);
1483+
GUI.contentColor = result == null ? Color.yellow : ((result.Equals("ERROR") || result.Equals(MonoNotSupportedText)) ? Color.red : Color.white);
14721484

14731485
// Determine label size
14741486
Vector2 size = _statStyle.CalcSize(new GUIContent(content));
@@ -2318,14 +2330,20 @@ void logChildren(GameObject obj, int tabAmount)
23182330
Parameter.Create("expression", "The expression to evaluate"),
23192331
input =>
23202332
{
2333+
if (_monoEvaluator == null)
2334+
{
2335+
DevConsole.LogError(MonoNotSupportedText);
2336+
return;
2337+
}
2338+
23212339
try
23222340
{
23232341
if (!input.EndsWith(";"))
23242342
{
23252343
input += ";";
23262344
}
23272345

2328-
object result = _monoEvaluator?.Evaluate(input) ?? null;
2346+
object result = _monoEvaluator.Evaluate(input);
23292347

23302348
if (result == null)
23312349
{
@@ -2355,14 +2373,20 @@ void logChildren(GameObject obj, int tabAmount)
23552373
Parameter.Create("statement", "The statement to execute"),
23562374
input =>
23572375
{
2376+
if (_monoEvaluator == null)
2377+
{
2378+
DevConsole.LogError(MonoNotSupportedText);
2379+
return;
2380+
}
2381+
23582382
try
23592383
{
23602384
if (!input.EndsWith(";"))
23612385
{
23622386
input += ";";
23632387
}
23642388

2365-
if (_monoEvaluator?.Run(input) ?? false)
2389+
if (_monoEvaluator.Run(input))
23662390
{
23672391
LogSuccess("Successfully executed the C# expression or statement.");
23682392
}
@@ -2384,6 +2408,12 @@ void logChildren(GameObject obj, int tabAmount)
23842408
"Display a list of all active using statements",
23852409
() =>
23862410
{
2411+
if (_monoEvaluator == null)
2412+
{
2413+
DevConsole.LogError(MonoNotSupportedText);
2414+
return;
2415+
}
2416+
23872417
string usings = _monoEvaluator.GetUsing();
23882418

23892419
if (string.IsNullOrEmpty(usings))
@@ -2404,6 +2434,12 @@ void logChildren(GameObject obj, int tabAmount)
24042434
"Display a list of all local variables defined",
24052435
() =>
24062436
{
2437+
if (_monoEvaluator == null)
2438+
{
2439+
DevConsole.LogError(MonoNotSupportedText);
2440+
return;
2441+
}
2442+
24072443
string vars = _monoEvaluator.GetVars();
24082444

24092445
if (string.IsNullOrEmpty(vars))
@@ -2516,16 +2552,18 @@ void logChildren(GameObject obj, int tabAmount)
25162552
AddCommand(Command.Create(
25172553
"stats_list",
25182554
"",
2519-
"Display a list of the stored developer console stats that can be displayed on-screen",
2555+
"Display a list of the tracked developer console stats that can be displayed on-screen",
25202556
() =>
25212557
{
25222558
if (!_stats.Any())
25232559
{
2524-
DevConsole.Log($"There are no stored developer console stats. Use {GetCommand("stats_set").GetFormattedName()} to set one up.");
2560+
DevConsole.Log($"There are no tracked developer console stats. Use {GetCommand("stats_set").GetFormattedName()} to set one up.");
25252561
return;
25262562
}
25272563

2528-
LogCollection(_stats, x => $"{x.Key}: {x.Value} ({x.Value.Desc}){(_hiddenStats.Contains(x.Key) ? " [Disabled]" : "")}.");
2564+
LogSeperator("Tracked developer console stats");
2565+
LogCollection(_stats, x => $"<b>{x.Key}:</b> {x.Value} ({x.Value.Desc}){(_hiddenStats.Contains(x.Key) ? " <i>[Disabled]</i>" : "")}.");
2566+
LogSeperator();
25292567
}
25302568
));
25312569

@@ -2647,6 +2685,25 @@ void logChildren(GameObject obj, int tabAmount)
26472685
}
26482686
));
26492687

2688+
AddCommand(Command.Create<int>(
2689+
"stats_fontsize",
2690+
"",
2691+
"Query or set the font size of the tracked developer console stats",
2692+
Parameter.Create("fontSize", $"Size of the font (default: {StatDefaultFontSize})"),
2693+
f =>
2694+
{
2695+
if (f <= 0)
2696+
{
2697+
LogError("Font size must be non-zero and positive.");
2698+
return;
2699+
}
2700+
2701+
_statFontSize = f;
2702+
LogSuccess($"Set the stats font size to {_statFontSize} (was {_oldStatFontSize}).");
2703+
},
2704+
() => LogVariable("Stats font size", _statFontSize)
2705+
));
2706+
26502707
#endregion
26512708

26522709
#region Misc commands
@@ -2866,7 +2923,7 @@ private void InitAttributes()
28662923
continue;
28672924
}
28682925

2869-
string name = $"var_{field.Name}";
2926+
string name = attribute.Name ?? field.Name;
28702927
_stats[name] = new ReflectedStat(field);
28712928
if (!attribute.StartEnabled)
28722929
{
@@ -2882,7 +2939,7 @@ private void InitAttributes()
28822939
continue;
28832940
}
28842941

2885-
string name = $"var_{property.Name}";
2942+
string name = attribute.Name ?? property.Name;
28862943
_stats[name] = new ReflectedStat(property);
28872944
if (!attribute.StartEnabled)
28882945
{
@@ -3443,6 +3500,7 @@ private void SavePreferences()
34433500
DevConsoleData.SetObject(PrefShowStats, _isDisplayingStats);
34443501
DevConsoleData.SetObject(PrefStats, _stats.Where(x => x.Value is EvaluatedStat).ToDictionary(x => x.Key, x => ((EvaluatedStat)x.Value).Expression));
34453502
DevConsoleData.SetObject(PrefHiddenStats, new HashSet<string>(_hiddenStats.Where(x => _stats.Keys.Contains(x))));
3503+
DevConsoleData.SetObject(PrefStatsFontSize, _statFontSize);
34463504

34473505
DevConsoleData.Save();
34483506
}
@@ -3472,6 +3530,7 @@ private void LoadPreferences()
34723530
_stats.Add(stat.Key, new EvaluatedStat(stat.Value));
34733531
}
34743532
_hiddenStats = DevConsoleData.GetObject(PrefHiddenStats, new HashSet<string>());
3533+
_statFontSize = _oldStatFontSize = DevConsoleData.GetObject(PrefStatsFontSize, StatDefaultFontSize);
34753534

34763535
DevConsoleData.Clear();
34773536
}
@@ -3490,26 +3549,33 @@ private void InitMonoEvaluator()
34903549
return;
34913550
}
34923551

3493-
CompilerSettings settings = new CompilerSettings();
3494-
3495-
// Add assembly references to the settings
3496-
foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies())
3552+
try
34973553
{
3498-
if (assembly == null)
3554+
CompilerSettings settings = new CompilerSettings();
3555+
3556+
// Add assembly references to the settings
3557+
foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies())
34993558
{
3500-
continue;
3501-
}
3559+
if (assembly == null)
3560+
{
3561+
continue;
3562+
}
35023563

3503-
settings.AssemblyReferences.Add(assembly.FullName);
3504-
}
3564+
settings.AssemblyReferences.Add(assembly.FullName);
3565+
}
35053566

3506-
CompilerContext context = new CompilerContext(settings, new ConsoleReportPrinter());
3507-
_monoEvaluator = new Evaluator(context);
3567+
CompilerContext context = new CompilerContext(settings, new ConsoleReportPrinter());
3568+
_monoEvaluator = new Evaluator(context);
35083569

3509-
// Add the included using statements
3510-
foreach (string includedUsing in _includedUsings)
3570+
// Add the included using statements
3571+
foreach (string includedUsing in _includedUsings)
3572+
{
3573+
_monoEvaluator.Run($"using {includedUsing};");
3574+
}
3575+
}
3576+
catch (Exception)
35113577
{
3512-
_monoEvaluator.Run($"using {includedUsing};");
3578+
_monoEvaluator = null;
35133579
}
35143580
}
35153581

@@ -3627,6 +3693,11 @@ public EvaluatedStat(string expression)
36273693

36283694
public override object GetResult(Evaluator evaluator)
36293695
{
3696+
if (evaluator == null)
3697+
{
3698+
return MonoNotSupportedText;
3699+
}
3700+
36303701
return evaluator?.Evaluate(Expression) ?? null;
36313702
}
36323703

Runtime/DevConsoleTrackedStatAttribute.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,26 @@ public DevConsoleTrackedStatAttribute(bool startEnabled = true)
2323
StartEnabled = startEnabled;
2424
}
2525

26+
/// <summary>
27+
/// Track a field or property and have it displayed on-screen.
28+
/// </summary>
29+
/// <param name="name">Custom name to use for the tracked stat.</param>
30+
/// <param name="startEnabled">Whether to have the stat displayed by default.</param>
31+
public DevConsoleTrackedStatAttribute(string name, bool startEnabled = true)
32+
{
33+
Name = name;
34+
StartEnabled = startEnabled;
35+
}
36+
2637
#endregion
2738

2839
#region Properties
2940

41+
/// <summary>
42+
/// Custom name to use for the tracked stat, otherwise use the target name.
43+
/// </summary>
44+
public string Name { get; }
45+
3046
/// <summary>
3147
/// Whether the stat is displayed by default.
3248
/// </summary>

0 commit comments

Comments
 (0)