Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
IndexFileCommand.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 
6 using SiliconStudio.Core.MicroThreading;
7 using SiliconStudio.Core.Storage;
8 using SiliconStudio.Core.IO;
9 using SiliconStudio.Core.Serialization.Assets;
10 
11 namespace SiliconStudio.BuildEngine
12 {
13  /// <summary>
14  /// A <see cref="Command"/> that reads and/or writes to the index file.
15  /// </summary>
16  public abstract class IndexFileCommand : Command
17  {
18  private BuildTransaction buildTransaction;
19  internal static ObjectDatabase ObjectDatabase;
20 
21  public static MicroThreadLocal<DatabaseFileProvider> DatabaseFileProvider = new MicroThreadLocal<DatabaseFileProvider>(() =>
22  {
23  throw new InvalidOperationException("No VirtualFileProvider set for this microthread.");
24  });
25 
26  private static readonly IDictionary<ObjectUrl, OutputObject> commomOutputObjects = new Dictionary<ObjectUrl, OutputObject>();
27 
28  public static void MergeOutputObjects(IDictionary<ObjectUrl, OutputObject> outputObjects)
29  {
30  lock (commomOutputObjects)
31  {
32  foreach (var outputObject in outputObjects)
33  commomOutputObjects[outputObject.Key] = outputObject.Value;
34  }
35  }
36 
37  private static IEnumerable<IDictionary<ObjectUrl, OutputObject>> GetOutputObjectsGroups(IEnumerable<IDictionary<ObjectUrl, OutputObject>> transactionOutputObjectsGroups)
38  {
39  if (transactionOutputObjectsGroups != null)
40  {
41  foreach (var outputObjects in transactionOutputObjectsGroups)
42  yield return outputObjects;
43  }
44  yield return commomOutputObjects;
45  }
46 
47 
48  internal static void MountDatabases(IExecuteContext executeContext)
49  {
50  MountDatabases(CreateTransaction(executeContext.GetOutputObjectsGroups()));
51  }
52 
53  public static void MountDatabases(ICommandContext commandContext)
54  {
55  MountDatabases(CreateTransaction(commandContext.GetOutputObjectsGroups()));
56  }
57 
58  private static void MountDatabases(BuildTransaction transaction)
59  {
60  DatabaseFileProvider.Value = CreateDatabases(transaction);
61  }
62 
63  private static BuildTransaction CreateTransaction(IEnumerable<IDictionary<ObjectUrl, OutputObject>> transactionOutputObjectsGroups)
64  {
65  return new BuildTransaction(GetOutputObjectsGroups(transactionOutputObjectsGroups));
66  }
67 
68  private static DatabaseFileProvider CreateDatabases(BuildTransaction transaction)
69  {
70  return new DatabaseFileProvider(new BuildTransaction.DatabaseAssetIndexMap(transaction), ObjectDatabase);
71  }
72 
74  {
75  return CreateDatabases(CreateTransaction(null));
76  }
77 
78  internal static void UnmountDatabases(IExecuteContext executeContext)
79  {
80  DatabaseFileProvider.Value = null;
81  }
82 
83  public override void PreCommand(ICommandContext commandContext)
84  {
85  base.PreCommand(commandContext);
86 
87  buildTransaction = CreateTransaction(commandContext.GetOutputObjectsGroups());
88  MountDatabases(buildTransaction);
89  }
90 
91  public override void PostCommand(ICommandContext commandContext, ResultStatus status)
92  {
93  base.PostCommand(commandContext, status);
94 
95  if (status == ResultStatus.Successful)
96  {
97  // Save list of newly changed URLs in CommandResult.OutputObjects
98  foreach (var entry in buildTransaction.GetTransactionIdMap())
99  {
100  commandContext.RegisterOutput(entry.Key, entry.Value);
101  }
102 
103  // Note: In case of remote process, the remote process will save the index map.
104  // Alternative would be to not save it and just forward results to the master builder who would commit results locally.
105  // Not sure which is the best.
106  //
107  // Anyway, current approach should be OK for now since the index map is "process-safe" (as long as we load new values as necessary).
108  //AssetIndexMap.Save();
109  }
110 
111  DatabaseFileProvider.Value = null;
112  }
113  }
114 }
override void PreCommand(ICommandContext commandContext)
static void MountDatabases(ICommandContext commandContext)
static DatabaseFileProvider GetCommonDatabase()
ResultStatus
Status of a command.
Definition: ResultStatus.cs:8
Gives access to the object database.
static void MergeOutputObjects(IDictionary< ObjectUrl, OutputObject > outputObjects)
A Command that reads and/or writes to the index file.
override void PostCommand(ICommandContext commandContext, ResultStatus status)
IEnumerable< IDictionary< ObjectUrl, OutputObject > > GetOutputObjectsGroups()