private void LoadScriptAssembly(Assembly asm, string filePath)
{
var types = GetJITtedTypes(asm, filePath);
foreach (var type in types.Where(a => a.GetInterfaces().Contains(typeof(IScript)) && !a.IsAbstract))
{
try
{
// Make sure there's only one copy of each script.
if (_scripts.ContainsKey(type.Name))
{
Log.Error("Script classes must have unique names, duplicate '{0}' found in '{1}'.", type.Name, Path.GetFileName(filePath));
continue;
}
// Remove overridden classes from list
var overide = type.GetCustomAttribute<OverrideAttribute>();
if (overide != null)
{
if (_scripts.ContainsKey(overide.TypeName))
{
_scripts.Remove(overide.TypeName);
}
else
Log.Warning("Override: Script class '{0}' not found ({1} @ {2}).", overide.TypeName, type.Name, Path.GetFileName(filePath));
}
// Remove classes from list that are defined in remove attributes
var removes = type.GetCustomAttribute<RemoveAttribute>();
if (removes != null)
{
foreach (var rm in removes.TypeNames)
{
if (_scripts.ContainsKey(rm))
{
_scripts.Remove(rm);
}
else
Log.Warning("Remove: Script class '{0}' not found ({1} @ {2}).", rm, type.Name, Path.GetFileName(filePath));
}
}
// Don't load script if feature is not enabled
var enabled = type.GetCustomAttribute<IfEnabledAttribute>();
if (enabled != null && !AuraData.FeaturesDb.IsEnabled(enabled.Feature))
continue;
// Don't load script if feature is enabled
var notEnabled = type.GetCustomAttribute<IfNotEnabledAttribute>();
if (notEnabled != null && AuraData.FeaturesDb.IsEnabled(notEnabled.Feature))
continue;
// Add class to load list, even if it's a dummy for remove,
// we can't be sure it's not supposed to get initialized.
_scripts[type.Name] = type;
}
catch (Exception ex)
{
Log.Exception(ex, "Error while loading script '{0}' ({1}).", type.Name, ex.Message);
}
}
}