[199] | 1 | using System;
|
---|
| 2 | using System.IO;
|
---|
[324] | 3 | using UnityEngine;
|
---|
[329] | 4 | using UnityEngine.Profiling;
|
---|
[325] | 5 | using Object = UnityEngine.Object;
|
---|
[199] | 6 |
|
---|
[325] | 7 | namespace AllocsFixes.FileCache {
|
---|
[199] | 8 | // Special "cache" for map tile folder as both map rendering and webserver access files in there.
|
---|
| 9 | // Only map rendering tiles are cached. Writing is done by WriteThrough.
|
---|
[325] | 10 | public class MapTileCache : AbstractCache {
|
---|
| 11 | private readonly byte[] transparentTile;
|
---|
[199] | 12 | private CurrentZoomFile[] cache;
|
---|
| 13 |
|
---|
[325] | 14 | public MapTileCache (int _tileSize) {
|
---|
[324] | 15 | Texture2D tex = new Texture2D (_tileSize, _tileSize);
|
---|
| 16 | Color nullColor = new Color (0, 0, 0, 0);
|
---|
[269] | 17 | for (int x = 0; x < _tileSize; x++) {
|
---|
| 18 | for (int y = 0; y < _tileSize; y++) {
|
---|
| 19 | tex.SetPixel (x, y, nullColor);
|
---|
| 20 | }
|
---|
| 21 | }
|
---|
[325] | 22 |
|
---|
[269] | 23 | transparentTile = tex.EncodeToPNG ();
|
---|
[325] | 24 | Object.Destroy (tex);
|
---|
[199] | 25 | }
|
---|
| 26 |
|
---|
[351] | 27 | public void SetZoomCount (int _count) {
|
---|
| 28 | cache = new CurrentZoomFile[_count];
|
---|
[329] | 29 | for (int i = 0; i < cache.Length; i++) {
|
---|
| 30 | cache [i] = new CurrentZoomFile ();
|
---|
| 31 | }
|
---|
[199] | 32 | }
|
---|
| 33 |
|
---|
[351] | 34 | public byte[] LoadTile (int _zoomlevel, string _filename) {
|
---|
[199] | 35 | try {
|
---|
| 36 | lock (cache) {
|
---|
[351] | 37 | CurrentZoomFile cacheEntry = cache [_zoomlevel];
|
---|
[199] | 38 |
|
---|
[391] | 39 | if (cacheEntry.filename != null && cacheEntry.filename.Equals (_filename)) {
|
---|
| 40 | return cacheEntry.pngData;
|
---|
| 41 | }
|
---|
[199] | 42 |
|
---|
[391] | 43 | cacheEntry.filename = _filename;
|
---|
| 44 |
|
---|
| 45 | if (!File.Exists (_filename)) {
|
---|
| 46 | cacheEntry.pngData = null;
|
---|
| 47 | return null;
|
---|
[199] | 48 | }
|
---|
[325] | 49 |
|
---|
[391] | 50 | Profiler.BeginSample ("ReadPng");
|
---|
| 51 | cacheEntry.pngData = ReadAllBytes (_filename);
|
---|
| 52 | Profiler.EndSample ();
|
---|
| 53 |
|
---|
[329] | 54 | return cacheEntry.pngData;
|
---|
[199] | 55 | }
|
---|
| 56 | } catch (Exception e) {
|
---|
[329] | 57 | Log.Warning ("Error in MapTileCache.LoadTile: " + e);
|
---|
[199] | 58 | }
|
---|
[325] | 59 |
|
---|
[199] | 60 | return null;
|
---|
| 61 | }
|
---|
| 62 |
|
---|
[351] | 63 | public void SaveTile (int _zoomlevel, byte[] _contentPng) {
|
---|
[199] | 64 | try {
|
---|
| 65 | lock (cache) {
|
---|
[351] | 66 | CurrentZoomFile cacheEntry = cache [_zoomlevel];
|
---|
[331] | 67 |
|
---|
| 68 | string file = cacheEntry.filename;
|
---|
| 69 | if (string.IsNullOrEmpty (file)) {
|
---|
[326] | 70 | return;
|
---|
[199] | 71 | }
|
---|
[329] | 72 |
|
---|
[351] | 73 | cacheEntry.pngData = _contentPng;
|
---|
[326] | 74 |
|
---|
[329] | 75 | Profiler.BeginSample ("WritePng");
|
---|
[331] | 76 | using (Stream stream = new FileStream (file, FileMode.Create, FileAccess.ReadWrite, FileShare.None,
|
---|
| 77 | 4096)) {
|
---|
[351] | 78 | stream.Write (_contentPng, 0, _contentPng.Length);
|
---|
[331] | 79 | }
|
---|
[329] | 80 | Profiler.EndSample ();
|
---|
[199] | 81 | }
|
---|
| 82 | } catch (Exception e) {
|
---|
[329] | 83 | Log.Warning ("Error in MapTileCache.SaveTile: " + e);
|
---|
[199] | 84 | }
|
---|
| 85 | }
|
---|
| 86 |
|
---|
[351] | 87 | public void ResetTile (int _zoomlevel) {
|
---|
[346] | 88 | try {
|
---|
| 89 | lock (cache) {
|
---|
[351] | 90 | cache [_zoomlevel].filename = null;
|
---|
| 91 | cache [_zoomlevel].pngData = null;
|
---|
[346] | 92 | }
|
---|
| 93 | } catch (Exception e) {
|
---|
| 94 | Log.Warning ("Error in MapTileCache.ResetTile: " + e);
|
---|
| 95 | }
|
---|
| 96 | }
|
---|
| 97 |
|
---|
[351] | 98 | public override byte[] GetFileContent (string _filename) {
|
---|
[199] | 99 | try {
|
---|
| 100 | lock (cache) {
|
---|
| 101 | foreach (CurrentZoomFile czf in cache) {
|
---|
[351] | 102 | if (czf.filename != null && czf.filename.Equals (_filename)) {
|
---|
[329] | 103 | return czf.pngData;
|
---|
[325] | 104 | }
|
---|
[199] | 105 | }
|
---|
| 106 |
|
---|
[391] | 107 | return !File.Exists (_filename) ? transparentTile : ReadAllBytes (_filename);
|
---|
[199] | 108 | }
|
---|
| 109 | } catch (Exception e) {
|
---|
[329] | 110 | Log.Warning ("Error in MapTileCache.GetFileContent: " + e);
|
---|
[199] | 111 | }
|
---|
[325] | 112 |
|
---|
[199] | 113 | return null;
|
---|
| 114 | }
|
---|
| 115 |
|
---|
[392] | 116 | public override (int, int) Invalidate () {
|
---|
| 117 | return (0, 0);
|
---|
| 118 | }
|
---|
| 119 |
|
---|
[331] | 120 | private static byte[] ReadAllBytes (string _path) {
|
---|
| 121 | using (FileStream fileStream = new FileStream(_path, FileMode.Open, FileAccess.Read, FileShare.Read, 4096)) {
|
---|
| 122 | int bytesRead = 0;
|
---|
| 123 | int bytesLeft = (int) fileStream.Length;
|
---|
| 124 | byte[] result = new byte[bytesLeft];
|
---|
| 125 | while (bytesLeft > 0) {
|
---|
| 126 | int readThisTime = fileStream.Read (result, bytesRead, bytesLeft);
|
---|
| 127 | if (readThisTime == 0) {
|
---|
| 128 | throw new IOException ("Unexpected end of stream");
|
---|
| 129 | }
|
---|
| 130 |
|
---|
| 131 | bytesRead += readThisTime;
|
---|
| 132 | bytesLeft -= readThisTime;
|
---|
| 133 | }
|
---|
| 134 |
|
---|
| 135 | return result;
|
---|
| 136 | }
|
---|
| 137 | }
|
---|
| 138 |
|
---|
| 139 |
|
---|
[329] | 140 | private class CurrentZoomFile {
|
---|
[325] | 141 | public string filename;
|
---|
[329] | 142 | public byte[] pngData;
|
---|
[325] | 143 | }
|
---|
[199] | 144 | }
|
---|
[325] | 145 | }
|
---|