0.2.0-dev Download ZIP
224 lines ·8.7 KB Raw
| 1 | using System.Text; |
| 2 | using EvilEngine.Adapters.Gmod.Lua; |
| 3 | using EvilEngine.Adapters.Source.Sounds; |
| 4 | using EvilEngine.Conversion; |
| 5 | using EvilEngine.Mounts; |
| 6 | |
| 7 | namespace EvilEngine.Adapters.Gmod.Sounds; |
| 8 | |
| 9 | public sealed class GarrysModSounds : SourceSoundRecords |
| 10 | { |
| 11 | public const string Target = "sound.Add"; |
| 12 | |
| 13 | private const string Left = "so it's left for the GLua layer to add"; |
| 14 | |
| 15 | private const string Stools = "weapons/gmod_tool/stools"; |
| 16 | |
| 17 | private static readonly string[] Folders = ["lua/autorun", "lua/autorun/server", "lua/autorun/client"]; |
| 18 | |
| 19 | private static readonly string[] Gamemodes = ["base", "sandbox"]; |
| 20 | |
| 21 | private static readonly (string Global, string Folder)[] Scripted = |
| 22 | [("ENT", "entities"), ("SWEP", "weapons"), ("EFFECT", "effects")]; |
| 23 | |
| 24 | private static readonly string[] Entry = ["shared.lua", "init.lua", "cl_init.lua"]; |
| 25 | |
| 26 | public override string Id => "gmod.sounds"; |
| 27 | |
| 28 | public override int Version => 1; |
| 29 | |
| 30 | protected override IReadOnlyList<string> InterfacePrefixes => ["ui/", "garrysmod/ui_"]; |
| 31 | |
| 32 | protected override IEnumerable<SoundDefinition> Added(ConversionContext context, List<string> notes) |
| 33 | { |
| 34 | var reader = new LuaReader([Target], |
| 35 | path => context.TryRead(path, out var data) ? Encoding.UTF8.GetString(data.Span) : null, |
| 36 | path => context.Exists(path)); |
| 37 | reader.Define("CLIENT", LuaValue.True); |
| 38 | reader.Define("SERVER", LuaValue.True); |
| 39 | foreach (var folder in Folders) |
| 40 | foreach (var path in Lua(context, folder)) |
| 41 | reader.Read(path); |
| 42 | |
| 43 | foreach (var gamemode in Gamemodes) |
| 44 | { |
| 45 | reader.DefineTable("GM", |
| 46 | [("Folder", new LuaString($"gamemodes/{gamemode}")), ("FolderName", new LuaString(gamemode))]); |
| 47 | reader.Read($"gamemodes/{gamemode}/gamemode/init.lua"); |
| 48 | reader.Read($"gamemodes/{gamemode}/gamemode/cl_init.lua"); |
| 49 | } |
| 50 | |
| 51 | reader.Define("GM", LuaValue.Nil); |
| 52 | string[] roots = ["lua", .. Gamemodes.Select(gamemode => $"gamemodes/{gamemode}/entities")]; |
| 53 | foreach (var (global, folder) in Scripted) |
| 54 | foreach (var root in roots) |
| 55 | foreach (var (name, path) in Classes(context, $"{root}/{folder}")) |
| 56 | { |
| 57 | reader.DefineTable(global, Fields(global, $"{folder}/{name}")); |
| 58 | reader.Read(path); |
| 59 | reader.Define(global, LuaValue.Nil); |
| 60 | } |
| 61 | |
| 62 | foreach (var root in roots) |
| 63 | foreach (var path in Lua(context, $"{root}/{Stools}")) |
| 64 | { |
| 65 | reader.DefineTable("TOOL", |
| 66 | [("Mode", new LuaString(Path.GetFileNameWithoutExtension(path).ToLowerInvariant()))]); |
| 67 | reader.Read(path); |
| 68 | reader.Define("TOOL", LuaValue.Nil); |
| 69 | } |
| 70 | |
| 71 | var reading = reader.Finish(); |
| 72 | notes.AddRange(reading.Notes.Where(note => note.Target == Target) |
| 73 | .Select(note => $"{note.File}:{note.Line}: {note.Message}, {Left}.").Distinct()); |
| 74 | var definitions = new List<SoundDefinition>(); |
| 75 | foreach (var call in reading.Calls.Where(call => call.Target == Target)) |
| 76 | if (Definition(call, notes) is { } definition) |
| 77 | definitions.Add(definition); |
| 78 | return definitions; |
| 79 | } |
| 80 | |
| 81 | public static SoundDefinition? Definition(LuaCall call, List<string> notes) |
| 82 | { |
| 83 | var where = $"{call.File}:{call.Line}"; |
| 84 | if (call.Argument(0) is not LuaTable { Partial: false } table) |
| 85 | { |
| 86 | notes.Add( |
| 87 | $"{where}: {Target} is given {call.Argument(0).Describe()}, not a table the reader holds, {Left}."); |
| 88 | return null; |
| 89 | } |
| 90 | |
| 91 | if (table.Get(new LuaString("name")) is not LuaString { Value: var name }) |
| 92 | { |
| 93 | notes.Add($"{where}: {Target}'s name is {table.Get(new LuaString("name")).Describe()}, {Left}."); |
| 94 | return null; |
| 95 | } |
| 96 | |
| 97 | var definition = new SoundDefinition(name, where); |
| 98 | foreach (var (key, value) in table.Fields) |
| 99 | { |
| 100 | if (key is not string field) |
| 101 | continue; |
| 102 | switch (field) |
| 103 | { |
| 104 | case "name": |
| 105 | break; |
| 106 | case "sound": |
| 107 | if (value is LuaString single) |
| 108 | { |
| 109 | definition.Waves.Add(single.Value); |
| 110 | } |
| 111 | else if (value is LuaTable { Partial: false } list && Enumerable.Range(1, list.Length) |
| 112 | .Select(index => list.Get(new LuaNumber(index))).ToArray() is var items && |
| 113 | items.All(item => item is LuaString)) |
| 114 | { |
| 115 | definition.Waves.AddRange(items.Cast<LuaString>().Select(item => item.Value)); |
| 116 | } |
| 117 | else |
| 118 | { |
| 119 | notes.Add( |
| 120 | $"{where}: {name}'s sound is {value.Describe()}, not a path or a list of them, {Left}."); |
| 121 | return null; |
| 122 | } |
| 123 | |
| 124 | break; |
| 125 | case "channel" or "volume" or "level" or "pitch": |
| 126 | if (Text(value) is not { } text) |
| 127 | { |
| 128 | notes.Add( |
| 129 | $"{where}: {name}'s {field} is {value.Describe()}, which the reader can't hold, {Left}."); |
| 130 | return null; |
| 131 | } |
| 132 | |
| 133 | switch (field) |
| 134 | { |
| 135 | case "channel": |
| 136 | definition.Channel = text; |
| 137 | break; |
| 138 | case "volume": |
| 139 | definition.Volume = text; |
| 140 | break; |
| 141 | case "level": |
| 142 | definition.Level = text; |
| 143 | break; |
| 144 | default: |
| 145 | definition.Pitch = text; |
| 146 | break; |
| 147 | } |
| 148 | |
| 149 | break; |
| 150 | default: |
| 151 | notes.Add($"{where}: {name} sets '{field}', which {Target} doesn't read, so it's skipped."); |
| 152 | break; |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | return definition; |
| 157 | } |
| 158 | |
| 159 | private static string? Text(LuaValue value) |
| 160 | { |
| 161 | return value switch |
| 162 | { |
| 163 | LuaNumber number => LuaValue.Format(number.Value), |
| 164 | LuaString text => text.Value, |
| 165 | LuaGlobal global => global.Path, |
| 166 | LuaTable { Partial: false } range when range.Length is 1 or 2 && range.Fields.Count == range.Length => |
| 167 | Enumerable.Range(1, range.Length).Select(index => Text(range.Get(new LuaNumber(index)))) |
| 168 | .ToArray() is var parts && |
| 169 | parts.All(part => part is not null && !part.Contains(',')) |
| 170 | ? string.Join(",", parts) |
| 171 | : null, |
| 172 | _ => null |
| 173 | }; |
| 174 | } |
| 175 | |
| 176 | private static IEnumerable<(string Key, LuaValue Value)> Fields(string global, string folder) |
| 177 | { |
| 178 | yield return ("Folder", new LuaString(folder)); |
| 179 | if (global != "SWEP") |
| 180 | yield break; |
| 181 | yield return ("Primary", new LuaTable(0)); |
| 182 | yield return ("Secondary", new LuaTable(0)); |
| 183 | } |
| 184 | |
| 185 | private static IEnumerable<string> Lua(ConversionContext context, string folder) |
| 186 | { |
| 187 | return context.List(folder) |
| 188 | .Where(entry => entry.Path.Extension.Equals("lua", StringComparison.OrdinalIgnoreCase)) |
| 189 | .Select(entry => entry.Path.Value) |
| 190 | .Order(StringComparer.OrdinalIgnoreCase); |
| 191 | } |
| 192 | |
| 193 | private static IEnumerable<(string Name, string Path)> Classes(ConversionContext context, string folder) |
| 194 | { |
| 195 | var classes = new SortedDictionary<string, string>(StringComparer.OrdinalIgnoreCase); |
| 196 | var folders = new SortedDictionary<string, List<VirtualPath>>(StringComparer.OrdinalIgnoreCase); |
| 197 | var depth = folder.Count(c => c == '/') + 1; |
| 198 | foreach (var file in context.List(folder, true)) |
| 199 | { |
| 200 | var parts = file.Path.Value.Split('/'); |
| 201 | if (parts.Length == depth + 1 && file.Path.Extension.Equals("lua", StringComparison.OrdinalIgnoreCase)) |
| 202 | { |
| 203 | classes.TryAdd(file.Path.WithoutExtension.Name, file.Path.Value); |
| 204 | } |
| 205 | else if (parts.Length == depth + 2) |
| 206 | { |
| 207 | if (!folders.TryGetValue(parts[depth], out var files)) |
| 208 | folders[parts[depth]] = files = []; |
| 209 | files.Add(file.Path); |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | foreach (var (name, files) in folders) |
| 214 | foreach (var wanted in Entry) |
| 215 | if (files.FindIndex(file => |
| 216 | file.Name.Equals(wanted, StringComparison.OrdinalIgnoreCase)) is var at and >= 0) |
| 217 | { |
| 218 | classes[name] = files[at].Value; |
| 219 | break; |
| 220 | } |
| 221 | |
| 222 | return classes.Select(pair => (pair.Key, pair.Value)); |
| 223 | } |
| 224 | } |