| [454] | 1 | using System; | 
|---|
|  | 2 | using System.IO; | 
|---|
|  | 3 | using Webserver; | 
|---|
|  | 4 | using Webserver.FileCache; | 
|---|
|  | 5 | using Webserver.UrlHandlers; | 
|---|
| [224] | 6 |  | 
|---|
| [331] | 7 | namespace AllocsFixes { | 
|---|
| [324] | 8 | public class API : IModApi { | 
|---|
| [454] | 9 | private Mod modInstance; | 
|---|
| [367] | 10 |  | 
|---|
| [369] | 11 | public void InitMod (Mod _modInstance) { | 
|---|
| [454] | 12 | modInstance = _modInstance; | 
|---|
| [325] | 13 |  | 
|---|
| [454] | 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 | } | 
|---|
| [224] | 24 |  | 
|---|
| [454] | 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 | }; | 
|---|
| [291] | 37 | } | 
|---|
| [267] | 38 |  | 
|---|
| [454] | 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 |  | 
|---|
| [224] | 48 | } | 
|---|
| [325] | 49 | } | 
|---|