diff options
Diffstat (limited to 'OpenSim/Region/Framework/ModuleLoader.cs')
-rw-r--r-- | OpenSim/Region/Framework/ModuleLoader.cs | 255 |
1 files changed, 255 insertions, 0 deletions
diff --git a/OpenSim/Region/Framework/ModuleLoader.cs b/OpenSim/Region/Framework/ModuleLoader.cs new file mode 100644 index 0000000..d393919 --- /dev/null +++ b/OpenSim/Region/Framework/ModuleLoader.cs | |||
@@ -0,0 +1,255 @@ | |||
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.Collections.Generic; | ||
30 | using System.IO; | ||
31 | using System.Reflection; | ||
32 | using log4net; | ||
33 | using Nini.Config; | ||
34 | using OpenSim.Region.Framework.Interfaces; | ||
35 | using OpenSim.Region.Framework.Scenes; | ||
36 | |||
37 | namespace OpenSim.Region.Framework | ||
38 | { | ||
39 | public class ModuleLoader | ||
40 | { | ||
41 | private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); | ||
42 | |||
43 | public Dictionary<string, Assembly> LoadedAssemblys = new Dictionary<string, Assembly>(); | ||
44 | |||
45 | private readonly List<IRegionModule> m_loadedModules = new List<IRegionModule>(); | ||
46 | private readonly Dictionary<string, IRegionModule> m_loadedSharedModules = new Dictionary<string, IRegionModule>(); | ||
47 | private readonly IConfigSource m_config; | ||
48 | |||
49 | public ModuleLoader(IConfigSource config) | ||
50 | { | ||
51 | m_config = config; | ||
52 | } | ||
53 | |||
54 | public IRegionModule[] GetLoadedSharedModules | ||
55 | { | ||
56 | get | ||
57 | { | ||
58 | IRegionModule[] regionModules = new IRegionModule[m_loadedSharedModules.Count]; | ||
59 | m_loadedSharedModules.Values.CopyTo(regionModules, 0); | ||
60 | return regionModules; | ||
61 | } | ||
62 | } | ||
63 | |||
64 | public List<IRegionModule> PickupModules(Scene scene, string moduleDir) | ||
65 | { | ||
66 | DirectoryInfo dir = new DirectoryInfo(moduleDir); | ||
67 | List<IRegionModule> modules = new List<IRegionModule>(); | ||
68 | |||
69 | foreach (FileInfo fileInfo in dir.GetFiles("*.dll")) | ||
70 | { | ||
71 | modules.AddRange(LoadRegionModules(fileInfo.FullName, scene)); | ||
72 | } | ||
73 | return modules; | ||
74 | } | ||
75 | |||
76 | public void LoadDefaultSharedModule(IRegionModule module) | ||
77 | { | ||
78 | if (m_loadedSharedModules.ContainsKey(module.Name)) | ||
79 | { | ||
80 | m_log.ErrorFormat("[MODULES]: Module name \"{0}\" already exists in module list. Module not added!", module.Name); | ||
81 | } | ||
82 | else | ||
83 | { | ||
84 | m_loadedSharedModules.Add(module.Name, module); | ||
85 | } | ||
86 | } | ||
87 | |||
88 | |||
89 | public void InitialiseSharedModules(Scene scene) | ||
90 | { | ||
91 | foreach (IRegionModule module in m_loadedSharedModules.Values) | ||
92 | { | ||
93 | module.Initialise(scene, m_config); | ||
94 | scene.AddModule(module.Name, module); //should be doing this? | ||
95 | } | ||
96 | } | ||
97 | |||
98 | public void InitializeModule(IRegionModule module, Scene scene) | ||
99 | { | ||
100 | module.Initialise(scene, m_config); | ||
101 | scene.AddModule(module.Name, module); | ||
102 | m_loadedModules.Add(module); | ||
103 | } | ||
104 | |||
105 | /// <summary> | ||
106 | /// Loads/initialises a Module instance that can be used by multiple Regions | ||
107 | /// </summary> | ||
108 | /// <param name="dllName"></param> | ||
109 | /// <param name="moduleName"></param> | ||
110 | public void LoadSharedModule(string dllName, string moduleName) | ||
111 | { | ||
112 | IRegionModule module = LoadModule(dllName, moduleName); | ||
113 | |||
114 | if (module != null) | ||
115 | LoadSharedModule(module); | ||
116 | } | ||
117 | |||
118 | /// <summary> | ||
119 | /// Loads/initialises a Module instance that can be used by multiple Regions | ||
120 | /// </summary> | ||
121 | /// <param name="module"></param> | ||
122 | public void LoadSharedModule(IRegionModule module) | ||
123 | { | ||
124 | if (!m_loadedSharedModules.ContainsKey(module.Name)) | ||
125 | { | ||
126 | m_loadedSharedModules.Add(module.Name, module); | ||
127 | } | ||
128 | } | ||
129 | |||
130 | public List<IRegionModule> LoadRegionModules(string dllName, Scene scene) | ||
131 | { | ||
132 | IRegionModule[] modules = LoadModules(dllName); | ||
133 | List<IRegionModule> initializedModules = new List<IRegionModule>(); | ||
134 | |||
135 | if (modules.Length > 0) | ||
136 | { | ||
137 | m_log.InfoFormat("[MODULES]: Found Module Library [{0}]", dllName); | ||
138 | foreach (IRegionModule module in modules) | ||
139 | { | ||
140 | if (!module.IsSharedModule) | ||
141 | { | ||
142 | m_log.InfoFormat("[MODULES]: [{0}]: Initializing.", module.Name); | ||
143 | InitializeModule(module, scene); | ||
144 | initializedModules.Add(module); | ||
145 | } | ||
146 | else | ||
147 | { | ||
148 | m_log.InfoFormat("[MODULES]: [{0}]: Loading Shared Module.", module.Name); | ||
149 | LoadSharedModule(module); | ||
150 | } | ||
151 | } | ||
152 | } | ||
153 | return initializedModules; | ||
154 | } | ||
155 | |||
156 | public void LoadRegionModule(string dllName, string moduleName, Scene scene) | ||
157 | { | ||
158 | IRegionModule module = LoadModule(dllName, moduleName); | ||
159 | if (module != null) | ||
160 | { | ||
161 | InitializeModule(module, scene); | ||
162 | } | ||
163 | } | ||
164 | |||
165 | /// <summary> | ||
166 | /// Loads a external Module (if not already loaded) and creates a new instance of it. | ||
167 | /// </summary> | ||
168 | /// <param name="dllName"></param> | ||
169 | /// <param name="moduleName"></param> | ||
170 | public IRegionModule LoadModule(string dllName, string moduleName) | ||
171 | { | ||
172 | IRegionModule[] modules = LoadModules(dllName); | ||
173 | |||
174 | foreach (IRegionModule module in modules) | ||
175 | { | ||
176 | if ((module != null) && (module.Name == moduleName)) | ||
177 | { | ||
178 | return module; | ||
179 | } | ||
180 | } | ||
181 | |||
182 | return null; | ||
183 | } | ||
184 | |||
185 | public IRegionModule[] LoadModules(string dllName) | ||
186 | { | ||
187 | List<IRegionModule> modules = new List<IRegionModule>(); | ||
188 | |||
189 | Assembly pluginAssembly; | ||
190 | if (!LoadedAssemblys.TryGetValue(dllName, out pluginAssembly)) | ||
191 | { | ||
192 | try | ||
193 | { | ||
194 | pluginAssembly = Assembly.LoadFrom(dllName); | ||
195 | LoadedAssemblys.Add(dllName, pluginAssembly); | ||
196 | } | ||
197 | catch (BadImageFormatException) | ||
198 | { | ||
199 | //m_log.InfoFormat("[MODULES]: The file [{0}] is not a module assembly.", e.FileName); | ||
200 | } | ||
201 | } | ||
202 | |||
203 | if (pluginAssembly != null) | ||
204 | { | ||
205 | try | ||
206 | { | ||
207 | foreach (Type pluginType in pluginAssembly.GetTypes()) | ||
208 | { | ||
209 | if (pluginType.IsPublic) | ||
210 | { | ||
211 | if (!pluginType.IsAbstract) | ||
212 | { | ||
213 | if (pluginType.GetInterface("IRegionModule") != null) | ||
214 | { | ||
215 | modules.Add((IRegionModule)Activator.CreateInstance(pluginType)); | ||
216 | } | ||
217 | } | ||
218 | } | ||
219 | } | ||
220 | } | ||
221 | catch (ReflectionTypeLoadException) | ||
222 | { | ||
223 | m_log.InfoFormat("[MODULES]: Could not load types for [{0}].", pluginAssembly.FullName); | ||
224 | } | ||
225 | } | ||
226 | |||
227 | return modules.ToArray(); | ||
228 | } | ||
229 | |||
230 | public void PostInitialise() | ||
231 | { | ||
232 | foreach (IRegionModule module in m_loadedSharedModules.Values) | ||
233 | { | ||
234 | module.PostInitialise(); | ||
235 | } | ||
236 | |||
237 | foreach (IRegionModule module in m_loadedModules) | ||
238 | { | ||
239 | module.PostInitialise(); | ||
240 | } | ||
241 | } | ||
242 | |||
243 | public void ClearCache() | ||
244 | { | ||
245 | LoadedAssemblys.Clear(); | ||
246 | } | ||
247 | |||
248 | public void UnloadModule(IRegionModule rm) | ||
249 | { | ||
250 | rm.Close(); | ||
251 | |||
252 | m_loadedModules.Remove(rm); | ||
253 | } | ||
254 | } | ||
255 | } | ||