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))
|
foreach (var cp in ContentPackageManager.AllPackages.Concat(ContentPackageManager.EnabledPackages.All))
|
||||||
{
|
{
|
||||||
if (packagesAdded.Contains(cp)) { continue; }
|
if (packagesAdded.Contains(cp)) { continue; }
|
||||||
|
|
||||||
var path = $"{Path.GetFullPath(Path.GetDirectoryName(cp.Path)).Replace('\\', '/')}/";
|
var path = $"{Path.GetFullPath(Path.GetDirectoryName(cp.Path)).Replace('\\', '/')}/";
|
||||||
if (ShouldRun(cp, path))
|
if (ShouldRun(cp, path))
|
||||||
{
|
{
|
||||||
@@ -102,7 +101,6 @@ namespace Barotrauma
|
|||||||
{
|
{
|
||||||
paths.Add(cp.Name, path);
|
paths.Add(cp.Name, path);
|
||||||
}
|
}
|
||||||
|
|
||||||
packagesAdded.Add(cp);
|
packagesAdded.Add(cp);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -169,14 +167,14 @@ namespace Barotrauma
|
|||||||
return syntaxTrees;
|
return syntaxTrees;
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<Type> Compile()
|
public List<Type> Compile()
|
||||||
{
|
{
|
||||||
IEnumerable<SyntaxTree> syntaxTrees = ParseSources();
|
IEnumerable<SyntaxTree> syntaxTrees = ParseSources();
|
||||||
|
|
||||||
var options = new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary)
|
var options = new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary)
|
||||||
.WithMetadataImportOptions(MetadataImportOptions.All)
|
.WithMetadataImportOptions(MetadataImportOptions.All)
|
||||||
.WithOptimizationLevel(OptimizationLevel.Release)
|
.WithOptimizationLevel(OptimizationLevel.Release)
|
||||||
.WithAllowUnsafe(false);
|
.WithAllowUnsafe(true);
|
||||||
var compilation = CSharpCompilation.Create(CsScriptAssembly, syntaxTrees, defaultReferences, options);
|
var compilation = CSharpCompilation.Create(CsScriptAssembly, syntaxTrees, defaultReferences, options);
|
||||||
|
|
||||||
using (var mem = new MemoryStream())
|
using (var mem = new MemoryStream())
|
||||||
@@ -188,7 +186,9 @@ namespace Barotrauma
|
|||||||
|
|
||||||
string errStr = "CS MODS NOT LOADED | Compilation errors:";
|
string errStr = "CS MODS NOT LOADED | Compilation errors:";
|
||||||
foreach (Diagnostic diagnostic in failures)
|
foreach (Diagnostic diagnostic in failures)
|
||||||
|
{
|
||||||
errStr += $"\n{diagnostic}";
|
errStr += $"\n{diagnostic}";
|
||||||
|
}
|
||||||
LuaCsSetup.PrintCsError(errStr);
|
LuaCsSetup.PrintCsError(errStr);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
@@ -200,7 +200,16 @@ namespace Barotrauma
|
|||||||
|
|
||||||
if (Assembly != null)
|
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
|
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)
|
private static string[] DirSearch(string sDir)
|
||||||
{
|
{
|
||||||
if (!Directory.Exists(sDir))
|
if (!Directory.Exists(sDir))
|
||||||
@@ -220,7 +246,11 @@ namespace Barotrauma
|
|||||||
|
|
||||||
public void Clear()
|
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
|
private static readonly Dictionary<Assembly, ImmutableArray<Type>> cachedNonAbstractTypes
|
||||||
= new Dictionary<Assembly, ImmutableArray<Type>>();
|
= new Dictionary<Assembly, ImmutableArray<Type>>();
|
||||||
|
|
||||||
|
|
||||||
public static IEnumerable<Type> GetDerivedNonAbstract<T>()
|
public static IEnumerable<Type> GetDerivedNonAbstract<T>()
|
||||||
{
|
{
|
||||||
Assembly assembly = typeof(T).Assembly;
|
Assembly assembly = typeof(T).Assembly;
|
||||||
if (!cachedNonAbstractTypes.ContainsKey(assembly))
|
if (!cachedNonAbstractTypes.ContainsKey(assembly))
|
||||||
{
|
{
|
||||||
cachedNonAbstractTypes[assembly] = assembly.GetTypes()
|
AddNonAbstractAssemblyTypes(assembly);
|
||||||
.Where(t => !t.IsAbstract).ToImmutableArray();
|
|
||||||
}
|
}
|
||||||
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
|
public static Option<TBase> ParseDerived<TBase, TInput>(TInput input) where TInput : notnull
|
||||||
{
|
{
|
||||||
static Option<TBase> none() => Option<TBase>.None();
|
static Option<TBase> none() => Option<TBase>.None();
|
||||||
@@ -60,4 +104,4 @@ namespace Barotrauma
|
|||||||
return derivedTypes.Select(parseOfType).FirstOrDefault(t => t.IsSome()) ?? none();
|
return derivedTypes.Select(parseOfType).FirstOrDefault(t => t.IsSome()) ?? none();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user