コード例 #1
0
        /// Reads the settings from the Google Drive App Data Folder settings file. If the file doesn't
        /// exist, it will return null. If there is a failure in reading the file, a LoadSettingsFailed
        /// exception will be thrown.
        private async Task <UserData> DeserializeFromDriveAsync(CancellationToken token)
        {
            var fileId = await GetSettingsFileIdAsync(token);

            if (string.IsNullOrEmpty(fileId))
            {
                return(null);
            }
            var          downloadRequest = m_DriveService.Files.Get(fileId);
            MemoryStream jsonStream      = new MemoryStream();
            var          result          = await DriveAccess.Retry(() => {
                jsonStream.Seek(0, SeekOrigin.Begin);
                return(downloadRequest.DownloadAsync(jsonStream, token));
            });

            if (result.Status != DownloadStatus.Completed)
            {
                throw new LoadSettingsFailed($"{sm_SettingsFileName} failed to download.");
            }
            try {
                jsonStream.Seek(0, SeekOrigin.Begin);
                return(NewtonsoftJsonSerializer.Instance.Deserialize <UserData>(jsonStream));
            } catch (JsonException ex) {
                throw new LoadSettingsFailed($"{sm_SettingsFileName} was incorrectly formatted.", ex);
            }
        }
コード例 #2
0
        /// Saves the User data to Google Drive AppData folder. Will overwrite an existing Settings.json
        /// if it exists.
        private async Task <bool> SerializeToDriveAsync(CancellationToken token)
        {
            string json       = JsonConvert.SerializeObject(m_UserData);
            var    jsonStream = new MemoryStream(Encoding.UTF8.GetBytes(json));

            var fileId = await GetSettingsFileIdAsync(token);

            // the action varies slightly depending on whether we're creating a new file or overwriting an
            // old one.
            if (string.IsNullOrEmpty(fileId))
            {
                var file = new DriveData.File {
                    Parents = new[] { "appDataFolder" },
                    Name    = sm_SettingsFileName,
                };
                var request = m_DriveService.Files.Create(file, jsonStream, "application/json");
                var result  = await DriveAccess.Retry(() => {
                    jsonStream.Seek(0, SeekOrigin.Begin);
                    return(request.UploadAsync(token));
                });

                return(result.Status == UploadStatus.Completed);
            }
            else
            {
                var request = m_DriveService.Files.Update(new DriveData.File(), fileId, jsonStream,
                                                          "application/json");
                var result = await DriveAccess.Retry(() => {
                    jsonStream.Seek(0, SeekOrigin.Begin);
                    return(request.UploadAsync(token));
                });

                return(result.Status == UploadStatus.Completed);
            }
        }
コード例 #3
0
        /// Attempts to find the id of the settings file in the drive appdata folder. Will return null
        /// if no settings file is found.
        private async Task <string> GetSettingsFileIdAsync(CancellationToken token)
        {
            var findRequest = m_DriveService.Files.List();

            findRequest.Q      = $"name = '{sm_SettingsFileName}' and trashed = false";
            findRequest.Spaces = "appDataFolder";
            findRequest.Fields = "nextPageToken, files(id, name, modifiedTime)";
            var files = await DriveAccess.Retry(() => findRequest.ExecuteAsync(token));

            return(files.Files.OrderByDescending(x => x.ModifiedTime).FirstOrDefault()?.Id);
        }