using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
namespace UnityEngine.Advertisements.Editor {
///
/// Responsible for finding all SkAdNetwork files on the local filesystem by searching through the users project directory and all includes packages.
///
internal class SkAdNetworkLocalSourceProvider {
private const int k_MaxPackageLookupTimeoutInSeconds = 30;
private string[] m_PackagePaths;
public SkAdNetworkLocalSourceProvider() {
m_PackagePaths = GetAllPackagePaths();
}
public IEnumerable GetSources(string filename, string extension) {
return GetLocalFilePaths(filename, extension).Select(x => new SkAdNetworkLocalSource(x)).ToArray();
}
///
/// Finds a file on the local filesystem by looking the project directory, and all package directories
///
/// the filename to look for
/// the filename extension to look for
/// a full path to the file
private IEnumerable GetLocalFilePaths(string filename, string fileExtension) {
return m_PackagePaths
.Prepend(Directory.GetCurrentDirectory())
.SelectMany(path => Directory.GetFiles(path, string.IsNullOrEmpty(fileExtension) ? filename : $"{filename}.{fileExtension}" , SearchOption.AllDirectories))
.ToList();
}
///
/// Returns a list of paths to the root folder of each package included in the users project.
/// These may be in different locations on disk depending on where the package is being stored/cached.
///
private static string[] GetAllPackagePaths(bool offlineMode = true)
{
var list = UnityEditor.PackageManager.Client.List(offlineMode);
if (list == null) {
return Array.Empty();
}
var timeSpan = TimeSpan.FromSeconds(k_MaxPackageLookupTimeoutInSeconds);
var startTime = DateTime.Now;
while (!list.IsCompleted && (DateTime.Now - startTime) < timeSpan) {
Thread.Sleep(10);
}
if (list.Error != null) {
return Array.Empty();
}
return list.Result.Select(packageInfo => packageInfo.assetPath).ToArray();
}
}
}