source: OniSplit/Exporter.cs@ 1160

Last change on this file since 1160 was 1114, checked in by iritscen, 5 years ago

Adding OniSplit source code (v0.9.99.0). Many thanks to Neo for all his work over the years.

File size: 2.3 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.IO;
4using System.Text.RegularExpressions;
5
6namespace Oni
7{
8 internal abstract class Exporter
9 {
10 private readonly InstanceFileManager fileManager;
11 private readonly string outputDirPath;
12 private readonly Dictionary<string, string> fileNames;
13 private Regex nameFilter;
14
15 protected Exporter(InstanceFileManager fileManager, string outputDirPath)
16 {
17 this.fileManager = fileManager;
18 this.outputDirPath = outputDirPath;
19 this.fileNames = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
20 }
21
22 public InstanceFileManager InstanceFileManager => fileManager;
23
24 public string OutputDirPath => outputDirPath;
25
26 public Regex NameFilter
27 {
28 get { return nameFilter; }
29 set { nameFilter = value; }
30 }
31
32 public void ExportFiles(IEnumerable<string> sourceFilePaths)
33 {
34 Directory.CreateDirectory(outputDirPath);
35
36 foreach (var sourceFilePath in sourceFilePaths)
37 ExportFile(sourceFilePath);
38
39 Flush();
40 }
41
42 protected virtual void ExportFile(string sourceFilePath)
43 {
44 Console.WriteLine(sourceFilePath);
45
46 var file = fileManager.OpenFile(sourceFilePath);
47 var descriptors = GetSupportedDescriptors(file);
48
49 if (nameFilter != null)
50 descriptors = descriptors.FindAll(x => x.HasName && nameFilter.IsMatch(x.FullName));
51
52 foreach (var descriptor in descriptors)
53 ExportInstance(descriptor);
54 }
55
56 protected abstract void ExportInstance(InstanceDescriptor descriptor);
57
58 protected virtual void Flush()
59 {
60 }
61
62 protected virtual List<InstanceDescriptor> GetSupportedDescriptors(InstanceFile file)
63 {
64 return file.GetNamedDescriptors();
65 }
66
67 protected string CreateFileName(InstanceDescriptor descriptor, string fileExtension)
68 {
69 string name = Importer.EncodeFileName(descriptor.FullName, fileNames);
70 return Path.Combine(outputDirPath, name + fileExtension);
71 }
72 }
73}
Note: See TracBrowser for help on using the repository browser.