-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathResources.cs
274 lines (242 loc) · 10.2 KB
/
Resources.cs
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/*
Copyright © Bryan Apellanes 2015
*/
using System;
using System.Collections.Generic;
using System.Collections;
using System.Text;
using System.Reflection;
using System.IO;
using System.Drawing;
using Naizari.Extensions;
using Naizari.Helpers;
using Naizari.Logging;
namespace Naizari
{
//TODO: Fix this class to properly handle all files (somehow). Make sure all methods are thread safe
public static class Resources
{
static Dictionary<string, string> javaScriptFiles;
static Dictionary<string, string> javascriptFriendlyNamesToQualifiedScriptPath;
static Dictionary<string, Bitmap> images;
static Dictionary<string, string> textFiles;
static Dictionary<string, string> textFilesByName;
static Dictionary<string, string> sqlFiles;
static Dictionary<string, string> depFiles;
static Resources()
{
Resources.javaScriptFiles = new Dictionary<string, string>(50);
Resources.javascriptFriendlyNamesToQualifiedScriptPath = new Dictionary<string, string>(50);
Resources.images = new Dictionary<string, Bitmap>(50);
Resources.textFiles = new Dictionary<string, string>(50);
Resources.sqlFiles = new Dictionary<string, string>(50);
Resources.depFiles = new Dictionary<string, string>(50);
Resources.textFilesByName = new Dictionary<string, string>(50);
}
public static void LoadResources(Assembly assemblyToLoad)
{
Load(assemblyToLoad);
}
private static volatile List<Assembly> loaded = new List<Assembly>();
private static object loadLock = new object();
public static void Load(Assembly assemblyToLoad)
{
if (!loaded.Contains(assemblyToLoad))
{
lock (loadLock)
{
if (!loaded.Contains(assemblyToLoad))
{
foreach (string resource in assemblyToLoad.GetManifestResourceNames())
{
string ext = Path.GetExtension(resource);
if (ext.ToLowerInvariant().Equals(".js"))
{
LoadJavascript(assemblyToLoad, resource);
}
LoadImages(assemblyToLoad, resource, ext);
LoadTextFiles(assemblyToLoad, resource, ext);
LoadSqlScripts(assemblyToLoad, resource, ext);
LoadDepFiles(assemblyToLoad, resource, ext);
}
}
}
}
}
public static string ReadTextFile(string fileName)
{
return ReadTextFile(fileName, Assembly.GetExecutingAssembly());
}
public static string ReadTextFile(string fileName, Assembly assemblyToReadFrom)
{
Load(assemblyToReadFrom);
if (textFilesByName.ContainsKey(fileName))
return textFilesByName[fileName];
ExceptionHelper.Throw<FileNotFoundException>("The file {0} was not found in the specified assembly '{1}'", fileName, assemblyToReadFrom.FullName);
return string.Empty;
}
#region private loading methods
private static void LoadDepFiles(Assembly assemblyToLoad, string resource, string ext)
{
if (ext.ToLowerInvariant().Equals(".dep"))
{
Stream File = assemblyToLoad.GetManifestResourceStream(resource);
using (File)
{
using (StreamReader sr = new StreamReader(File))
{
if (!Resources.depFiles.ContainsKey(resource.ToLower()))
{
Resources.depFiles.Add(resource.ToLower(), sr.ReadToEnd());
}
}
}
}
}
private static void LoadSqlScripts(Assembly assemblyToLoad, string resource, string ext)
{
if (ext.ToLowerInvariant().Equals(".sql"))
{
Stream File = assemblyToLoad.GetManifestResourceStream(resource);
using (File)
{
using (StreamReader sr = new StreamReader(File))
{
if (!Resources.sqlFiles.ContainsKey(resource.ToLower()))
{
Resources.sqlFiles.Add(resource.ToLower(), sr.ReadToEnd());
}
}
}
}
}
private static void LoadTextFiles(Assembly assemblyToLoad, string resource, string ext)
{
ext = ext.ToLowerInvariant();
if (ext.Equals(".txt") || ext.Equals(".ascx") || ext.Equals(".cs"))
{
Stream File = assemblyToLoad.GetManifestResourceStream(resource);
string fileText = string.Empty;
using (File)
{
using (StreamReader sr = new StreamReader(File))
{
if (!Resources.textFiles.ContainsKey(resource.ToLower()))
{
fileText = sr.ReadToEnd();
}
}
}
if (!textFiles.ContainsKey(resource.ToLower()))
Resources.textFiles.Add(resource.ToLower(), fileText);
string fileName = GetFileNameFromResourceName(resource.ToLower());
if (!textFilesByName.ContainsKey(fileName))
Resources.textFilesByName.Add(fileName, fileText);
}
}
static object imageFilesLock = new object();
private static void LoadImages(Assembly assemblyToLoad, string resource, string ext)
{
if (ext.ToLowerInvariant().Equals(".gif") ||
ext.ToLowerInvariant().Equals(".jpg") ||
ext.ToLowerInvariant().Equals(".jpeg") ||
ext.ToLowerInvariant().Equals(".png"))
{
lock (imageFilesLock)
{
if (!Resources.images.ContainsKey(resource.ToLower()))
{
Bitmap image = (Bitmap)Bitmap.FromStream(assemblyToLoad.GetManifestResourceStream(resource));
Resources.images.Add(resource.ToLower(), image);
}
}
}
}
static object scriptFilesLock = new object();
static object scriptFilesByNameLock = new object();
public static void LoadJavascript(Assembly assemblyToLoad)
{
foreach (string resource in assemblyToLoad.GetManifestResourceNames())
{
string ext = Path.GetExtension(resource);
if (ext.ToLowerInvariant().Equals(".js"))
{
LoadJavascript(assemblyToLoad, resource);
}
}
}
private static void LoadJavascript(Assembly assemblyToLoad, string resource)
{
string fileName = GetFileNameFromResourceName(resource).ToLowerInvariant();
Stream file = assemblyToLoad.GetManifestResourceStream(resource);
using (file)
{
using (StreamReader sr = new StreamReader(file))
{
lock (scriptFilesLock)
{
if (!Resources.javaScriptFiles.ContainsKey(resource.ToLower()))
Resources.javaScriptFiles.Add(resource.ToLower(), sr.ReadToEnd());
}
}
}
lock (scriptFilesByNameLock)
{
string qualifiedScriptPath = resource.ToLowerInvariant();
if (Resources.javascriptFriendlyNamesToQualifiedScriptPath.ContainsKey(fileName))
{
string current = resource.ToLowerInvariant();
string previous = Resources.javascriptFriendlyNamesToQualifiedScriptPath[fileName];
if (!current.Equals(previous))
{
Log.Default.AddEntry("A script named {0} with a different resource path has already been added, that script will not be available by friendly name. current: {1}, previous: {2}", LogEventType.Warning, fileName, current, previous);
Resources.javascriptFriendlyNamesToQualifiedScriptPath[fileName] = current;
}
}
else
{
Resources.javascriptFriendlyNamesToQualifiedScriptPath.Add(fileName, qualifiedScriptPath);
}
}
}
private static string GetFileNameFromResourceName(string resource)
{
string[] splitName = resource.Split(new string[] { "." }, StringSplitOptions.RemoveEmptyEntries);
string fileName = string.Empty;
if (splitName.Length >= 2)
fileName = splitName[splitName.Length - 2] + "." + splitName[splitName.Length - 1];
else
fileName = splitName[splitName.Length];
return fileName;
}
#endregion
public static Dictionary<string, string> JavaScriptFriendlyNamesToQualifiedScriptPath
{
get { return Resources.javascriptFriendlyNamesToQualifiedScriptPath; }
}
public static Dictionary<string, string> JavaScript
{
get { return Resources.javaScriptFiles; }
}
public static Dictionary<string, Bitmap> Images
{
get { return Resources.images; }
}
public static Dictionary<string, string> TextFiles
{
get { return Resources.textFiles; }
}
public static Dictionary<string, string> TextFilesByName
{
get { return Resources.textFilesByName; }
}
public static Dictionary<string, string> SqlFiles
{
get { return Resources.sqlFiles; }
}
public static Dictionary<string, string> DepFiles
{
get { return Resources.depFiles; }
}
}
}