aboutsummaryrefslogtreecommitdiff
path: root/VNLib.Plugins.Extensions.Loading/UserLoading.cs
blob: ae55094fe0c1680135055c359c00bbe48ad0c8ef (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
using System;
using System.Linq;
using System.Threading;
using System.Collections.Generic;
using System.Runtime.CompilerServices;

using VNLib.Utils.Logging;
using VNLib.Utils.Extensions;
using VNLib.Plugins.Essentials.Users;

namespace VNLib.Plugins.Extensions.Loading.Users
{
    /// <summary>
    /// Contains extension methods for plugins to load the "users" system
    /// </summary>
    public static class UserLoading
    {
        public const string USER_CUSTOM_ASSEMBLY = "user_custom_asm";
        public const string DEFAULT_USER_ASM = "VNLib.Plugins.Essentials.Users.dll";
        public const string ONLOAD_METHOD_NAME = "OnPluginLoading";

        private static readonly ConditionalWeakTable<PluginBase, Lazy<IUserManager>> UsersTable = new();

        /// <summary>
        /// Gets or loads the plugin's ambient <see cref="IUserManager"/>, with the specified user-table name,
        /// or the default table name
        /// </summary>
        /// <param name="plugin"></param>
        /// <returns>The ambient <see cref="IUserManager"/> for the current plugin</returns>
        /// <exception cref="KeyNotFoundException"></exception>
        /// <exception cref="ObjectDisposedException"></exception>
        public static IUserManager GetUserManager(this PluginBase plugin)
        {
            plugin.ThrowIfUnloaded();
            //Get stored or load
            return UsersTable.GetValue(plugin, LoadUsers).Value;
        }

        private static Lazy<IUserManager> LoadUsers(PluginBase pbase)
        {
            //lazy callack
            IUserManager LoadManager()
            {
                //Try to load a custom user assembly for exporting IUserManager
                string? customAsm = pbase.PluginConfig.GetPropString(USER_CUSTOM_ASSEMBLY);
                //See if host config defined the path
                customAsm ??= pbase.HostConfig.GetPropString(USER_CUSTOM_ASSEMBLY);
                //Finally default
                customAsm ??= DEFAULT_USER_ASM;

                //Try to load a custom assembly
                AssemblyLoader<IUserManager> loader = pbase.LoadAssembly<IUserManager>(customAsm);
                try
                {
                    //Get the runtime type
                    Type runtimeType = loader.Resource.GetType();

                    //Get the onplugin load method
                    Action<object>? onLoadMethod = runtimeType.GetMethods()
                        .Where(static p => p.IsPublic && !p.IsAbstract && ONLOAD_METHOD_NAME.Equals(p.Name))
                        .Select(p => p.CreateDelegate<Action<object>>(loader.Resource))
                        .FirstOrDefault();

                    //Call the onplugin load method
                    onLoadMethod?.Invoke(pbase);

                    if (pbase.IsDebug())
                    {
                        pbase.Log.Verbose("Loading user manager from assembly {name}", runtimeType.AssemblyQualifiedName);
                    }

                    //Return the loaded instance (may raise exception)
                    return loader.Resource;
                }
                catch
                {
                    loader.Dispose();
                    throw;
                }
            }
            return new Lazy<IUserManager>(LoadManager, LazyThreadSafetyMode.PublicationOnly);
        }
    }
}