PlayGamesClientConfiguration.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  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 || (UNITY_IPHONE && !NO_GPGS))
  17. namespace GooglePlayGames.BasicApi
  18. {
  19. using GooglePlayGames.BasicApi.Multiplayer;
  20. using GooglePlayGames.OurUtils;
  21. using System.Collections.Generic;
  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. /// The invitation delegate.
  70. /// </summary>
  71. private readonly InvitationReceivedDelegate mInvitationDelegate;
  72. /// <summary>
  73. /// The match delegate.
  74. /// </summary>
  75. private readonly MatchDelegate mMatchDelegate;
  76. /// <summary>
  77. /// Initializes a new instance of the <see cref="GooglePlayGames.BasicApi.PlayGamesClientConfiguration"/> struct.
  78. /// </summary>
  79. /// <param name="builder">Builder for this configuration.</param>
  80. private PlayGamesClientConfiguration(Builder builder)
  81. {
  82. this.mEnableSavedGames = builder.HasEnableSaveGames();
  83. this.mInvitationDelegate = builder.GetInvitationDelegate();
  84. this.mMatchDelegate = builder.GetMatchDelegate();
  85. this.mScopes = builder.getScopes();
  86. this.mHidePopups = builder.IsHidingPopups();
  87. this.mRequestAuthCode = builder.IsRequestingAuthCode();
  88. this.mForceRefresh = builder.IsForcingRefresh();
  89. this.mRequestEmail = builder.IsRequestingEmail();
  90. this.mRequestIdToken = builder.IsRequestingIdToken();
  91. this.mAccountName = builder.GetAccountName();
  92. }
  93. /// <summary>
  94. /// Gets a value indicating whether this <see cref="GooglePlayGames.BasicApi.PlayGamesClientConfiguration"/>
  95. /// enable saved games.
  96. /// </summary>
  97. /// <value><c>true</c> if enable saved games; otherwise, <c>false</c>.</value>
  98. public bool EnableSavedGames
  99. {
  100. get
  101. {
  102. return mEnableSavedGames;
  103. }
  104. }
  105. public bool IsHidingPopups
  106. {
  107. get
  108. {
  109. return mHidePopups;
  110. }
  111. }
  112. public bool IsRequestingAuthCode
  113. {
  114. get
  115. {
  116. return mRequestAuthCode;
  117. }
  118. }
  119. public bool IsForcingRefresh
  120. {
  121. get
  122. {
  123. return mForceRefresh;
  124. }
  125. }
  126. public bool IsRequestingEmail
  127. {
  128. get
  129. {
  130. return mRequestEmail;
  131. }
  132. }
  133. public bool IsRequestingIdToken
  134. {
  135. get
  136. {
  137. return mRequestIdToken;
  138. }
  139. }
  140. public string AccountName
  141. {
  142. get
  143. {
  144. return mAccountName;
  145. }
  146. }
  147. /// <summary>
  148. /// Gets a array of scopes to be requested from the user.
  149. /// </summary>
  150. /// <value>String array of scopes.</value>
  151. public string[] Scopes
  152. {
  153. get
  154. {
  155. return mScopes;
  156. }
  157. }
  158. /// <summary>
  159. /// Gets the invitation delegate.
  160. /// </summary>
  161. /// <value>The invitation delegate.</value>
  162. public InvitationReceivedDelegate InvitationDelegate
  163. {
  164. get
  165. {
  166. return mInvitationDelegate;
  167. }
  168. }
  169. /// <summary>
  170. /// Gets the match delegate.
  171. /// </summary>
  172. /// <value>The match delegate.</value>
  173. public MatchDelegate MatchDelegate
  174. {
  175. get
  176. {
  177. return mMatchDelegate;
  178. }
  179. }
  180. /// <summary>
  181. /// Builder class for the configuration.
  182. /// </summary>
  183. public class Builder
  184. {
  185. /// <summary>
  186. /// The flag to enable save games. Default is false.
  187. /// </summary>
  188. private bool mEnableSaveGames = false;
  189. /// <summary>
  190. /// The scopes to request from the user. Default is none.
  191. /// </summary>
  192. private List<string> mScopes = null;
  193. /// <summary>
  194. /// The flag indicating that popup UI should be hidden.
  195. /// </summary>
  196. private bool mHidePopups = false;
  197. /// <summary>
  198. /// The flag to indicate a server auth code should be requested when authenticating.
  199. /// </summary>
  200. private bool mRequestAuthCode = false;
  201. /// <summary>
  202. /// The flag indicating the auth code should be refresh, causing re-consent and issuing a new refresh token.
  203. /// </summary>
  204. private bool mForceRefresh = false;
  205. /// <summary>
  206. /// The flag indicating the email address should returned when authenticating.
  207. /// </summary>
  208. private bool mRequestEmail = false;
  209. /// <summary>
  210. /// The flag indicating the id token should be returned when authenticating.
  211. /// </summary>
  212. private bool mRequestIdToken = false;
  213. /// <summary>
  214. /// The account name to use as a default when authenticating.
  215. /// </summary>
  216. /// <remarks>
  217. /// This is only used when requesting auth code or id token.
  218. /// </remarks>
  219. private string mAccountName = null;
  220. /// <summary>
  221. /// The invitation delegate. Default is a no-op;
  222. /// </summary>
  223. private InvitationReceivedDelegate mInvitationDelegate = delegate
  224. {
  225. };
  226. /// <summary>
  227. /// The match delegate. Default is a no-op.
  228. /// </summary>
  229. private MatchDelegate mMatchDelegate = delegate
  230. {
  231. };
  232. /// <summary>
  233. /// Enables the saved games.
  234. /// </summary>
  235. /// <returns>The builder.</returns>
  236. public Builder EnableSavedGames()
  237. {
  238. mEnableSaveGames = true;
  239. return this;
  240. }
  241. /// <summary>
  242. /// Enables hiding popups. This is recommended for VR apps.
  243. /// </summary>
  244. /// <returns>The hide popups.</returns>
  245. public Builder EnableHidePopups()
  246. {
  247. mHidePopups = true;
  248. return this;
  249. }
  250. public Builder RequestServerAuthCode(bool forceRefresh)
  251. {
  252. mRequestAuthCode = true;
  253. mForceRefresh = forceRefresh;
  254. return this;
  255. }
  256. public Builder RequestEmail()
  257. {
  258. mRequestEmail = true;
  259. return this;
  260. }
  261. public Builder RequestIdToken()
  262. {
  263. mRequestIdToken = true;
  264. return this;
  265. }
  266. public Builder SetAccountName(string accountName)
  267. {
  268. mAccountName = accountName;
  269. return this;
  270. }
  271. /// <summary>
  272. /// Requests an Oauth scope from the user.
  273. /// </summary>
  274. /// <remarks>
  275. /// Not setting one will default to 'games_lite' and will not show a consent
  276. /// dialog to the user. Valid examples are 'profile' and 'email'.
  277. /// Full list: https://developers.google.com/identity/protocols/googlescopes
  278. /// To exchange the auth code with an id_token (or user id) on your server,
  279. /// you must add at least one scope.
  280. /// </remarks>
  281. /// <returns>The builder.</returns>
  282. public Builder AddOauthScope(string scope)
  283. {
  284. if (mScopes == null) mScopes = new List<string>();
  285. mScopes.Add(scope);
  286. return this;
  287. }
  288. /// <summary>
  289. /// Adds the invitation delegate. This is called when an invitation
  290. /// is received.
  291. /// </summary>
  292. /// <returns>The builder</returns>
  293. /// <param name="invitationDelegate">Invitation delegate.</param>
  294. public Builder WithInvitationDelegate(InvitationReceivedDelegate invitationDelegate)
  295. {
  296. this.mInvitationDelegate = Misc.CheckNotNull(invitationDelegate);
  297. return this;
  298. }
  299. /// <summary>
  300. /// Adds the match delegate. This is called when a match notification
  301. /// is received.
  302. /// </summary>
  303. /// <returns>The builder.</returns>
  304. /// <param name="matchDelegate">Match delegate.</param>
  305. public Builder WithMatchDelegate(MatchDelegate matchDelegate)
  306. {
  307. this.mMatchDelegate = Misc.CheckNotNull(matchDelegate);
  308. return this;
  309. }
  310. /// <summary>
  311. /// Build this instance.
  312. /// </summary>
  313. /// <returns>the client configuration instance</returns>
  314. public PlayGamesClientConfiguration Build()
  315. {
  316. return new PlayGamesClientConfiguration(this);
  317. }
  318. /// <summary>
  319. /// Determines whether this instance has enable save games.
  320. /// </summary>
  321. /// <returns><c>true</c> if this instance has enable save games; otherwise, <c>false</c>.</returns>
  322. internal bool HasEnableSaveGames()
  323. {
  324. return mEnableSaveGames;
  325. }
  326. internal bool IsRequestingAuthCode()
  327. {
  328. return mRequestAuthCode;
  329. }
  330. internal bool IsHidingPopups()
  331. {
  332. return mHidePopups;
  333. }
  334. internal bool IsForcingRefresh()
  335. {
  336. return mForceRefresh;
  337. }
  338. internal bool IsRequestingEmail()
  339. {
  340. return mRequestEmail;
  341. }
  342. internal bool IsRequestingIdToken()
  343. {
  344. return mRequestIdToken;
  345. }
  346. internal string GetAccountName()
  347. {
  348. return mAccountName;
  349. }
  350. /// <summary>
  351. /// Gets the Oauth scopes to be requested from the user.
  352. /// </summary>
  353. /// <returns>String array of scopes.</returns>
  354. internal string[] getScopes() {
  355. return mScopes == null? new string[0] : mScopes.ToArray();
  356. }
  357. /// <summary>
  358. /// Gets the match delegate.
  359. /// </summary>
  360. /// <returns>The match delegate.</returns>
  361. internal MatchDelegate GetMatchDelegate()
  362. {
  363. return mMatchDelegate;
  364. }
  365. /// <summary>
  366. /// Gets the invitation delegate.
  367. /// </summary>
  368. /// <returns>The invitation delegate.</returns>
  369. internal InvitationReceivedDelegate GetInvitationDelegate()
  370. {
  371. return mInvitationDelegate;
  372. }
  373. }
  374. }
  375. }
  376. #endif