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