public static GameModeRegistry DiscoverSupportedGameModes(EnvironmentInfo p_eifEnvironmentInfo)
{
Trace.TraceInformation("Discovering Game Mode Factories...");
Trace.Indent();
string strGameModesPath = Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), "GameModes");
Trace.TraceInformation("Looking in: {0}", strGameModesPath);
GameModeRegistry gmrRegistry = new GameModeRegistry();
string[] strAssemblies = Directory.GetFiles(strGameModesPath, "*.dll");
foreach (string strAssembly in strAssemblies)
{
Trace.TraceInformation("Checking: {0}", Path.GetFileName(strAssembly));
Trace.Indent();
Assembly asmGameMode = Assembly.LoadFrom(strAssembly);
try
{
Type[] tpeTypes = asmGameMode.GetExportedTypes();
foreach (Type tpeType in tpeTypes)
{
if (typeof(IGameModeFactory).IsAssignableFrom(tpeType) && !tpeType.IsAbstract)
{
Trace.TraceInformation("Initializing: {0}", tpeType.FullName);
Trace.Indent();
ConstructorInfo cifConstructor = tpeType.GetConstructor(new Type[] { typeof(IEnvironmentInfo) });
if (cifConstructor == null)
{
Trace.TraceInformation("No constructor accepting one argument of type IEnvironmentInfo found.");
Trace.Unindent();
continue;
}
IGameModeFactory gmfGameModeFactory = (IGameModeFactory)cifConstructor.Invoke(new object[] { p_eifEnvironmentInfo });
gmrRegistry.RegisterGameMode(gmfGameModeFactory);
Trace.Unindent();
}
}
}
catch (FileNotFoundException e)
{
Trace.TraceError(String.Format("Cannot load {0}: cannot find dependency {1}", strAssembly, e.FileName));
//some dependencies were missing, so we couldn't load the assembly
// given that these are plugins we don't have control over the dependecies:
// we may not even know what they (we can get their name, but if it's a custom
// dll not part of the client code base, we can't provide it even if we wanted to)
// there's nothing we can do, so simply skip the assembly
}
Trace.Unindent();
}
Trace.Unindent();
return gmrRegistry;
}