source: TFP-WebServer/WebServer/src/WebAPI/APIs/GameData/Mods.cs@ 474

Last change on this file since 474 was 467, checked in by alloc, 16 months ago

21.1.16.2 WebServer release
Added "id"s to messages in log API / event
Added "baseUrl" to mods API for WebMods

File size: 2.7 KB
Line 
1using JetBrains.Annotations;
2using Utf8Json;
3using Webserver.Permissions;
4
5namespace Webserver.WebAPI.APIs.GameData {
6 [UsedImplicitly]
7 public class Mods : AbsRestApi {
8 private readonly byte[] loadedWebMods;
9
10 public Mods (Web _parent) {
11 JsonWriter writer = new JsonWriter ();
12 writer.WriteBeginArray ();
13
14 for (int i = 0; i < _parent.WebMods.Count; i++) {
15 WebMod webMod = _parent.WebMods [i];
16
17 if (i > 0) {
18 writer.WriteValueSeparator ();
19 }
20
21 writeModJson (ref writer, webMod);
22 }
23
24 writer.WriteEndArray ();
25
26 loadedWebMods = writer.ToUtf8ByteArray ();
27 }
28
29 private static void writeModJson (ref JsonWriter _writer, WebMod _webMod) {
30 _writer.WriteBeginObject ();
31
32 _writer.WritePropertyName ("name");
33 _writer.WriteString (_webMod.ParentMod.Name);
34
35 _writer.WriteValueSeparator ();
36 _writer.WritePropertyName ("displayName");
37 _writer.WriteString (_webMod.ParentMod.DisplayName);
38
39 _writer.WriteValueSeparator ();
40 _writer.WritePropertyName ("description");
41 _writer.WriteString (_webMod.ParentMod.Description);
42
43 _writer.WriteValueSeparator ();
44 _writer.WritePropertyName ("author");
45 _writer.WriteString (_webMod.ParentMod.Author);
46
47 _writer.WriteValueSeparator ();
48 _writer.WritePropertyName ("version");
49 _writer.WriteString (_webMod.ParentMod.VersionString);
50
51 _writer.WriteValueSeparator ();
52 _writer.WritePropertyName ("website");
53 _writer.WriteString (_webMod.ParentMod.Website);
54
55 writeWebModJson (ref _writer, _webMod);
56
57 _writer.WriteEndObject ();
58 }
59
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
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;
95 }
96}
Note: See TracBrowser for help on using the repository browser.