source: OniSplit/Xml/FilmToXmlConverter.cs@ 1148

Last change on this file since 1148 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.9 KB
RevLine 
[1114]1using System;
2using System.IO;
3using System.Xml;
4using Oni.Metadata;
5
6namespace Oni.Xml
7{
8 internal class FilmToXmlConverter : RawXmlExporter
9 {
10 public FilmToXmlConverter(BinaryReader reader, XmlWriter writer)
11 : base(reader, writer)
12 {
13 }
14
15 private static readonly MetaType filmHeader = new MetaStruct("FilmHeader",
16 new Field(MetaType.Vector3, "Position"),
17 new Field(MetaType.Float, "Facing"),
18 new Field(MetaType.Float, "DesiredFacing"),
19 new Field(MetaType.Float, "HeadFacing"),
20 new Field(MetaType.Float, "HeadPitch"),
21 new Field(MetaType.Int32, "FrameCount"),
22 new Field(MetaType.Padding(28)));
23
24 private static readonly MetaType filmAnimations = new MetaStruct("FilmAnimations",
25 new Field(MetaType.Array(2, MetaType.String128), "Animations"));
26
27 private static readonly MetaType filmFrames = new MetaStruct("FilmFrames",
28 new Field(MetaType.VarArray(new MetaStruct("Frame",
29 new Field(MetaType.Vector2, "MouseDelta"),
30 new Field(MetaType.Enum<InstanceMetadata.FILMKeys>(), "Keys"),
31 new Field(MetaType.Int32, "Frame"),
32 new Field(MetaType.Padding(4))
33 )), "Frames")
34 );
35
36 public static void Convert(string filePath, string outputDirPath)
37 {
38 using (var reader = new BinaryReader(filePath, true))
39 using (var writer = CreateXmlWriter(Path.Combine(outputDirPath, Path.GetFileNameWithoutExtension(filePath) + ".xml")))
40 {
41 writer.WriteStartElement("Instance");
42 writer.WriteAttributeString("id", "0");
43 writer.WriteAttributeString("type", "FILM");
44
45 var converter = new FilmToXmlConverter(reader, writer);
46
47 reader.Position = filmAnimations.Size;
48 filmHeader.Accept(converter);
49
50 reader.Position = 0;
51 filmAnimations.Accept(converter);
52
53 reader.Position = filmAnimations.Size + filmHeader.Size;
54 filmFrames.Accept(converter);
55
56 writer.WriteEndElement();
57 }
58 }
59
60 private static XmlWriter CreateXmlWriter(string filePath)
61 {
62 var settings = new XmlWriterSettings
63 {
64 CloseOutput = true,
65 Indent = true,
66 IndentChars = " "
67 };
68
69 var stream = File.Create(filePath);
70 var writer = XmlWriter.Create(stream, settings);
71
72 try
73 {
74 writer.WriteStartElement("Oni");
75 }
76 catch
77 {
78#if NETCORE
79 writer.Dispose();
80#else
81 writer.Close();
82#endif
83 throw;
84 }
85
86 return writer;
87 }
88 }
89}
Note: See TracBrowser for help on using the repository browser.