-
Notifications
You must be signed in to change notification settings - Fork 6.7k
/
PluginPair.cs
213 lines (185 loc) · 6.76 KB
/
PluginPair.cs
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Runtime.Loader;
using Microsoft.PowerToys.Settings.UI.Library;
using Wox.Plugin.Logger;
using Wox.Plugin.Properties;
namespace Wox.Plugin
{
public class PluginPair
{
public IPlugin Plugin { get; internal set; }
public PluginMetadata Metadata { get; internal set; }
public PluginPair(PluginMetadata metadata)
{
this.Metadata = metadata;
LoadPlugin();
}
public bool IsPluginInitialized { get; set; }
public void InitializePlugin(IPublicAPI api)
{
if (Metadata.Disabled)
{
Log.Info($"Do not initialize {Metadata.Name} as it is disabled.", GetType());
return;
}
if (IsPluginInitialized)
{
Log.Info($"{Metadata.Name} plugin is already initialized", GetType());
return;
}
var stopWatch = new Stopwatch();
stopWatch.Start();
if (!InitPlugin(api))
{
return;
}
stopWatch.Stop();
IsPluginInitialized = true;
Metadata.InitTime += stopWatch.ElapsedMilliseconds;
Log.Info($"Total initialize cost for <{Metadata.Name}> is <{Metadata.InitTime}ms>", GetType());
return;
}
public void Update(PowerLauncherPluginSettings setting, IPublicAPI api)
{
if (setting == null || api == null)
{
return;
}
if (Metadata.Disabled && !setting.Disabled)
{
Metadata.Disabled = false;
InitializePlugin(api);
if (!IsPluginInitialized)
{
var title = string.Format(CultureInfo.CurrentCulture, Resources.FailedToLoadPluginTitle, Metadata.Name);
api.ShowMsg(title, Resources.FailedToLoadPluginDescription, string.Empty, false);
}
}
else
{
Metadata.Disabled = setting.Disabled;
}
Metadata.ActionKeyword = setting.ActionKeyword;
Metadata.IsGlobal = setting.IsGlobal;
(Plugin as ISettingProvider)?.UpdateSettings(setting);
if (IsPluginInitialized && !Metadata.Disabled)
{
(Plugin as IReloadable)?.ReloadData();
}
}
public override string ToString()
{
return Metadata.Name;
}
public override bool Equals(object obj)
{
if (obj is PluginPair r)
{
// Using Ordinal since this is used internally
return string.Equals(r.Metadata.ID, Metadata.ID, StringComparison.Ordinal);
}
else
{
return false;
}
}
public override int GetHashCode()
{
// Using Ordinal since this is used internally
var hashcode = Metadata.ID?.GetHashCode(StringComparison.Ordinal) ?? 0;
return hashcode;
}
private void LoadPlugin()
{
var stopWatch = new Stopwatch();
stopWatch.Start();
CreatePluginInstance();
stopWatch.Stop();
Metadata.InitTime += stopWatch.ElapsedMilliseconds;
Log.Info($"Load cost for <{Metadata.Name}> is <{Metadata.InitTime}ms>", GetType());
}
private bool CreatePluginInstance()
{
if (Plugin != null)
{
Log.Warn($"{Metadata.Name} plugin was already loaded", GetType());
return true;
}
try
{
_assembly = AssemblyLoadContext.Default.LoadFromAssemblyPath(Metadata.ExecuteFilePath);
}
#pragma warning disable CA1031 // Do not catch general exception types
catch (Exception e)
#pragma warning restore CA1031 // Do not catch general exception types
{
Log.Exception($"Couldn't load assembly for {Metadata.Name} in {Metadata.ExecuteFilePath}", e, MethodBase.GetCurrentMethod().DeclaringType);
return false;
}
Type[] types;
try
{
types = _assembly.GetTypes();
}
catch (ReflectionTypeLoadException e)
{
Log.Exception($"Couldn't get assembly types for {Metadata.Name} in {Metadata.ExecuteFilePath}. The plugin might be corrupted. Uninstall PowerToys, manually delete the install folder and reinstall.", e, MethodBase.GetCurrentMethod().DeclaringType);
return false;
}
Type type;
try
{
type = types.First(o => o.IsClass && !o.IsAbstract && o.GetInterfaces().Contains(typeof(IPlugin)));
}
catch (InvalidOperationException e)
{
Log.Exception($"Can't find class implement IPlugin for <{Metadata.Name}> in {Metadata.ExecuteFilePath}", e, MethodBase.GetCurrentMethod().DeclaringType);
return false;
}
try
{
Plugin = (IPlugin)Activator.CreateInstance(type);
}
#pragma warning disable CA1031 // Do not catch general exception types
catch (Exception e)
#pragma warning restore CA1031 // Do not catch general exception types
{
Log.Exception($"Can't create instance for <{Metadata.Name}> in {Metadata.ExecuteFilePath}", e, MethodBase.GetCurrentMethod().DeclaringType);
return false;
}
return true;
}
private bool InitPlugin(IPublicAPI api)
{
if (Plugin == null)
{
Log.Warn($"Can not initialize {Metadata.Name} plugin as it was not loaded", GetType());
return false;
}
try
{
Plugin.Init(new PluginInitContext
{
CurrentPluginMetadata = Metadata,
API = api,
});
}
#pragma warning disable CA1031 // Do not catch general exception types
catch (Exception e)
#pragma warning restore CA1031 // Do not catch general exception types
{
Log.Exception($"Fail to Init plugin: {Metadata.Name}", e, GetType());
return false;
}
return true;
}
private Assembly _assembly;
}
}