You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
82 lines
3.9 KiB
82 lines
3.9 KiB
using System.Collections; |
|
using System.Collections.Generic; |
|
using System.Diagnostics; |
|
using System.Linq; |
|
using System.Text.RegularExpressions; |
|
using UnityEngine; |
|
using UnityEngine.Assertions; |
|
|
|
namespace UDE_HAND_INTERACTION |
|
{ |
|
public static class AssertManager |
|
{ |
|
public const string HiglightColor = "#3366ff"; |
|
|
|
[Conditional("UNITY_ASSERTIONS")] |
|
public static void AssertField<TValue>(this Component component, |
|
TValue value, string variableName, |
|
string whyItFailed = null, string whereItFailed = null, string howToFix = null) |
|
where TValue : class |
|
{ |
|
string gameObjectName = component.name; |
|
string componentName = component.GetType().Name; |
|
string niceVariableName = Nicify(variableName); |
|
string variableType = typeof(TValue).Name; |
|
|
|
Assert.IsNotNull(value, |
|
(whereItFailed ?? $"At GameObject <color={HiglightColor}><b>{gameObjectName}</b></color>, component <b>{componentName}</b>. ") + |
|
(whyItFailed ?? $"Required <b>{niceVariableName}</b> reference is missing. ") + |
|
(howToFix ?? $"Assign a <b>{variableType}</b> to the field <b>{niceVariableName}</b>.")); |
|
} |
|
|
|
[Conditional("UNITY_ASSERTIONS")] |
|
public static void AssertCollectionItems<TValue>(this Component component, |
|
IEnumerable<TValue> value, string variableName, |
|
string whyItFailed = null, string whereItFailed = null, string howToFix = null) |
|
{ |
|
string gameObjectName = component.name; |
|
string componentName = component.GetType().Name; |
|
string niceVariableName = Nicify(variableName); |
|
string variableType = typeof(TValue).Name; |
|
|
|
int index = 0; |
|
foreach (TValue item in value) |
|
{ |
|
Assert.IsFalse(item is null, |
|
(whereItFailed ?? $"At GameObject <color={HiglightColor}><b>{gameObjectName}</b></color>, component <b>{componentName}</b>. ") + |
|
(whyItFailed ?? $"Invalid item in the collection <b>{niceVariableName}</b> at index <b>{index}</b>. ") + |
|
(howToFix ?? $"Assign a <b>{variableType}</b> to the collection <b>{niceVariableName}</b> at index <b>{index}</b>. ")); |
|
index++; |
|
} |
|
} |
|
|
|
[Conditional("UNITY_ASSERTIONS")] |
|
public static void AssertCollectionField<TValue>(this Component component, |
|
IEnumerable<TValue> value, string variableName, |
|
string whyItFailed = null, string whereFailed = null, string howToFix = null) |
|
{ |
|
|
|
string gameObjectName = component.name; |
|
string componentName = component.GetType().Name; |
|
string niceVariableName = Nicify(variableName); |
|
string variableType = typeof(TValue).Name; |
|
|
|
Assert.IsTrue(value != null && value.Count() > 0, |
|
(whereFailed ?? $"At GameObject <color={HiglightColor}><b>{gameObjectName}</b></color>: the <b>{componentName}</b> component has an missing or empty <b>{niceVariableName}</b> collection. ") + |
|
(whyItFailed ?? "") + |
|
(howToFix ?? $"Assign at least one <b>{variableType}</b> to the collection <b>{niceVariableName}</b>. ")); |
|
|
|
component.AssertCollectionItems(value, variableName); |
|
} |
|
|
|
public static string Nicify(string variableName) |
|
{ |
|
variableName = Regex.Replace(variableName, "_([a-z])", match => match.Value.ToUpper(), RegexOptions.Compiled); |
|
variableName = Regex.Replace(variableName, "m_|_", " ", RegexOptions.Compiled); |
|
variableName = Regex.Replace(variableName, "k([A-Z])", "$1", RegexOptions.Compiled); |
|
variableName = Regex.Replace(variableName, "([A-Z])", " $1", RegexOptions.Compiled); |
|
variableName = variableName.Trim(); |
|
return variableName; |
|
} |
|
} |
|
}
|
|
|