| 1 | using System;
|
|---|
| 2 | using System.IO;
|
|---|
| 3 | using Webserver;
|
|---|
| 4 | using Webserver.FileCache;
|
|---|
| 5 | using Webserver.UrlHandlers;
|
|---|
| 6 |
|
|---|
| 7 | namespace AllocsFixes {
|
|---|
| 8 | public class API : IModApi {
|
|---|
| 9 | private Mod modInstance;
|
|---|
| 10 |
|
|---|
| 11 | public void InitMod (Mod _modInstance) {
|
|---|
| 12 | modInstance = _modInstance;
|
|---|
| 13 |
|
|---|
| 14 | Web.ServerInitialized += _web => {
|
|---|
| 15 | try {
|
|---|
| 16 | const string legacyModUrl = "/legacymap";
|
|---|
| 17 | const string legacyFilesFoldername = "webserver_legacy";
|
|---|
| 18 | string legacyFilePath = $"{modInstance.Path}/{legacyFilesFoldername}";
|
|---|
| 19 |
|
|---|
| 20 | if (!Directory.Exists (legacyFilePath)) {
|
|---|
| 21 | Log.Out ($"Legacy webmod feature not started (folder \"{legacyFilesFoldername}\" not found in Allocs_WebAndMapRendering mod folder)");
|
|---|
| 22 | return;
|
|---|
| 23 | }
|
|---|
| 24 |
|
|---|
| 25 | // TODO: Read from config
|
|---|
| 26 | bool useStaticCache = false;
|
|---|
| 27 |
|
|---|
| 28 | _web.RegisterPathHandler ("/legacymap.htm", new SimpleRedirectHandler ($"{legacyModUrl}/index.html"));
|
|---|
| 29 | _web.RegisterPathHandler ($"{legacyModUrl}/", new StaticHandler (legacyFilePath, useStaticCache ? (AbstractCache) new SimpleCache () : new DirectAccess (), false));
|
|---|
| 30 |
|
|---|
| 31 | int webPort = GamePrefs.GetInt (EnumUtils.Parse<EnumGamePrefs> (nameof (EnumGamePrefs.WebDashboardPort)));
|
|---|
| 32 | Log.Out ($"Started legacy webmod feature on port {webPort}, local adress {legacyModUrl}");
|
|---|
| 33 | } catch (Exception e) {
|
|---|
| 34 | Log.Out ("Error in Web.ctor: " + e);
|
|---|
| 35 | }
|
|---|
| 36 | };
|
|---|
| 37 | }
|
|---|
| 38 |
|
|---|
| 39 | // public static void SetResponseTextContent (HttpListenerResponse _context.Response, string _text) {
|
|---|
| 40 | // byte[] buf = Encoding.UTF8.GetBytes (_text);
|
|---|
| 41 | // _context.Response.ContentLength64 = buf.Length;
|
|---|
| 42 | // _context.Response.ContentType = "text/html";
|
|---|
| 43 | // _context.Response.ContentEncoding = Encoding.UTF8;
|
|---|
| 44 | // _context.Response.OutputStream.Write (buf, 0, buf.Length);
|
|---|
| 45 | // }
|
|---|
| 46 |
|
|---|
| 47 |
|
|---|
| 48 | }
|
|---|
| 49 | }
|
|---|