HotPatchAndroidResolverScope.cs 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Reflection;
  5. using UnityEditor;
  6. namespace HotPatch.Editor
  7. {
  8. internal sealed class HotPatchAndroidResolverScope : IDisposable
  9. {
  10. readonly List<(PropertyInfo Property, bool Value)> previous = new List<(PropertyInfo, bool)>();
  11. public HotPatchAndroidResolverScope()
  12. {
  13. if (EditorUserBuildSettings.activeBuildTarget != BuildTarget.Android) return;
  14. var type = AppDomain.CurrentDomain.GetAssemblies().Select(a => a.GetType("GooglePlayServices.SettingsDialog"))
  15. .FirstOrDefault(t => t != null);
  16. if (type == null) return;
  17. try
  18. {
  19. foreach (var name in new[] { "AutoResolveOnBuild", "EnableAutoResolution" })
  20. {
  21. var property = type.GetProperty(name, BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
  22. if (property == null || !property.CanRead || !property.CanWrite || property.PropertyType != typeof(bool)) continue;
  23. previous.Add((property, (bool)property.GetValue(null)));
  24. property.SetValue(null, false);
  25. }
  26. }
  27. catch { Dispose(); throw; }
  28. }
  29. public void Dispose()
  30. {
  31. foreach (var item in previous) item.Property.SetValue(null, item.Value);
  32. previous.Clear();
  33. }
  34. }
  35. }