Assignment for RMIT Mixed Reality in 2020
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.

117 lines
4.3 KiB

  1. // SDK Scripting Define Symbol Predicate|SDK_Base|003
  2. namespace VRTK
  3. {
  4. #if UNITY_EDITOR
  5. using UnityEditor;
  6. #endif
  7. using UnityEngine;
  8. using System;
  9. /// <summary>
  10. /// Specifies a method to be used as a predicate to allow VRTK_SDKManager to automatically add and remove scripting define symbols. Only allowed on static methods that take no arguments and return a `bool`.
  11. /// </summary>
  12. [Serializable]
  13. [AttributeUsage(AttributeTargets.Method, AllowMultiple = true, Inherited = false)]
  14. public sealed class SDK_ScriptingDefineSymbolPredicateAttribute : Attribute, ISerializationCallbackReceiver
  15. {
  16. /// <summary>
  17. /// The prefix of scripting define symbols that must be used to be able to automatically remove the symbols.
  18. /// </summary>
  19. public const string RemovableSymbolPrefix = "VRTK_DEFINE_";
  20. /// <summary>
  21. /// The scripting define symbol to conditionally add or remove.
  22. /// </summary>
  23. public string symbol;
  24. #if UNITY_EDITOR
  25. /// <summary>
  26. /// The build target group to use when conditionally adding or removing symbol.
  27. /// </summary>
  28. [NonSerialized]
  29. public BuildTargetGroup buildTargetGroup;
  30. #endif
  31. [SerializeField]
  32. private string buildTargetGroupName;
  33. private SDK_ScriptingDefineSymbolPredicateAttribute()
  34. {
  35. }
  36. /// <summary>
  37. /// Creates a new attribute.
  38. /// </summary>
  39. /// <param name="symbol">The scripting define symbol to conditionally add or remove. Needs to start with `RemovableSymbolPrefix` to be able to automatically remove the symbol. `null` and `string.Empty` are not allowed.</param>
  40. /// <param name="buildTargetGroupName">The name of a constant of `BuildTargetGroup`. `BuildTargetGroup.Unknown`, `null` and `string.Empty` are not allowed.</param>
  41. public SDK_ScriptingDefineSymbolPredicateAttribute(string symbol, string buildTargetGroupName)
  42. {
  43. if (symbol == null)
  44. {
  45. VRTK_Logger.Fatal(new ArgumentNullException("symbol"));
  46. return;
  47. }
  48. if (symbol == string.Empty)
  49. {
  50. VRTK_Logger.Fatal(new ArgumentOutOfRangeException("symbol", symbol, "An empty string isn't allowed."));
  51. return;
  52. }
  53. this.symbol = symbol;
  54. if (buildTargetGroupName == null)
  55. {
  56. VRTK_Logger.Fatal(new ArgumentNullException("buildTargetGroupName"));
  57. return;
  58. }
  59. if (buildTargetGroupName == string.Empty)
  60. {
  61. VRTK_Logger.Fatal(new ArgumentOutOfRangeException("buildTargetGroupName", buildTargetGroupName, "An empty string isn't allowed."));
  62. return;
  63. }
  64. SetBuildTarget(buildTargetGroupName);
  65. }
  66. /// <summary>
  67. /// Creates a new attribute by copying an existing one.
  68. /// </summary>
  69. /// <param name="attributeToCopy">The attribute to copy.</param>
  70. public SDK_ScriptingDefineSymbolPredicateAttribute(SDK_ScriptingDefineSymbolPredicateAttribute attributeToCopy)
  71. {
  72. symbol = attributeToCopy.symbol;
  73. SetBuildTarget(attributeToCopy.buildTargetGroupName);
  74. }
  75. public void OnBeforeSerialize()
  76. {
  77. }
  78. public void OnAfterDeserialize()
  79. {
  80. SetBuildTarget(buildTargetGroupName);
  81. }
  82. private void SetBuildTarget(string groupName)
  83. {
  84. buildTargetGroupName = groupName;
  85. #if UNITY_EDITOR
  86. Type buildTargetGroupType = typeof(BuildTargetGroup);
  87. try
  88. {
  89. buildTargetGroup = (BuildTargetGroup)Enum.Parse(buildTargetGroupType, groupName);
  90. }
  91. catch (Exception exception)
  92. {
  93. VRTK_Logger.Fatal(new ArgumentOutOfRangeException(string.Format("'{0}' isn't a valid constant name of '{1}'.", groupName, buildTargetGroupType.Name), exception));
  94. return;
  95. }
  96. if (buildTargetGroup == BuildTargetGroup.Unknown)
  97. {
  98. VRTK_Logger.Fatal(new ArgumentOutOfRangeException("groupName", groupName, string.Format("'{0}' isn't allowed.", groupName)));
  99. return;
  100. }
  101. #endif
  102. }
  103. }
  104. }