public override async UniTask RunAsync() { var startTime = Time.time; var filePath = string.Concat(RootPath, '/', Path); foreach (var representation in converter.Representations) { var fullPath = string.Concat(filePath, representation.Extension); if (!File.Exists(fullPath)) { continue; } rawData = await IOUtils.ReadFileAsync(fullPath); break; } if (rawData is null) { var usedExtensions = string.Join("/", converter.Representations.Select(r => r.Extension)); Debug.LogError($"Failed to load `{filePath}({usedExtensions})` resource using local file system: File not found."); SetResult(new Resource <TResource>(Path, null, Provider)); return; } var obj = await converter.ConvertAsync(rawData, System.IO.Path.GetFileNameWithoutExtension(Path)); var result = new Resource <TResource>(Path, obj, Provider); SetResult(result); logAction?.Invoke($"Resource `{Path}` loaded {StringUtils.FormatFileSize(rawData.Length)} over {Time.time - startTime:0.###} seconds."); }
public override async UniTask RunAsync() { var startTime = Time.time; var usedCache = false; // Check if cached version of the file could be used. rawData = await TryLoadFileCacheAsync(Path); // Cached version is not valid or doesn't exist; download or export the file. if (rawData is null) { // 4. Load file metadata from Google Drive. var filePath = string.IsNullOrEmpty(RootPath) ? Path : string.Concat(RootPath, '/', Path); var fileMeta = await GetFileMetaAsync(filePath); if (fileMeta is null) { Debug.LogError($"Failed to resolve '{filePath}' Google Drive metadata."); SetResult(new Resource <TResource>(Path, null, Provider)); return; } if (converter is IGoogleDriveConverter <TResource> ) { rawData = await ExportFileAsync(fileMeta); } else { rawData = await DownloadFileAsync(fileMeta); } // 5. Cache the downloaded file. await WriteFileCacheAsync(Path, fileMeta.Id, rawData); } else { usedCache = true; } // In case we used native requests the resource will already be set, so no need to use converters. if (!ObjectUtils.IsValid(loadedObject)) { loadedObject = await converter.ConvertAsync(rawData, System.IO.Path.GetFileNameWithoutExtension(Path)); } var result = new Resource <TResource>(Path, loadedObject, Provider); SetResult(result); logAction?.Invoke($"Resource '{Path}' loaded {StringUtils.FormatFileSize(rawData.Length)} over {Time.time - startTime:0.###} seconds from " + (usedCache ? "cache." : "Google Drive.")); if (downloadRequest != null) { downloadRequest.Dispose(); } }
public override async Task Run() { await base.Run(); var startTime = Time.time; var usedCache = false; // Check if cached version of the file could be used. rawData = await TryLoadFileCacheAsync(Resource.Path); // Cached version is not valid or doesn't exist; download or export the file. if (rawData == null) { // 4. Load file metadata from Google Drive. var filePath = string.IsNullOrEmpty(RootPath) ? Resource.Path : string.Concat(RootPath, '/', Resource.Path); var fileMeta = await GetFileMetaAsync(filePath); if (fileMeta == null) { Debug.LogError($"Failed to resolve '{filePath}' Google Drive metadata."); HandleOnCompleted(); return; } if (converter is IGoogleDriveConverter <TResource> ) { rawData = await ExportFileAsync(fileMeta); } else { rawData = await DownloadFileAsync(fileMeta); } // 5. Cache the downloaded file. await WriteFileCacheAsync(Resource.Path, fileMeta.Id, rawData); } else { usedCache = true; } // In case we used native requests the resource will already be set, so no need to use converters. if (!Resource.IsValid) { Resource.Object = await converter.ConvertAsync(rawData); } logAction?.Invoke($"Resource '{Resource.Path}' loaded {StringUtils.FormatFileSize(rawData.Length)} over {Time.time - startTime:0.###} seconds from " + (usedCache ? "cache." : "Google Drive.")); HandleOnCompleted(); }
public override async Task Run() { await base.Run(); var startTime = Time.time; var filePath = string.IsNullOrEmpty(RootPath) ? Resource.Path : string.Concat(RootPath, '/', Resource.Path); filePath = string.Concat(Application.dataPath, "/", filePath); foreach (var representation in converter.Representations) { var fullPath = string.Concat(filePath, representation.Extension); if (!File.Exists(fullPath)) { continue; } rawData = await IOUtils.ReadFileAsync(fullPath); break; } if (rawData == null) { var usedExtensions = string.Join("/", converter.Representations.Select(r => r.Extension)); Debug.LogError($"Failed to load `{filePath}({usedExtensions})` resource using local file system: File not found."); } else { Resource.Object = await converter.ConvertAsync(rawData); logAction?.Invoke($"Resource `{Resource.Path}` loaded {StringUtils.FormatFileSize(rawData.Length)} over {Time.time - startTime:0.###} seconds."); } HandleOnCompleted(); }