forked from TASEmulators/BizHawk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCoreInventory.cs
209 lines (191 loc) · 6.02 KB
/
CoreInventory.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using BizHawk.Emulation.Common;
using BizHawk.Emulation.DiscSystem;
namespace BizHawk.Emulation.Cores
{
/// <summary>
/// finds and instantiates IEmulator cores
/// </summary>
public class CoreInventory
{
public class Core
{
public readonly string Name;
public readonly Type Type;
public readonly ConstructorInfo CTor;
public Core(string Name, Type Type, ConstructorInfo CTor)
{
this.Name = Name;
this.Type = Type;
this.CTor = CTor;
var pp = CTor.GetParameters();
for (int i = 0; i < pp.Length ; i++)
{
var p = pp[i];
string pname = p.Name.ToLowerInvariant();
Type expectedtype;
if (!paramtypes.TryGetValue(pname, out expectedtype))
throw new InvalidOperationException(string.Format("Unexpected parameter name {0} in constructor for {1}", p.Name, Type));
// disabling the typecheck here doesn't really hurt anything, because the Invoke call will still catch any forbidden casts
// it does allow us to write "MySettingsType settings" instead of "object settings"
// if (expectedtype != p.ParameterType)
// throw new InvalidOperationException(string.Format("Unexpected type mismatch in parameter {0} in constructor for {1}", p.Name, Type));
parammap.Add(pname, i);
}
}
// map parameter names to locations in the constructor
private readonly Dictionary<string, int> parammap = new Dictionary<string, int>();
// expected names and types of the parameters
private static readonly Dictionary<string, Type> paramtypes = new Dictionary<string, Type>();
static Core()
{
var pp = typeof(Core).GetMethod("Create").GetParameters();
foreach (var p in pp)
paramtypes.Add(p.Name.ToLowerInvariant(), p.ParameterType);
}
void bp(object[] parameters, string name, object value)
{
int i;
if (parammap.TryGetValue(name, out i))
parameters[i] = value;
}
/// <summary>
/// instatiate an emulator core
/// </summary>
/// <param name="comm"></param>
/// <param name="game"></param>
/// <param name="rom"></param>
/// <param name="deterministic"></param>
/// <param name="settings"></param>
/// <param name="syncsettings"></param>
/// <returns></returns>
public IEmulator Create
(
CoreComm comm,
GameInfo game,
byte[] rom,
byte[] file,
bool deterministic,
object settings,
object syncsettings
)
{
object[] o = new object[parammap.Count];
bp(o, "comm", comm);
bp(o, "game", game);
bp(o, "rom", rom);
bp(o, "file", file);
bp(o, "deterministic", deterministic);
bp(o, "settings", settings);
bp(o, "syncsettings", syncsettings);
return (IEmulator)CTor.Invoke(o);
}
}
private readonly Dictionary<string, List<Core>> systems = new Dictionary<string, List<Core>>();
private void ProcessConstructor(Type type, string system, CoreAttributes coreattr, ConstructorInfo cons)
{
Core core = new Core(coreattr.CoreName, type, cons);
List<Core> ss;
if (!systems.TryGetValue(system, out ss))
{
ss = new List<Core>();
systems.Add(system, ss);
}
ss.Add(core);
}
/// <summary>
/// find a core matching a particular game.system
/// </summary>
/// <param name="system"></param>
/// <returns></returns>
public Core this[string system]
{
get
{
List<Core> ss = systems[system];
if (ss.Count != 1)
throw new InvalidOperationException("Ambiguous core selection!");
return ss[0];
}
}
/// <summary>
/// find a core matching a particular game.system with a particular coreattributes.name
/// </summary>
/// <param name="system"></param>
/// <param name="core"></param>
/// <returns></returns>
public Core this[string system, string core]
{
get
{
List<Core> ss = systems[system];
foreach (Core c in ss)
{
if (c.Name == core)
return c;
}
throw new InvalidOperationException("No such core!");
}
}
/// <summary>
/// find an exact core type. slow lookup.
/// </summary>
/// <param name="type"></param>
/// <returns></returns>
public Core FindByType(Type type)
{
foreach (List<Core> cc in systems.Values)
{
foreach (Core c in cc)
{
if (c.Type == type)
return c;
}
}
throw new InvalidOperationException("No such core!");
}
/// <summary>
/// create a core inventory, collecting all IEmulators from some assembilies
/// </summary>
/// <param name="assys"></param>
public CoreInventory(IEnumerable<Assembly> assys)
{
foreach (var assy in assys)
{
foreach (var typ in assy.GetTypes())
{
if (typ.GetInterfaces().Contains(typeof(IEmulator)))
{
var coreattr = typ.GetCustomAttributes(typeof(CoreAttributes), false);
if (coreattr.Length != 1)
throw new InvalidOperationException(string.Format("IEmulator {0} without CoreAttributes!", typ));
var cons = typ.GetConstructors(BindingFlags.Public | BindingFlags.Instance)
.Where(c => c.GetCustomAttributes(typeof(CoreConstructorAttribute), false).Length > 0).ToList();
foreach(var con in cons)
{
foreach (string system in ((CoreConstructorAttribute)con.GetCustomAttributes(typeof(CoreConstructorAttribute), false)[0]).Systems)
{
ProcessConstructor(typ, system, (CoreAttributes)coreattr[0], con);
}
}
}
}
}
}
public static readonly CoreInventory Instance = new CoreInventory(new[] { typeof(CoreInventory).Assembly });
}
[AttributeUsage(AttributeTargets.Constructor)]
public class CoreConstructorAttribute : Attribute
{
public IEnumerable<string> Systems { get { return _systems; } }
private readonly List<string> _systems = new List<string>();
public CoreConstructorAttribute(params string[] Systems)
{
_systems.AddRange(Systems);
}
}
}