source: OniSplit/Importer.cs@ 1125

Last change on this file since 1125 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: 3.8 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Globalization;
4using System.IO;
5using System.Text;
6
7namespace Oni
8{
9 internal abstract class Importer
10 {
11 private readonly ImporterFile file;
12 private Dictionary<string, ImporterTask> dependencies;
13
14 public Importer()
15 {
16 this.file = new ImporterFile();
17 }
18
19 protected Importer(long templateChecksum)
20 {
21 this.file = new ImporterFile(templateChecksum);
22 }
23
24 public ImporterFile ImporterFile => file;
25
26 public virtual void Import(string filePath, string outputDirPath)
27 {
28 }
29
30 public virtual void BeginImport()
31 {
32 file.BeginImport();
33
34 dependencies = new Dictionary<string, ImporterTask>();
35 }
36
37 public BinaryWriter RawWriter => file.RawWriter;
38
39 public void AddDependency(string filePath, TemplateTag type)
40 {
41 if (!dependencies.ContainsKey(filePath))
42 dependencies[filePath] = new ImporterTask(filePath, type);
43 }
44
45 public ICollection<ImporterTask> Dependencies
46 {
47 get
48 {
49 if (dependencies == null)
50 return new ImporterTask[0];
51
52 return dependencies.Values;
53 }
54 }
55
56 public ImporterDescriptor CreateInstance(TemplateTag tag, string name = null) => file.CreateInstance(tag, name);
57
58 public int WriteRawPart(byte[] data) => file.WriteRawPart(data);
59
60 public int WriteRawPart(string text) => file.WriteRawPart(text);
61
62 public void Write(string outputDirPath) => file.Write(outputDirPath);
63
64 protected static string MakeInstanceName(TemplateTag tag, string name)
65 {
66 string tagName = tag.ToString();
67
68 if (!name.StartsWith(tagName, StringComparison.Ordinal))
69 name = tagName + name;
70
71 return name;
72 }
73
74 public static string EncodeFileName(string name, Dictionary<string, string> fileNames = null)
75 {
76 foreach (char c in Path.GetInvalidFileNameChars())
77 name = name.Replace(c.ToString(), string.Format(CultureInfo.InvariantCulture, "%{0:X2}", (int)c));
78
79 if (fileNames != null)
80 {
81 string existingName;
82
83 while (fileNames.TryGetValue(name, out existingName))
84 {
85 int i = 0;
86
87 if (name.Length > 4)
88 i = 4;
89
90 while (i < name.Length && char.ToLowerInvariant(name[i]) != char.ToLowerInvariant(existingName[i]))
91 i++;
92
93 name = name.Substring(0, i) + string.Format(CultureInfo.InvariantCulture, "%{0:X2}", (int)name[i]) + name.Substring(i + 1);
94 }
95
96 fileNames[name] = name;
97 }
98
99 return name;
100 }
101
102 public static string DecodeFileName(string fileName)
103 {
104 fileName = Path.GetFileNameWithoutExtension(fileName);
105
106 var buffer = new StringBuilder();
107
108 for (int i, startIndex = 0; startIndex != -1; startIndex = i)
109 {
110 i = fileName.IndexOf('%', startIndex);
111
112 if (i == -1)
113 {
114 buffer.Append(fileName, startIndex, fileName.Length - startIndex);
115 }
116 else
117 {
118 buffer.Append(fileName, startIndex, i - startIndex);
119 buffer.Append((char)Int32.Parse(fileName.Substring(i + 1, 2), NumberStyles.HexNumber));
120 i += 3;
121 }
122 }
123
124 return buffer.ToString();
125 }
126 }
127}
Note: See TracBrowser for help on using the repository browser.