Misc.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // <copyright file="Misc.cs" company="Google Inc.">
  2. // Copyright (C) 2014 Google Inc.
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. // </copyright>
  16. namespace GooglePlayGames.OurUtils
  17. {
  18. using System;
  19. using UnityEngine;
  20. public static class Misc
  21. {
  22. public static bool BuffersAreIdentical(byte[] a, byte[] b)
  23. {
  24. if (a == b)
  25. {
  26. // not only identical but the very same!
  27. return true;
  28. }
  29. if (a == null || b == null)
  30. {
  31. // one of them is null, the other one isn't
  32. return false;
  33. }
  34. if (a.Length != b.Length)
  35. {
  36. return false;
  37. }
  38. for (int i = 0; i < a.Length; i++)
  39. {
  40. if (a[i] != b[i])
  41. {
  42. return false;
  43. }
  44. }
  45. return true;
  46. }
  47. public static byte[] GetSubsetBytes(byte[] array, int offset, int length)
  48. {
  49. if (array == null)
  50. {
  51. throw new ArgumentNullException("array");
  52. }
  53. if (offset < 0 || offset >= array.Length)
  54. {
  55. throw new ArgumentOutOfRangeException("offset");
  56. }
  57. if (length < 0 || (array.Length - offset) < length)
  58. {
  59. throw new ArgumentOutOfRangeException("length");
  60. }
  61. if (offset == 0 && length == array.Length)
  62. {
  63. return array;
  64. }
  65. byte[] piece = new byte[length];
  66. Array.Copy(array, offset, piece, 0, length);
  67. return piece;
  68. }
  69. public static T CheckNotNull<T>(T value)
  70. {
  71. if (value == null)
  72. {
  73. throw new ArgumentNullException();
  74. }
  75. return value;
  76. }
  77. public static T CheckNotNull<T>(T value, string paramName)
  78. {
  79. if (value == null)
  80. {
  81. throw new ArgumentNullException(paramName);
  82. }
  83. return value;
  84. }
  85. public static bool IsApiException(AndroidJavaObject exception) {
  86. var exceptionClassName = exception.Call<AndroidJavaObject>("getClass")
  87. .Call<String>("getName");
  88. return exceptionClassName == "com.google.android.gms.common.api.ApiException";
  89. }
  90. }
  91. }