PlayGamesClientConfiguration.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. // <copyright file="PlayGamesClientConfiguration.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. #if UNITY_ANDROID
  17. namespace GooglePlayGames.BasicApi
  18. {
  19. using GooglePlayGames.OurUtils;
  20. using System.Collections.Generic;
  21. using System.Linq;
  22. /// <summary>
  23. /// Provides configuration for <see cref="PlayGamesPlatform"/>. If you wish to use either Saved
  24. /// Games or Cloud Save, you must create an instance of this class with those features enabled.
  25. /// Note that Cloud Save is deprecated, and is not available for new games. You should strongly
  26. /// favor Saved Game.
  27. /// </summary>
  28. public struct PlayGamesClientConfiguration
  29. {
  30. /// <summary>
  31. /// The default configuration.
  32. /// </summary>
  33. public static readonly PlayGamesClientConfiguration DefaultConfiguration =
  34. new Builder()
  35. .Build();
  36. /// <summary>
  37. /// Flag indicating to enable saved games API.
  38. /// </summary>
  39. private readonly bool mEnableSavedGames;
  40. /// <summary>
  41. /// Array of scopes to be requested from user. None is considered as 'games_lite'.
  42. /// </summary>
  43. private readonly string[] mScopes;
  44. /// <summary>
  45. /// The flag to indicate a server auth code should be requested when authenticating.
  46. /// </summary>
  47. private readonly bool mRequestAuthCode;
  48. /// <summary>
  49. /// The flag indicating the auth code should be refresh, causing re-consent and issuing a new refresh token.
  50. /// </summary>
  51. private readonly bool mForceRefresh;
  52. /// <summary>
  53. /// The flag indicating popup UIs should be hidden.
  54. /// </summary>
  55. private readonly bool mHidePopups;
  56. /// <summary>
  57. /// The flag indicating the email address should returned when authenticating.
  58. /// </summary>
  59. private readonly bool mRequestEmail;
  60. /// <summary>
  61. /// The flag indicating the id token should be returned when authenticating.
  62. /// </summary>
  63. private readonly bool mRequestIdToken;
  64. /// <summary>
  65. /// The account name to attempt to use when signing in. Null indicates use the default.
  66. /// </summary>
  67. private readonly string mAccountName;
  68. /// <summary>
  69. /// Initializes a new instance of the <see cref="GooglePlayGames.BasicApi.PlayGamesClientConfiguration"/> struct.
  70. /// </summary>
  71. /// <param name="builder">Builder for this configuration.</param>
  72. private PlayGamesClientConfiguration(Builder builder)
  73. {
  74. this.mEnableSavedGames = builder.HasEnableSaveGames();
  75. this.mScopes = builder.getScopes();
  76. this.mHidePopups = builder.IsHidingPopups();
  77. this.mRequestAuthCode = builder.IsRequestingAuthCode();
  78. this.mForceRefresh = builder.IsForcingRefresh();
  79. this.mRequestEmail = builder.IsRequestingEmail();
  80. this.mRequestIdToken = builder.IsRequestingIdToken();
  81. this.mAccountName = builder.GetAccountName();
  82. }
  83. /// <summary>
  84. /// Gets a value indicating whether this <see cref="GooglePlayGames.BasicApi.PlayGamesClientConfiguration"/>
  85. /// enable saved games.
  86. /// </summary>
  87. /// <value><c>true</c> if enable saved games; otherwise, <c>false</c>.</value>
  88. public bool EnableSavedGames
  89. {
  90. get { return mEnableSavedGames; }
  91. }
  92. public bool IsHidingPopups
  93. {
  94. get { return mHidePopups; }
  95. }
  96. public bool IsRequestingAuthCode
  97. {
  98. get { return mRequestAuthCode; }
  99. }
  100. public bool IsForcingRefresh
  101. {
  102. get { return mForceRefresh; }
  103. }
  104. public bool IsRequestingEmail
  105. {
  106. get { return mRequestEmail; }
  107. }
  108. public bool IsRequestingIdToken
  109. {
  110. get { return mRequestIdToken; }
  111. }
  112. public string AccountName
  113. {
  114. get { return mAccountName; }
  115. }
  116. /// <summary>
  117. /// Gets a array of scopes to be requested from the user.
  118. /// </summary>
  119. /// <value>String array of scopes.</value>
  120. public string[] Scopes
  121. {
  122. get { return mScopes; }
  123. }
  124. public static bool operator ==(PlayGamesClientConfiguration c1, PlayGamesClientConfiguration c2)
  125. {
  126. if (c1.EnableSavedGames != c2.EnableSavedGames ||
  127. c1.IsForcingRefresh != c2.IsForcingRefresh ||
  128. c1.IsHidingPopups != c2.IsHidingPopups ||
  129. c1.IsRequestingEmail != c2.IsRequestingEmail ||
  130. c1.IsRequestingAuthCode != c2.IsRequestingAuthCode ||
  131. !c1.Scopes.SequenceEqual(c2.Scopes) ||
  132. c1.AccountName != c2.AccountName)
  133. {
  134. return false;
  135. }
  136. return true;
  137. }
  138. public static bool operator !=(PlayGamesClientConfiguration c1, PlayGamesClientConfiguration c2)
  139. {
  140. return !(c1 == c2);
  141. }
  142. /// <summary>
  143. /// Builder class for the configuration.
  144. /// </summary>
  145. public class Builder
  146. {
  147. /// <summary>
  148. /// The flag to enable save games. Default is false.
  149. /// </summary>
  150. private bool mEnableSaveGames = false;
  151. /// <summary>
  152. /// The scopes to request from the user. Default is none.
  153. /// </summary>
  154. private List<string> mScopes = null;
  155. /// <summary>
  156. /// The flag indicating that popup UI should be hidden.
  157. /// </summary>
  158. private bool mHidePopups = false;
  159. /// <summary>
  160. /// The flag to indicate a server auth code should be requested when authenticating.
  161. /// </summary>
  162. private bool mRequestAuthCode = false;
  163. /// <summary>
  164. /// The flag indicating the auth code should be refresh, causing re-consent and issuing a new refresh token.
  165. /// </summary>
  166. private bool mForceRefresh = false;
  167. /// <summary>
  168. /// The flag indicating the email address should returned when authenticating.
  169. /// </summary>
  170. private bool mRequestEmail = false;
  171. /// <summary>
  172. /// The flag indicating the id token should be returned when authenticating.
  173. /// </summary>
  174. private bool mRequestIdToken = false;
  175. /// <summary>
  176. /// The account name to use as a default when authenticating.
  177. /// </summary>
  178. /// <remarks>
  179. /// This is only used when requesting auth code or id token.
  180. /// </remarks>
  181. private string mAccountName = null;
  182. /// <summary>
  183. /// Enables the saved games.
  184. /// </summary>
  185. /// <returns>The builder.</returns>
  186. public Builder EnableSavedGames()
  187. {
  188. mEnableSaveGames = true;
  189. return this;
  190. }
  191. /// <summary>
  192. /// Enables hiding popups. This is recommended for VR apps.
  193. /// </summary>
  194. /// <returns>The hide popups.</returns>
  195. public Builder EnableHidePopups()
  196. {
  197. mHidePopups = true;
  198. return this;
  199. }
  200. public Builder RequestServerAuthCode(bool forceRefresh)
  201. {
  202. mRequestAuthCode = true;
  203. mForceRefresh = forceRefresh;
  204. return this;
  205. }
  206. public Builder RequestEmail()
  207. {
  208. mRequestEmail = true;
  209. return this;
  210. }
  211. public Builder RequestIdToken()
  212. {
  213. mRequestIdToken = true;
  214. return this;
  215. }
  216. public Builder SetAccountName(string accountName)
  217. {
  218. mAccountName = accountName;
  219. return this;
  220. }
  221. /// <summary>
  222. /// Requests an Oauth scope from the user.
  223. /// </summary>
  224. /// <remarks>
  225. /// Not setting one will default to 'games_lite' and will not show a consent
  226. /// dialog to the user. Valid examples are 'profile' and 'email'.
  227. /// Full list: https://developers.google.com/identity/protocols/googlescopes
  228. /// To exchange the auth code with an id_token (or user id) on your server,
  229. /// you must add at least one scope.
  230. /// </remarks>
  231. /// <returns>The builder.</returns>
  232. public Builder AddOauthScope(string scope)
  233. {
  234. if (mScopes == null) mScopes = new List<string>();
  235. mScopes.Add(scope);
  236. return this;
  237. }
  238. /// <summary>
  239. /// Build this instance.
  240. /// </summary>
  241. /// <returns>the client configuration instance</returns>
  242. public PlayGamesClientConfiguration Build()
  243. {
  244. return new PlayGamesClientConfiguration(this);
  245. }
  246. /// <summary>
  247. /// Determines whether this instance has enable save games.
  248. /// </summary>
  249. /// <returns><c>true</c> if this instance has enable save games; otherwise, <c>false</c>.</returns>
  250. internal bool HasEnableSaveGames()
  251. {
  252. return mEnableSaveGames;
  253. }
  254. internal bool IsRequestingAuthCode()
  255. {
  256. return mRequestAuthCode;
  257. }
  258. internal bool IsHidingPopups()
  259. {
  260. return mHidePopups;
  261. }
  262. internal bool IsForcingRefresh()
  263. {
  264. return mForceRefresh;
  265. }
  266. internal bool IsRequestingEmail()
  267. {
  268. return mRequestEmail;
  269. }
  270. internal bool IsRequestingIdToken()
  271. {
  272. return mRequestIdToken;
  273. }
  274. internal string GetAccountName()
  275. {
  276. return mAccountName;
  277. }
  278. /// <summary>
  279. /// Gets the Oauth scopes to be requested from the user.
  280. /// </summary>
  281. /// <returns>String array of scopes.</returns>
  282. internal string[] getScopes()
  283. {
  284. return mScopes == null ? new string[0] : mScopes.ToArray();
  285. }
  286. }
  287. public override int GetHashCode()
  288. {
  289. unchecked
  290. {
  291. int hash = 17;
  292. hash = hash * 31 + EnableSavedGames.GetHashCode();
  293. hash = hash * 31 + IsForcingRefresh.GetHashCode();
  294. hash = hash * 31 + IsHidingPopups.GetHashCode();
  295. hash = hash * 31 + IsRequestingEmail.GetHashCode();
  296. hash = hash * 31 + IsRequestingAuthCode.GetHashCode();
  297. hash = hash * 31 + Scopes.GetHashCode();
  298. hash = hash * 31 + AccountName.GetHashCode();
  299. return hash;
  300. }
  301. }
  302. public override bool Equals(object obj)
  303. {
  304. return this == (PlayGamesClientConfiguration) obj;
  305. }
  306. }
  307. }
  308. #endif