Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
PackageAnalysis.cs
Go to the documentation of this file.
1 // Copyright (c) 2014 Silicon Studio Corp. (http://siliconstudio.co.jp)
2 // This file is distributed under GPL v3. See LICENSE.md for details.
3 using System;
4 using System.Collections.Generic;
5 using System.IO;
6 using System.Linq;
7 using SiliconStudio.Assets.Diagnostics;
8 using SiliconStudio.Core.Diagnostics;
9 using SiliconStudio.Core.IO;
10 using SiliconStudio.Core.Serialization;
11 
12 namespace SiliconStudio.Assets.Analysis
13 {
14  /// <summary>
15  /// An analysis to check the validity of a <see cref="Package"/>, convert <see cref="UFile"/> or <see cref="UDirectory"/>
16  /// references to absolute/relative paths, check asset references...etc, change <see cref="IContentReference"/> location
17  /// if location changed.
18  /// </summary>
19  public sealed class PackageAnalysis
20  {
21  private readonly Package package;
22  private readonly PackageAnalysisParameters parameters;
23 
24  public PackageAnalysis(Package package, PackageAnalysisParameters parameters = null)
25  {
26  if (package == null) throw new ArgumentNullException("package");
27  this.parameters = parameters ?? new PackageAnalysisParameters();
28  this.package = package;
29  }
30 
31  /// <summary>
32  /// Gets the parameters used for this analysis.
33  /// </summary>
34  /// <value>The parameters.</value>
35  public PackageAnalysisParameters Parameters
36  {
37  get
38  {
39  return parameters;
40  }
41  }
42 
43  /// <summary>
44  /// Runs a full analysis on this package.
45  /// </summary>
46  /// <returns>LoggerResult.</returns>
47  public LoggerResult Run()
48  {
49  var log = new LoggerResult();
50  Run(log);
51  return log;
52  }
53 
54  /// <summary>
55  /// Runs a full analysis on this package.
56  /// </summary>
57  /// <param name="log">The log.</param>
58  public void Run(ILogger log)
59  {
60  if (log == null) throw new ArgumentNullException("log");
61 
62  // If the package doesn't have a meta name, fix it here
63  if (string.IsNullOrWhiteSpace(package.Meta.Name) && package.FullPath != null)
64  {
65  package.Meta.Name = package.FullPath.GetFileName();
66  package.IsDirty = true;
67  }
68 
69  if (Parameters.IsPackageCheckDependencies)
70  {
71  CheckDependencies().CopyTo(log);
72  }
73 
74  if (Parameters.IsProcessingUPaths)
75  {
76  ProcessPackageUPaths();
77  }
78 
79  ProcessAssets().CopyTo(log);
80  }
81 
82  /// <summary>
83  /// Checks the package.
84  /// </summary>
85  /// <returns>LoggerResult.</returns>
87  {
88  var log = new LoggerResult();
89 
90  // Can only check dependencies if we are inside a session
91  if (package.Session == null)
92  {
93  return log;
94  }
95 
96  // If ProjetcPath is null, the package was not saved.
97  if (Parameters.ConvertUPathTo == UPathType.Relative && package.FullPath == null)
98  {
99  log.Error(package, null, AssetMessageCode.PackageFilePathNotSet);
100  return log;
101  }
102 
103  // 1. Check all store package references
104  foreach (var packageDependency in package.Meta.Dependencies)
105  {
106  // Try to find the package reference
107  var subPackage = package.Session.Packages.Find(packageDependency);
108  if (subPackage == null)
109  {
110  if (packageDependency.Name == PackageStore.Instance.DefaultPackageName)
111  {
112  log.Warning(package, null, AssetMessageCode.PackageDependencyModified, packageDependency, PackageStore.Instance.DefaultPackageMinVersion);
113  packageDependency.Version = PackageStore.Instance.DefaultPackageMinVersion;
114  package.IsDirty = true;
115  }
116  else
117  {
118  log.Error(package, null, AssetMessageCode.PackageNotFound, packageDependency);
119  }
120  }
121  }
122 
123  // 2. Check all local package references
124  foreach (var packageReference in package.LocalDependencies)
125  {
126  // Try to find the package reference
127  var newSubPackage = package.Session.Packages.Find(packageReference.Id);
128  if (newSubPackage == null)
129  {
130  log.Error(package, null, AssetMessageCode.PackageNotFound, packageReference.Location);
131  continue;
132  }
133 
134  if (newSubPackage.FullPath == null || newSubPackage.IsSystem)
135  {
136  continue;
137  }
138 
139  // If package was found, check that the path is correctly setup
140  var pathToSubPackage = Parameters.ConvertUPathTo == UPathType.Relative ? newSubPackage.FullPath.MakeRelative(package.RootDirectory) : newSubPackage.FullPath;
141  if (packageReference.Location != pathToSubPackage)
142  {
143  // Modify package path to be relative if different
144  packageReference.Location = pathToSubPackage;
145 
146  if (Parameters.SetDirtyFlagOnAssetWhenFixingUFile)
147  {
148  package.IsDirty = true;
149  }
150  }
151  }
152 
153  // TODO: Check profiles
154 
155  return log;
156  }
157 
158  /// <summary>
159  /// Processes the UPaths on package (but not on assets, use <see cref="ProcessAssets"/> for this)
160  /// </summary>
161  public void ProcessPackageUPaths()
162  {
163  if (package.FullPath == null)
164  {
165  return;
166  }
167 
168  var packageReferenceLinks = AssetReferenceAnalysis.Visit(package);
169  CommonAnalysis.UpdatePaths(package, packageReferenceLinks.Where(link => link.Reference is UPath), Parameters);
170  }
171 
173  {
174  var log = new LoggerResult();
175 
176  var assets = (package.TemporaryAssets.Count > 0 ? (IEnumerable<AssetItem>)package.TemporaryAssets : package.Assets);
177 
178  foreach (var assetItem in assets)
179  {
180  AssetAnalysis.Run(assetItem, log, Parameters);
181  }
182 
183  return log;
184  }
185  }
186 }
string DefaultPackageName
Gets or sets the default package name (mainly used in dev environment).
static PackageStore Instance
Gets the default package manager.
PackageAnalysis(Package package, PackageAnalysisParameters parameters=null)
SiliconStudio.Core.Diagnostics.LoggerResult LoggerResult
A logger that stores messages locally useful for internal log scenarios.
Definition: LoggerResult.cs:14
Class PackageAnalysisParameters. This class cannot be inherited.
Manage packages locally installed and accessible on the store.
Definition: PackageStore.cs:21
An analysis to check the validity of a Package, convert UFile or UDirectory references to absolute/re...
LoggerResult CheckDependencies()
Checks the package.
UPathType
Describes if a UPath is relative or absolute.
Definition: UPathType.cs:8
Base class that describes a uniform path and provides method to manipulate them. Concrete class are U...
Definition: UPath.cs:21
void Run(ILogger log)
Runs a full analysis on this package.
LoggerResult Run()
Runs a full analysis on this package.
A package managing assets.
Definition: Package.cs:28
void ProcessPackageUPaths()
Processes the UPaths on package (but not on assets, use ProcessAssets for this)
Interface for logging.
Definition: ILogger.cs:8