source: OniSplit/Level/LevelImporter.cs@ 1182

Last change on this file since 1182 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.5 KB
RevLine 
[1114]1using System;
2using System.Collections.Generic;
3using System.IO;
4using System.Xml;
5
6namespace Oni.Level
7{
8 internal partial class LevelImporter : Importer
9 {
10 private readonly TextWriter info;
11 private readonly TextWriter error;
12 private bool debug;
13 private string outputDirPath;
14 private LevelDatWriter.DatLevel level;
15 private string sharedPath;
16 private InstanceFileManager sharedManager;
17 private Dictionary<string, InstanceDescriptor> sharedCache;
18 private Dictionary<string, Dae.Scene> sceneCache;
19
20 public LevelImporter()
21 {
22 info = Console.Out;
23 error = Console.Error;
24 }
25
26 public bool Debug
27 {
28 get { return debug; }
29 set { debug = value; }
30 }
31
32 public override void Import(string filePath, string outputDirPath)
33 {
34 this.outputDirPath = outputDirPath;
35 this.textureImporter = new Motoko.TextureImporter3(outputDirPath);
36
37 Read(filePath);
38
39 WriteLevel();
40 WriteObjects();
41 }
42
43 private void Read(string filePath)
44 {
45 level = new LevelDatWriter.DatLevel();
46 level.name = Path.GetFileNameWithoutExtension(filePath);
47
48 string basePath = Path.GetDirectoryName(filePath);
49
50 var settings = new XmlReaderSettings {
51 IgnoreWhitespace = true,
52 IgnoreProcessingInstructions = true,
53 IgnoreComments = true
54 };
55
56 using (var xml = XmlReader.Create(filePath, settings))
57 {
58 xml.ReadStartElement("Oni");
59 ReadLevel(xml, basePath);
60 xml.ReadEndElement();
61 }
62
63 ImportModel(basePath);
64 }
65
66 private void ReadLevel(XmlReader xml, string basePath)
67 {
68 string path = xml.GetAttribute("SharedPath");
69
70 if (!string.IsNullOrEmpty(path))
71 sharedPath = Path.GetFullPath(Path.Combine(basePath, path));
72 else
73 sharedPath = Path.GetFullPath(Path.Combine(basePath, "classes"));
74
75 sharedManager = new InstanceFileManager();
76 sharedManager.AddSearchPath(sharedPath);
77
78 string name = xml.GetAttribute("Name");
79
80 if (!string.IsNullOrEmpty(name))
81 level.name = name;
82
83 xml.ReadStartElement("Level");
84
85 ReadModel(xml, basePath);
86 ReadSky(xml, basePath);
87 ReadObjects(xml, basePath);
88 ReadFilms(xml, basePath);
89 ReadCameras(xml, basePath);
90
91 xml.ReadEndElement();
92 }
93
94 private void WriteLevel()
95 {
96 BeginImport();
97 LevelDatWriter.Write(this, level);
98 Write(outputDirPath);
99 textureImporter.Write();
100 }
101
102 private Dae.Scene LoadScene(string filePath)
103 {
104 if (sceneCache == null)
105 {
106 sceneCache = new Dictionary<string, Dae.Scene>(StringComparer.OrdinalIgnoreCase);
107 }
108
109 filePath = Path.GetFullPath(filePath);
110 Dae.Scene scene;
111
112 if (!sceneCache.TryGetValue(filePath, out scene))
113 {
114 scene = Dae.Reader.ReadFile(filePath);
115 sceneCache.Add(filePath, scene);
116 }
117
118 return scene;
119 }
120
121 private InstanceDescriptor FindSharedInstance(TemplateTag tag, string name)
122 {
123 if (sharedCache == null)
124 sharedCache = new Dictionary<string, InstanceDescriptor>(StringComparer.Ordinal);
125
126 string fullName = tag.ToString() + name;
127 InstanceDescriptor descriptor;
128
129 if (!sharedCache.TryGetValue(fullName, out descriptor))
130 {
131 var file = sharedManager.FindInstance(fullName);
132
133 if (file == null)
134 error.WriteLine("Could not find {0} instance {1}", tag, name);
135 else if (file.Descriptors[0].Template.Tag != tag)
136 error.WriteLine("Found '{0}' but its type {1} doesn't match the expected type {2}", name, file.Descriptors[0].Template.Tag, tag);
137 else
138 descriptor = file.Descriptors[0];
139
140 sharedCache.Add(fullName, descriptor);
141 }
142
143 return descriptor;
144 }
145 }
146}
Note: See TracBrowser for help on using the repository browser.