using JetBrains.Annotations; using Utf8Json; using Webserver.Permissions; namespace Webserver.WebAPI.APIs { [UsedImplicitly] public class Mods : AbsRestApi { private readonly byte[] loadedWebMods; public Mods (Web _parent) { JsonWriter writer = new JsonWriter (); writer.WriteBeginArray (); for (int i = 0; i < _parent.webMods.Count; i++) { WebMod webMod = _parent.webMods [i]; if (i > 0) { writer.WriteValueSeparator (); } writer.WriteBeginObject (); writeModJson (ref writer, webMod); if (webMod.ReactBundle != null || webMod.CssPath != null) { writer.WriteValueSeparator (); writer.WritePropertyName ("web"); writer.WriteBeginObject (); string webModReactBundle = webMod.ReactBundle; if (webModReactBundle != null) { writer.WritePropertyName ("bundle"); writer.WriteString (webModReactBundle); } string webModCssFile = webMod.CssPath; if (webModCssFile != null) { if (webModReactBundle != null) { writer.WriteValueSeparator (); } writer.WritePropertyName ("css"); writer.WriteString (webModCssFile); } writer.WriteEndObject (); } writer.WriteEndObject (); } writer.WriteEndArray (); loadedWebMods = writer.ToUtf8ByteArray (); } private void writeModJson (ref JsonWriter _writer, WebMod _webMod) { _writer.WritePropertyName ("name"); _writer.WriteString (_webMod.ParentMod.Name); _writer.WriteValueSeparator (); _writer.WritePropertyName ("displayName"); JsonCommons.WriteStringOrNull (ref _writer, _webMod.ParentMod.DisplayName); _writer.WriteValueSeparator (); _writer.WritePropertyName ("description"); JsonCommons.WriteStringOrNull (ref _writer, _webMod.ParentMod.Description); _writer.WriteValueSeparator (); _writer.WritePropertyName ("author"); JsonCommons.WriteStringOrNull (ref _writer, _webMod.ParentMod.Author); _writer.WriteValueSeparator (); _writer.WritePropertyName ("version"); JsonCommons.WriteStringOrNull (ref _writer, _webMod.ParentMod.VersionString); _writer.WriteValueSeparator (); _writer.WritePropertyName ("website"); JsonCommons.WriteStringOrNull (ref _writer, _webMod.ParentMod.Website); } protected override void HandleRestGet (RequestContext _context) { PrepareEnvelopedResult (out JsonWriter writer); writer.WriteRaw (loadedWebMods); SendEnvelopedResult (_context, ref writer); } public override int DefaultPermissionLevel () => AdminWebModules.PermissionLevelGuest; } }