// ============================================================================ // RynthCore.Engine - Plugins/PluginManifestGate.cs // The engine's use of plugin manifests (RynthCore.PluginSdk.Manifest, compiled in): // - before any plugin DLL is loaded, read each one's embedded manifest from the // file (no LoadLibrary: a refused NativeAOT plugin would otherwise keep a whole // runtime in acclient's 4 GB for good) and decide which may load, in what order; // - before a plugin's Init, check the plugins it requires started. // A plugin without a manifest is never refused and keeps its place in the list, // so a plugin list with no manifests loads exactly as before. // // BCL + the manifest files only, so tools\PluginManifestTests compiles it too. // PluginManager owns the logging and the chat notice. // ============================================================================ using System; using System.Collections.Generic; using System.IO; using RynthCore.PluginSdk.Manifest; namespace RynthCore.Engine.Plugins; internal static class PluginManifestGate { internal sealed class LoadPlan { /// Paths to load, in order, with their manifests (null for a plugin without one). public List<(string Path, RynthPluginManifest? Manifest)> Load { get; } = new(); /// Plugins not loaded, with the reason (one sentence, fit for chat). public List Refused { get; } = new(); /// Log lines: manifests found, broken manifests, notes. public List Log { get; } = new(); } /// /// Reads the manifests of and resolves them against /// . gives a DLL's version resource /// (for dependency versions of plugins whose manifest has none); null skips it. /// public static LoadPlan Plan(IReadOnlyList paths, uint engineApi, Func? fileVersion = null) { var plan = new LoadPlan(); var candidates = new List(paths.Count); int withManifest = 0; foreach (string path in paths) { RynthPluginManifest? manifest = null; try { manifest = PluginManifestReader.Read(path); } catch (InvalidDataException ex) { // A broken manifest is a build problem, not the player's: load it the old way. plan.Log.Add($"{Path.GetFileName(path)}: manifest unreadable ({ex.Message}) - loading it as a plugin without one."); } catch (Exception ex) when (ex is IOException or UnauthorizedAccessException) { plan.Log.Add($"{Path.GetFileName(path)}: could not read its manifest ({ex.Message}) - loading it as a plugin without one."); } string fv = ""; if (fileVersion != null && string.IsNullOrEmpty(manifest?.Version)) { try { fv = fileVersion(path) ?? ""; } catch { fv = ""; } } if (manifest != null) { withManifest++; plan.Log.Add($"{Path.GetFileName(path)}: manifest {Describe(manifest)}."); } candidates.Add(new PluginCandidate(path, manifest, fv)); } PluginResolution res = PluginManifestResolver.Resolve(candidates, engineApi); foreach (PluginCandidate c in res.Order) plan.Load.Add((c.Key, c.Manifest)); plan.Refused.AddRange(res.Refused); foreach (string note in res.Notes) plan.Log.Add(note); if (withManifest == 0 && paths.Count > 0) plan.Log.Add("no plugin has a manifest; loading them as listed."); return plan; } /// /// Null when every plugin requires has started; otherwise why not. /// answers by plugin name: true started, false loaded but not /// started (failed or refused at Init), null not loaded at all. /// public static string? CheckRequiredStarted(RynthPluginManifest? manifest, Func started) { if (manifest == null) return null; foreach (RynthPluginDependency d in manifest.Dependencies) { if (d.Optional) continue; bool? s = started(d.Name); if (s == true) continue; return s == false ? $"{manifest.Name} needs {d.Name}, which failed to start" : $"{manifest.Name} needs {d.Name}, which is not loaded"; } return null; } /// "RynthOracle 2026.10.5.1, API 71+, needs RynthAi (optional)". public static string Describe(RynthPluginManifest m) { string api = m.MaxEngineApi != 0 ? $"API {m.MinEngineApi}-{m.MaxEngineApi}" : $"API {m.MinEngineApi}+"; string s = $"{m.Name}{(m.Version.Length > 0 ? " " + m.Version : "")}, {api}"; if (m.Dependencies.Count > 0) { var parts = new List(m.Dependencies.Count); foreach (RynthPluginDependency d in m.Dependencies) parts.Add(d.Name + (d.MinVersion.Length > 0 ? " " + d.MinVersion + "+" : "") + (d.Optional ? " (optional)" : "")); s += ", needs " + string.Join(", ", parts); } return s; } }