source: OniSplit/DatPacker.cs@ 1115

Last change on this file since 1115 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: 4.2 KB
RevLine 
[1114]1using System;
2using System.Collections.Generic;
3using Oni.Collections;
4
5namespace Oni
6{
7 internal sealed class DatPacker
8 {
9 private readonly List<string> inputPaths = new List<string>();
10 private string targetFilePath;
11 private bool targetBigEndian;
12 private long targetTemplateChecksum;
13
14 public List<string> InputPaths => inputPaths;
15
16 public string TargetFilePath
17 {
18 get { return targetFilePath; }
19 set { targetFilePath = value; }
20 }
21
22 public bool TargetBigEndian
23 {
24 get { return targetBigEndian; }
25 set { targetBigEndian = value; }
26 }
27
28 public long TargetTemplateChecksum
29 {
30 get { return targetTemplateChecksum; }
31 set { targetTemplateChecksum = value; }
32 }
33
34 public void Pack(InstanceFileManager fileManager, IEnumerable<string> filePaths)
35 {
36 var inputs = new List<InstanceFile>();
37 var seenFilePaths = new Set<string>(StringComparer.OrdinalIgnoreCase);
38
39 foreach (string filePath in filePaths)
40 {
41 if (seenFilePaths.Add(filePath))
42 inputs.Add(fileManager.OpenFile(filePath));
43 }
44
45 inputs.Reverse();
46
47 var descriptors = GetImportedDescriptors(inputs);
48
49 if (descriptors.Count > 0)
50 {
51 var writer = InstanceFileWriter.CreateV31(targetTemplateChecksum, targetBigEndian);
52 writer.AddDescriptors(descriptors, true);
53
54 Console.WriteLine("Writing {0}", targetFilePath);
55
56 writer.Write(targetFilePath);
57 }
58 }
59
60 public void Import(InstanceFileManager fileManager, string[] inputDirPaths)
61 {
62 Console.WriteLine("Reading files from {0}", string.Join(";", inputDirPaths));
63
64 var inputsFiles = fileManager.OpenDirectories(inputDirPaths);
65 var descriptors = GetImportedDescriptors(inputsFiles);
66
67 if (descriptors.Count > 0)
68 {
69 var writer = InstanceFileWriter.CreateV31(targetTemplateChecksum, targetBigEndian);
70 writer.AddDescriptors(descriptors, true);
71
72 Console.WriteLine("Writing {0}", targetFilePath);
73
74 writer.Write(targetFilePath);
75 }
76 }
77
78 private static List<InstanceDescriptor> GetImportedDescriptors(List<InstanceFile> inputFiles)
79 {
80 var namedDescriptors = new Set<string>(StringComparer.Ordinal);
81 var ignored = new Set<InstanceDescriptor>();
82
83 foreach (var file in inputFiles)
84 {
85 foreach (var descriptor in file.GetNamedDescriptors())
86 {
87 if (namedDescriptors.Contains(descriptor.FullName))
88 {
89 //Console.Error.WriteLine("WARNING: More than one instance has name {0}, ignoring.", descriptor.FullName);
90 ignored.Add(descriptor);
91 }
92 else
93 {
94 namedDescriptors.Add(descriptor.FullName);
95 }
96 }
97 }
98
99 inputFiles.Sort((x, y) => string.Compare(x.Descriptors[0].FullName, y.Descriptors[0].FullName, StringComparison.Ordinal));
100
101 var descriptors = new List<InstanceDescriptor>(4096);
102
103 foreach (var file in inputFiles)
104 {
105 foreach (var descriptor in file.Descriptors)
106 {
107 if (ignored.Contains(descriptor))
108 continue;
109
110 if (descriptor.HasName)
111 {
112 if (descriptor.IsPlaceholder && namedDescriptors.Contains(descriptor.FullName))
113 continue;
114
115 namedDescriptors.Add(descriptor.FullName);
116 }
117
118 descriptors.Add(descriptor);
119 }
120 }
121
122 descriptors.Sort((x, y) => x.Template.IsLeaf.CompareTo(y.Template.IsLeaf));
123
124 return descriptors;
125 }
126 }
127}
Note: See TracBrowser for help on using the repository browser.