forked from Elfocrash/L2dotNET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHtmCache.cs
More file actions
123 lines (107 loc) · 3.78 KB
/
Copy pathHtmCache.cs
File metadata and controls
123 lines (107 loc) · 3.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using L2dotNET.DataContracts.GameModels;
using L2dotNET.Utility;
using NLog;
namespace L2dotNET.Tables
{
public class HtmCache : IInitialisable
{
private static readonly Logger Log = LogManager.GetCurrentClassLogger();
private List<L2Html> _htmCache;
private List<string> _htmFiles;
public bool Initialised { get; private set; }
private readonly Config.Config _config;
public HtmCache(Config.Config config)
{
_config = config;
}
public async Task Initialise()
{
if (Initialised)
{
return;
}
_htmCache = new List<L2Html>();
_htmFiles = DirSearch("./html");
if (!_config.ServerConfig.LazyHtmlCache)
{
BuildHtmCache();
Log.Info($"Cache Built. Loaded {_htmCache.Count} files.");
}
else
{
Log.Info($"Lazy Cached {_htmFiles.Count} files.");
}
Initialised = true;
}
public void BuildHtmCache()
{
foreach (string file in _htmFiles)
{
CacheFile(file);
}
}
public string GetHtmByFilename(string filename)
{
if (string.IsNullOrEmpty(filename))
return string.Empty;
L2Html html = _htmCache.FirstOrDefault(x => x.Filename.EqualsIgnoreCase(filename));
if (_config.ServerConfig.LazyHtmlCache && html == null)
{
//Fallback for non loaded files
string predictedFile = _htmFiles.FirstOrDefault(f => Path.GetFileNameWithoutExtension(f) == filename);
if (predictedFile != null)
{
CacheFile(predictedFile);
html = _htmCache.FirstOrDefault(x => x.Filename.EqualsIgnoreCase(filename));
}
}
return html != null ? html.Content : string.Empty;
}
public void CacheFile(string filepath)
{
string content = File.ReadAllText(filepath, Encoding.UTF8);
content = content.Replace("\r\n", "\n");
_htmCache.Add(new L2Html(Path.GetFileNameWithoutExtension(filepath), content, filepath));
}
public string GetHtmByFilepath(string filename)
{
if (string.IsNullOrEmpty(filename))
return string.Empty;
L2Html html = _htmCache.FirstOrDefault(x => x.Filepath.EqualsIgnoreCase(filename));
if (_config.ServerConfig.LazyHtmlCache && html == null)
{
//Fallback for non loaded files
string predictedFile = _htmFiles.FirstOrDefault(f => f == filename);
if (predictedFile != null)
{
CacheFile(predictedFile);
//Try Again
html = _htmCache.FirstOrDefault(x => x.Filename.EqualsIgnoreCase(Path.GetFileNameWithoutExtension(predictedFile)));
}
}
return html != null ? html.Content : string.Empty;
}
private List<string> DirSearch(string sDir)
{
List<string> files = new List<string>();
sDir += Path.AltDirectorySeparatorChar;
try
{
files.AddRange(Directory.GetFiles(sDir));
foreach (string d in Directory.GetDirectories(sDir))
files.AddRange(DirSearch(d));
}
catch (Exception excpt)
{
Log.Error(excpt.Message);
}
return files;
}
}
}