暂无描述
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

SkAdNetworkLocalSourceProvider.cs 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Threading;
  6. namespace UnityEngine.Advertisements.Editor {
  7. /// <summary>
  8. /// Responsible for finding all SkAdNetwork files on the local filesystem by searching through the users project directory and all includes packages.
  9. /// </summary>
  10. internal class SkAdNetworkLocalSourceProvider {
  11. private const int k_MaxPackageLookupTimeoutInSeconds = 30;
  12. private string[] m_PackagePaths;
  13. public SkAdNetworkLocalSourceProvider() {
  14. m_PackagePaths = GetAllPackagePaths();
  15. }
  16. public IEnumerable<SkAdNetworkLocalSource> GetSources(string filename, string extension) {
  17. return GetLocalFilePaths(filename, extension).Select(x => new SkAdNetworkLocalSource(x)).ToArray();
  18. }
  19. /// <summary>
  20. /// Finds a file on the local filesystem by looking the project directory, and all package directories
  21. /// </summary>
  22. /// <param name="filename">the filename to look for</param>
  23. /// <param name="fileExtension">the filename extension to look for</param>
  24. /// <returns>a full path to the file</returns>
  25. private IEnumerable<string> GetLocalFilePaths(string filename, string fileExtension) {
  26. return m_PackagePaths
  27. .Prepend(Directory.GetCurrentDirectory())
  28. .SelectMany(path => Directory.GetFiles(path, string.IsNullOrEmpty(fileExtension) ? filename : $"{filename}.{fileExtension}" , SearchOption.AllDirectories))
  29. .ToList();
  30. }
  31. /// <summary>
  32. /// Returns a list of paths to the root folder of each package included in the users project.
  33. /// These may be in different locations on disk depending on where the package is being stored/cached.
  34. /// </summary>
  35. private static string[] GetAllPackagePaths(bool offlineMode = true)
  36. {
  37. var list = UnityEditor.PackageManager.Client.List(offlineMode);
  38. if (list == null) {
  39. return Array.Empty<string>();
  40. }
  41. var timeSpan = TimeSpan.FromSeconds(k_MaxPackageLookupTimeoutInSeconds);
  42. var startTime = DateTime.Now;
  43. while (!list.IsCompleted && (DateTime.Now - startTime) < timeSpan) {
  44. Thread.Sleep(10);
  45. }
  46. if (list.Error != null) {
  47. return Array.Empty<string>();
  48. }
  49. return list.Result.Select(packageInfo => packageInfo.assetPath).ToArray();
  50. }
  51. }
  52. }