source: binary-improvements2/7dtd-server-fixes/src/FileCache/MapTileCache.cs@ 399

Last change on this file since 399 was 392, checked in by alloc, 2 years ago

Added command to invalidate file caches
Added a debug+profiling build target

File size: 3.6 KB
Line 
1using System;
2using System.IO;
3using UnityEngine;
4using UnityEngine.Profiling;
5using Object = UnityEngine.Object;
6
7namespace 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 public override (int, int) Invalidate () {
117 return (0, 0);
118 }
119
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
140 private class CurrentZoomFile {
141 public string filename;
142 public byte[] pngData;
143 }
144 }
145}
Note: See TracBrowser for help on using the repository browser.