[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 |
|
---|
| 21 | writeModJson (ref writer, webMod);
|
---|
[402] | 22 | }
|
---|
[408] | 23 |
|
---|
[402] | 24 | writer.WriteEndArray ();
|
---|
[391] | 25 |
|
---|
[402] | 26 | loadedWebMods = writer.ToUtf8ByteArray ();
|
---|
[391] | 27 | }
|
---|
| 28 |
|
---|
[467] | 29 | private static void writeModJson (ref JsonWriter _writer, WebMod _webMod) {
|
---|
| 30 | _writer.WriteBeginObject ();
|
---|
| 31 |
|
---|
[426] | 32 | _writer.WritePropertyName ("name");
|
---|
| 33 | _writer.WriteString (_webMod.ParentMod.Name);
|
---|
| 34 |
|
---|
| 35 | _writer.WriteValueSeparator ();
|
---|
| 36 | _writer.WritePropertyName ("displayName");
|
---|
[434] | 37 | _writer.WriteString (_webMod.ParentMod.DisplayName);
|
---|
[426] | 38 |
|
---|
| 39 | _writer.WriteValueSeparator ();
|
---|
| 40 | _writer.WritePropertyName ("description");
|
---|
[434] | 41 | _writer.WriteString (_webMod.ParentMod.Description);
|
---|
[426] | 42 |
|
---|
| 43 | _writer.WriteValueSeparator ();
|
---|
| 44 | _writer.WritePropertyName ("author");
|
---|
[434] | 45 | _writer.WriteString (_webMod.ParentMod.Author);
|
---|
[426] | 46 |
|
---|
| 47 | _writer.WriteValueSeparator ();
|
---|
| 48 | _writer.WritePropertyName ("version");
|
---|
[434] | 49 | _writer.WriteString (_webMod.ParentMod.VersionString);
|
---|
[426] | 50 |
|
---|
| 51 | _writer.WriteValueSeparator ();
|
---|
| 52 | _writer.WritePropertyName ("website");
|
---|
[434] | 53 | _writer.WriteString (_webMod.ParentMod.Website);
|
---|
[467] | 54 |
|
---|
| 55 | writeWebModJson (ref _writer, _webMod);
|
---|
| 56 |
|
---|
| 57 | _writer.WriteEndObject ();
|
---|
[426] | 58 | }
|
---|
[432] | 59 |
|
---|
[467] | 60 | private static void writeWebModJson (ref JsonWriter _writer, WebMod _webMod) {
|
---|
| 61 | if (_webMod.ModUrl != null) {
|
---|
| 62 | _writer.WriteValueSeparator ();
|
---|
| 63 |
|
---|
| 64 | _writer.WritePropertyName ("web");
|
---|
| 65 | _writer.WriteBeginObject ();
|
---|
| 66 |
|
---|
| 67 | _writer.WritePropertyName ("baseUrl");
|
---|
| 68 | _writer.WriteString (_webMod.ModUrl);
|
---|
| 69 |
|
---|
| 70 | string webModReactBundle = _webMod.ReactBundle;
|
---|
| 71 | if (webModReactBundle != null) {
|
---|
| 72 | _writer.WriteValueSeparator ();
|
---|
| 73 | _writer.WritePropertyName ("bundle");
|
---|
| 74 | _writer.WriteString (webModReactBundle);
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 | string webModCssFile = _webMod.CssPath;
|
---|
| 78 | if (webModCssFile != null) {
|
---|
| 79 | _writer.WriteValueSeparator ();
|
---|
| 80 | _writer.WritePropertyName ("css");
|
---|
| 81 | _writer.WriteString (webModCssFile);
|
---|
| 82 | }
|
---|
| 83 |
|
---|
| 84 | _writer.WriteEndObject ();
|
---|
| 85 | }
|
---|
| 86 | }
|
---|
| 87 |
|
---|
[432] | 88 | protected override void HandleRestGet (RequestContext _context) {
|
---|
| 89 | PrepareEnvelopedResult (out JsonWriter writer);
|
---|
| 90 | writer.WriteRaw (loadedWebMods);
|
---|
| 91 | SendEnvelopedResult (_context, ref writer);
|
---|
| 92 | }
|
---|
| 93 |
|
---|
| 94 | public override int DefaultPermissionLevel () => AdminWebModules.PermissionLevelGuest;
|
---|
[391] | 95 | }
|
---|
[426] | 96 | }
|
---|