Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
AssemblyHash.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.Collections.Generic;
4 using System.Reflection;
5 using System.Text;
6 
7 using SiliconStudio.Core.Diagnostics;
8 using SiliconStudio.Core.Storage;
9 
10 namespace SiliconStudio.BuildEngine
11 {
12  internal static class AssemblyHash
13  {
14  private static readonly Logger Log = GlobalLogger.GetLogger("AssemblyHash");
15 
16  /// <summary>
17  /// Computes the hash from an assembly based on its AssemblyFileVersion. Recurse to all assembly dependencies.
18  /// </summary>
19  /// <param name="assembly">The assembly.</param>
20  /// <returns>A full hash of this assembly, including all its dependencies.</returns>
21  public static string ComputeAssemblyHash(Assembly assembly)
22  {
23  string hash;
24  lock (assemblyToHash)
25  {
26  if (!assemblyToHash.TryGetValue(assembly, out hash))
27  {
28  var assemblies = new HashSet<Assembly>();
29  var text = new StringBuilder();
30  ComputeAssemblyHash(assembly, assemblies, text);
31  hash = ObjectId.FromBytes(Encoding.UTF8.GetBytes(text.ToString())).ToString();
32  assemblyToHash.Add(assembly, hash);
33  Log.Info("Assembly Hash [{0}] => [{1}]", assembly.GetName().Name, hash);
34  }
35  }
36  return hash;
37  }
38 
39  private static readonly Dictionary<Assembly, string> assemblyToHash = new Dictionary<Assembly, string>();
40 
41  private static void ComputeAssemblyHash(Assembly assembly, HashSet<Assembly> assemblies, StringBuilder outputString)
42  {
43  if (assemblies.Contains(assembly))
44  return;
45 
46  outputString.Append(assembly.FullName);
47 
48  var attribute = assembly.GetCustomAttribute<AssemblyFileVersionAttribute>();
49  if (attribute != null)
50  {
51  outputString.Append(",").Append(attribute.Version);
52  outputString.AppendLine();
53  }
54 
55  assemblies.Add(assembly);
56  foreach (var assemblyName in assembly.GetReferencedAssemblies())
57  {
58  var assemblyRef = Assembly.Load(assemblyName);
59  ComputeAssemblyHash(assemblyRef, assemblies, outputString);
60  }
61  }
62  }
63 }
Base implementation for ILogger.
Definition: Logger.cs:10
Output message to log right away.