source: binary-improvements/7dtd-server-fixes/src/FileCache/MapTileCache.cs@ 330

Last change on this file since 330 was 329, checked in by alloc, 6 years ago

Some small optimizations to the map rendering stuff and added profiling instrumentation

File size: 2.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 cacheEntry.filename = filename;
41
42 if (!File.Exists (filename)) {
43 cacheEntry.pngData = null;
44 return null;
45 }
46
47 Profiler.BeginSample ("ReadPng");
48 cacheEntry.pngData = File.ReadAllBytes (filename);
49 Profiler.EndSample ();
50 }
51
52 return cacheEntry.pngData;
53 }
54 } catch (Exception e) {
55 Log.Warning ("Error in MapTileCache.LoadTile: " + e);
56 }
57
58 return null;
59 }
60
61 public void SaveTile (int zoomlevel, byte[] contentPng) {
62 try {
63 lock (cache) {
64 CurrentZoomFile cacheEntry = cache [zoomlevel];
65
66 if (string.IsNullOrEmpty (cacheEntry.filename)) {
67 return;
68 }
69
70 cacheEntry.pngData = contentPng;
71
72 Profiler.BeginSample ("WritePng");
73 File.WriteAllBytes (cacheEntry.filename, contentPng);
74 Profiler.EndSample ();
75 }
76 } catch (Exception e) {
77 Log.Warning ("Error in MapTileCache.SaveTile: " + e);
78 }
79 }
80
81 public override byte[] GetFileContent (string filename) {
82 try {
83 lock (cache) {
84 foreach (CurrentZoomFile czf in cache) {
85 if (czf.filename != null && czf.filename.Equals (filename)) {
86 return czf.pngData;
87 }
88 }
89
90 if (!File.Exists (filename)) {
91 return transparentTile;
92 }
93
94 return File.ReadAllBytes (filename);
95 }
96 } catch (Exception e) {
97 Log.Warning ("Error in MapTileCache.GetFileContent: " + e);
98 }
99
100 return null;
101 }
102
103 private class CurrentZoomFile {
104 public string filename;
105 public byte[] pngData;
106 }
107 }
108}
Note: See TracBrowser for help on using the repository browser.