[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];
|
---|
[329] | 38 |
|
---|
[351] | 39 | if (cacheEntry.filename == null || !cacheEntry.filename.Equals (_filename)) {
|
---|
| 40 | cacheEntry.filename = _filename;
|
---|
[199] | 41 |
|
---|
[351] | 42 | if (!File.Exists (_filename)) {
|
---|
[329] | 43 | cacheEntry.pngData = null;
|
---|
[199] | 44 | return null;
|
---|
| 45 | }
|
---|
| 46 |
|
---|
[329] | 47 | Profiler.BeginSample ("ReadPng");
|
---|
[351] | 48 | cacheEntry.pngData = ReadAllBytes (_filename);
|
---|
[329] | 49 | Profiler.EndSample ();
|
---|
[199] | 50 | }
|
---|
[325] | 51 |
|
---|
[329] | 52 | return cacheEntry.pngData;
|
---|
[199] | 53 | }
|
---|
| 54 | } catch (Exception e) {
|
---|
[329] | 55 | Log.Warning ("Error in MapTileCache.LoadTile: " + e);
|
---|
[199] | 56 | }
|
---|
[325] | 57 |
|
---|
[199] | 58 | return null;
|
---|
| 59 | }
|
---|
| 60 |
|
---|
[351] | 61 | public void SaveTile (int _zoomlevel, byte[] _contentPng) {
|
---|
[199] | 62 | try {
|
---|
| 63 | lock (cache) {
|
---|
[351] | 64 | CurrentZoomFile cacheEntry = cache [_zoomlevel];
|
---|
[331] | 65 |
|
---|
| 66 | string file = cacheEntry.filename;
|
---|
| 67 | if (string.IsNullOrEmpty (file)) {
|
---|
[326] | 68 | return;
|
---|
[199] | 69 | }
|
---|
[329] | 70 |
|
---|
[351] | 71 | cacheEntry.pngData = _contentPng;
|
---|
[326] | 72 |
|
---|
[329] | 73 | Profiler.BeginSample ("WritePng");
|
---|
[331] | 74 | using (Stream stream = new FileStream (file, FileMode.Create, FileAccess.ReadWrite, FileShare.None,
|
---|
| 75 | 4096)) {
|
---|
[351] | 76 | stream.Write (_contentPng, 0, _contentPng.Length);
|
---|
[331] | 77 | }
|
---|
[329] | 78 | Profiler.EndSample ();
|
---|
[199] | 79 | }
|
---|
| 80 | } catch (Exception e) {
|
---|
[329] | 81 | Log.Warning ("Error in MapTileCache.SaveTile: " + e);
|
---|
[199] | 82 | }
|
---|
| 83 | }
|
---|
| 84 |
|
---|
[351] | 85 | public void ResetTile (int _zoomlevel) {
|
---|
[346] | 86 | try {
|
---|
| 87 | lock (cache) {
|
---|
[351] | 88 | cache [_zoomlevel].filename = null;
|
---|
| 89 | cache [_zoomlevel].pngData = null;
|
---|
[346] | 90 | }
|
---|
| 91 | } catch (Exception e) {
|
---|
| 92 | Log.Warning ("Error in MapTileCache.ResetTile: " + e);
|
---|
| 93 | }
|
---|
| 94 | }
|
---|
| 95 |
|
---|
[351] | 96 | public override byte[] GetFileContent (string _filename) {
|
---|
[199] | 97 | try {
|
---|
| 98 | lock (cache) {
|
---|
| 99 | foreach (CurrentZoomFile czf in cache) {
|
---|
[351] | 100 | if (czf.filename != null && czf.filename.Equals (_filename)) {
|
---|
[329] | 101 | return czf.pngData;
|
---|
[325] | 102 | }
|
---|
[199] | 103 | }
|
---|
| 104 |
|
---|
[351] | 105 | if (!File.Exists (_filename)) {
|
---|
[269] | 106 | return transparentTile;
|
---|
[199] | 107 | }
|
---|
[325] | 108 |
|
---|
[351] | 109 | return ReadAllBytes (_filename);
|
---|
[199] | 110 | }
|
---|
| 111 | } catch (Exception e) {
|
---|
[329] | 112 | Log.Warning ("Error in MapTileCache.GetFileContent: " + e);
|
---|
[199] | 113 | }
|
---|
[325] | 114 |
|
---|
[199] | 115 | return null;
|
---|
| 116 | }
|
---|
| 117 |
|
---|
[331] | 118 | private static byte[] ReadAllBytes (string _path) {
|
---|
| 119 | using (FileStream fileStream = new FileStream(_path, FileMode.Open, FileAccess.Read, FileShare.Read, 4096)) {
|
---|
| 120 | int bytesRead = 0;
|
---|
| 121 | int bytesLeft = (int) fileStream.Length;
|
---|
| 122 | byte[] result = new byte[bytesLeft];
|
---|
| 123 | while (bytesLeft > 0) {
|
---|
| 124 | int readThisTime = fileStream.Read (result, bytesRead, bytesLeft);
|
---|
| 125 | if (readThisTime == 0) {
|
---|
| 126 | throw new IOException ("Unexpected end of stream");
|
---|
| 127 | }
|
---|
| 128 |
|
---|
| 129 | bytesRead += readThisTime;
|
---|
| 130 | bytesLeft -= readThisTime;
|
---|
| 131 | }
|
---|
| 132 |
|
---|
| 133 | return result;
|
---|
| 134 | }
|
---|
| 135 | }
|
---|
| 136 |
|
---|
| 137 |
|
---|
[329] | 138 | private class CurrentZoomFile {
|
---|
[325] | 139 | public string filename;
|
---|
[329] | 140 | public byte[] pngData;
|
---|
[325] | 141 | }
|
---|
[199] | 142 | }
|
---|
[325] | 143 | }
|
---|