ComparerBase.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. using System;
  2. using UnityEngine;
  3. using Object = System.Object;
  4. namespace UnityTest
  5. {
  6. public abstract class ComparerBase : ActionBase
  7. {
  8. public enum CompareToType
  9. {
  10. CompareToObject,
  11. CompareToConstantValue,
  12. CompareToNull
  13. }
  14. public CompareToType compareToType = CompareToType.CompareToObject;
  15. public GameObject other;
  16. public string otherPropertyPath = "";
  17. protected abstract bool Compare (object a, object b);
  18. protected override bool Compare(object objVal)
  19. {
  20. object objOtherVal;
  21. if (compareToType == CompareToType.CompareToConstantValue)
  22. {
  23. objOtherVal = ConstValue;
  24. }
  25. else if (compareToType == CompareToType.CompareToNull)
  26. {
  27. objOtherVal = null;
  28. }
  29. else
  30. {
  31. if (other == null)
  32. objOtherVal = null;
  33. else
  34. objOtherVal = GetPropertyValue (other,
  35. otherPropertyPath,
  36. GetAccepatbleTypesForB ());
  37. }
  38. return Compare (objVal,
  39. objOtherVal);
  40. }
  41. public virtual Type[] GetAccepatbleTypesForB()
  42. {
  43. return null;
  44. }
  45. #region Const value
  46. public virtual object ConstValue { get; set; }
  47. public virtual object GetDefaultConstValue()
  48. {
  49. throw new NotImplementedException();
  50. }
  51. #endregion
  52. public virtual Type GetSecondParameterType() { return typeof(object); }
  53. public object GetOtherPropertyValue ()
  54. {
  55. switch (compareToType)
  56. {
  57. case CompareToType.CompareToObject:
  58. return PropertyResolver.GetPropertyValueFromString(other,
  59. otherPropertyPath);
  60. case CompareToType.CompareToConstantValue:
  61. return ConstValue;
  62. case CompareToType.CompareToNull:
  63. default:
  64. return null;
  65. }
  66. }
  67. }
  68. [Serializable]
  69. public abstract class ComparerBaseGeneric<T> : ComparerBaseGeneric<T,T>
  70. {
  71. }
  72. [Serializable]
  73. public abstract class ComparerBaseGeneric<T1, T2> : ComparerBase
  74. {
  75. public T2 constantValueGeneric = default(T2);
  76. public override Object ConstValue
  77. {
  78. get
  79. {
  80. return constantValueGeneric;
  81. }
  82. set
  83. {
  84. constantValueGeneric = (T2) value;
  85. }
  86. }
  87. public override Object GetDefaultConstValue()
  88. {
  89. return default(T2);
  90. }
  91. protected override bool Compare(object a, object b)
  92. {
  93. var type = typeof(T2);
  94. if (b == null && type.IsValueType)
  95. {
  96. throw new ArgumentException("Null was passed to a value-type argument");
  97. }
  98. return Compare((T1)a, (T2)b);
  99. }
  100. protected abstract bool Compare(T1 a, T2 b);
  101. public override Type[] GetAccepatbleTypesForA()
  102. {
  103. return new[] { typeof(T1) };
  104. }
  105. public override Type[] GetAccepatbleTypesForB ()
  106. {
  107. return new[] {typeof (T2)};
  108. }
  109. public override Type GetParameterType()
  110. {
  111. return typeof(T1);
  112. }
  113. public override Type GetSecondParameterType()
  114. {
  115. return typeof(T2);
  116. }
  117. }
  118. }