Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
FileOperationCommand.cs
Go to the documentation of this file.
1 using System;
2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.IO;
5 using System.Threading.Tasks;
6 using SiliconStudio.Core.Diagnostics;
7 using SiliconStudio.Core.Serialization;
8 using SiliconStudio.Core.Serialization.Assets;
9 using SiliconStudio.Core.Storage;
10 
11 namespace SiliconStudio.BuildEngine
12 {
13  [Description("File operation")]
15  {
16  /// <inheritdoc/>
17  public override string Title { get { return Type + " " + (Source ?? "[Source]"); } }
18 
19  public enum Operation
20  {
21  Move,
22  Copy,
23  Delete
24  }
25 
26  public string Source { get; set; }
27  public string Target { get; set; }
28  public bool Overwrite { get; set; }
29  public Operation Type { get; set; }
30 
31  protected override async Task<ResultStatus> DoCommandOverride(ICommandContext commandContext)
32  {
33  try
34  {
35  switch (Type)
36  {
37  case Operation.Move:
38  if (File.Exists(Target))
39  File.Delete(Target);
40 
41  File.Move(Source, Target);
42  commandContext.RegisterOutput(new ObjectUrl(UrlType.File, Target), ObjectId.Empty);
43  break;
44  case Operation.Copy:
45  var sourceStream = File.OpenRead(Source);
46  var destStream = File.OpenWrite(Target);
47  await sourceStream.CopyToAsync(destStream);
48  commandContext.RegisterOutput(new ObjectUrl(UrlType.File, Target), ObjectId.Empty);
49  break;
50  case Operation.Delete:
51  File.Delete(Source);
52  break;
53  }
54 
55  return ResultStatus.Successful;
56  }
57  catch (Exception e)
58  {
59  commandContext.Logger.Error(e.Message);
60  return ResultStatus.Failed;
61  }
62  }
63 
65  {
66  yield return new ObjectUrl(UrlType.File, Source);
67  }
68 
69  protected override void ComputeParameterHash(Stream stream)
70  {
71  base.ComputeParameterHash(stream);
72 
73  var writer = new BinarySerializationWriter(stream);
74  writer.Write(Source);
75  writer.Write(Target);
76  writer.Write(Type);
77  }
78 
79  public override string ToString()
80  {
81  if (Type == Operation.Delete)
82  return Type + " " + (Source ?? "[Source]");
83  return Type + " " + (Source ?? "[Source]") + " > " + (Target ?? "[Target]");
84  }
85  }
86 }
override IEnumerable< ObjectUrl > GetInputFiles()
Gets the list of input files (that can be deduced without running the command, only from command para...
Implements SerializationStream as a binary writer.
System.IO.File File
override async Task< ResultStatus > DoCommandOverride(ICommandContext commandContext)
The method to override containing the actual command code. It is called by the DoCommand function ...