Skip to content

Commit ea654c3

Browse files
committed
Fixed generic or nullable parameter types not having their type displayed nicely.
1 parent 3ad31ad commit ea654c3

File tree

3 files changed

+32
-2
lines changed

3 files changed

+32
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1414
- Added command: "colour" - displays the provided colour in the developer console.
1515
- Changed DevConsole.InvokeCoroutine method to return the Coroutine instance.
1616
- Fixed Unity logs from other threads not showing in the developer console log.
17+
- Fixed generic or nullable parameter types not having their type displayed nicely.
1718

1819
## [0.2.1-alpha] - 2021-08-09
1920
- Added documentation.

Runtime/DevConsole.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using System;
1010
using System.Collections;
1111
using System.Collections.Generic;
12+
using System.Linq;
1213
using System.Runtime.CompilerServices;
1314
using UnityEngine;
1415

@@ -356,6 +357,27 @@ IEnumerator Invoke()
356357
return _console.StartCoroutine(Invoke());
357358
}
358359

360+
/// <summary>
361+
/// Get the user-friendly name of the parameter type.
362+
/// </summary>
363+
/// <param name="type"></param>
364+
/// <returns></returns>
365+
internal static string GetFriendlyName(Type type)
366+
{
367+
if (type.IsGenericType)
368+
{
369+
Type nullable = Nullable.GetUnderlyingType(type);
370+
if (nullable != null)
371+
{
372+
return $"{GetFriendlyName(nullable)}?";
373+
}
374+
375+
return $"{type.Name.Split('`')[0]}<{string.Join(", ", type.GetGenericArguments().Select(x => GetFriendlyName(x)))}>";
376+
}
377+
378+
return type.Name;
379+
}
380+
359381
#region Invoke events
360382

361383
internal static void InvokeOnConsoleEnabled()

Runtime/Parameter.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// Created by: DavidFDev
44

55
using System;
6+
using System.Linq;
67
using System.Reflection;
78

89
namespace DavidFDev.DevConsole
@@ -50,6 +51,11 @@ private Parameter() { }
5051
/// </summary>
5152
internal Type Type { get; private set; }
5253

54+
/// <summary>
55+
/// User-friendly name of the parameter type.
56+
/// </summary>
57+
internal string FriendlyTypeName { get; private set; }
58+
5359
/// <summary>
5460
/// Name of the parameter.
5561
/// </summary>
@@ -66,7 +72,7 @@ private Parameter() { }
6672

6773
public override string ToString()
6874
{
69-
return $"({Type.Name}){Name}";
75+
return $"({FriendlyTypeName}){Name}";
7076
}
7177

7278
/// <summary>
@@ -87,6 +93,7 @@ internal Parameter SetType<T>()
8793
internal Parameter SetType(Type type)
8894
{
8995
Type = type;
96+
FriendlyTypeName = DevConsole.GetFriendlyName(type);
9097

9198
// If the type is an enum, add special help text
9299
if (type.IsEnum)
@@ -125,7 +132,7 @@ internal Parameter SetType(Type type)
125132
/// <returns></returns>
126133
internal string ToFormattedString()
127134
{
128-
return $"<i>({Type.Name})</i><b>{Name}</b>";
135+
return $"<i>({FriendlyTypeName})</i><b>{Name}</b>";
129136
}
130137

131138
#endregion

0 commit comments

Comments
 (0)