// // Copyright (C) 2014 Google Inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. // namespace GooglePlayGames.BasicApi.SavedGame { using System; using GooglePlayGames.OurUtils; /// /// A struct representing the mutation of saved game metadata. Fields can either have a new value /// or be untouched (in which case the corresponding field in the saved game metadata will be /// untouched). Instances must be built using /// and once created, these instances are immutable and threadsafe. /// public struct SavedGameMetadataUpdate { private readonly bool mDescriptionUpdated; private readonly string mNewDescription; private readonly bool mCoverImageUpdated; private readonly byte[] mNewPngCoverImage; private readonly TimeSpan? mNewPlayedTime; /// /// Initializes a new instance of the struct using the specified builder. /// /// The builder used to initialize the saved game metadata update. private SavedGameMetadataUpdate(Builder builder) { mDescriptionUpdated = builder.mDescriptionUpdated; mNewDescription = builder.mNewDescription; mCoverImageUpdated = builder.mCoverImageUpdated; mNewPngCoverImage = builder.mNewPngCoverImage; mNewPlayedTime = builder.mNewPlayedTime; } /// /// Gets whether the description has been updated in the metadata. /// public bool IsDescriptionUpdated { get { return mDescriptionUpdated; } } /// /// Gets the updated description for the saved game, if it has been changed. /// public string UpdatedDescription { get { return mNewDescription; } } /// /// Gets whether the cover image has been updated in the metadata. /// public bool IsCoverImageUpdated { get { return mCoverImageUpdated; } } /// /// Gets the updated PNG cover image, if it has been changed. /// public byte[] UpdatedPngCoverImage { get { return mNewPngCoverImage; } } /// /// Gets whether the played time has been updated in the metadata. /// public bool IsPlayedTimeUpdated { get { return mNewPlayedTime.HasValue; } } /// /// Gets the updated played time, if it has been changed. /// public TimeSpan? UpdatedPlayedTime { get { return mNewPlayedTime; } } /// /// A builder for constructing instances of . /// public struct Builder { internal bool mDescriptionUpdated; internal string mNewDescription; internal bool mCoverImageUpdated; internal byte[] mNewPngCoverImage; internal TimeSpan? mNewPlayedTime; /// /// Sets the description to be updated in the saved game metadata. /// /// The new description to set. /// The builder with the updated description. public Builder WithUpdatedDescription(string description) { mNewDescription = Misc.CheckNotNull(description); mDescriptionUpdated = true; return this; } /// /// Sets the PNG cover image to be updated in the saved game metadata. /// /// The new PNG image data for the cover image. /// The builder with the updated cover image. public Builder WithUpdatedPngCoverImage(byte[] newPngCoverImage) { mCoverImageUpdated = true; mNewPngCoverImage = newPngCoverImage; return this; } /// /// Sets the played time to be updated in the saved game metadata. /// /// The new played time to set. /// The builder with the updated played time. /// Thrown if the played time exceeds the maximum allowed value. public Builder WithUpdatedPlayedTime(TimeSpan newPlayedTime) { if (newPlayedTime.TotalMilliseconds > ulong.MaxValue) { throw new InvalidOperationException("Timespans longer than ulong.MaxValue " + "milliseconds are not allowed"); } mNewPlayedTime = newPlayedTime; return this; } /// /// Builds a new instance with the configured updates. /// /// A new instance of . public SavedGameMetadataUpdate Build() { return new SavedGameMetadataUpdate(this); } } } }