SRDebuggerUtil.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Reflection;
  5. using SRF.Helpers;
  6. using UnityEngine;
  7. using UnityEngine.EventSystems;
  8. namespace SRDebugger.Internal
  9. {
  10. public static class SRDebuggerUtil
  11. {
  12. public static bool IsMobilePlatform
  13. {
  14. get
  15. {
  16. if (Application.isMobilePlatform)
  17. {
  18. return true;
  19. }
  20. switch (Application.platform)
  21. {
  22. case RuntimePlatform.MetroPlayerX86:
  23. case RuntimePlatform.MetroPlayerX64:
  24. case RuntimePlatform.MetroPlayerARM:
  25. return true;
  26. default:
  27. return false;
  28. }
  29. }
  30. }
  31. public static bool EnsureEventSystemExists()
  32. {
  33. if (!Settings.Instance.EnableEventSystemGeneration)
  34. {
  35. return false;
  36. }
  37. if (EventSystem.current != null)
  38. {
  39. return false;
  40. }
  41. EventSystem eventSystem = UnityEngine.Object.FindObjectOfType<EventSystem>();
  42. if (eventSystem != null && eventSystem.gameObject.activeSelf && eventSystem.enabled)
  43. {
  44. return false;
  45. }
  46. UnityEngine.Debug.LogWarning("[SRDebugger] No EventSystem found in scene - creating a default one.");
  47. SRDebuggerUtil.CreateDefaultEventSystem();
  48. return true;
  49. }
  50. public static void CreateDefaultEventSystem()
  51. {
  52. GameObject gameObject = new GameObject("EventSystem");
  53. gameObject.AddComponent<EventSystem>();
  54. gameObject.AddComponent<StandaloneInputModule>();
  55. }
  56. public static ICollection<OptionDefinition> ScanForOptions(object obj)
  57. {
  58. List<OptionDefinition> list = new List<OptionDefinition>();
  59. MemberInfo[] members = obj.GetType().GetMembers(BindingFlags.Instance | BindingFlags.Public | BindingFlags.InvokeMethod | BindingFlags.GetProperty | BindingFlags.SetProperty);
  60. foreach (MemberInfo memberInfo in members)
  61. {
  62. CategoryAttribute attribute = SRReflection.GetAttribute<CategoryAttribute>(memberInfo);
  63. string category = (attribute != null) ? attribute.Category : "Default";
  64. SROptions.SortAttribute attribute2 = SRReflection.GetAttribute<SROptions.SortAttribute>(memberInfo);
  65. int sortPriority = (attribute2 != null) ? attribute2.SortPriority : 0;
  66. SROptions.DisplayNameAttribute attribute3 = SRReflection.GetAttribute<SROptions.DisplayNameAttribute>(memberInfo);
  67. string name = (attribute3 != null) ? attribute3.Name : memberInfo.Name;
  68. if (memberInfo is PropertyInfo)
  69. {
  70. PropertyInfo propertyInfo = memberInfo as PropertyInfo;
  71. if (propertyInfo.GetGetMethod() != null)
  72. {
  73. if ((propertyInfo.GetGetMethod().Attributes & MethodAttributes.Static) == MethodAttributes.PrivateScope)
  74. {
  75. list.Add(new OptionDefinition(name, category, sortPriority, new SRF.Helpers.PropertyReference(obj, propertyInfo)));
  76. }
  77. }
  78. }
  79. else if (memberInfo is MethodInfo)
  80. {
  81. MethodInfo methodInfo = memberInfo as MethodInfo;
  82. if (!methodInfo.IsStatic)
  83. {
  84. if (methodInfo.ReturnType == typeof(void) && methodInfo.GetParameters().Length <= 0)
  85. {
  86. list.Add(new OptionDefinition(name, category, sortPriority, new MethodReference(obj, methodInfo)));
  87. }
  88. }
  89. }
  90. }
  91. return list;
  92. }
  93. public static string GetNumberString(int value, int max, string exceedsMaxString)
  94. {
  95. if (value >= max)
  96. {
  97. return exceedsMaxString;
  98. }
  99. return value.ToString();
  100. }
  101. public static void ConfigureCanvas(Canvas canvas)
  102. {
  103. if (Settings.Instance.UseDebugCamera)
  104. {
  105. canvas.worldCamera = Service.DebugCamera.Camera;
  106. canvas.renderMode = RenderMode.ScreenSpaceCamera;
  107. }
  108. }
  109. }
  110. }