diff options
Diffstat (limited to '')
-rw-r--r-- | OpenSim/Region/Framework/ModuleLoader.cs | 263 |
1 files changed, 0 insertions, 263 deletions
diff --git a/OpenSim/Region/Framework/ModuleLoader.cs b/OpenSim/Region/Framework/ModuleLoader.cs deleted file mode 100644 index 32ee674..0000000 --- a/OpenSim/Region/Framework/ModuleLoader.cs +++ /dev/null | |||
@@ -1,263 +0,0 @@ | |||
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 OpenSimulator 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 | //m_log.DebugFormat("[MODULES]: Looking for modules in {0}", dllName); | ||
188 | |||
189 | List<IRegionModule> modules = new List<IRegionModule>(); | ||
190 | |||
191 | Assembly pluginAssembly; | ||
192 | if (!LoadedAssemblys.TryGetValue(dllName, out pluginAssembly)) | ||
193 | { | ||
194 | try | ||
195 | { | ||
196 | pluginAssembly = Assembly.LoadFrom(dllName); | ||
197 | LoadedAssemblys.Add(dllName, pluginAssembly); | ||
198 | } | ||
199 | catch (BadImageFormatException) | ||
200 | { | ||
201 | //m_log.InfoFormat("[MODULES]: The file [{0}] is not a module assembly.", e.FileName); | ||
202 | } | ||
203 | } | ||
204 | |||
205 | if (pluginAssembly != null) | ||
206 | { | ||
207 | try | ||
208 | { | ||
209 | foreach (Type pluginType in pluginAssembly.GetTypes()) | ||
210 | { | ||
211 | if (pluginType.IsPublic) | ||
212 | { | ||
213 | if (!pluginType.IsAbstract) | ||
214 | { | ||
215 | if (pluginType.GetInterface("IRegionModule") != null) | ||
216 | { | ||
217 | modules.Add((IRegionModule)Activator.CreateInstance(pluginType)); | ||
218 | } | ||
219 | } | ||
220 | } | ||
221 | } | ||
222 | } | ||
223 | catch (Exception e) | ||
224 | { | ||
225 | m_log.ErrorFormat( | ||
226 | "[MODULES]: Could not load types for plugin DLL {0}. Exception {1} {2}", | ||
227 | pluginAssembly.FullName, e.Message, e.StackTrace); | ||
228 | |||
229 | // justincc: Right now this is fatal to really get the user's attention | ||
230 | // TomMeta: WTF? No, how about we /don't/ throw a fatal exception when there's no need to? | ||
231 | //throw e; | ||
232 | } | ||
233 | } | ||
234 | |||
235 | return modules.ToArray(); | ||
236 | } | ||
237 | |||
238 | public void PostInitialise() | ||
239 | { | ||
240 | foreach (IRegionModule module in m_loadedSharedModules.Values) | ||
241 | { | ||
242 | module.PostInitialise(); | ||
243 | } | ||
244 | |||
245 | foreach (IRegionModule module in m_loadedModules) | ||
246 | { | ||
247 | module.PostInitialise(); | ||
248 | } | ||
249 | } | ||
250 | |||
251 | public void ClearCache() | ||
252 | { | ||
253 | LoadedAssemblys.Clear(); | ||
254 | } | ||
255 | |||
256 | public void UnloadModule(IRegionModule rm) | ||
257 | { | ||
258 | rm.Close(); | ||
259 | |||
260 | m_loadedModules.Remove(rm); | ||
261 | } | ||
262 | } | ||
263 | } | ||