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

Last change on this file since 210 was 199, checked in by alloc, 10 years ago

fixes

File size: 1.9 KB
RevLine 
[199]1using System;
2using System.Collections.Generic;
3using System.IO;
4
5namespace AllocsFixes.FileCache
6{
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 {
11 private struct CurrentZoomFile
12 {
13 public string filename;
14 public byte[] data;
15 }
16
17 private CurrentZoomFile[] cache;
18
19 public MapTileCache ()
20 {
21 }
22
23 public void SetZoomCount (int count)
24 {
25 cache = new CurrentZoomFile[count];
26 }
27
28 public byte[] LoadTile (int zoomlevel, string filename)
29 {
30 try {
31 lock (cache) {
32 if (cache [zoomlevel].filename == null || !cache [zoomlevel].filename.Equals (filename)) {
33 cache [zoomlevel].filename = filename;
34
35 if (!File.Exists (filename)) {
36 cache [zoomlevel].data = null;
37 return null;
38 }
39
40 cache [zoomlevel].data = File.ReadAllBytes (filename);
41 }
42 return cache [zoomlevel].data;
43 }
44 } catch (Exception e) {
45 Log.Out ("Error in MapTileCache.LoadTile: " + e);
46 }
47 return null;
48 }
49
50 public void SaveTile (int zoomlevel, byte[] content)
51 {
52 try {
53 lock (cache) {
54 if (cache [zoomlevel].filename != null) {
55 cache [zoomlevel].data = content;
56 File.WriteAllBytes (cache [zoomlevel].filename, content);
57 }
58 }
59 } catch (Exception e) {
60 Log.Out ("Error in MapTileCache.SaveTile: " + e);
61 }
62 }
63
64 public override byte[] GetFileContent (string filename)
65 {
66 try {
67 lock (cache) {
68 foreach (CurrentZoomFile czf in cache) {
69 if (czf.filename != null && czf.filename.Equals (filename))
70 return czf.data;
71 }
72
73 if (!File.Exists (filename)) {
74 return null;
75 }
76 return File.ReadAllBytes (filename);
77 }
78 } catch (Exception e) {
79 Log.Out ("Error in MapTileCache.GetFileContent: " + e);
80 }
81 return null;
82 }
83
84 }
85}
86
Note: See TracBrowser for help on using the repository browser.