private bool ProcessSpecialRoutes(HttpRequestEventArgs e) { var req = e.Request; var res = e.Response; var rawRequestPath = req.RawUrl; byte[] textBytes; //Debug.Log($"WebMapAPI: ProcessSpecialRoutes() RawUrl: {rawRequestPath}"); if (rawRequestPath.Contains("?")) { rawRequestPath = rawRequestPath.Substring(0, rawRequestPath.IndexOf('?')); // Debug.Log($"WebMapAPI: ProcessSpecialRoutes() RawUrl: {rawRequestPath}"); } switch (rawRequestPath) { case "/config": res.Headers.Add(HttpResponseHeader.CacheControl, "no-cache"); res.Headers.Add("Access-Control-Allow-Origin: *"); res.ContentType = "application/json"; res.StatusCode = 200; textBytes = Encoding.UTF8.GetBytes(WebMapConfig.makeClientConfigJSON()); res.ContentLength64 = textBytes.Length; res.Close(textBytes, true); return(true); case "/map": // Doing things this way to make the full map harder to accidentally see. res.Headers.Add(HttpResponseHeader.CacheControl, "public, max-age=604800, immutable"); res.Headers.Add("Access-Control-Allow-Origin: *"); res.ContentType = "application/octet-stream"; res.StatusCode = 200; res.ContentLength64 = mapImageData.Length; res.Close(mapImageData, true); return(true); case "/fog": res.Headers.Add(HttpResponseHeader.CacheControl, "no-cache"); res.Headers.Add("Access-Control-Allow-Origin: *"); res.ContentType = "image/png"; res.StatusCode = 200; var fogBytes = fogTexture.EncodeToPNG(); res.ContentLength64 = fogBytes.Length; res.Close(fogBytes, true); return(true); case "/pins": res.Headers.Add(HttpResponseHeader.CacheControl, "no-cache"); res.Headers.Add("Access-Control-Allow-Origin: *"); res.ContentType = "text/csv"; res.StatusCode = 200; var text = String.Join("\n", pins); textBytes = Encoding.UTF8.GetBytes(text); res.ContentLength64 = textBytes.Length; res.Close(textBytes, true); return(true); } return(false); }
//The Awake() method is run at the very start when the game is initialized. public void Awake() { var harmony = new Harmony("com.kylepaulsen.valheim.webmap"); Harmony.CreateAndPatchAll(Assembly.GetExecutingAssembly(), (string)null); var pluginPath = System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); WebMapConfig.readConfigFile(Path.Combine(pluginPath, "config.json")); var mapDataPath = Path.Combine(pluginPath, "map_data"); Directory.CreateDirectory(mapDataPath); worldDataPath = Path.Combine(mapDataPath, WebMapConfig.getWorldName()); Directory.CreateDirectory(worldDataPath); mapDataServer = new MapDataServer(); mapDataServer.ListenAsync(); var mapImagePath = Path.Combine(worldDataPath, "map"); try { mapDataServer.mapImageData = File.ReadAllBytes(mapImagePath); } catch (Exception e) { Debug.Log("WebMap: Failed to read map image data from disk. " + e.Message); } var fogImagePath = Path.Combine(worldDataPath, "fog.png"); try { var fogTexture = new Texture2D(WebMapConfig.TEXTURE_SIZE, WebMapConfig.TEXTURE_SIZE); var fogBytes = File.ReadAllBytes(fogImagePath); fogTexture.LoadImage(fogBytes); mapDataServer.fogTexture = fogTexture; } catch (Exception e) { Debug.Log("WebMap: Failed to read fog image data from disk... Making new fog image..." + e.Message); var fogTexture = new Texture2D(WebMapConfig.TEXTURE_SIZE, WebMapConfig.TEXTURE_SIZE, TextureFormat.RGB24, false); var fogColors = new Color32[WebMapConfig.TEXTURE_SIZE * WebMapConfig.TEXTURE_SIZE]; for (var t = 0; t < fogColors.Length; t++) { fogColors[t] = Color.black; } fogTexture.SetPixels32(fogColors); var fogPngBytes = fogTexture.EncodeToPNG(); mapDataServer.fogTexture = fogTexture; try { File.WriteAllBytes(fogImagePath, fogPngBytes); } catch { Debug.Log("WebMap: FAILED TO WRITE FOG FILE!"); } } InvokeRepeating("UpdateFogTexture", WebMapConfig.UPDATE_FOG_TEXTURE_INTERVAL, WebMapConfig.UPDATE_FOG_TEXTURE_INTERVAL); InvokeRepeating("SaveFogTexture", WebMapConfig.SAVE_FOG_TEXTURE_INTERVAL, WebMapConfig.SAVE_FOG_TEXTURE_INTERVAL); var mapPinsFile = Path.Combine(worldDataPath, "pins.csv"); try { var pinsLines = File.ReadAllLines(mapPinsFile); mapDataServer.pins = new List <string>(pinsLines); } catch (Exception e) { Debug.Log("WebMap: Failed to read pins.csv from disk. " + e.Message); } }