diff options
author | lbsa71 | 2007-10-10 18:24:13 +0000 |
---|---|---|
committer | lbsa71 | 2007-10-10 18:24:13 +0000 |
commit | a40e7100a232827cffb813bbbbe9374324ad3448 (patch) | |
tree | 7f16f392d653f155ca90d5477c842db9c881f3ed /OpenSim/Region/Environment/ModuleLoader.cs | |
parent | Added unit tests, based on nunit for the IInventoryData (diff) | |
download | opensim-SC_OLD-a40e7100a232827cffb813bbbbe9374324ad3448.zip opensim-SC_OLD-a40e7100a232827cffb813bbbbe9374324ad3448.tar.gz opensim-SC_OLD-a40e7100a232827cffb813bbbbe9374324ad3448.tar.bz2 opensim-SC_OLD-a40e7100a232827cffb813bbbbe9374324ad3448.tar.xz |
* Gave ModuleLoader some good lovin'
* Introduced ModuleLoader.PickupModules that currently picks up IRegionModule:s from /bin
* Made LogBase thread-safe (or at least not thread-ignorant)
* Ignored some genned files
Diffstat (limited to 'OpenSim/Region/Environment/ModuleLoader.cs')
-rw-r--r-- | OpenSim/Region/Environment/ModuleLoader.cs | 371 |
1 files changed, 209 insertions, 162 deletions
diff --git a/OpenSim/Region/Environment/ModuleLoader.cs b/OpenSim/Region/Environment/ModuleLoader.cs index 43c1aae..b24b651 100644 --- a/OpenSim/Region/Environment/ModuleLoader.cs +++ b/OpenSim/Region/Environment/ModuleLoader.cs | |||
@@ -1,162 +1,209 @@ | |||
1 | using System; | 1 | using System; |
2 | using System.Collections.Generic; | 2 | using System.Collections.Generic; |
3 | using System.IO; | 3 | using System.IO; |
4 | using System.Reflection; | 4 | using System.Reflection; |
5 | using OpenSim.Region.Environment.Interfaces; | 5 | using OpenSim.Framework.Console; |
6 | using OpenSim.Region.Environment.Modules; | 6 | using OpenSim.Region.Environment.Interfaces; |
7 | using OpenSim.Region.Environment.Scenes; | 7 | using OpenSim.Region.Environment.Modules; |
8 | 8 | using OpenSim.Region.Environment.Scenes; | |
9 | namespace OpenSim.Region.Environment | 9 | |
10 | { | 10 | namespace OpenSim.Region.Environment |
11 | public class ModuleLoader | 11 | { |
12 | { | 12 | public class ModuleLoader |
13 | public Dictionary<string, Assembly> LoadedAssemblys = new Dictionary<string, Assembly>(); | 13 | { |
14 | 14 | public Dictionary<string, Assembly> LoadedAssemblys = new Dictionary<string, Assembly>(); | |
15 | public List<IRegionModule> LoadedModules = new List<IRegionModule>(); | 15 | |
16 | public Dictionary<string, IRegionModule> LoadedSharedModules = new Dictionary<string, IRegionModule>(); | 16 | public List<IRegionModule> LoadedModules = new List<IRegionModule>(); |
17 | 17 | public Dictionary<string, IRegionModule> LoadedSharedModules = new Dictionary<string, IRegionModule>(); | |
18 | public ModuleLoader() | 18 | private readonly LogBase m_log; |
19 | { | 19 | |
20 | } | 20 | public ModuleLoader(LogBase log) |
21 | 21 | { | |
22 | /// <summary> | 22 | m_log = log; |
23 | /// Should have a module factory? | 23 | } |
24 | /// </summary> | 24 | |
25 | /// <param name="scene"></param> | 25 | /// <summary> |
26 | public void CreateDefaultModules(Scene scene, string exceptModules) | 26 | /// Should have a module factory? |
27 | { | 27 | /// </summary> |
28 | IRegionModule module = new XferModule(); | 28 | /// <param name="scene"></param> |
29 | InitialiseModule(module, scene); | 29 | //public void CreateDefaultModules(Scene scene, string exceptModules) |
30 | 30 | //{ | |
31 | module = new ChatModule(); | 31 | // IRegionModule module = new XferModule(); |
32 | InitialiseModule(module, scene); | 32 | // InitializeModule(module, scene); |
33 | 33 | ||
34 | module = new AvatarProfilesModule(); | 34 | // module = new ChatModule(); |
35 | InitialiseModule(module, scene); | 35 | // InitializeModule(module, scene); |
36 | 36 | ||
37 | module = new XMLRPCModule(); | 37 | // module = new AvatarProfilesModule(); |
38 | InitialiseModule(module, scene); | 38 | // InitializeModule(module, scene); |
39 | 39 | ||
40 | module = new WorldCommModule(); | 40 | // module = new XMLRPCModule(); |
41 | InitialiseModule(module, scene); | 41 | // InitializeModule(module, scene); |
42 | 42 | ||
43 | LoadRegionModule("OpenSim.Region.ExtensionsScriptModule.dll", "ExtensionsScriptingModule", scene); | 43 | // module = new WorldCommModule(); |
44 | 44 | // InitializeModule(module, scene); | |
45 | string lslPath = Path.Combine("ScriptEngines", "OpenSim.Region.ScriptEngine.DotNetEngine.dll"); | 45 | |
46 | LoadRegionModule(lslPath, "LSLScriptingModule", scene); | 46 | // LoadRegionModule("OpenSim.Region.ExtensionsScriptModule.dll", "ExtensionsScriptingModule", scene); |
47 | } | 47 | |
48 | 48 | // string lslPath = Path.Combine("ScriptEngines", "OpenSim.Region.ScriptEngine.DotNetEngine.dll"); | |
49 | 49 | // LoadRegionModule(lslPath, "LSLScriptingModule", scene); | |
50 | public void LoadDefaultSharedModules(string exceptModules) | 50 | //} |
51 | { | 51 | |
52 | DynamicTextureModule dynamicModule = new DynamicTextureModule(); | 52 | public void PickupModules(Scene scene) |
53 | LoadedSharedModules.Add(dynamicModule.GetName(), dynamicModule); | 53 | { |
54 | } | 54 | string moduleDir = "."; |
55 | 55 | ||
56 | public void InitialiseSharedModules(Scene scene) | 56 | DirectoryInfo dir = new DirectoryInfo(moduleDir); |
57 | { | 57 | |
58 | foreach (IRegionModule module in LoadedSharedModules.Values) | 58 | foreach (FileInfo fileInfo in dir.GetFiles("*.dll")) |
59 | { | 59 | { |
60 | module.Initialise(scene); | 60 | LoadRegionModules(fileInfo.FullName, scene); |
61 | scene.AddModule(module.GetName(), module); //should be doing this? | 61 | } |
62 | } | 62 | } |
63 | } | 63 | |
64 | 64 | public void LoadDefaultSharedModules(string exceptModules) | |
65 | private void InitialiseModule(IRegionModule module, Scene scene) | 65 | { |
66 | { | 66 | DynamicTextureModule dynamicModule = new DynamicTextureModule(); |
67 | module.Initialise(scene); | 67 | LoadedSharedModules.Add(dynamicModule.Name, dynamicModule); |
68 | scene.AddModule(module.GetName(), module); | 68 | } |
69 | LoadedModules.Add(module); | 69 | |
70 | } | 70 | public void InitialiseSharedModules(Scene scene) |
71 | 71 | { | |
72 | /// <summary> | 72 | foreach (IRegionModule module in LoadedSharedModules.Values) |
73 | /// Loads/initialises a Module instance that can be used by mutliple Regions | 73 | { |
74 | /// </summary> | 74 | module.Initialise(scene); |
75 | /// <param name="dllName"></param> | 75 | scene.AddModule(module.Name, module); //should be doing this? |
76 | /// <param name="moduleName"></param> | 76 | } |
77 | /// <param name="scene"></param> | 77 | } |
78 | public void LoadSharedModule(string dllName, string moduleName) | 78 | |
79 | { | 79 | private void InitializeModule(IRegionModule module, Scene scene) |
80 | IRegionModule module = LoadModule(dllName, moduleName); | 80 | { |
81 | if (module != null) | 81 | module.Initialise(scene); |
82 | { | 82 | scene.AddModule(module.Name, module); |
83 | LoadedSharedModules.Add(module.GetName(), module); | 83 | LoadedModules.Add(module); |
84 | } | 84 | } |
85 | } | 85 | |
86 | 86 | /// <summary> | |
87 | public void LoadRegionModule(string dllName, string moduleName, Scene scene) | 87 | /// Loads/initialises a Module instance that can be used by mutliple Regions |
88 | { | 88 | /// </summary> |
89 | IRegionModule module = LoadModule(dllName, moduleName); | 89 | /// <param name="dllName"></param> |
90 | if (module != null) | 90 | /// <param name="moduleName"></param> |
91 | { | 91 | /// <param name="scene"></param> |
92 | InitialiseModule(module, scene); | 92 | public void LoadSharedModule(string dllName, string moduleName) |
93 | } | 93 | { |
94 | } | 94 | IRegionModule module = LoadModule(dllName, moduleName); |
95 | 95 | if (module != null) | |
96 | /// <summary> | 96 | { |
97 | /// Loads a external Module (if not already loaded) and creates a new instance of it. | 97 | LoadedSharedModules.Add(module.Name, module); |
98 | /// </summary> | 98 | } |
99 | /// <param name="dllName"></param> | 99 | } |
100 | /// <param name="moduleName"></param> | 100 | |
101 | /// <param name="scene"></param> | 101 | public void LoadRegionModules(string dllName, Scene scene) |
102 | public IRegionModule LoadModule(string dllName, string moduleName) | 102 | { |
103 | { | 103 | IRegionModule[] modules = LoadModules(dllName); |
104 | Assembly pluginAssembly = null; | 104 | |
105 | if (LoadedAssemblys.ContainsKey(dllName)) | 105 | if (modules.Length > 0) |
106 | { | 106 | { |
107 | pluginAssembly = LoadedAssemblys[dllName]; | 107 | m_log.Verbose("MODULES", "Found Module Library [{0}]", dllName ); |
108 | } | 108 | foreach (IRegionModule module in modules) |
109 | else | 109 | { |
110 | { | 110 | m_log.Verbose("MODULES", " [{0}]: Initializing.", module.Name); |
111 | pluginAssembly = Assembly.LoadFrom(dllName); | 111 | InitializeModule(module, scene); |
112 | LoadedAssemblys.Add(dllName, pluginAssembly); | 112 | } |
113 | } | 113 | } |
114 | 114 | } | |
115 | IRegionModule module = null; | 115 | |
116 | foreach (Type pluginType in pluginAssembly.GetTypes()) | 116 | public void LoadRegionModule(string dllName, string moduleName, Scene scene) |
117 | { | 117 | { |
118 | if (pluginType.IsPublic) | 118 | IRegionModule module = LoadModule(dllName, moduleName); |
119 | { | 119 | if (module != null) |
120 | if (!pluginType.IsAbstract) | 120 | { |
121 | { | 121 | InitializeModule(module, scene); |
122 | Type typeInterface = pluginType.GetInterface("IRegionModule", true); | 122 | } |
123 | 123 | } | |
124 | if (typeInterface != null) | 124 | |
125 | { | 125 | /// <summary> |
126 | module = | 126 | /// Loads a external Module (if not already loaded) and creates a new instance of it. |
127 | (IRegionModule) Activator.CreateInstance(pluginAssembly.GetType(pluginType.ToString())); | 127 | /// </summary> |
128 | break; | 128 | /// <param name="dllName"></param> |
129 | } | 129 | /// <param name="moduleName"></param> |
130 | typeInterface = null; | 130 | /// <param name="scene"></param> |
131 | } | 131 | public IRegionModule LoadModule(string dllName, string moduleName) |
132 | } | 132 | { |
133 | } | 133 | IRegionModule[] modules = LoadModules(dllName); |
134 | pluginAssembly = null; | 134 | |
135 | 135 | foreach (IRegionModule module in modules) | |
136 | if ((module != null) || (module.GetName() == moduleName)) | 136 | { |
137 | { | 137 | if ((module != null) && (module.Name == moduleName)) |
138 | return module; | 138 | { |
139 | } | 139 | return module; |
140 | 140 | } | |
141 | return null; | 141 | } |
142 | } | 142 | |
143 | 143 | return null; | |
144 | public void PostInitialise() | 144 | } |
145 | { | 145 | |
146 | foreach (IRegionModule module in LoadedSharedModules.Values) | 146 | public IRegionModule[] LoadModules(string dllName) |
147 | { | 147 | { |
148 | module.PostInitialise(); | 148 | List<IRegionModule> modules = new List<IRegionModule>(); |
149 | } | 149 | |
150 | 150 | Assembly pluginAssembly; | |
151 | foreach (IRegionModule module in LoadedModules) | 151 | if (!LoadedAssemblys.TryGetValue(dllName, out pluginAssembly )) |
152 | { | 152 | { |
153 | module.PostInitialise(); | 153 | try |
154 | } | 154 | { |
155 | } | 155 | pluginAssembly = Assembly.LoadFrom(dllName); |
156 | 156 | LoadedAssemblys.Add(dllName, pluginAssembly); | |
157 | public void ClearCache() | 157 | } |
158 | { | 158 | catch( BadImageFormatException e ) |
159 | LoadedAssemblys.Clear(); | 159 | { |
160 | } | 160 | m_log.Error( "MODULES", "The file [{0}] is not a valid assembly.", e.FileName ); |
161 | } | 161 | } |
162 | } | 162 | } |
163 | |||
164 | |||
165 | if (pluginAssembly != null) | ||
166 | { | ||
167 | foreach (Type pluginType in pluginAssembly.GetTypes()) | ||
168 | { | ||
169 | if (pluginType.IsPublic) | ||
170 | { | ||
171 | if (!pluginType.IsAbstract) | ||
172 | { | ||
173 | //if (dllName.Contains("OpenSim.Region.Environment")) | ||
174 | //{ | ||
175 | // int i = 1; | ||
176 | // i++; | ||
177 | //} | ||
178 | |||
179 | if( pluginType.GetInterface("IRegionModule") != null ) | ||
180 | { | ||
181 | modules.Add((IRegionModule) Activator.CreateInstance(pluginType)); | ||
182 | } | ||
183 | } | ||
184 | } | ||
185 | } | ||
186 | } | ||
187 | |||
188 | return modules.ToArray(); | ||
189 | } | ||
190 | |||
191 | public void PostInitialise() | ||
192 | { | ||
193 | foreach (IRegionModule module in LoadedSharedModules.Values) | ||
194 | { | ||
195 | module.PostInitialise(); | ||
196 | } | ||
197 | |||
198 | foreach (IRegionModule module in LoadedModules) | ||
199 | { | ||
200 | module.PostInitialise(); | ||
201 | } | ||
202 | } | ||
203 | |||
204 | public void ClearCache() | ||
205 | { | ||
206 | LoadedAssemblys.Clear(); | ||
207 | } | ||
208 | } | ||
209 | } | ||