4 using System.Collections.Generic;
8 using SiliconStudio.Core;
9 using SiliconStudio.Core.Diagnostics;
10 using SiliconStudio.Core.IO;
12 namespace SiliconStudio.Assets
23 private static readonly Lazy<PackageStore> DefaultPackageStore =
new Lazy<PackageStore>(() =>
new PackageStore());
25 private const string DefaultEnvironmentSdkDir =
"SiliconStudioParadoxSdkDir";
27 private const string CommonTargets =
@"Targets\SiliconStudio.Common.targets";
29 private const string ParadoxSolution =
@"build\Paradox.sln";
31 private readonly
Package defaultPackage;
33 private readonly
UDirectory globalInstallationPath;
37 private readonly
bool isDev;
39 private readonly
UDirectory defaultPackageDirectory;
41 private readonly NugetStore store;
47 private PackageStore(
string installationPath = null,
string defaultPackageName =
"Paradox",
string defaultPackageVersion = ParadoxVersion.CurrentAsText)
50 if (installationPath != null)
52 if (!IsRootDirectory(installationPath))
54 throw new ArgumentException(
"Invalid Paradox installation path [{0}]".ToFormat(installationPath),
"installationPath");
57 globalInstallationPath = installationPath;
61 DefaultPackageName = defaultPackageName;
68 var thisAssemblyLocation = typeof(
PackageStore).Assembly.Location;
69 var binDirectory =
new FileInfo(thisAssemblyLocation).Directory;
70 if (binDirectory != null && binDirectory.Parent != null && binDirectory.Parent.Parent != null)
72 var defaultPackageDirectoryTemp = binDirectory.Parent.Parent;
75 if (IsPackageDirectory(defaultPackageDirectoryTemp.FullName, DefaultPackageName))
77 defaultPackageDirectory = defaultPackageDirectoryTemp.FullName;
81 throw new InvalidOperationException(
"The current assembly [{0}] is not part of the package [{1}]".ToFormat(thisAssemblyLocation, DefaultPackageName));
84 if (globalInstallationPath == null)
87 if (defaultPackageDirectoryTemp.Parent != null && IsRootDirectory(defaultPackageDirectoryTemp.Parent.FullName))
89 globalInstallationPath = defaultPackageDirectoryTemp.Parent.FullName;
91 else if (IsRootDirectory(defaultPackageDirectory))
94 globalInstallationPath = defaultPackageDirectory;
100 throw new InvalidOperationException(
"The current assembly [{0}] must be loaded from a valid installation".ToFormat(thisAssemblyLocation));
104 if (globalInstallationPath == null)
106 var rootDirectory = Environment.GetEnvironmentVariable(DefaultEnvironmentSdkDir);
107 if (!
string.IsNullOrWhiteSpace(rootDirectory) && IsRootDirectory(rootDirectory))
109 globalInstallationPath = rootDirectory;
114 if (globalInstallationPath == null)
116 throw new InvalidOperationException(
"Unable to find a valid Paradox installation or dev path");
121 var defaultPackageFile = GetPackageFile(defaultPackageDirectory, DefaultPackageName);
122 defaultPackage = Package.Load(logger, defaultPackageFile, GetDefaultPackageLoadParameters());
123 if (defaultPackage == null)
125 throw new InvalidOperationException(
"Error while loading default package from [{0}]: {1}".ToFormat(defaultPackageFile, logger.ToText()));
127 defaultPackage.IsSystem =
true;
130 isDev = defaultPackageDirectory != null && IsRootDevDirectory(defaultPackageDirectory);
133 if (NugetStore.IsStoreDirectory(globalInstallationPath))
135 packagesDirectory = UPath.Combine(globalInstallationPath, (
UDirectory)NugetStore.DefaultGamePackagesDirectory);
136 store =
new NugetStore(globalInstallationPath) { DefaultPackageId = DefaultPackageName };
144 public string DefaultPackageName {
get;
private set; }
172 return defaultPackage;
183 return globalInstallationPath;
195 return Enumerable.Empty<
PackageMeta>().AsQueryable();
198 var packages = store.Manager.SourceRepository.Search(null,
false);
201 var orderedPackages = packages.OrderByDescending(p => p.DownloadCount).ThenBy(p => p.Id);
206 return orderedPackages.Select(PackageMeta.FromNuGet);
215 var packages =
new List<Package> { defaultPackage };
221 var metas = store.Manager.LocalRepository.GetPackages();
222 foreach (var meta
in metas)
224 var path = store.PathResolver.GetPackageDirectory(meta.Id, meta.Version);
226 var
package = Package.Load(log, path, GetDefaultPackageLoadParameters());
227 if (package != null && packages.All(packageRegistered => packageRegistered.Meta.Name != defaultPackage.Meta.Name))
229 package.IsSystem =
true;
230 packages.Add(package);
249 if (packageName == null)
throw new ArgumentNullException(
"packageName");
250 var directory = GetPackageDirectory(packageName, versionRange, allowPreleaseVersion, allowUnlisted);
262 return DefaultPackageStore.Value;
272 private UDirectory GetPackageDirectory(
string packageName, PackageVersionRange versionRange,
bool allowPreleaseVersion =
false,
bool allowUnlisted =
false)
274 if (packageName == null)
throw new ArgumentNullException(
"packageName");
278 var versionSpec = versionRange.ToVersionSpec();
279 var
package = store.Manager.LocalRepository.FindPackage(packageName, versionSpec, allowPreleaseVersion, allowUnlisted);
284 var directory = store.PathResolver.GetPackageDirectory(package);
285 if (directory != null)
293 return DefaultPackageName == packageName ? defaultPackageDirectory : null;
296 private static string GetCommonTargets(
string directory)
298 if (directory == null)
throw new ArgumentNullException(
"directory");
299 return Path.Combine(directory, CommonTargets);
302 private static string GetPackageFile(
string directory,
string packageName)
304 if (directory == null)
throw new ArgumentNullException(
"directory");
305 return Path.Combine(directory, packageName + Package.PackageFileExtension);
308 private static bool IsRootDirectory(
string directory)
310 if (directory == null)
throw new ArgumentNullException(
"directory");
311 var commonTargets = GetCommonTargets(directory);
312 return File.Exists(commonTargets);
315 private static bool IsPackageDirectory(
string directory,
string packageName)
317 if (directory == null)
throw new ArgumentNullException(
"directory");
318 var packageFile = GetPackageFile(directory, packageName);
319 return File.Exists(packageFile);
322 private static bool IsRootDevDirectory(
string directory)
324 if (directory == null)
throw new ArgumentNullException(
"directory");
325 var paradoxSolution = Path.Combine(directory, ParadoxSolution);
326 return File.Exists(paradoxSolution);
IEnumerable< PackageMeta > GetPackages()
Gets the packages available online.
IEnumerable< Package > GetInstalledPackages()
Gets the packages installed locally.
SiliconStudio.Core.Diagnostics.LoggerResult LoggerResult
Manage packages locally installed and accessible on the store.
UFile GetPackageFileName(string packageName, PackageVersionRange versionRange=null, bool allowPreleaseVersion=true, bool allowUnlisted=false)
Gets the filename to the specific package.
Defines a normalized directory path. See UPath for details. This class cannot be inherited.
const string PackageFileExtension
The file extension used for Package.
A package managing assets.
Parameters used for loading a package.
A hybrid implementation of SemVer that supports semantic versioning as described at http://semver...
Defines a normalized file path. See UPath for details. This class cannot be inherited.
A dependency to a range of version.