diff options
Diffstat (limited to '')
-rw-r--r-- | OpenSim/Framework/PluginLoader.cs | 115 |
1 files changed, 115 insertions, 0 deletions
diff --git a/OpenSim/Framework/PluginLoader.cs b/OpenSim/Framework/PluginLoader.cs new file mode 100644 index 0000000..26df3d1 --- /dev/null +++ b/OpenSim/Framework/PluginLoader.cs | |||
@@ -0,0 +1,115 @@ | |||
1 | /* | ||
2 | * Copyright (c) Contributors, http://opensimulator.org/ | ||
3 | * See CONTRIBUTORS.TXT for a full list of copyright holders. | ||
4 | * | ||
5 | * Redistribution and use in source and binary forms, with or without | ||
6 | * modification, are permitted provided that the following conditions are met: | ||
7 | * * Redistributions of source code must retain the above copyright | ||
8 | * notice, this list of conditions and the following disclaimer. | ||
9 | * * Redistributions in binary form must reproduce the above copyright | ||
10 | * notice, this list of conditions and the following disclaimer in the | ||
11 | * documentation and/or other materials provided with the distribution. | ||
12 | * * Neither the name of the OpenSim Project nor the | ||
13 | * names of its contributors may be used to endorse or promote products | ||
14 | * derived from this software without specific prior written permission. | ||
15 | * | ||
16 | * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY | ||
17 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | ||
18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
19 | * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY | ||
20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | ||
21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | ||
23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | ||
25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
26 | */ | ||
27 | |||
28 | using System; | ||
29 | using System.IO; | ||
30 | using System.Collections.Generic; | ||
31 | using System.Reflection; | ||
32 | using log4net; | ||
33 | using Mono.Addins; | ||
34 | |||
35 | namespace OpenSim.Framework | ||
36 | { | ||
37 | public class PluginLoader <T> : IDisposable where T : IPlugin | ||
38 | { | ||
39 | private static readonly ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); | ||
40 | private List<T> loaded = new List<T>(); | ||
41 | |||
42 | public PluginLoader (string dir) | ||
43 | { | ||
44 | AddPluginDir (dir); | ||
45 | } | ||
46 | |||
47 | public void AddPluginDir (string dir) | ||
48 | { | ||
49 | suppress_console_output_ (true); | ||
50 | AddinManager.Initialize (dir); | ||
51 | suppress_console_output_ (false); | ||
52 | } | ||
53 | |||
54 | public delegate void Initialiser (IPlugin p); | ||
55 | private void default_initialiser_ (IPlugin p) { p.Initialise(); } | ||
56 | |||
57 | public void Load (string extpoint) | ||
58 | { | ||
59 | Load (extpoint, default_initialiser_); | ||
60 | } | ||
61 | |||
62 | public void Load (string extpoint, Initialiser initialize) | ||
63 | { | ||
64 | AddinManager.Registry.Update (null); | ||
65 | |||
66 | ExtensionNodeList ns = AddinManager.GetExtensionNodes(extpoint); | ||
67 | foreach (TypeExtensionNode n in ns) | ||
68 | { | ||
69 | T p = (T) n.CreateInstance(); | ||
70 | initialize (p); | ||
71 | Plugins.Add (p); | ||
72 | |||
73 | log.Info("[PLUGINS]: Loading plugin " + n.Path + "/" + p.Name); | ||
74 | } | ||
75 | } | ||
76 | |||
77 | public List<T> Plugins | ||
78 | { | ||
79 | get { return loaded; } | ||
80 | } | ||
81 | |||
82 | public void Dispose () | ||
83 | { | ||
84 | foreach (T p in Plugins) | ||
85 | p.Dispose (); | ||
86 | } | ||
87 | |||
88 | public void ClearCache() | ||
89 | { | ||
90 | // The Mono addin manager (in Mono.Addins.dll version 0.2.0.0) occasionally seems to corrupt its addin cache | ||
91 | // Hence, as a temporary solution we'll remove it before each startup | ||
92 | if (Directory.Exists("addin-db-000")) | ||
93 | Directory.Delete("addin-db-000", true); | ||
94 | |||
95 | if (Directory.Exists("addin-db-001")) | ||
96 | Directory.Delete("addin-db-001", true); | ||
97 | } | ||
98 | |||
99 | private static TextWriter prev_console_; | ||
100 | public void suppress_console_output_ (bool save) | ||
101 | { | ||
102 | if (save) | ||
103 | { | ||
104 | prev_console_ = System.Console.Out; | ||
105 | System.Console.SetOut(new StreamWriter(Stream.Null)); | ||
106 | } | ||
107 | else | ||
108 | { | ||
109 | if (prev_console_ != null) | ||
110 | System.Console.SetOut(prev_console_); | ||
111 | } | ||
112 | } | ||
113 | |||
114 | } | ||
115 | } | ||