[391] | 1 | using JetBrains.Annotations;
|
---|
[402] | 2 | using Utf8Json;
|
---|
[426] | 3 | using Webserver.Permissions;
|
---|
[391] | 4 |
|
---|
[434] | 5 | namespace Webserver.WebAPI.APIs.GameData {
|
---|
[391] | 6 | [UsedImplicitly]
|
---|
[426] | 7 | public class Mods : AbsRestApi {
|
---|
[402] | 8 | private readonly byte[] loadedWebMods;
|
---|
[391] | 9 |
|
---|
[426] | 10 | public Mods (Web _parent) {
|
---|
[402] | 11 | JsonWriter writer = new JsonWriter ();
|
---|
| 12 | writer.WriteBeginArray ();
|
---|
| 13 |
|
---|
[434] | 14 | for (int i = 0; i < _parent.WebMods.Count; i++) {
|
---|
| 15 | WebMod webMod = _parent.WebMods [i];
|
---|
[426] | 16 |
|
---|
[408] | 17 | if (i > 0) {
|
---|
[402] | 18 | writer.WriteValueSeparator ();
|
---|
| 19 | }
|
---|
[426] | 20 |
|
---|
[402] | 21 | writer.WriteBeginObject ();
|
---|
[408] | 22 |
|
---|
[426] | 23 | writeModJson (ref writer, webMod);
|
---|
[391] | 24 |
|
---|
[426] | 25 | if (webMod.ReactBundle != null || webMod.CssPath != null) {
|
---|
[402] | 26 | writer.WriteValueSeparator ();
|
---|
[408] | 27 |
|
---|
[426] | 28 | writer.WritePropertyName ("web");
|
---|
| 29 | writer.WriteBeginObject ();
|
---|
| 30 |
|
---|
| 31 | string webModReactBundle = webMod.ReactBundle;
|
---|
| 32 | if (webModReactBundle != null) {
|
---|
| 33 | writer.WritePropertyName ("bundle");
|
---|
| 34 | writer.WriteString (webModReactBundle);
|
---|
| 35 | }
|
---|
| 36 |
|
---|
| 37 | string webModCssFile = webMod.CssPath;
|
---|
| 38 | if (webModCssFile != null) {
|
---|
| 39 | if (webModReactBundle != null) {
|
---|
| 40 | writer.WriteValueSeparator ();
|
---|
| 41 | }
|
---|
| 42 |
|
---|
| 43 | writer.WritePropertyName ("css");
|
---|
| 44 | writer.WriteString (webModCssFile);
|
---|
| 45 | }
|
---|
| 46 |
|
---|
| 47 | writer.WriteEndObject ();
|
---|
[391] | 48 | }
|
---|
[408] | 49 |
|
---|
[402] | 50 | writer.WriteEndObject ();
|
---|
| 51 | }
|
---|
[408] | 52 |
|
---|
[402] | 53 | writer.WriteEndArray ();
|
---|
[391] | 54 |
|
---|
[402] | 55 | loadedWebMods = writer.ToUtf8ByteArray ();
|
---|
[391] | 56 | }
|
---|
| 57 |
|
---|
[426] | 58 | private void writeModJson (ref JsonWriter _writer, WebMod _webMod) {
|
---|
| 59 | _writer.WritePropertyName ("name");
|
---|
| 60 | _writer.WriteString (_webMod.ParentMod.Name);
|
---|
| 61 |
|
---|
| 62 | _writer.WriteValueSeparator ();
|
---|
| 63 | _writer.WritePropertyName ("displayName");
|
---|
[434] | 64 | _writer.WriteString (_webMod.ParentMod.DisplayName);
|
---|
[426] | 65 |
|
---|
| 66 | _writer.WriteValueSeparator ();
|
---|
| 67 | _writer.WritePropertyName ("description");
|
---|
[434] | 68 | _writer.WriteString (_webMod.ParentMod.Description);
|
---|
[426] | 69 |
|
---|
| 70 | _writer.WriteValueSeparator ();
|
---|
| 71 | _writer.WritePropertyName ("author");
|
---|
[434] | 72 | _writer.WriteString (_webMod.ParentMod.Author);
|
---|
[426] | 73 |
|
---|
| 74 | _writer.WriteValueSeparator ();
|
---|
| 75 | _writer.WritePropertyName ("version");
|
---|
[434] | 76 | _writer.WriteString (_webMod.ParentMod.VersionString);
|
---|
[426] | 77 |
|
---|
| 78 | _writer.WriteValueSeparator ();
|
---|
| 79 | _writer.WritePropertyName ("website");
|
---|
[434] | 80 | _writer.WriteString (_webMod.ParentMod.Website);
|
---|
[426] | 81 | }
|
---|
[432] | 82 |
|
---|
| 83 | protected override void HandleRestGet (RequestContext _context) {
|
---|
| 84 | PrepareEnvelopedResult (out JsonWriter writer);
|
---|
| 85 | writer.WriteRaw (loadedWebMods);
|
---|
| 86 | SendEnvelopedResult (_context, ref writer);
|
---|
| 87 | }
|
---|
| 88 |
|
---|
| 89 | public override int DefaultPermissionLevel () => AdminWebModules.PermissionLevelGuest;
|
---|
[391] | 90 | }
|
---|
[426] | 91 | }
|
---|