NamedValue.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Reflection;
  4. namespace UnityEngine.InputSystem.Utilities
  5. {
  6. /// <summary>
  7. /// A combination of a name and a value assignment for it.
  8. /// </summary>
  9. public struct NamedValue : IEquatable<NamedValue>
  10. {
  11. public const string Separator = ",";
  12. /// <summary>
  13. /// Name of the parameter.
  14. /// </summary>
  15. public string name { get; set; }
  16. /// <summary>
  17. /// Value of the parameter.
  18. /// </summary>
  19. public PrimitiveValue value { get; set; }
  20. public TypeCode type => value.type;
  21. public NamedValue ConvertTo(TypeCode type)
  22. {
  23. return new NamedValue
  24. {
  25. name = name,
  26. value = value.ConvertTo(type)
  27. };
  28. }
  29. public static NamedValue From<TValue>(string name, TValue value)
  30. where TValue : struct
  31. {
  32. return new NamedValue
  33. {
  34. name = name,
  35. value = PrimitiveValue.From(value)
  36. };
  37. }
  38. public override string ToString()
  39. {
  40. return $"{name}={value}";
  41. }
  42. public bool Equals(NamedValue other)
  43. {
  44. return string.Equals(name, other.name, StringComparison.InvariantCultureIgnoreCase)
  45. && value == other.value;
  46. }
  47. public override bool Equals(object obj)
  48. {
  49. if (ReferenceEquals(null, obj))
  50. return false;
  51. return obj is NamedValue parameterValue && Equals(parameterValue);
  52. }
  53. public override int GetHashCode()
  54. {
  55. unchecked
  56. {
  57. var hashCode = (name != null ? name.GetHashCode() : 0);
  58. hashCode = (hashCode * 397) ^ value.GetHashCode();
  59. return hashCode;
  60. }
  61. }
  62. public static bool operator==(NamedValue left, NamedValue right)
  63. {
  64. return left.Equals(right);
  65. }
  66. public static bool operator!=(NamedValue left, NamedValue right)
  67. {
  68. return !left.Equals(right);
  69. }
  70. public static NamedValue[] ParseMultiple(string parameterString)
  71. {
  72. if (parameterString == null)
  73. throw new ArgumentNullException(nameof(parameterString));
  74. parameterString = parameterString.Trim();
  75. if (string.IsNullOrEmpty(parameterString))
  76. return null;
  77. var parameterCount = parameterString.CountOccurrences(Separator[0]) + 1;
  78. var parameters = new NamedValue[parameterCount];
  79. var index = 0;
  80. for (var i = 0; i < parameterCount; ++i)
  81. {
  82. var parameter = ParseParameter(parameterString, ref index);
  83. parameters[i] = parameter;
  84. }
  85. return parameters;
  86. }
  87. public static NamedValue Parse(string str)
  88. {
  89. var index = 0;
  90. return ParseParameter(str, ref index);
  91. }
  92. private static NamedValue ParseParameter(string parameterString, ref int index)
  93. {
  94. var parameter = new NamedValue();
  95. var parameterStringLength = parameterString.Length;
  96. // Skip whitespace.
  97. while (index < parameterStringLength && char.IsWhiteSpace(parameterString[index]))
  98. ++index;
  99. // Parse name.
  100. var nameStart = index;
  101. while (index < parameterStringLength)
  102. {
  103. var nextChar = parameterString[index];
  104. if (nextChar == '=' || nextChar == Separator[0] || char.IsWhiteSpace(nextChar))
  105. break;
  106. ++index;
  107. }
  108. parameter.name = parameterString.Substring(nameStart, index - nameStart);
  109. // Skip whitespace.
  110. while (index < parameterStringLength && char.IsWhiteSpace(parameterString[index]))
  111. ++index;
  112. if (index == parameterStringLength || parameterString[index] != '=')
  113. {
  114. // No value given so take "=true" as implied.
  115. parameter.value = true;
  116. }
  117. else
  118. {
  119. ++index; // Skip over '='.
  120. // Skip whitespace.
  121. while (index < parameterStringLength && char.IsWhiteSpace(parameterString[index]))
  122. ++index;
  123. // Parse value.
  124. var valueStart = index;
  125. while (index < parameterStringLength &&
  126. !(parameterString[index] == Separator[0] || char.IsWhiteSpace(parameterString[index])))
  127. ++index;
  128. ////TODO: use Substring struct here so that we don't allocate lots of useless strings
  129. var value = parameterString.Substring(valueStart, index - valueStart);
  130. parameter.value = PrimitiveValue.FromString(value);
  131. }
  132. if (index < parameterStringLength && parameterString[index] == Separator[0])
  133. ++index;
  134. return parameter;
  135. }
  136. public void ApplyToObject(object instance)
  137. {
  138. if (instance == null)
  139. throw new System.ArgumentNullException(nameof(instance));
  140. var instanceType = instance.GetType();
  141. ////REVIEW: what about properties?
  142. var field = instanceType.GetField(name,
  143. BindingFlags.IgnoreCase | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
  144. if (field == null)
  145. throw new ArgumentException(
  146. $"Cannot find public field '{name}' in '{instanceType.Name}' (while trying to apply parameter)", nameof(instance));
  147. ////REVIEW: would be awesome to be able to do this without boxing
  148. var fieldTypeCode = Type.GetTypeCode(field.FieldType);
  149. field.SetValue(instance, value.ConvertTo(fieldTypeCode).ToObject());
  150. }
  151. public static void ApplyAllToObject<TParameterList>(object instance, TParameterList parameters)
  152. where TParameterList : IEnumerable<NamedValue>
  153. {
  154. foreach (var parameter in parameters)
  155. parameter.ApplyToObject(instance);
  156. }
  157. }
  158. }