SavedGameMetadataUpdate.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. // <copyright file="SavedGameMetadataUpdate.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.BasicApi.SavedGame
  17. {
  18. using System;
  19. using GooglePlayGames.OurUtils;
  20. /// <summary>
  21. /// A struct representing the mutation of saved game metadata. Fields can either have a new value
  22. /// or be untouched (in which case the corresponding field in the saved game metadata will be
  23. /// untouched). Instances must be built using <see cref="SavedGameMetadataUpdate.Builder"/>
  24. /// and once created, these instances are immutable and threadsafe.
  25. /// </summary>
  26. public struct SavedGameMetadataUpdate
  27. {
  28. private readonly bool mDescriptionUpdated;
  29. private readonly string mNewDescription;
  30. private readonly bool mCoverImageUpdated;
  31. private readonly byte[] mNewPngCoverImage;
  32. private readonly TimeSpan? mNewPlayedTime;
  33. private SavedGameMetadataUpdate(Builder builder)
  34. {
  35. mDescriptionUpdated = builder.mDescriptionUpdated;
  36. mNewDescription = builder.mNewDescription;
  37. mCoverImageUpdated = builder.mCoverImageUpdated;
  38. mNewPngCoverImage = builder.mNewPngCoverImage;
  39. mNewPlayedTime = builder.mNewPlayedTime;
  40. }
  41. public bool IsDescriptionUpdated
  42. {
  43. get { return mDescriptionUpdated; }
  44. }
  45. public string UpdatedDescription
  46. {
  47. get { return mNewDescription; }
  48. }
  49. public bool IsCoverImageUpdated
  50. {
  51. get { return mCoverImageUpdated; }
  52. }
  53. public byte[] UpdatedPngCoverImage
  54. {
  55. get { return mNewPngCoverImage; }
  56. }
  57. public bool IsPlayedTimeUpdated
  58. {
  59. get { return mNewPlayedTime.HasValue; }
  60. }
  61. public TimeSpan? UpdatedPlayedTime
  62. {
  63. get { return mNewPlayedTime; }
  64. }
  65. public struct Builder
  66. {
  67. internal bool mDescriptionUpdated;
  68. internal string mNewDescription;
  69. internal bool mCoverImageUpdated;
  70. internal byte[] mNewPngCoverImage;
  71. internal TimeSpan? mNewPlayedTime;
  72. public Builder WithUpdatedDescription(string description)
  73. {
  74. mNewDescription = Misc.CheckNotNull(description);
  75. mDescriptionUpdated = true;
  76. return this;
  77. }
  78. public Builder WithUpdatedPngCoverImage(byte[] newPngCoverImage)
  79. {
  80. mCoverImageUpdated = true;
  81. mNewPngCoverImage = newPngCoverImage;
  82. return this;
  83. }
  84. public Builder WithUpdatedPlayedTime(TimeSpan newPlayedTime)
  85. {
  86. if (newPlayedTime.TotalMilliseconds > ulong.MaxValue)
  87. {
  88. throw new InvalidOperationException("Timespans longer than ulong.MaxValue " +
  89. "milliseconds are not allowed");
  90. }
  91. mNewPlayedTime = newPlayedTime;
  92. return this;
  93. }
  94. public SavedGameMetadataUpdate Build()
  95. {
  96. return new SavedGameMetadataUpdate(this);
  97. }
  98. }
  99. }
  100. }