-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModelAssemblyManager.cs
More file actions
59 lines (53 loc) · 2.09 KB
/
Copy pathModelAssemblyManager.cs
File metadata and controls
59 lines (53 loc) · 2.09 KB
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
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using Heddle.Native;
namespace Heddle.LanguageServices
{
/// <summary>
/// Owns the collectible model <see cref="ModelAssemblyContext"/> and its engine registration (phase 6 D14).
/// Load registers the assemblies with <see cref="AssemblyHelper"/> so engine type resolution can see their
/// types; unload clears that registration (dropping the engine's static references) then <c>Unload()</c>s the
/// context, retaining only a <see cref="WeakReference"/> for the collection check.
/// </summary>
internal sealed class ModelAssemblyManager
{
private ModelAssemblyContext _context;
/// <summary>A weak reference to the last-unloaded context — used by the reload/collection test.</summary>
internal WeakReference LastUnloaded { get; private set; }
internal IReadOnlyList<Assembly> Load(IReadOnlyList<string> paths)
{
if (paths == null || paths.Count == 0)
return Array.Empty<Assembly>();
var context = new ModelAssemblyContext(paths);
var assemblies = new List<Assembly>();
foreach (var path in paths)
{
if (string.IsNullOrEmpty(path) || !File.Exists(path))
continue;
try
{
assemblies.Add(context.LoadFromBytes(Path.GetFullPath(path)));
}
catch
{
// an unreadable/invalid model assembly degrades to typeless for that file (D20 log-level)
}
}
if (assemblies.Count > 0)
AssemblyHelper.RegisterModelAssemblies(assemblies);
_context = context;
return assemblies;
}
internal void Unload()
{
if (_context == null)
return;
AssemblyHelper.UnregisterModelAssemblies();
LastUnloaded = new WeakReference(_context);
_context.Unload();
_context = null;
}
}
}