1 //
2 // Copyright (c) 2010-2018 Antmicro
3 // Copyright (c) 2011-2015 Realtime Embedded
4 //
5 // This file is licensed under the MIT License.
6 // Full license text is available in 'licenses/MIT.txt'.
7 //
8 using System;
9 using Mono.Cecil;
10 using System.Linq;
11 using Antmicro.Renode.Plugins;
12 using Antmicro.Renode.Core;
13 
14 namespace Antmicro.Renode.Utilities
15 {
16     public class PluginDescriptor
17     {
PluginDescriptor(TypeDefinition td, bool hidden)18         public PluginDescriptor(TypeDefinition td, bool hidden)
19         {
20             ThisType = td;
21             IsHidden = hidden;
22             var pluginAttribute = td.CustomAttributes.SingleOrDefault(x => x.AttributeType.GetFullNameOfMember() == typeof(PluginAttribute).FullName);
23             if(pluginAttribute != null)
24             {
25                 Version = Version.Parse((string)pluginAttribute.Properties.Single(x => x.Name == "Version").Argument.Value);
26                 Name = (string)pluginAttribute.Properties.Single(x => x.Name == "Name").Argument.Value;
27                 Description = (string)pluginAttribute.Properties.Single(x => x.Name == "Description").Argument.Value;
28                 Vendor = (string)pluginAttribute.Properties.Single(x => x.Name == "Vendor").Argument.Value;
29                 var dependencies = pluginAttribute.Properties.SingleOrDefault(x => x.Name == "Dependencies").Argument.Value;
30                 if(dependencies != null)
31                 {
32                     Dependencies = ((CustomAttributeArgument[])dependencies).Select(x => ((TypeReference)x.Value).Resolve()).ToArray();
33                 }
34                 var modes = pluginAttribute.Properties.SingleOrDefault(x => x.Name == "Modes").Argument.Value;
35                 Modes = modes != null ? ((CustomAttributeArgument[])modes).Select(x => x.Value).Cast<string>().ToArray() : new string[0];
36             }
37         }
38 
39         public string FullName
40         {
41             get
42             {
43                 return "{0}:{1}:{2}".FormatWith(Name, Version, Vendor);
44             }
45         }
46 
47         public string Name { get; private set; }
48         public Version Version { get; private set; }
49         public string Description { get; private set; }
50         public string Vendor { get; private set; }
51         public TypeDefinition ThisType { get; private set; }
52         public TypeDefinition[] Dependencies { get; private set; }
53         public string[] Modes { get; private set; }
54         public bool IsHidden { get; private set; }
55 
CreatePlugin()56         public object CreatePlugin()
57         {
58             var type = TypeManager.Instance.GetTypeByName(ThisType.GetFullNameOfMember());
59             return ObjectCreator.Instance.Spawn(type);
60         }
61 
Equals(object obj)62         public override bool Equals(object obj)
63         {
64             var objAsPluginDescriptor = obj as PluginDescriptor;
65             if(objAsPluginDescriptor != null)
66             {
67                 return Name == objAsPluginDescriptor.Name &&
68                     Version.Equals(objAsPluginDescriptor.Version) &&
69                     Description == objAsPluginDescriptor.Description &&
70                     Vendor == objAsPluginDescriptor.Vendor &&
71                     ((Modes == null && objAsPluginDescriptor.Modes == null) ||
72                         Enumerable.SequenceEqual(Modes, objAsPluginDescriptor.Modes)) &&
73                     ((Dependencies == null && objAsPluginDescriptor.Dependencies == null) ||
74                         Enumerable.SequenceEqual(Dependencies, objAsPluginDescriptor.Dependencies));
75             }
76 
77             return false;
78         }
79 
GetHashCode()80         public override int GetHashCode()
81         {
82             unchecked
83             {
84                 var hash = Name.GetHashCode();
85                 hash = (hash * 397) ^ Version.GetHashCode();
86                 hash = (hash * 397) ^ Description.GetHashCode();
87                 hash = (hash * 397) ^ Vendor.GetHashCode();
88                 if(Modes != null)
89                 {
90                     foreach(var mode in Modes)
91                     {
92                         hash = (hash * 397) ^ mode.GetHashCode();
93                     }
94                 }
95                 if(Dependencies != null)
96                 {
97                     foreach(var dependency in Dependencies)
98                     {
99                         hash = (hash * 397) ^ dependency.GetHashCode();
100                     }
101                 }
102                 return hash;
103             }
104         }
105     }
106 }
107 
108