[391] | 1 | using System.IO;
|
---|
| 2 | using AllocsFixes.FileCache;
|
---|
| 3 | using Webserver.UrlHandlers;
|
---|
| 4 |
|
---|
| 5 | namespace Webserver {
|
---|
| 6 | public class WebMod {
|
---|
| 7 | private const string modsBaseUrl = "/webmods/";
|
---|
| 8 | private const string reactBundleName = "bundle.js";
|
---|
| 9 | private const string stylingFileName = "styling.css";
|
---|
| 10 |
|
---|
| 11 | public readonly Mod ParentMod;
|
---|
| 12 | public readonly string ReactBundle; // Absolute web path to the React bundle if the mod has one, e.g. "/webmods/myMod/bundle.js"
|
---|
| 13 | public readonly string CssPath; // Absolute web path to a CSS if the mod has one, e.g. "/webmods/myMod/styling.css";
|
---|
| 14 |
|
---|
| 15 | public WebMod (Web _parentWeb, Mod _parentMod, bool _useStaticCache) {
|
---|
| 16 | string folder = _parentMod.Path + "/WebMod";
|
---|
| 17 | if (!Directory.Exists (folder)) {
|
---|
| 18 | throw new InvalidDataException("No WebMod folder in mod");
|
---|
| 19 | }
|
---|
| 20 |
|
---|
| 21 | string urlWebModBase = $"{modsBaseUrl}{_parentMod.FolderName}/";
|
---|
| 22 |
|
---|
| 23 | ReactBundle = folder + "/" + reactBundleName;
|
---|
| 24 | if (File.Exists (ReactBundle)) {
|
---|
| 25 | ReactBundle = urlWebModBase + reactBundleName;
|
---|
| 26 | } else {
|
---|
| 27 | ReactBundle = null;
|
---|
| 28 | }
|
---|
| 29 |
|
---|
| 30 | CssPath = folder + "/" + stylingFileName;
|
---|
| 31 | if (File.Exists (CssPath)) {
|
---|
| 32 | CssPath = urlWebModBase + stylingFileName;
|
---|
| 33 | } else {
|
---|
| 34 | CssPath = null;
|
---|
| 35 | }
|
---|
| 36 |
|
---|
| 37 | if (ReactBundle == null && CssPath == null) {
|
---|
| 38 | throw new InvalidDataException($"WebMod folder has neither a {reactBundleName} nor a {stylingFileName}");
|
---|
| 39 | }
|
---|
| 40 |
|
---|
| 41 | ParentMod = _parentMod;
|
---|
| 42 |
|
---|
| 43 | _parentWeb.RegisterPathHandler (urlWebModBase, new StaticHandler (
|
---|
| 44 | folder,
|
---|
| 45 | _useStaticCache ? (AbstractCache) new SimpleCache () : new DirectAccess (),
|
---|
| 46 | false)
|
---|
| 47 | );
|
---|
| 48 | }
|
---|
| 49 | }
|
---|
| 50 | }
|
---|