Пример #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);
        }
Пример #4
0
            public GoogleDriveFileInfo(DriveData.File file, string source)
            {
                m_File     = file;
                m_FileName = Path.Combine(CachePath, m_File.Id + ".tilt");
                if (File.Exists(m_FileName))
                {
                    var fileInfo = new FileInfo(m_FileName);
                    if (DriveAccess.IsNewer(m_File, fileInfo))
                    {
                        File.Delete(m_FileName);
                    }
                    else
                    {
                        m_TiltFile = new TiltFile(m_FileName);
                    }
                }

                m_Source = source;
            }
Пример #5
0
            private void OnGUI()
            {
                if (!Application.isPlaying)
                {
                    EditorGUILayout.HelpBox("Only works in Play Mode.", MessageType.Info);
                    return;
                }
                if (App.Instance == null || App.DriveSync == null)
                {
                    EditorGUILayout.HelpBox("DriveSync has not been constructed yet.", MessageType.Info);
                    return;
                }

                DriveSync   ds = App.DriveSync;
                DriveAccess da = App.DriveAccess;

                EditorGUILayout.Toggle("m_InitTask", ds.m_InitTask != null);
                EditorGUILayout.Toggle("m_SyncTask", ds.m_SyncTask != null);
                EditorGUILayout.Toggle("m_UpdateTask", ds.m_UpdateTask != null);
                EditorGUILayout.Toggle("Initialized", ds.m_Initialized);
                EditorGUILayout.Toggle("Uninitializing", ds.m_Uninitializing);
                if (da.HasSpaceQuota)
                {
                    float megabytes = ((float)App.DriveAccess.DriveFreeSpace) / (1024 * 1024);
                    EditorGUILayout.LabelField("Free space (MB)", $"{megabytes}MB");
                }
                else
                {
                    GUILayout.Label("Drive has no usage quota.");
                }
                GUILayout.Label("Transfers:");
                foreach (var transfer in ds.m_Transfers.Keys)
                {
                    GUILayout.Label($"{transfer.Item.AbsoluteLocalPath}\\{transfer.Item.Name}");
                }
                EditorGUILayout.LabelField("Remaining to transfer:", $"{ds.m_ToTransfer.Count}");
                EditorGUILayout.LabelField("m_TotalBytesToTransfer:", $"{ds.m_TotalBytesToTransfer}");
                EditorGUILayout.LabelField("m_BytesTransferred:", $"{ds.m_BytesTransferred}");
                long bytesTransferred = ds.m_BytesTransferred;

                bytesTransferred += ds.m_Transfers.Keys.Sum(x => x.BytesTransferred);
                EditorGUILayout.LabelField("Running bytes tally", $"{bytesTransferred}");
                EditorGUILayout.LabelField("Progress:", $"{ds.Progress}");
                GUILayout.Label("Folders to transfer:");
                foreach (var folder in ds.m_Folders.Where(x => x?.Drive != null))
                {
                    EditorGUILayout.LabelField(folder.FolderType.ToString(), folder.Drive.Id);
                }
                GUILayout.Label("Drive State");
                EditorGUILayout.Toggle("GoogleIdentity.LoggedIn", App.GoogleIdentity.LoggedIn);
                EditorGUILayout.Toggle("DriveAccess.Initializing", App.DriveAccess.Initializing);
                EditorGUILayout.Toggle("DriveAccess.Ready", App.DriveAccess.Ready);
                EditorGUILayout.Toggle("DriveSync.Initializing", App.DriveSync.Initializing);
                EditorGUILayout.Toggle("DriveSync.Initialized", App.DriveSync.Initialized);
                EditorGUILayout.Toggle("DriveSync.Syncing", App.DriveSync.Syncing);
                var sketchset = SketchCatalog.m_Instance.GetSet(SketchSetType.Drive);

                if (sketchset != null)
                {
                    EditorGUILayout.Toggle("SketchSet.IsReadyForAccess", sketchset.IsReadyForAccess);
                    EditorGUILayout.Toggle("SketchSet.IsActivelyRefreshingSketches",
                                           sketchset.IsActivelyRefreshingSketches);
                }
            }