Brak opisu
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.

CollabPlugin.cs 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. using System;
  2. using System.Linq;
  3. using System.Reflection;
  4. using UnityEditor;
  5. using PackageManager = UnityEditor.PackageManager;
  6. using Unity.PlasticSCM.Editor.UI;
  7. namespace Unity.PlasticSCM.Editor
  8. {
  9. public static class CollabPlugin
  10. {
  11. public static bool IsEnabled()
  12. {
  13. return IsCollabInstanceEnabled();
  14. }
  15. internal static void Disable()
  16. {
  17. SetCollabEnabledInstanceAs(false);
  18. SetCollabEnabledInProjectSettingsAs(false);
  19. }
  20. internal static void GetVersion(Action<string> onGetVersionCompleted)
  21. {
  22. PackageManager.Requests.ListRequest listRequest = PackageManager.Client.List(true);
  23. RunGetVersion(listRequest, onGetVersionCompleted);
  24. }
  25. internal static void Enable()
  26. {
  27. SetCollabEnabledInstanceAs(true);
  28. SetCollabEnabledInProjectSettingsAs(true);
  29. }
  30. static void SetCollabEnabledInstanceAs(bool value)
  31. {
  32. object collabInstance = GetCollabInstance();
  33. if (collabInstance == null)
  34. return;
  35. // Invokes Collab.instance.SetCollabEnabledForCurrentProject(false)
  36. SetCollabEnabledForCurrentProject(collabInstance, value);
  37. }
  38. static void RunGetVersion(
  39. PackageManager.Requests.ListRequest listRequest,
  40. Action<string> onGetVersionCompleted)
  41. {
  42. EditorDispatcher.Dispatch(() =>
  43. {
  44. if (!listRequest.IsCompleted)
  45. {
  46. RunGetVersion(listRequest, onGetVersionCompleted);
  47. return;
  48. }
  49. string pluginVersion = string.Empty;
  50. if (listRequest.Status == PackageManager.StatusCode.Success &&
  51. listRequest.Result != null)
  52. {
  53. PackageManager.PackageInfo collabPackage = listRequest.Result
  54. .FirstOrDefault(package => package.name == mCollabPackageName);
  55. if (collabPackage != null)
  56. pluginVersion = collabPackage.version;
  57. }
  58. onGetVersionCompleted.Invoke(pluginVersion);
  59. });
  60. }
  61. static void SetCollabEnabledInProjectSettingsAs(bool value)
  62. {
  63. // Invokes PlayerSettings.SetCloudServiceEnabled("Collab", false)
  64. SetCloudServiceEnabled("Collab", value);
  65. AssetDatabase.SaveAssets();
  66. }
  67. static bool IsCollabInstanceEnabled()
  68. {
  69. object collabInstance = GetCollabInstance();
  70. if (collabInstance == null)
  71. return false;
  72. // Invokes Collab.instance.IsCollabEnabledForCurrentProject()
  73. return IsCollabEnabledForCurrentProject(collabInstance);
  74. }
  75. static void SetCollabEnabledForCurrentProject(object collabInstance, bool enable)
  76. {
  77. MethodInfo InternalSetCollabEnabledForCurrentProject =
  78. CollabType.GetMethod("SetCollabEnabledForCurrentProject");
  79. if (InternalSetCollabEnabledForCurrentProject == null)
  80. return;
  81. InternalSetCollabEnabledForCurrentProject.
  82. Invoke(collabInstance, new object[] { enable });
  83. }
  84. static void SetCloudServiceEnabled(string setting, bool enable)
  85. {
  86. MethodInfo InternalSetCloudServiceEnabled = PlayerSettingsType.GetMethod(
  87. "SetCloudServiceEnabled",
  88. BindingFlags.NonPublic | BindingFlags.Static);
  89. if (InternalSetCloudServiceEnabled == null)
  90. return;
  91. InternalSetCloudServiceEnabled.
  92. Invoke(null, new object[] { setting, enable });
  93. }
  94. static object GetCollabInstance()
  95. {
  96. if (CollabType == null)
  97. return null;
  98. PropertyInfo InternalInstance =
  99. CollabType.GetProperty("instance");
  100. if (InternalInstance == null)
  101. return null;
  102. return InternalInstance.GetValue(null, null);
  103. }
  104. static bool IsCollabEnabledForCurrentProject(object collabInstance)
  105. {
  106. MethodInfo InternalIsCollabEnabledForCurrentProject =
  107. CollabType.GetMethod("IsCollabEnabledForCurrentProject");
  108. if (InternalIsCollabEnabledForCurrentProject == null)
  109. return false;
  110. return (bool)InternalIsCollabEnabledForCurrentProject.
  111. Invoke(collabInstance, null);
  112. }
  113. static readonly Type CollabType =
  114. typeof(UnityEditor.Editor).Assembly.
  115. GetType("UnityEditor.Collaboration.Collab");
  116. static readonly Type PlayerSettingsType =
  117. typeof(UnityEditor.PlayerSettings);
  118. static readonly string mCollabPackageName = "com.unity.collab-proxy";
  119. }
  120. }