[454] | 1 | using System;
|
---|
| 2 | using System.IO;
|
---|
[455] | 3 | using JetBrains.Annotations;
|
---|
[454] | 4 | using Webserver.FileCache;
|
---|
| 5 | using Webserver.UrlHandlers;
|
---|
[224] | 6 |
|
---|
[455] | 7 | namespace AllocsFixes.Web {
|
---|
| 8 | [UsedImplicitly]
|
---|
[324] | 9 | public class API : IModApi {
|
---|
[455] | 10 | private static Mod modInstance;
|
---|
[367] | 11 |
|
---|
[369] | 12 | public void InitMod (Mod _modInstance) {
|
---|
[454] | 13 | modInstance = _modInstance;
|
---|
[325] | 14 |
|
---|
[455] | 15 | Webserver.Web.ServerInitialized += OnWebServerInitialized;
|
---|
| 16 | }
|
---|
[224] | 17 |
|
---|
[455] | 18 | private static void OnWebServerInitialized (Webserver.Web _web) {
|
---|
| 19 | try {
|
---|
| 20 | const string legacyModUrl = "/legacymap";
|
---|
| 21 | const string legacyFilesFoldername = "webserver_legacy";
|
---|
| 22 | string legacyFilePath = $"{modInstance.Path}/{legacyFilesFoldername}";
|
---|
[454] | 23 |
|
---|
[455] | 24 | if (!Directory.Exists (legacyFilePath)) {
|
---|
| 25 | Log.Out ($"Legacy webmod feature not started (folder \"{legacyFilesFoldername}\" not found in Allocs_WebAndMapRendering mod folder)");
|
---|
| 26 | return;
|
---|
[454] | 27 | }
|
---|
[267] | 28 |
|
---|
[455] | 29 | // TODO: Read from config
|
---|
| 30 | bool useStaticCache = false;
|
---|
[454] | 31 |
|
---|
[455] | 32 | _web.RegisterPathHandler ($"{legacyModUrl}", new SimpleRedirectHandler ($"{legacyModUrl}/index.html"));
|
---|
| 33 | _web.RegisterPathHandler ($"{legacyModUrl}/", new StaticHandler (legacyFilePath, useStaticCache ? new SimpleCache () : new DirectAccess (), false));
|
---|
| 34 |
|
---|
| 35 | int webPort = GamePrefs.GetInt (EnumUtils.Parse<EnumGamePrefs> (nameof(EnumGamePrefs.WebDashboardPort)));
|
---|
[464] | 36 | Log.Out ($"Started legacy webmod feature on port {webPort}, local address {legacyModUrl}");
|
---|
[455] | 37 | } catch (Exception e) {
|
---|
| 38 | Log.Out ($"Error in Web.ctor: {e}");
|
---|
| 39 | }
|
---|
| 40 | }
|
---|
[224] | 41 | }
|
---|
[325] | 42 | }
|
---|