-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPluginBase.cs
More file actions
111 lines (90 loc) · 4.05 KB
/
PluginBase.cs
File metadata and controls
111 lines (90 loc) · 4.05 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
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
using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices.Marshalling;
using Hi3Helper.Plugin.Core.Management.PresetConfig;
using Hi3Helper.Plugin.Core.Update;
using Hi3Helper.Plugin.Core.Utility;
using Microsoft.Extensions.Logging;
namespace Hi3Helper.Plugin.Core;
/// <summary>
/// The base class for accessing plugin metadata and preset configuration.
/// </summary>
/// <remarks>
/// This <see cref="PluginBase"/> base class requires the abstract methods to be implemented, except for the <see cref="IPlugin.CancelAsync(in Guid)"/> method.
/// </remarks>
[GeneratedComClass]
public abstract partial class PluginBase : IPlugin
{
/// <inheritdoc/>
public abstract void GetPluginName(out string? result);
/// <inheritdoc/>
public abstract void GetPluginDescription(out string? result);
/// <inheritdoc/>
public abstract void GetPluginAuthor(out string? result);
/// <inheritdoc/>
public abstract unsafe void GetPluginCreationDate(out DateTime* result);
/// <inheritdoc/>
public abstract void GetPresetConfigCount(out int count);
/// <inheritdoc/>
public abstract void GetPresetConfig(int index, out IPluginPresetConfig result);
/// <inheritdoc/>
public virtual void GetPluginSelfUpdater(out IPluginSelfUpdate? selfUpdate) => Unsafe.SkipInit(out selfUpdate);
/// <inheritdoc/>
public virtual void GetPluginAppIconUrl(out string result) => Unsafe.SkipInit(out result);
/// <inheritdoc/>
public virtual void GetNotificationPosterUrl(out string result) => Unsafe.SkipInit(out result);
/// <inheritdoc/>
public void CancelAsync(in Guid cancelToken)
{
// Cancel the async operation using the provided cancel token
ComCancellationTokenVault.CancelToken(in cancelToken);
}
/// <inheritdoc/>
public void SetPluginProxySettings(string? hostUri, string? username, string? password, out bool isSuccess)
{
// If all nulls or empty, assume as it resets the configuration, then return true.
if (string.IsNullOrEmpty(hostUri) &&
string.IsNullOrEmpty(username) &&
string.IsNullOrEmpty(password))
{
SharedStatic.InstanceLogger.LogTrace("[IPlugin::SetPluginProxySettings] Proxy has been disabled!");
SharedStatic.ProxyHost = null;
SharedStatic.ProxyUsername = null;
SharedStatic.ProxyPassword = null;
isSuccess = true;
return;
}
// Try parse host URI and check if the username is not blank while the password isn't.
if (!Uri.TryCreate(hostUri, UriKind.Absolute, out SharedStatic.ProxyHost) ||
(string.IsNullOrEmpty(username) && !string.IsNullOrEmpty(password)))
{
SharedStatic.ProxyHost = null;
isSuccess = false;
return;
}
SharedStatic.InstanceLogger.LogTrace("[IPlugin::SetPluginProxySettings] Proxy has been enabled! Hostname: {Hostname} as: {Username}", hostUri, username);
// Set the username and password and return true.
SharedStatic.ProxyUsername = username;
SharedStatic.ProxyPassword = password;
isSuccess = true;
}
/// <inheritdoc/>
public void SetPluginLocaleId(string? localeId) => SharedStatic.SetPluginCurrentLocale(localeId);
/// <inheritdoc cref="IFree.Free"/>
public void Free() => Dispose();
public virtual void Dispose()
{
// Cancel all the cancellable async operations first before disposing all plugin instance
ComCancellationTokenVault.DangerousCancelAndUnregisterAllToken();
// Then continue disposing all the plugin.
GetPresetConfigCount(out int presetConfigCount);
for (int i = 0; i < presetConfigCount; i++)
{
GetPresetConfig(i, out IPluginPresetConfig presetConfig);
presetConfig.Free();
}
GC.SuppressFinalize(this);
GetPluginName(out string? pluginName);
SharedStatic.InstanceLogger.LogTrace("[PluginBase::Dispose] Plugin: {PluginName} has been disposed!", pluginName);
}
}