Skip to content
0.2.0-dev Download ZIP
147 lines ·6.3 KB Raw
1using System.Globalization;
2using System.Text;
3using EvilEngine.Formats.Toml;
4
5namespace EvilEngine.Formats.Records;
6
7internal static class SoundscapeText
8{
9 public static SoundscapeRecord Read(ContentId id, TomlNode node)
10 {
11 var record = new SoundscapeRecord(id)
12 {
13 Loops = node.Tables("loop").Select(ReadLoop).ToArray(),
14 Randoms = node.Tables("random").Select(ReadRandom).ToArray(),
15 Includes = node.Tables("include").Select(ReadInclude).ToArray(),
16 Games = SoundText.ReadGames(node)
17 };
18 node.RejectUnknownKeys();
19 return record;
20 }
21
22 public static void Write(StringBuilder text, string header, SoundscapeRecord record)
23 {
24 var table = header[..^1];
25 foreach (var loop in record.Loops)
26 {
27 text.Append("\n[").Append(table).Append(".loop]]\n");
28 text.Append("file = ").Append(SoundText.Inline(loop.File)).Append('\n');
29 Sound(text, loop.Volume, loop.Pitch, loop.Level);
30 Slot(text, "position", loop.Position, record.Id);
31 }
32
33 foreach (var random in record.Randoms)
34 {
35 if (random.Files.Count == 0)
36 throw new ArgumentException($"{record.Id} has a random sound with no files.");
37 if (!(random.Time.Min >= 0))
38 throw new ArgumentException($"{record.Id} has a random sound whose time is below 0.");
39 if (random.Around && random.Position is not null)
40 throw new ArgumentException($"{record.Id} has a random sound both at a position and around.");
41 text.Append("\n[").Append(table).Append(".random]]\n");
42 text.Append("time = ").Append(SoundText.Range(random.Time)).Append('\n');
43 Sound(text, random.Volume, random.Pitch, random.Level);
44 Slot(text, "position", random.Position, record.Id);
45 if (random.Around)
46 text.Append("around = true\n");
47 text.Append("files = [\n")
48 .Append(string.Concat(random.Files.Select(file => $" {SoundText.Inline(file)},\n")))
49 .Append("]\n");
50 }
51
52 foreach (var include in record.Includes)
53 {
54 text.Append("\n[").Append(table).Append(".include]]\n");
55 text.Append("soundscape = ").Append(TomlText.Quote(include.Soundscape.ToString())).Append('\n');
56 if (include.Volume != new SoundRange(1))
57 text.Append("volume = ").Append(SoundText.Range(include.Volume)).Append('\n');
58 if (include.Offset != 0)
59 Slot(text, "offset", include.Offset, record.Id);
60 Slot(text, "position", include.Position, record.Id);
61 Slot(text, "ambient_position", include.AmbientPosition, record.Id);
62 }
63
64 SoundText.WriteGames(text, header, record.Games);
65 }
66
67 private static SoundscapeLoop ReadLoop(TomlNode node)
68 {
69 var defaults = new SoundscapeLoop(SoundText.ReadFile(node.OptionalTable("file") ??
70 throw node.Error($"needs {node.Name("file")}.")));
71 var loop = defaults with
72 {
73 Volume = SoundText.Range(node, "volume", defaults.Volume),
74 Pitch = SoundText.Range(node, "pitch", defaults.Pitch),
75 Level = SoundText.Range(node, "level", defaults.Level),
76 Position = Slot(node, "position")
77 };
78 node.RejectUnknownKeys();
79 return loop;
80 }
81
82 private static SoundscapeRandom ReadRandom(TomlNode node)
83 {
84 if (!node.Has("time"))
85 throw node.Error($"needs {node.Name("time")}.");
86 var time = SoundText.Range(node, "time", new SoundRange(0));
87 if (!(time.Min >= 0))
88 throw node.Error($"{node.Name("time")} must not be below 0.");
89 var files = node.Tables("files").Select(SoundText.ReadFile).ToArray();
90 if (files.Length == 0)
91 throw node.Error($"needs at least one of {node.Name("files")}.");
92 var defaults = new SoundscapeRandom(files, time);
93 var random = defaults with
94 {
95 Volume = SoundText.Range(node, "volume", defaults.Volume),
96 Pitch = SoundText.Range(node, "pitch", defaults.Pitch),
97 Level = SoundText.Range(node, "level", defaults.Level),
98 Position = Slot(node, "position"),
99 Around = node.OptionalBool("around", false)
100 };
101 if (random.Around && random.Position is not null)
102 throw node.Error($"takes {node.Name("position")} or {node.Name("around")}, not both.");
103 node.RejectUnknownKeys();
104 return random;
105 }
106
107 private static SoundscapeInclude ReadInclude(TomlNode node)
108 {
109 var include = new SoundscapeInclude(node.RequiredId("soundscape"))
110 {
111 Volume = SoundText.Range(node, "volume", new SoundRange(1)),
112 Offset = Slot(node, "offset") ?? 0,
113 Position = Slot(node, "position"),
114 AmbientPosition = Slot(node, "ambient_position")
115 };
116 node.RejectUnknownKeys();
117 return include;
118 }
119
120 private static void Sound(StringBuilder text, SoundRange volume, SoundRange pitch, SoundRange level)
121 {
122 if (volume != new SoundRange(1))
123 text.Append("volume = ").Append(SoundText.Range(volume)).Append('\n');
124 if (pitch != new SoundRange(100))
125 text.Append("pitch = ").Append(SoundText.Range(pitch)).Append('\n');
126 if (level != new SoundRange(75))
127 text.Append("level = ").Append(SoundText.Range(level)).Append('\n');
128 }
129
130 private static int? Slot(TomlNode node, string key)
131 {
132 var value = node.OptionalInteger(key);
133 if (value is < 0 or >= SoundscapeRecords.Positions)
134 throw node.Error($"{node.Name(key)} must be a position from 0 to {SoundscapeRecords.Positions - 1}.");
135 return (int?)value;
136 }
137
138 private static void Slot(StringBuilder text, string key, int? slot, ContentId id)
139 {
140 if (slot is not { } value)
141 return;
142 if (value is < 0 or >= SoundscapeRecords.Positions)
143 throw new ArgumentException(
144 $"{id} has {key} {value}, which isn't a position from 0 to {SoundscapeRecords.Positions - 1}.");
145 text.Append(key).Append(" = ").Append(value.ToString(CultureInfo.InvariantCulture)).Append('\n');
146 }
147}