Changeset 454 for binary-improvements/MapRendering/API.cs
- Timestamp:
- Jul 28, 2023, 8:42:10 PM (16 months ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
binary-improvements/MapRendering/API.cs
r420 r454 1 using AllocsFixes.NetConnections.Servers.Web; 2 using AllocsFixes.NetConnections.Servers.Web.Handlers; 1 using System; 2 using System.IO; 3 using Webserver; 4 using Webserver.FileCache; 5 using Webserver.UrlHandlers; 3 6 4 7 namespace AllocsFixes { 5 8 public class API : IModApi { 6 private Web webInstance;9 private Mod modInstance; 7 10 8 11 public void InitMod (Mod _modInstance) { 9 ModEvents.GameStartDone.RegisterHandler (GameStartDone); 10 ModEvents.GameShutdown.RegisterHandler (GameShutdown); 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 }; 11 37 } 12 38 13 private void GameStartDone () { 14 // ReSharper disable once ObjectCreationAsStatement 15 if (!ConnectionManager.Instance.IsServer) { 16 return; 17 } 18 19 webInstance = new Web (); 20 LogBuffer.Init (); 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 // } 21 46 22 if (ItemIconHandler.Instance != null) { 23 ItemIconHandler.Instance.LoadIcons (); 24 } 25 } 26 27 private void GameShutdown () { 28 webInstance?.Shutdown (); 29 } 47 30 48 } 31 49 }
Note:
See TracChangeset
for help on using the changeset viewer.