1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Xml;
|
---|
4 | using Oni.Metadata;
|
---|
5 | using Oni.Particles;
|
---|
6 | using Oni.Xml;
|
---|
7 |
|
---|
8 | namespace Oni.Sound
|
---|
9 | {
|
---|
10 | internal class SabdXmlImporter : RawXmlImporter
|
---|
11 | {
|
---|
12 | #region Private data
|
---|
13 | private static readonly Dictionary<string, string> modifierMap = new Dictionary<string, string>();
|
---|
14 | private static readonly Dictionary<string, string> typeMap = new Dictionary<string, string>();
|
---|
15 | #endregion
|
---|
16 |
|
---|
17 | static SabdXmlImporter()
|
---|
18 | {
|
---|
19 | var modifiers = new[] {
|
---|
20 | "Any", "Crouch", "Jump", "Heavy Damage", "Medium Damage", "Light Damage"
|
---|
21 | };
|
---|
22 |
|
---|
23 | var types = new[] {
|
---|
24 | "Block", "Draw Weapon", "Fall", "Fly", "Getting Hit", "Holster",
|
---|
25 | "Kick", "Knockdown", "Land", "Jump", "Pickup", "Punch",
|
---|
26 | "Reload Pistol", "Reload Rifle", "Reload Stream", "Reload Superball",
|
---|
27 | "Reload Vandegraf", "Reload Scram Cannon", "Reload Mercury Bow", "Reload Screamer",
|
---|
28 | "Run", "Slide", "Stand", "Starle", "Walk", "Powerup", "Roll", "Falling Flail"
|
---|
29 | };
|
---|
30 |
|
---|
31 | foreach (string modifier in modifiers)
|
---|
32 | modifierMap.Add(modifier.Replace(" ", ""), modifier);
|
---|
33 |
|
---|
34 | foreach (string type in types)
|
---|
35 | typeMap.Add(type.Replace(" ", ""), type);
|
---|
36 | }
|
---|
37 |
|
---|
38 | private SabdXmlImporter(XmlReader reader, BinaryWriter writer)
|
---|
39 | : base(reader, writer)
|
---|
40 | {
|
---|
41 | }
|
---|
42 |
|
---|
43 | public static void Import(XmlReader reader, BinaryWriter writer)
|
---|
44 | {
|
---|
45 | var importer = new SabdXmlImporter(reader, writer);
|
---|
46 | importer.Import();
|
---|
47 | }
|
---|
48 |
|
---|
49 | #region private class SoundAnimationData
|
---|
50 |
|
---|
51 | private class SoundAnimationData
|
---|
52 | {
|
---|
53 | #region Private data
|
---|
54 | private string variant;
|
---|
55 | private List<SoundAssignment> assignments;
|
---|
56 | #endregion
|
---|
57 |
|
---|
58 | private enum Tag
|
---|
59 | {
|
---|
60 | SAFT = 0x54464153,
|
---|
61 | SAVT = 0x54564153,
|
---|
62 | SASA = 0x41534153
|
---|
63 | }
|
---|
64 |
|
---|
65 | public SoundAnimationData(XmlReader xml)
|
---|
66 | {
|
---|
67 | variant = xml.GetAttribute("Variant");
|
---|
68 |
|
---|
69 | xml.ReadStartElement("SoundAnimation");
|
---|
70 | assignments = new List<SoundAssignment>();
|
---|
71 |
|
---|
72 | while (xml.IsStartElement("Assignment"))
|
---|
73 | {
|
---|
74 | xml.ReadStartElement();
|
---|
75 | assignments.Add(new SoundAssignment(xml));
|
---|
76 | xml.ReadEndElement();
|
---|
77 | }
|
---|
78 |
|
---|
79 | xml.ReadEndElement();
|
---|
80 | }
|
---|
81 |
|
---|
82 | public void Write(BinaryWriter writer)
|
---|
83 | {
|
---|
84 | writer.Write((int)Tag.SAFT);
|
---|
85 | writer.Write(4);
|
---|
86 | writer.Write(6);
|
---|
87 | writer.Write((int)Tag.SAVT);
|
---|
88 | writer.Write(32);
|
---|
89 | writer.Write(variant, 32);
|
---|
90 |
|
---|
91 | foreach (SoundAssignment assignment in assignments)
|
---|
92 | {
|
---|
93 | writer.Write((int)Tag.SASA);
|
---|
94 | writer.Write(132);
|
---|
95 | assignment.Write(writer);
|
---|
96 | }
|
---|
97 | }
|
---|
98 | }
|
---|
99 |
|
---|
100 | #endregion
|
---|
101 | #region private class SoundAssignment
|
---|
102 |
|
---|
103 | private class SoundAssignment
|
---|
104 | {
|
---|
105 | #region Private data
|
---|
106 | private int frame;
|
---|
107 | private string modifier;
|
---|
108 | private string type;
|
---|
109 | private string animationName;
|
---|
110 | private string soundName;
|
---|
111 | #endregion
|
---|
112 |
|
---|
113 | public SoundAssignment(XmlReader xml)
|
---|
114 | {
|
---|
115 | xml.ReadStartElement("Target");
|
---|
116 |
|
---|
117 | if (xml.LocalName == "Animation")
|
---|
118 | {
|
---|
119 | type = "Animation";
|
---|
120 | animationName = xml.ReadElementContentAsString("Animation", "");
|
---|
121 | }
|
---|
122 | else
|
---|
123 | {
|
---|
124 | type = xml.ReadElementContentAsString("Type", "");
|
---|
125 | animationName = string.Empty;
|
---|
126 |
|
---|
127 | if (!typeMap.TryGetValue(type, out type))
|
---|
128 | throw new NotSupportedException(string.Format("Unknown assignment type '{0}' found", type));
|
---|
129 | }
|
---|
130 |
|
---|
131 | if (xml.IsStartElement("Modifier"))
|
---|
132 | modifier = xml.ReadElementContentAsString();
|
---|
133 | else
|
---|
134 | modifier = "Any";
|
---|
135 |
|
---|
136 | if (!modifierMap.TryGetValue(modifier, out modifier))
|
---|
137 | throw new NotSupportedException(string.Format("Unknown assignment modifier '{0}' found", modifier));
|
---|
138 |
|
---|
139 | xml.ReadStartElement("Frame");
|
---|
140 | frame = xml.ReadContentAsInt();
|
---|
141 | xml.ReadEndElement();
|
---|
142 |
|
---|
143 | xml.ReadEndElement();
|
---|
144 |
|
---|
145 | soundName = xml.ReadElementContentAsString("Sound", "");
|
---|
146 | }
|
---|
147 |
|
---|
148 | public void Write(BinaryWriter writer)
|
---|
149 | {
|
---|
150 | writer.Write(frame);
|
---|
151 | writer.Write(modifier, 32);
|
---|
152 | writer.Write(type, 32);
|
---|
153 | writer.Write(animationName, 32);
|
---|
154 | writer.Write(soundName, 32);
|
---|
155 | }
|
---|
156 | }
|
---|
157 |
|
---|
158 | #endregion
|
---|
159 |
|
---|
160 | private void Import()
|
---|
161 | {
|
---|
162 | var data = new SoundAnimationData(Xml);
|
---|
163 | data.Write(Writer);
|
---|
164 | }
|
---|
165 | }
|
---|
166 | }
|
---|