1 | using System;
|
---|
2 | using System.IO;
|
---|
3 | using System.Xml;
|
---|
4 | using Oni.Imaging;
|
---|
5 | using Oni.Metadata;
|
---|
6 | using Oni.Xml;
|
---|
7 |
|
---|
8 | namespace Oni.Motoko
|
---|
9 | {
|
---|
10 | internal sealed class TextureXmlExporter : RawXmlExporter
|
---|
11 | {
|
---|
12 | private InstanceDescriptor txmp;
|
---|
13 | private string outputDirPath;
|
---|
14 | private string baseFileName;
|
---|
15 |
|
---|
16 | private TextureXmlExporter(BinaryReader reader, XmlWriter writer)
|
---|
17 | : base(reader, writer)
|
---|
18 | {
|
---|
19 | }
|
---|
20 |
|
---|
21 | public static void Export(InstanceDescriptor txmp, XmlWriter writer, string outputDirPath, string baseFileName)
|
---|
22 | {
|
---|
23 | using (var reader = txmp.OpenRead(128))
|
---|
24 | {
|
---|
25 | var exporter = new TextureXmlExporter(reader, writer) {
|
---|
26 | txmp = txmp,
|
---|
27 | outputDirPath = outputDirPath,
|
---|
28 | baseFileName = baseFileName
|
---|
29 | };
|
---|
30 |
|
---|
31 | exporter.Export();
|
---|
32 | }
|
---|
33 | }
|
---|
34 |
|
---|
35 | private void Export()
|
---|
36 | {
|
---|
37 | var flags = (InstanceMetadata.TXMPFlags)Reader.ReadInt32();
|
---|
38 | int width = Reader.ReadInt16();
|
---|
39 | int height = Reader.ReadInt16();
|
---|
40 | var format = (InstanceMetadata.TXMPFormat)Reader.ReadInt32();
|
---|
41 | var txan = Reader.ReadInstance();
|
---|
42 | var envmap = Reader.ReadInstance();
|
---|
43 | int dataOffset = Reader.ReadInt32();
|
---|
44 |
|
---|
45 | //
|
---|
46 | // Cleanup unwanted/unneeded flags.
|
---|
47 | //
|
---|
48 |
|
---|
49 | flags &= ~(InstanceMetadata.TXMPFlags.Unknown0010
|
---|
50 | | InstanceMetadata.TXMPFlags.SwapBytes
|
---|
51 | | InstanceMetadata.TXMPFlags.HasEnvMap);
|
---|
52 |
|
---|
53 | Xml.WriteStartElement("Texture");
|
---|
54 |
|
---|
55 | string name = txmp.FullName;
|
---|
56 |
|
---|
57 | if (name.StartsWith("TXMP", StringComparison.Ordinal))
|
---|
58 | name = name.Substring(4);
|
---|
59 |
|
---|
60 | Xml.WriteElementString("Flags", flags.ToString().Replace(",", " "));
|
---|
61 | Xml.WriteElementString("Format", format.ToString());
|
---|
62 |
|
---|
63 | if (envmap != null)
|
---|
64 | Xml.WriteElementString("EnvMap", envmap.FullName);
|
---|
65 |
|
---|
66 | if (txan == null)
|
---|
67 | {
|
---|
68 | string fileName = baseFileName + ".tga";
|
---|
69 | TgaWriter.Write(TextureDatReader.Read(txmp).Surfaces[0], Path.Combine(outputDirPath, fileName));
|
---|
70 |
|
---|
71 | Xml.WriteElementString("Image", fileName);
|
---|
72 | }
|
---|
73 | else
|
---|
74 | {
|
---|
75 | WriteAnimationFrames2(txan);
|
---|
76 | }
|
---|
77 |
|
---|
78 | Xml.WriteEndElement();
|
---|
79 | }
|
---|
80 |
|
---|
81 | private void WriteAnimationFrames2(InstanceDescriptor txan)
|
---|
82 | {
|
---|
83 | using (var txanReader = txan.OpenRead(12))
|
---|
84 | {
|
---|
85 | int speed = txanReader.ReadInt16();
|
---|
86 | txanReader.Skip(6);
|
---|
87 | int count = txanReader.ReadInt32();
|
---|
88 |
|
---|
89 | Xml.WriteElementString("Speed", XmlConvert.ToString(speed));
|
---|
90 |
|
---|
91 | for (int i = 0; i < count; i++)
|
---|
92 | {
|
---|
93 | InstanceDescriptor frame;
|
---|
94 |
|
---|
95 | if (i == 0)
|
---|
96 | {
|
---|
97 | txanReader.Skip(4);
|
---|
98 | frame = txmp;
|
---|
99 | }
|
---|
100 | else
|
---|
101 | {
|
---|
102 | frame = txanReader.ReadInstance();
|
---|
103 | }
|
---|
104 |
|
---|
105 | string fileName = string.Format("{0}_{1:d3}.tga", baseFileName, i);
|
---|
106 | TgaWriter.Write(TextureDatReader.Read(frame).Surfaces[0], Path.Combine(outputDirPath, fileName));
|
---|
107 |
|
---|
108 | Xml.WriteElementString("Image", fileName);
|
---|
109 | }
|
---|
110 | }
|
---|
111 | }
|
---|
112 | }
|
---|
113 | }
|
---|