1 | using System;
|
---|
2 | using System.IO;
|
---|
3 | using UnityEngine;
|
---|
4 | using Object = UnityEngine.Object;
|
---|
5 |
|
---|
6 | namespace AllocsFixes.FileCache {
|
---|
7 | // Special "cache" for map tile folder as both map rendering and webserver access files in there.
|
---|
8 | // Only map rendering tiles are cached. Writing is done by WriteThrough.
|
---|
9 | public class MapTileCache : AbstractCache {
|
---|
10 | private readonly byte[] transparentTile;
|
---|
11 | private CurrentZoomFile[] cache;
|
---|
12 |
|
---|
13 | public MapTileCache (int _tileSize) {
|
---|
14 | Texture2D tex = new Texture2D (_tileSize, _tileSize);
|
---|
15 | Color nullColor = new Color (0, 0, 0, 0);
|
---|
16 | for (int x = 0; x < _tileSize; x++) {
|
---|
17 | for (int y = 0; y < _tileSize; y++) {
|
---|
18 | tex.SetPixel (x, y, nullColor);
|
---|
19 | }
|
---|
20 | }
|
---|
21 |
|
---|
22 | transparentTile = tex.EncodeToPNG ();
|
---|
23 | Object.Destroy (tex);
|
---|
24 | }
|
---|
25 |
|
---|
26 | public void SetZoomCount (int count) {
|
---|
27 | cache = new CurrentZoomFile[count];
|
---|
28 | }
|
---|
29 |
|
---|
30 | public byte[] LoadTile (int zoomlevel, string filename) {
|
---|
31 | try {
|
---|
32 | lock (cache) {
|
---|
33 | if (cache [zoomlevel].filename == null || !cache [zoomlevel].filename.Equals (filename)) {
|
---|
34 | cache [zoomlevel].filename = filename;
|
---|
35 |
|
---|
36 | if (!File.Exists (filename)) {
|
---|
37 | cache [zoomlevel].data = null;
|
---|
38 | return null;
|
---|
39 | }
|
---|
40 |
|
---|
41 | cache [zoomlevel].data = File.ReadAllBytes (filename);
|
---|
42 | }
|
---|
43 |
|
---|
44 | return cache [zoomlevel].data;
|
---|
45 | }
|
---|
46 | } catch (Exception e) {
|
---|
47 | Log.Out ("Error in MapTileCache.LoadTile: " + e);
|
---|
48 | }
|
---|
49 |
|
---|
50 | return null;
|
---|
51 | }
|
---|
52 |
|
---|
53 | public void SaveTile (int zoomlevel, byte[] content) {
|
---|
54 | try {
|
---|
55 | lock (cache) {
|
---|
56 | if (cache [zoomlevel].filename == null) {
|
---|
57 | return;
|
---|
58 | }
|
---|
59 |
|
---|
60 | cache [zoomlevel].data = content;
|
---|
61 | File.WriteAllBytes (cache [zoomlevel].filename, content);
|
---|
62 | }
|
---|
63 | } catch (Exception e) {
|
---|
64 | Log.Out ("Error in MapTileCache.SaveTile: " + e);
|
---|
65 | }
|
---|
66 | }
|
---|
67 |
|
---|
68 | public override byte[] GetFileContent (string filename) {
|
---|
69 | try {
|
---|
70 | lock (cache) {
|
---|
71 | foreach (CurrentZoomFile czf in cache) {
|
---|
72 | if (czf.filename != null && czf.filename.Equals (filename)) {
|
---|
73 | return czf.data;
|
---|
74 | }
|
---|
75 | }
|
---|
76 |
|
---|
77 | if (!File.Exists (filename)) {
|
---|
78 | return transparentTile;
|
---|
79 | }
|
---|
80 |
|
---|
81 | return File.ReadAllBytes (filename);
|
---|
82 | }
|
---|
83 | } catch (Exception e) {
|
---|
84 | Log.Out ("Error in MapTileCache.GetFileContent: " + e);
|
---|
85 | }
|
---|
86 |
|
---|
87 | return null;
|
---|
88 | }
|
---|
89 |
|
---|
90 | private struct CurrentZoomFile {
|
---|
91 | public string filename;
|
---|
92 | public byte[] data;
|
---|
93 | }
|
---|
94 | }
|
---|
95 | }
|
---|