Support for Reflection Lookup of Mod Types (#117)
* Modified reflection utils to support lookup across multiple assemblies. * Added registration of generated mod assembly to LuaCs Compiler.
This commit is contained in:
@@ -87,7 +87,6 @@ namespace Barotrauma
|
||||
foreach (var cp in ContentPackageManager.AllPackages.Concat(ContentPackageManager.EnabledPackages.All))
|
||||
{
|
||||
if (packagesAdded.Contains(cp)) { continue; }
|
||||
|
||||
var path = $"{Path.GetFullPath(Path.GetDirectoryName(cp.Path)).Replace('\\', '/')}/";
|
||||
if (ShouldRun(cp, path))
|
||||
{
|
||||
@@ -102,7 +101,6 @@ namespace Barotrauma
|
||||
{
|
||||
paths.Add(cp.Name, path);
|
||||
}
|
||||
|
||||
packagesAdded.Add(cp);
|
||||
}
|
||||
}
|
||||
@@ -169,14 +167,14 @@ namespace Barotrauma
|
||||
return syntaxTrees;
|
||||
}
|
||||
|
||||
public List<Type> Compile()
|
||||
public List<Type> Compile()
|
||||
{
|
||||
IEnumerable<SyntaxTree> syntaxTrees = ParseSources();
|
||||
|
||||
var options = new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary)
|
||||
.WithMetadataImportOptions(MetadataImportOptions.All)
|
||||
.WithOptimizationLevel(OptimizationLevel.Release)
|
||||
.WithAllowUnsafe(false);
|
||||
.WithAllowUnsafe(true);
|
||||
var compilation = CSharpCompilation.Create(CsScriptAssembly, syntaxTrees, defaultReferences, options);
|
||||
|
||||
using (var mem = new MemoryStream())
|
||||
@@ -188,7 +186,9 @@ namespace Barotrauma
|
||||
|
||||
string errStr = "CS MODS NOT LOADED | Compilation errors:";
|
||||
foreach (Diagnostic diagnostic in failures)
|
||||
{
|
||||
errStr += $"\n{diagnostic}";
|
||||
}
|
||||
LuaCsSetup.PrintCsError(errStr);
|
||||
}
|
||||
else
|
||||
@@ -200,7 +200,16 @@ namespace Barotrauma
|
||||
|
||||
if (Assembly != null)
|
||||
{
|
||||
return Assembly.GetTypes().Where(t => t.IsSubclassOf(typeof(ACsMod))).ToList();
|
||||
RegisterAssemblyWithNativeGame(Assembly);
|
||||
try
|
||||
{
|
||||
return Assembly.GetTypes().Where(t => t.IsSubclassOf(typeof(ACsMod))).ToList();
|
||||
}
|
||||
catch (ReflectionTypeLoadException re)
|
||||
{
|
||||
LuaCsSetup.PrintCsError($"Unable to load CsMod Types. {re.Message}");
|
||||
throw re;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -208,6 +217,23 @@ namespace Barotrauma
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// This function should be used whenever a new assembly is created. Wrapper to allow more complicated setup later if need be.
|
||||
/// </summary>
|
||||
private static void RegisterAssemblyWithNativeGame(Assembly assembly)
|
||||
{
|
||||
Barotrauma.ReflectionUtils.AddNonAbstractAssemblyTypes(assembly);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// This function should be used whenever a new assembly is about to be destroyed/unloaded. Wrapper to allow more complicated setup later if need be.
|
||||
/// </summary>
|
||||
/// <param name="assembly">Assembly to remove</param>
|
||||
private static void UnregisterAssemblyFromNativeGame(Assembly assembly)
|
||||
{
|
||||
Barotrauma.ReflectionUtils.RemoveAssemblyFromCache(assembly);
|
||||
}
|
||||
|
||||
private static string[] DirSearch(string sDir)
|
||||
{
|
||||
if (!Directory.Exists(sDir))
|
||||
@@ -220,7 +246,11 @@ namespace Barotrauma
|
||||
|
||||
public void Clear()
|
||||
{
|
||||
Assembly = null;
|
||||
if (Assembly != null)
|
||||
{
|
||||
UnregisterAssemblyFromNativeGame(Assembly);
|
||||
Assembly = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,17 +12,61 @@ namespace Barotrauma
|
||||
private static readonly Dictionary<Assembly, ImmutableArray<Type>> cachedNonAbstractTypes
|
||||
= new Dictionary<Assembly, ImmutableArray<Type>>();
|
||||
|
||||
|
||||
public static IEnumerable<Type> GetDerivedNonAbstract<T>()
|
||||
{
|
||||
Assembly assembly = typeof(T).Assembly;
|
||||
if (!cachedNonAbstractTypes.ContainsKey(assembly))
|
||||
{
|
||||
cachedNonAbstractTypes[assembly] = assembly.GetTypes()
|
||||
.Where(t => !t.IsAbstract).ToImmutableArray();
|
||||
AddNonAbstractAssemblyTypes(assembly);
|
||||
}
|
||||
return cachedNonAbstractTypes[assembly].Where(t => t.IsSubclassOf(typeof(T)));
|
||||
|
||||
#warning TODO: Add safety checks in case an assembly is unloaded without being removed from the cache.
|
||||
|
||||
List<Type> types = new List<Type>();
|
||||
foreach (var typearr in cachedNonAbstractTypes)
|
||||
{
|
||||
types = types.Concat(typearr.Value.Where(t => t.IsSubclassOf(typeof(T)))).ToList();
|
||||
}
|
||||
|
||||
return types;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds an assembly's Non-Abstract Types to the cache for Barotrauma's Type lookup.
|
||||
/// </summary>
|
||||
/// <param name="assembly">Assembly to be added</param>
|
||||
/// <param name="overwrite">Whether or not to overwrite an entry if the assembly already exists within it.</param>
|
||||
public static void AddNonAbstractAssemblyTypes(Assembly assembly, bool overwrite = false)
|
||||
{
|
||||
if (cachedNonAbstractTypes.ContainsKey(assembly))
|
||||
{
|
||||
if (!overwrite)
|
||||
{
|
||||
DebugConsole.LogError(
|
||||
$"ReflectionUtils::AddNonAbstractAssemblyTypes() | The assembly [{assembly.GetName()}] already exists in the cache.");
|
||||
return;
|
||||
}
|
||||
cachedNonAbstractTypes.Remove(assembly);
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
if (!cachedNonAbstractTypes.TryAdd(assembly, assembly.GetTypes().Where(t => !t.IsAbstract).ToImmutableArray()))
|
||||
DebugConsole.LogError($"ReflectionUtils::AddNonAbstractAssemblyTypes() | Unable to add types from Assembly to cache.");
|
||||
}
|
||||
catch (ReflectionTypeLoadException e)
|
||||
{
|
||||
DebugConsole.LogError($"ReflectionUtils::AddNonAbstractAssemblyTypes() | RTFException: Unable to load Assembly Types from {assembly.GetName()}.");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Removes an assembly from the cache for Barotrauma's Type lookup.
|
||||
/// </summary>
|
||||
/// <param name="assembly">Assembly to remove.</param>
|
||||
public static void RemoveAssemblyFromCache(Assembly assembly) => cachedNonAbstractTypes.Remove(assembly);
|
||||
|
||||
public static Option<TBase> ParseDerived<TBase, TInput>(TInput input) where TInput : notnull
|
||||
{
|
||||
static Option<TBase> none() => Option<TBase>.None();
|
||||
@@ -60,4 +104,4 @@ namespace Barotrauma
|
||||
return derivedTypes.Select(parseOfType).FirstOrDefault(t => t.IsSome()) ?? none();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user