aboutsummaryrefslogtreecommitdiff
path: root/lib/VNLib.Plugins.Extensions.Loading/src/AssemblyLoader.cs
blob: 2d47af5e1942b4210ce0ad6fa70b2ff4e9f40a70 (plain)
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*
* Copyright (c) 2024 Vaughn Nugent
* 
* Library: VNLib
* Package: VNLib.Plugins.Extensions.Loading
* File: AssemblyLoader.cs 
*
* AssemblyLoader.cs is part of VNLib.Plugins.Extensions.Loading which is part of the larger 
* VNLib collection of libraries and utilities.
*
* VNLib.Plugins.Extensions.Loading is free software: you can redistribute it and/or modify 
* it under the terms of the GNU Affero General Public License as 
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* VNLib.Plugins.Extensions.Loading is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program.  If not, see https://www.gnu.org/licenses/.
*/

using System;
using System.IO;
using System.Threading;
using System.Reflection;
using System.Runtime.Loader;

using VNLib.Utils.IO;
using VNLib.Utils.Resources;

namespace VNLib.Plugins.Extensions.Loading
{

    /// <summary>
    /// <para>
    /// Represents a disposable assembly loader wrapper for 
    /// exporting a single type from a loaded assembly
    /// </para>
    /// <para>
    /// If the loaded type implements <see cref="IDisposable"/> the 
    /// dispose method is called when the loader is disposed
    /// </para>
    /// </summary>
    /// <typeparam name="T">The exported type to manage</typeparam>
    public sealed class AssemblyLoader<T> : ManagedLibrary, IDisposable
    {
        private readonly CancellationTokenRegistration _reg;
        private readonly LazyInitializer<T> _instance;
        private bool disposedValue;

        /// <summary>
        /// The instance of the loaded type
        /// </summary>
        public T Resource => _instance.Instance;

        private AssemblyLoader(string assemblyPath, AssemblyLoadContext parentContext, CancellationToken unloadToken)
            :base(assemblyPath, parentContext)
        {
            //Init lazy type loader
            _instance = new(LoadTypeFromAssembly<T>);            
            //Register dispose
            _reg = unloadToken.Register(Dispose);
        }
      

        /// <summary>
        /// Creates a method delegate for the given method name from
        /// the instance wrapped by the current loader
        /// </summary>
        /// <typeparam name="TDelegate"></typeparam>
        /// <param name="methodName">The name of the method to recover</param>
        /// <returns>The delegate method wrapper if found, null otherwise</returns>
        /// <exception cref="ArgumentNullException"></exception>
        /// <exception cref="AmbiguousMatchException"></exception>
        public TDelegate? TryGetMethod<TDelegate>(string methodName) where TDelegate : Delegate
        {
            //get the type info of the actual resource
            return Resource.GetType()
                .GetMethod(methodName, BindingFlags.Public | BindingFlags.Instance)
                ?.CreateDelegate<TDelegate>(Resource);
        }

        private void Dispose(bool disposing)
        {
            if (!disposedValue)
            {
                //Call base unload during dispose (or finalize)
                OnUnload();

                //Always cleanup registration
                _reg.Dispose();

                if (disposing)
                {
                    //If the instance is disposable, call its dispose method on unload
                    if (_instance.IsLoaded && _instance.Instance is IDisposable disposable)
                    {
                        disposable.Dispose();
                    }
                }
               
                disposedValue = true;
            }
        }

        /// <summary>
        /// Cleans up any unused internals
        /// </summary>
        ~AssemblyLoader()
        {
            // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
            Dispose(disposing: false);
        }

       
        /// <summary>
        /// Disposes the assembly loader and cleans up resources. If the <typeparamref name="T"/> 
        /// inherits <see cref="IDisposable"/> the intrance is disposed.
        /// </summary>
        public void Dispose()
        {
            // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
            Dispose(disposing: true);
            GC.SuppressFinalize(this);
        }

        /// <summary>
        /// Creates a new loader for the desired assembly. The assembly and its dependencies
        /// will be loaded into the specified context. If no context is specified the current assemblie's load
        /// context is captured.
        /// </summary>
        /// <param name="assemblyName">The name of the assmbly within the current plugin directory</param>
        /// <param name="unloadToken">The plugin unload token</param>
        /// <param name="loadContext">The assembly load context to load the assmbly into</param>
        /// <exception cref="FileNotFoundException"></exception>
        internal static AssemblyLoader<T> Load(string assemblyName, AssemblyLoadContext loadContext, CancellationToken unloadToken)
        {
            ArgumentNullException.ThrowIfNull(loadContext);

            //Make sure the file exists
            if (!FileOperations.FileExists(assemblyName))
            {
                throw new FileNotFoundException($"The desired assembly {assemblyName} could not be found at the file path");
            }

            //Create the loader from its absolute file path
            FileInfo fi = new(assemblyName);
            return new(fi.FullName, loadContext, unloadToken);
        }
    }
}