[1114] | 1 | using System;
|
---|
| 2 | using System.Collections.Generic;
|
---|
| 3 | using System.IO;
|
---|
| 4 |
|
---|
| 5 | namespace Oni
|
---|
| 6 | {
|
---|
| 7 | internal sealed class InstanceFileOperations
|
---|
| 8 | {
|
---|
| 9 | private InstanceFileManager fileManager;
|
---|
| 10 | private string destinationDir;
|
---|
| 11 | private readonly Dictionary<string, string> fileNames = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
|
---|
| 12 | private Dictionary<string, string> referencedFiles;
|
---|
| 13 | private readonly Dictionary<string, string> instances = new Dictionary<string, string>(StringComparer.Ordinal);
|
---|
| 14 |
|
---|
| 15 | public void Copy(InstanceFileManager fileManager, List<string> sourceFiles, string destinationDir)
|
---|
| 16 | {
|
---|
| 17 | Initialize(fileManager, sourceFiles, destinationDir);
|
---|
| 18 |
|
---|
| 19 | foreach (KeyValuePair<string, string> pair in referencedFiles)
|
---|
| 20 | {
|
---|
| 21 | if (File.Exists(pair.Value))
|
---|
| 22 | {
|
---|
| 23 | if (!Utils.AreFilesEqual(pair.Key, pair.Value))
|
---|
| 24 | Console.WriteLine("File {0} already exists at destination and it is different. File not copied.", pair.Value);
|
---|
| 25 | }
|
---|
| 26 | else
|
---|
| 27 | {
|
---|
| 28 | File.Copy(pair.Key, pair.Value);
|
---|
| 29 | }
|
---|
| 30 | }
|
---|
| 31 | }
|
---|
| 32 |
|
---|
| 33 | public void Move(InstanceFileManager fileManager, List<string> sourceFilePaths, string outputDirPath)
|
---|
| 34 | {
|
---|
| 35 | Initialize(fileManager, sourceFilePaths, outputDirPath);
|
---|
| 36 |
|
---|
| 37 | foreach (KeyValuePair<string, string> pair in referencedFiles)
|
---|
| 38 | {
|
---|
| 39 | if (File.Exists(pair.Value))
|
---|
| 40 | {
|
---|
| 41 | if (Utils.AreFilesEqual(pair.Key, pair.Value))
|
---|
| 42 | File.Delete(pair.Key);
|
---|
| 43 | else
|
---|
| 44 | Console.WriteLine("File {0} already exists at destination and it is different. Source file not moved.", pair.Value);
|
---|
| 45 | }
|
---|
| 46 | else
|
---|
| 47 | {
|
---|
| 48 | File.Move(pair.Key, pair.Value);
|
---|
| 49 | }
|
---|
| 50 | }
|
---|
| 51 | }
|
---|
| 52 |
|
---|
| 53 | public void MoveOverwrite(InstanceFileManager fileManager, List<string> sourceFilePaths, string outputDirPath)
|
---|
| 54 | {
|
---|
| 55 | Initialize(fileManager, sourceFilePaths, outputDirPath);
|
---|
| 56 |
|
---|
| 57 | foreach (KeyValuePair<string, string> pair in referencedFiles)
|
---|
| 58 | {
|
---|
| 59 | if (File.Exists(pair.Value))
|
---|
| 60 | File.Delete(pair.Value);
|
---|
| 61 |
|
---|
| 62 | File.Move(pair.Key, pair.Value);
|
---|
| 63 | }
|
---|
| 64 | }
|
---|
| 65 |
|
---|
| 66 | public void MoveDelete(InstanceFileManager fileManager, List<string> sourceFilePaths, string outputDirPath)
|
---|
| 67 | {
|
---|
| 68 | Initialize(fileManager, sourceFilePaths, outputDirPath);
|
---|
| 69 |
|
---|
| 70 | foreach (KeyValuePair<string, string> pair in referencedFiles)
|
---|
| 71 | {
|
---|
| 72 | if (File.Exists(pair.Value))
|
---|
| 73 | File.Delete(pair.Key);
|
---|
| 74 | else
|
---|
| 75 | File.Move(pair.Key, pair.Value);
|
---|
| 76 | }
|
---|
| 77 | }
|
---|
| 78 |
|
---|
| 79 | public void GetDependencies(InstanceFileManager fileManager, List<string> sourceFilePaths)
|
---|
| 80 | {
|
---|
| 81 | Initialize(fileManager, sourceFilePaths, null);
|
---|
| 82 |
|
---|
| 83 | foreach (string filePath in referencedFiles.Keys)
|
---|
| 84 | {
|
---|
| 85 | Console.WriteLine(filePath);
|
---|
| 86 | }
|
---|
| 87 | }
|
---|
| 88 |
|
---|
| 89 | private void Initialize(InstanceFileManager fileManager, List<string> inputFiles, string destinationDir)
|
---|
| 90 | {
|
---|
| 91 | this.fileManager = fileManager;
|
---|
| 92 | this.destinationDir = destinationDir;
|
---|
| 93 | this.referencedFiles = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
|
---|
| 94 |
|
---|
| 95 | if (destinationDir != null)
|
---|
| 96 | {
|
---|
| 97 | if (Directory.Exists(destinationDir))
|
---|
| 98 | {
|
---|
| 99 | //
|
---|
| 100 | // Get a list of existing files to avoid name conflicts due to instance names
|
---|
| 101 | // that differ only in case.
|
---|
| 102 | //
|
---|
| 103 |
|
---|
| 104 | foreach (string existingFilePath in Directory.GetFiles(destinationDir, "*.oni"))
|
---|
| 105 | {
|
---|
| 106 | string fileName = Path.GetFileNameWithoutExtension(existingFilePath);
|
---|
| 107 | string instanceName = Importer.DecodeFileName(existingFilePath);
|
---|
| 108 |
|
---|
| 109 | fileNames[fileName] = fileName;
|
---|
| 110 | instances[instanceName] = existingFilePath;
|
---|
| 111 | }
|
---|
| 112 | }
|
---|
| 113 | else
|
---|
| 114 | {
|
---|
| 115 | Directory.CreateDirectory(destinationDir);
|
---|
| 116 | }
|
---|
| 117 | }
|
---|
| 118 |
|
---|
| 119 | var sourceFiles = new Dictionary<string, string>(StringComparer.Ordinal);
|
---|
| 120 | string lastSourceDir = null;
|
---|
| 121 |
|
---|
| 122 | foreach (string inputFile in inputFiles)
|
---|
| 123 | {
|
---|
| 124 | string sourceDir = Path.GetDirectoryName(inputFile);
|
---|
| 125 |
|
---|
| 126 | if (sourceDir != lastSourceDir)
|
---|
| 127 | {
|
---|
| 128 | lastSourceDir = sourceDir;
|
---|
| 129 | sourceFiles.Clear();
|
---|
| 130 |
|
---|
| 131 | foreach (string file in Directory.GetFiles(sourceDir, "*.oni"))
|
---|
| 132 | sourceFiles[Importer.DecodeFileName(file)] = file;
|
---|
| 133 | }
|
---|
| 134 |
|
---|
| 135 | GetReferencedFiles(inputFile, sourceFiles);
|
---|
| 136 | }
|
---|
| 137 | }
|
---|
| 138 |
|
---|
| 139 | private void GetReferencedFiles(string sourceFile, Dictionary<string, string> sourceFiles)
|
---|
| 140 | {
|
---|
| 141 | AddReferencedFile(sourceFile);
|
---|
| 142 |
|
---|
| 143 | var instanceFile = fileManager.OpenFile(sourceFile);
|
---|
| 144 |
|
---|
| 145 | foreach (var descriptor in instanceFile.GetPlaceholders())
|
---|
| 146 | {
|
---|
| 147 | string referencedSourceFile;
|
---|
| 148 |
|
---|
| 149 | if (!sourceFiles.TryGetValue(descriptor.FullName, out referencedSourceFile)
|
---|
| 150 | || referencedFiles.ContainsKey(referencedSourceFile))
|
---|
| 151 | continue;
|
---|
| 152 |
|
---|
| 153 | GetReferencedFiles(referencedSourceFile, sourceFiles);
|
---|
| 154 | }
|
---|
| 155 | }
|
---|
| 156 |
|
---|
| 157 | private void AddReferencedFile(string filePath)
|
---|
| 158 | {
|
---|
| 159 | if (referencedFiles.ContainsKey(filePath))
|
---|
| 160 | return;
|
---|
| 161 |
|
---|
| 162 | string instanceName = Importer.DecodeFileName(filePath);
|
---|
| 163 | string destinationFile;
|
---|
| 164 |
|
---|
| 165 | if (!instances.TryGetValue(instanceName, out destinationFile))
|
---|
| 166 | {
|
---|
| 167 | if (destinationDir != null)
|
---|
| 168 | destinationFile = Path.Combine(destinationDir, Importer.EncodeFileName(instanceName, fileNames) + ".oni");
|
---|
| 169 | }
|
---|
| 170 |
|
---|
| 171 | referencedFiles.Add(filePath, destinationFile);
|
---|
| 172 | }
|
---|
| 173 | }
|
---|
| 174 | }
|
---|