using EvilEngine.Api; using EvilEngine.Mounts; namespace EvilEngine.Host; public enum PluginSide { Both, Client, Server } public sealed record PluginExport(string Name, SemanticVersion Version, Realm? Realm, IReadOnlyList Functions); public sealed record PluginManifest(string Id, string Name, SemanticVersion Version, int Api) { public const string FileName = "plugin.toml"; public IReadOnlyDictionary Needs { get; init; } = new Dictionary(); public IReadOnlyDictionary Wants { get; init; } = new Dictionary(); public IReadOnlyList Exports { get; init; } = []; public IReadOnlyList Mounts { get; init; } = []; public IReadOnlyDictionary Breaks { get; init; } = new Dictionary(); public IReadOnlyList After { get; init; } = []; public IReadOnlyList Before { get; init; } = []; public bool Global { get; init; } public string? Code { get; init; } public IReadOnlyList Phases { get; init; } = []; public PluginSide Side { get; init; } = PluginSide.Both; public static PluginManifest Read(string file) { return Parse(File.ReadAllText(file), file); } public static PluginManifest Parse(string text, string file) { var toml = TomlReader.Parse(text, file); var id = ManifestRules.Id(toml, toml.RequiredString("id")); var manifest = new PluginManifest( id, toml.RequiredString("name"), ManifestRules.Version(toml, toml.RequiredString("version")), (int)toml.RequiredInteger("api")) { Needs = ManifestRules.Ranges(toml, toml.StringTable("needs")), Wants = ManifestRules.Ranges(toml, toml.StringTable("wants")), Exports = toml.Tables("exports").Select(pair => Export(pair.Key, pair.Value)).ToArray(), Mounts = toml.TableArray("mounts").Select(Mount).ToArray(), Breaks = ManifestRules.Ranges(toml, toml.StringTable("breaks")), After = toml.Strings("after"), Before = toml.Strings("before"), Global = toml.OptionalBool("global", false), Code = toml.OptionalString("code") is { } code ? ManifestRules.CodePath(toml, code) : null, Phases = toml.Strings("phases"), Side = toml.OptionalString("side") switch { null or "both" => PluginSide.Both, "client" => PluginSide.Client, "server" => PluginSide.Server, var other => throw toml.Error($"'side' is \"both\", \"client\" or \"server\", not \"{other}\".") } }; toml.RejectUnknownKeys(); foreach (var phase in manifest.Phases) if (!phase.StartsWith($"{id}:", StringComparison.Ordinal)) throw toml.Error($"phase '{phase}' isn't in the Plugin's namespace '{id}:'."); return manifest; } private static PluginExport Export(string name, TomlReader toml) { var export = new PluginExport( name, ManifestRules.Version(toml, toml.RequiredString("version")), toml.OptionalString("realm") switch { null => null, "server" => Realm.Server, "client" => Realm.Client, "menu" => Realm.Menu, var other => throw toml.Error($"'realm' is \"server\", \"client\" or \"menu\", not \"{other}\".") }, toml.Strings("functions")); toml.RejectUnknownKeys(); return export; } private static MountRequirement Mount(TomlReader toml) { var requirement = new MountRequirement(toml.RequiredString("game")) { Parts = toml.Strings("parts"), Platforms = toml.Strings("platforms"), Builds = toml.Strings("builds") }; toml.RejectUnknownKeys(); return requirement; } }