using System.Globalization; using System.Text; using EvilEngine.Formats.Toml; namespace EvilEngine.Formats.Records; internal static class SoundscapeText { public static SoundscapeRecord Read(ContentId id, TomlNode node) { var record = new SoundscapeRecord(id) { Loops = node.Tables("loop").Select(ReadLoop).ToArray(), Randoms = node.Tables("random").Select(ReadRandom).ToArray(), Includes = node.Tables("include").Select(ReadInclude).ToArray(), Games = SoundText.ReadGames(node) }; node.RejectUnknownKeys(); return record; } public static void Write(StringBuilder text, string header, SoundscapeRecord record) { var table = header[..^1]; foreach (var loop in record.Loops) { text.Append("\n[").Append(table).Append(".loop]]\n"); text.Append("file = ").Append(SoundText.Inline(loop.File)).Append('\n'); Sound(text, loop.Volume, loop.Pitch, loop.Level); Slot(text, "position", loop.Position, record.Id); } foreach (var random in record.Randoms) { if (random.Files.Count == 0) throw new ArgumentException($"{record.Id} has a random sound with no files."); if (!(random.Time.Min >= 0)) throw new ArgumentException($"{record.Id} has a random sound whose time is below 0."); if (random.Around && random.Position is not null) throw new ArgumentException($"{record.Id} has a random sound both at a position and around."); text.Append("\n[").Append(table).Append(".random]]\n"); text.Append("time = ").Append(SoundText.Range(random.Time)).Append('\n'); Sound(text, random.Volume, random.Pitch, random.Level); Slot(text, "position", random.Position, record.Id); if (random.Around) text.Append("around = true\n"); text.Append("files = [\n") .Append(string.Concat(random.Files.Select(file => $" {SoundText.Inline(file)},\n"))) .Append("]\n"); } foreach (var include in record.Includes) { text.Append("\n[").Append(table).Append(".include]]\n"); text.Append("soundscape = ").Append(TomlText.Quote(include.Soundscape.ToString())).Append('\n'); if (include.Volume != new SoundRange(1)) text.Append("volume = ").Append(SoundText.Range(include.Volume)).Append('\n'); if (include.Offset != 0) Slot(text, "offset", include.Offset, record.Id); Slot(text, "position", include.Position, record.Id); Slot(text, "ambient_position", include.AmbientPosition, record.Id); } SoundText.WriteGames(text, header, record.Games); } private static SoundscapeLoop ReadLoop(TomlNode node) { var defaults = new SoundscapeLoop(SoundText.ReadFile(node.OptionalTable("file") ?? throw node.Error($"needs {node.Name("file")}."))); var loop = defaults with { Volume = SoundText.Range(node, "volume", defaults.Volume), Pitch = SoundText.Range(node, "pitch", defaults.Pitch), Level = SoundText.Range(node, "level", defaults.Level), Position = Slot(node, "position") }; node.RejectUnknownKeys(); return loop; } private static SoundscapeRandom ReadRandom(TomlNode node) { if (!node.Has("time")) throw node.Error($"needs {node.Name("time")}."); var time = SoundText.Range(node, "time", new SoundRange(0)); if (!(time.Min >= 0)) throw node.Error($"{node.Name("time")} must not be below 0."); var files = node.Tables("files").Select(SoundText.ReadFile).ToArray(); if (files.Length == 0) throw node.Error($"needs at least one of {node.Name("files")}."); var defaults = new SoundscapeRandom(files, time); var random = defaults with { Volume = SoundText.Range(node, "volume", defaults.Volume), Pitch = SoundText.Range(node, "pitch", defaults.Pitch), Level = SoundText.Range(node, "level", defaults.Level), Position = Slot(node, "position"), Around = node.OptionalBool("around", false) }; if (random.Around && random.Position is not null) throw node.Error($"takes {node.Name("position")} or {node.Name("around")}, not both."); node.RejectUnknownKeys(); return random; } private static SoundscapeInclude ReadInclude(TomlNode node) { var include = new SoundscapeInclude(node.RequiredId("soundscape")) { Volume = SoundText.Range(node, "volume", new SoundRange(1)), Offset = Slot(node, "offset") ?? 0, Position = Slot(node, "position"), AmbientPosition = Slot(node, "ambient_position") }; node.RejectUnknownKeys(); return include; } private static void Sound(StringBuilder text, SoundRange volume, SoundRange pitch, SoundRange level) { if (volume != new SoundRange(1)) text.Append("volume = ").Append(SoundText.Range(volume)).Append('\n'); if (pitch != new SoundRange(100)) text.Append("pitch = ").Append(SoundText.Range(pitch)).Append('\n'); if (level != new SoundRange(75)) text.Append("level = ").Append(SoundText.Range(level)).Append('\n'); } private static int? Slot(TomlNode node, string key) { var value = node.OptionalInteger(key); if (value is < 0 or >= SoundscapeRecords.Positions) throw node.Error($"{node.Name(key)} must be a position from 0 to {SoundscapeRecords.Positions - 1}."); return (int?)value; } private static void Slot(StringBuilder text, string key, int? slot, ContentId id) { if (slot is not { } value) return; if (value is < 0 or >= SoundscapeRecords.Positions) throw new ArgumentException( $"{id} has {key} {value}, which isn't a position from 0 to {SoundscapeRecords.Positions - 1}."); text.Append(key).Append(" = ").Append(value.ToString(CultureInfo.InvariantCulture)).Append('\n'); } }