Skip to content
0.2.0-dev Download ZIP
110 lines ·4.0 KB Raw
1using EvilEngine.Api;
2using EvilEngine.Mounts;
3
4namespace EvilEngine.Host;
5
6public enum PluginSide
7{
8 Both,
9 Client,
10 Server
11}
12
13public sealed record PluginExport(string Name, SemanticVersion Version, Realm? Realm, IReadOnlyList<string> Functions);
14
15public sealed record PluginManifest(string Id, string Name, SemanticVersion Version, int Api)
16{
17 public const string FileName = "plugin.toml";
18
19 public IReadOnlyDictionary<string, VersionRange> Needs { get; init; } = new Dictionary<string, VersionRange>();
20
21 public IReadOnlyDictionary<string, VersionRange> Wants { get; init; } = new Dictionary<string, VersionRange>();
22
23 public IReadOnlyList<PluginExport> Exports { get; init; } = [];
24
25 public IReadOnlyList<MountRequirement> Mounts { get; init; } = [];
26
27 public IReadOnlyDictionary<string, VersionRange> Breaks { get; init; } = new Dictionary<string, VersionRange>();
28
29 public IReadOnlyList<string> After { get; init; } = [];
30
31 public IReadOnlyList<string> Before { get; init; } = [];
32
33 public bool Global { get; init; }
34
35 public string? Code { get; init; }
36
37 public IReadOnlyList<string> Phases { get; init; } = [];
38
39 public PluginSide Side { get; init; } = PluginSide.Both;
40
41 public static PluginManifest Read(string file)
42 {
43 return Parse(File.ReadAllText(file), file);
44 }
45
46 public static PluginManifest Parse(string text, string file)
47 {
48 var toml = TomlReader.Parse(text, file);
49 var id = ManifestRules.Id(toml, toml.RequiredString("id"));
50 var manifest = new PluginManifest(
51 id,
52 toml.RequiredString("name"),
53 ManifestRules.Version(toml, toml.RequiredString("version")),
54 (int)toml.RequiredInteger("api"))
55 {
56 Needs = ManifestRules.Ranges(toml, toml.StringTable("needs")),
57 Wants = ManifestRules.Ranges(toml, toml.StringTable("wants")),
58 Exports = toml.Tables("exports").Select(pair => Export(pair.Key, pair.Value)).ToArray(),
59 Mounts = toml.TableArray("mounts").Select(Mount).ToArray(),
60 Breaks = ManifestRules.Ranges(toml, toml.StringTable("breaks")),
61 After = toml.Strings("after"),
62 Before = toml.Strings("before"),
63 Global = toml.OptionalBool("global", false),
64 Code = toml.OptionalString("code") is { } code ? ManifestRules.CodePath(toml, code) : null,
65 Phases = toml.Strings("phases"),
66 Side = toml.OptionalString("side") switch
67 {
68 null or "both" => PluginSide.Both,
69 "client" => PluginSide.Client,
70 "server" => PluginSide.Server,
71 var other => throw toml.Error($"'side' is \"both\", \"client\" or \"server\", not \"{other}\".")
72 }
73 };
74 toml.RejectUnknownKeys();
75 foreach (var phase in manifest.Phases)
76 if (!phase.StartsWith($"{id}:", StringComparison.Ordinal))
77 throw toml.Error($"phase '{phase}' isn't in the Plugin's namespace '{id}:'.");
78 return manifest;
79 }
80
81 private static PluginExport Export(string name, TomlReader toml)
82 {
83 var export = new PluginExport(
84 name,
85 ManifestRules.Version(toml, toml.RequiredString("version")),
86 toml.OptionalString("realm") switch
87 {
88 null => null,
89 "server" => Realm.Server,
90 "client" => Realm.Client,
91 "menu" => Realm.Menu,
92 var other => throw toml.Error($"'realm' is \"server\", \"client\" or \"menu\", not \"{other}\".")
93 },
94 toml.Strings("functions"));
95 toml.RejectUnknownKeys();
96 return export;
97 }
98
99 private static MountRequirement Mount(TomlReader toml)
100 {
101 var requirement = new MountRequirement(toml.RequiredString("game"))
102 {
103 Parts = toml.Strings("parts"),
104 Platforms = toml.Strings("platforms"),
105 Builds = toml.Strings("builds")
106 };
107 toml.RejectUnknownKeys();
108 return requirement;
109 }
110}