/
CompatibilityManager.cs
389 lines (349 loc) · 14.8 KB
/
CompatibilityManager.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
#region Copyright (C) 2005-2010 Team MediaPortal
// Copyright (C) 2005-2010 Team MediaPortal
// http://www.team-mediaportal.com
//
// MediaPortal is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 2 of the License, or
// (at your option) any later version.
//
// MediaPortal 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 General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with MediaPortal. If not, see <http://www.gnu.org/licenses/>.
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Xml;
namespace MediaPortal.Common.Utils
{
public class CompatibilityManager
{
protected class UsesSubsystemAttributeComparer : IEqualityComparer<UsesSubsystemAttribute>
{
public bool Equals(UsesSubsystemAttribute x, UsesSubsystemAttribute y)
{
return x.Subsystem == y.Subsystem;
}
public int GetHashCode(UsesSubsystemAttribute obj)
{
return obj.Subsystem.GetHashCode();
}
}
private static readonly HashSet<Assembly> AppAssemblies = new HashSet<Assembly>();
private static readonly Dictionary<string, Version> SubSystemVersions = new Dictionary<string, Version>();
private static readonly Version AppVersion;
public static readonly Version SkinVersion = new Version(1, 3, 0, 0);
private static readonly string MinRequiredVersionDefault = "1.1.8.0"; // 1.2.0 RC1
static CompatibilityManager()
{
AppVersion = Assembly.GetEntryAssembly().GetName().Version;
}
/// <summary>
/// Enumerate loaded assemblies and scan each of the, for subsystem compatibility attributes
/// </summary>
/// <remarks>
/// This method can be called repeatedly. Each time it will
/// detect any newly loaded assemblies and scan those for
/// additional compatibility attributes
/// </remarks>
private static void CheckLoadedAssemblies()
{
Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
if (assemblies.Length >= AppAssemblies.Count)
{
foreach (Assembly asm in assemblies)
{
if (AppAssemblies.Add(asm))
{
ScanAssembly(asm);
}
}
}
}
/// <summary>
/// Sets a subsystem's version, but only if there is no version information
/// about the subsystem or new version is later than the existing one.
/// </summary>
/// <param name="subSystem">The subsystem whose version to set</param>
/// <param name="version">The new version</param>
/// <returns>True if the version was set/changed</returns>
private static bool SetSubSystemVersion(string subSystem, Version version)
{
Version oldVersion;
if (!SubSystemVersions.TryGetValue(subSystem, out oldVersion) || CompareVersions(oldVersion, version) < 0)
{
SubSystemVersions[subSystem] = version;
return true;
}
return false;
}
/// <summary>
/// Scan a loaded assembly for subsystem compatibility attributes.
/// </summary>
/// <param name="asm">The assembly to scan</param>
private static void ScanAssembly(Assembly asm)
{
var mpAttributes =
asm.GetCustomAttributes(typeof(SubsystemVersionAttribute), false).Cast<SubsystemVersionAttribute>();
foreach (SubsystemVersionAttribute attr in mpAttributes)
{
string subSystem = attr.Subsystem;
while(!string.IsNullOrEmpty(subSystem))
{
if (!subSystem.EndsWith(".")) // ignore subsystems ending in dot, next iteration will pick up subsystem without the dot
{
SetSubSystemVersion(subSystem, attr.Version);
}
int pos = subSystem.LastIndexOf('.');
if (pos<0)
{
pos = 0;
}
subSystem = subSystem.Substring(0, pos);
}
}
}
/// <summary>
/// Compare two versions.
/// </summary>
/// <param name="v1"></param>
/// <param name="v2"></param>
/// <returns></returns>
/// <remarks>
/// The comparison done by this function differs from that of
/// the relational operators in Version class, in that any version with a
/// zero Build number is considered a release version and compares greater than
/// any other version with the same Major,Minor and Revision numbers
/// but non-zero Build number.
/// </remarks>
public static int CompareVersions(Version v1, Version v2)
{
//if (v1.Major == v2.Major && v1.Minor ==v2.Minor && v1.Revision == v2.Revision)
//{
// if (v1.Build == v2.Build)
// {
// return 0;
// }
// if (v1.Build == 0) // release is always higher than any SVN build
// {
// return 1;
// }
// if (v2.Build == 0) // release is always higher than any SVN build
// {
// return -1;
// }
// if (v1.Build < v2.Build)
// {
// return -1;
// }
// return 1;
//}
return v1.CompareTo(v2);
}
/// <summary>
/// Verify a plugin compatibility based on the subsystems used
/// and the MP version it was built against.
/// </summary>
/// <param name="plugin">The plugin to check</param>
/// <returns>True if the plugin is compatible with this version of the application</returns>
/// <remarks>
/// A plugin is compatible with this version of the application if
/// none of the subsystems it uses had a breaking change in a version
/// after the one the plugin was build against and the minimum required version
/// is older than the current application version.
/// A special subsystem "*" is used to signify that compatibility with all plugins
/// has been broken.
///
/// The plugin is any type that is decorated using the UsesSubsystemAttribute
/// and CompatibleVersionAttribute. If an attribute is not specified on the plugin,
/// its defining assembly is searched for the same attribute.
/// </remarks>
public static bool IsPluginCompatible(Type plugin)
{
var mpVersions =
(CompatibleVersionAttribute[])plugin.GetCustomAttributes(typeof(CompatibleVersionAttribute), true);
if (mpVersions.Length == 0)
{
mpVersions = (CompatibleVersionAttribute[])plugin.Assembly.GetCustomAttributes(typeof(CompatibleVersionAttribute), true);
}
var minRequiredVersion = new Version(MinRequiredVersionDefault);
var designedForVersion = new Version(1, 0, 0, 0);
if (mpVersions.Length > 0)
{
minRequiredVersion = mpVersions[0].MinRequiredVersion;
if (minRequiredVersion == CompatibleVersionAttribute.OwnAssemblyVersion)
{
minRequiredVersion = plugin.Assembly.GetName().Version;
}
designedForVersion = mpVersions[0].DesignedForVersion;
if (designedForVersion == CompatibleVersionAttribute.OwnAssemblyVersion)
{
designedForVersion = plugin.Assembly.GetName().Version;
}
}
CheckLoadedAssemblies();
Version lastFullyBreakingVersion;
if (CompareVersions(AppVersion, minRequiredVersion) < 0 || // MP version is too old
(SubSystemVersions.TryGetValue("*", out lastFullyBreakingVersion) &&
CompareVersions(lastFullyBreakingVersion, designedForVersion) > 0)) // MP breaking version after plugin released
{
return false;
}
IEnumerable<UsesSubsystemAttribute> subsystemsUsed = (UsesSubsystemAttribute[])plugin.GetCustomAttributes(typeof(UsesSubsystemAttribute), true);
subsystemsUsed = subsystemsUsed.Union((UsesSubsystemAttribute[])plugin.Assembly.GetCustomAttributes(typeof(UsesSubsystemAttribute), true),
new UsesSubsystemAttributeComparer()).Where(attr => attr.Used);
Version ver;
// Have all used subsystem known versions and prior to the one the plugin was designed for?
return subsystemsUsed.All(attr => SubSystemVersions.TryGetValue(attr.Subsystem, out ver) && CompareVersions(ver, designedForVersion) <= 0);
}
/// <summary>
/// Verify a plugin assembly compatibility based on the subsystems used
/// and the MP version it was built against.
/// </summary>
/// <param name="plugin">The plugin assemby to check</param>
/// <returns>True if the plugin is compatible with this version of the application</returns>
/// <remarks>
/// A plugin assembly is compatible with this version of the application if
/// none of the subsystems it uses had a breaking change in a version
/// after the one the plugin was build against and the minimum required version
/// is older than the current application version.
/// A special subsystem "*" is used to signify that compatibility with all plugins
/// has been broken.
///
/// The plugin is any assembly that is decorated using the UsesSubsystemAttribute
/// and CompatibleVersionAttribute.
/// </remarks>
public static bool IsPluginCompatible(Assembly plugin)
{
var mpVersions =
(CompatibleVersionAttribute[])plugin.GetCustomAttributes(typeof(CompatibleVersionAttribute), true);
var minRequiredVersion = new Version(MinRequiredVersionDefault);
var designedForVersion = new Version(1, 0, 0, 0);
if (mpVersions.Length > 0)
{
minRequiredVersion = mpVersions[0].MinRequiredVersion;
if (minRequiredVersion == CompatibleVersionAttribute.OwnAssemblyVersion)
{
minRequiredVersion = plugin.GetName().Version;
}
designedForVersion = mpVersions[0].DesignedForVersion;
if (designedForVersion == CompatibleVersionAttribute.OwnAssemblyVersion)
{
designedForVersion = plugin.GetName().Version;
}
}
CheckLoadedAssemblies();
Version lastFullyBreakingVersion;
if (CompareVersions(AppVersion, minRequiredVersion) < 0 || // MP version is too old
(SubSystemVersions.TryGetValue("*", out lastFullyBreakingVersion) &&
CompareVersions(lastFullyBreakingVersion, designedForVersion) > 0)) // MP breaking version after plugin released
{
return false;
}
IEnumerable<UsesSubsystemAttribute> subsystemsUsed = ((UsesSubsystemAttribute[])plugin.GetCustomAttributes(typeof(UsesSubsystemAttribute), true))
.Where(attr => attr.Used);
Version ver;
// Have all used subsystem known versions and prior to the one the plugin was designed for?
return subsystemsUsed.All(attr => SubSystemVersions.TryGetValue(attr.Subsystem, out ver) && CompareVersions(ver, designedForVersion) <= 0);
}
public static Version GetCurrentVersion()
{
CheckLoadedAssemblies();
Version version;
if (!SubSystemVersions.TryGetValue("*", out version))
{
return AppVersion;
}
return version;
}
public static Version GetCurrentMaxVersion()
{
CheckLoadedAssemblies();
return SubSystemVersions.Max(v => v.Value);
}
public static Version GetCurrentSubSystemVersion(string subSystem)
{
Version version = null;
SubSystemVersions.TryGetValue(subSystem, out version);
return version;
}
public static IEnumerable<UsesSubsystemAttribute> GetSubSystemsUsed(Assembly asm)
{
return ((UsesSubsystemAttribute[])asm.GetCustomAttributes(typeof(UsesSubsystemAttribute), true)).Where(attr => attr.Used);
}
public static IEnumerable<CompatibleVersionAttribute> GetRequestedVersions(Assembly asm)
{
return (CompatibleVersionAttribute[])asm.GetCustomAttributes(typeof(CompatibleVersionAttribute), true);
}
public static bool IsPluginCompatible(System.Xml.XmlElement rootNode)
{
XmlNode versionNode = rootNode.SelectSingleNode("CompatibleVersion/Items");
if(versionNode == null)
{
return false;
}
var minRequiredVersion = new Version(MinRequiredVersionDefault);
var designedForVersion = new Version(1, 0, 0, 0);
foreach (XmlNode node in versionNode.ChildNodes)
{
XmlNode minVersionNode = node.SelectSingleNode("MinRequiredVersion");
XmlNode designedVersionNode = node.SelectSingleNode("DesignedForVersion");
if (minVersionNode != null)
{
minRequiredVersion = new Version(minVersionNode.InnerText);
}
if (designedForVersion == null)
{
return false;
}
designedForVersion = new Version(designedVersionNode.InnerText);
break; //Break cause we only check first instance??
}
CheckLoadedAssemblies();
Version lastFullyBreakingVersion;
if (CompareVersions(AppVersion, minRequiredVersion) < 0 || // MP version is too old
(SubSystemVersions.TryGetValue("*", out lastFullyBreakingVersion) &&
CompareVersions(lastFullyBreakingVersion, designedForVersion) > 0)) // MP breaking version after plugin released
{
return false;
}
List<string> subsystemsUsed = new List<string>();
XmlNode subsystemNode = rootNode.SelectSingleNode("SubSystemsUsed/Items");
if (subsystemNode == null)
{
return false;
}
foreach (XmlNode node in subsystemNode.ChildNodes)
{
XmlAttribute nameAttrib = node.Attributes["Name"];
if(nameAttrib == null || string.IsNullOrEmpty(nameAttrib.InnerText))
{
continue;
}
subsystemsUsed.Add(nameAttrib.InnerText);
}
if (subsystemsUsed.Count == 0)
{
return true;
}
Version ver;
// Have all used subsystem known versions and prior to the one the plugin was designed for?
return subsystemsUsed.All(attr => SubSystemVersions.TryGetValue(attr, out ver) && CompareVersions(ver, designedForVersion) <= 0);
}
static readonly Dictionary<Version, string> MpReleaseApi = new Dictionary<Version, string>()
{
{ new Version("1.1.6.27644"), "1.2.0 Beta" },
{ new Version("1.2.100.0"), "1.3.0 Alpha" }
};
public static string MediaPortalReleaseForApiVersion(Version apiVersion)
{
return MpReleaseApi.First(v => v.Key >= apiVersion).Value;
}
}
}