diff options
Diffstat (limited to 'OpenSim/ApplicationPlugins/ScriptEngine/ComponentFactory.cs')
-rw-r--r-- | OpenSim/ApplicationPlugins/ScriptEngine/ComponentFactory.cs | 145 |
1 files changed, 0 insertions, 145 deletions
diff --git a/OpenSim/ApplicationPlugins/ScriptEngine/ComponentFactory.cs b/OpenSim/ApplicationPlugins/ScriptEngine/ComponentFactory.cs deleted file mode 100644 index c85634b..0000000 --- a/OpenSim/ApplicationPlugins/ScriptEngine/ComponentFactory.cs +++ /dev/null | |||
@@ -1,145 +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 | using System; | ||
28 | using System.Collections.Generic; | ||
29 | using System.IO; | ||
30 | using System.Reflection; | ||
31 | using log4net; | ||
32 | using OpenSim.ScriptEngine.Shared; | ||
33 | |||
34 | namespace OpenSim.ApplicationPlugins.ScriptEngine | ||
35 | { | ||
36 | public static class ComponentFactory | ||
37 | { | ||
38 | // Component providers are registered here wit a name (string) | ||
39 | // When a script engine is created the components are instanciated | ||
40 | public static Dictionary<string, Type> providers = new Dictionary<string, Type>(); | ||
41 | public static Dictionary<string, Type> scriptEngines = new Dictionary<string, Type>(); | ||
42 | |||
43 | internal static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); | ||
44 | private readonly static string nameIScriptEngineComponent = typeof(IScriptEngineComponent).Name; // keep interface name in managed code | ||
45 | private readonly static string nameIScriptEngine = typeof(IScriptEngine).Name; // keep interface name in managed code | ||
46 | |||
47 | public static string Name | ||
48 | { | ||
49 | get { return "SECS.ComponentFactory"; } | ||
50 | } | ||
51 | |||
52 | /// <summary> | ||
53 | /// Load components from directory | ||
54 | /// </summary> | ||
55 | /// <param name="directory"></param> | ||
56 | internal static void Load(string directory, string filter) | ||
57 | { | ||
58 | // We may want to change how this functions as currently it required unique class names for each component | ||
59 | |||
60 | foreach (string file in Directory.GetFiles(directory, filter)) | ||
61 | { | ||
62 | //m_log.DebugFormat("[ScriptEngine]: Loading: [{0}].", file); | ||
63 | Assembly componentAssembly = null; | ||
64 | try | ||
65 | { | ||
66 | componentAssembly = Assembly.LoadFrom(file); | ||
67 | } | ||
68 | catch | ||
69 | { | ||
70 | m_log.ErrorFormat("[{0}] Error loading: \"{1}\".", Name, file); | ||
71 | } | ||
72 | |||
73 | if (componentAssembly != null) | ||
74 | { | ||
75 | try | ||
76 | { | ||
77 | // Go through all types in the assembly | ||
78 | foreach (Type componentType in componentAssembly.GetTypes()) | ||
79 | { | ||
80 | if (componentType.IsPublic | ||
81 | && !componentType.IsAbstract) | ||
82 | { | ||
83 | //if (componentType.IsSubclassOf(typeof(ComponentBase))) | ||
84 | if (componentType.GetInterface(nameIScriptEngineComponent) != null) | ||
85 | { | ||
86 | // We have found an type which is derived from ProdiverBase, add it to provider list | ||
87 | m_log.InfoFormat("[{0}] Adding component: {1}", Name, componentType.Name); | ||
88 | lock (providers) | ||
89 | { | ||
90 | providers.Add(componentType.Name, componentType); | ||
91 | } | ||
92 | } | ||
93 | //if (componentType.IsSubclassOf(typeof(ScriptEngineBase))) | ||
94 | if (componentType.GetInterface(nameIScriptEngine) != null) | ||
95 | { | ||
96 | // We have found an type which is derived from RegionScriptEngineBase, add it to engine list | ||
97 | m_log.InfoFormat("[{0}] Adding script engine: {1}", Name, componentType.Name); | ||
98 | lock (scriptEngines) | ||
99 | { | ||
100 | scriptEngines.Add(componentType.Name, componentType); | ||
101 | } | ||
102 | } | ||
103 | } | ||
104 | } | ||
105 | } | ||
106 | catch | ||
107 | (ReflectionTypeLoadException re) | ||
108 | { | ||
109 | m_log.ErrorFormat("[{0}] Could not load component \"{1}\": {2}", Name, componentAssembly.FullName, re.ToString()); | ||
110 | int c = 0; | ||
111 | foreach (Exception e in re.LoaderExceptions) | ||
112 | { | ||
113 | c++; | ||
114 | m_log.ErrorFormat("[{0}] LoaderException {1}: {2}", Name, c, e.ToString()); | ||
115 | } | ||
116 | } | ||
117 | } //if | ||
118 | } //foreach | ||
119 | } | ||
120 | |||
121 | public static IScriptEngineComponent GetComponentInstance(string name, params Object[] args) | ||
122 | { | ||
123 | lock (providers) | ||
124 | { | ||
125 | if (!providers.ContainsKey(name)) | ||
126 | throw new Exception("ScriptEngine requested component named \"" + name + | ||
127 | "\" that does not exist."); | ||
128 | return Activator.CreateInstance(providers[name], args) as IScriptEngineComponent; | ||
129 | } | ||
130 | } | ||
131 | |||
132 | private readonly static string nameIScriptEngineRegionComponent = typeof(IScriptEngineRegionComponent).Name; // keep interface name in managed code | ||
133 | public static IScriptEngineComponent GetComponentInstance(RegionInfoStructure info, string name, params Object[] args) | ||
134 | { | ||
135 | IScriptEngineComponent c = GetComponentInstance(name, args); | ||
136 | |||
137 | // If module is IScriptEngineRegionComponent then it will have one instance per region and we will initialize it | ||
138 | if (c.GetType().GetInterface(nameIScriptEngineRegionComponent) != null) | ||
139 | ((IScriptEngineRegionComponent)c).Initialize(info); | ||
140 | |||
141 | return c; | ||
142 | } | ||
143 | |||
144 | } | ||
145 | } | ||