-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathResourceFactory.cs
38 lines (34 loc) · 1.16 KB
/
ResourceFactory.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
using System;
using System.Collections.Generic;
using System.Reflection;
namespace PiGameSharp
{
public static class ResourceFactory
{
internal static Dictionary<string, ConstructorInfo> registeredtypes = new Dictionary<string, ConstructorInfo>();
internal static Resource Construct(Dictionary<string, string> arguments)
{
if (!arguments.ContainsKey("type") || !registeredtypes.ContainsKey(arguments["type"]))
return null;
return (Resource)registeredtypes[arguments["type"]].Invoke(new object[] { arguments });
}
public static void Register(Assembly target)
{
foreach (Type t in target.GetTypes())
ScanType(t);
}
private static void ScanType(Type t)
{
if (registeredtypes.ContainsKey(t.Name))
return;
if (t.IsSubclassOf(typeof(Resource)))
{
ConstructorInfo ci = t.GetConstructor(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance, null, new Type[] { typeof(Dictionary<string, string>) }, null);
if (ci != null)
registeredtypes[t.Name] = ci;
}
foreach (Type tn in t.GetNestedTypes(BindingFlags.NonPublic | BindingFlags.Public))
ScanType(tn);
}
}
}