-
Notifications
You must be signed in to change notification settings - Fork 0
/
ResourceLibrary.cs
90 lines (82 loc) · 2.32 KB
/
ResourceLibrary.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
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
namespace PiGameSharp
{
public static class ResourceLibrary
{
static ResourceLibrary()
{
ResourceFactory.Register(typeof(ResourceLibrary).Assembly);
AppDomain.CurrentDomain.AssemblyLoad += delegate (object sender, AssemblyLoadEventArgs e) { ResourceFactory.Register(e.LoadedAssembly); Register(e.LoadedAssembly); };
foreach (Assembly a in AppDomain.CurrentDomain.GetAssemblies())
{
ResourceFactory.Register(a);
Register(a);
}
}
private static List<Resource> resources = new List<Resource>();
private static Cache cache = new Cache();
private static Dictionary<string, int> keymap = new Dictionary<string, int>();
public static Resource Get(int key)
{
Resource ret = cache.Lookup(key);
if (ret == null)
{
ret = resources.Find(x => x.Key == key);
if (ret != null)
cache.Add(ret);
}
return ret;
}
public static Resource Get(string name) => keymap.ContainsKey(name) ? Get(keymap[name]) : null;
public static void Register(Resource item)
{
if (resources.Contains(item))
return;
resources.Add(item);
}
public static void Register(Assembly target)
{
ResourceFactory.Register(target);
foreach (string name in target.GetManifestResourceNames())
{
Resource r = null;
Dictionary<string, string> arguments = new Dictionary<string, string>();
string[] args = name.Split('_');
string key = args[0];
for (int i = 1; i < args.Length; i++)
if (args[i].Contains("="))
arguments[args[i].Substring(0, args[i].IndexOf("="))] = args[i].Substring(args[i].IndexOf("=") + 1);
else
arguments[args[i]] = "on";
r = ResourceFactory.Construct(arguments);
if (r != null)
{
key = key + "." + arguments["type"];
if (keymap.ContainsKey(key))
continue;
r.DataSource = delegate
{
byte[] data;
using (Stream s = target.GetManifestResourceStream(name))
{
data = new byte[s.Length];
s.Read(data, 0, data.Length);
}
return data;
};
keymap[key] = r.Key;
Register(r);
}
}
}
public static void Dispose()
{
cache.Flush();
resources.Clear();
keymap.Clear();
}
}
}