diff options
Diffstat (limited to 'OpenSim/Region/CoreModules/Framework/Library/LibraryModule.cs')
-rw-r--r-- | OpenSim/Region/CoreModules/Framework/Library/LibraryModule.cs | 219 |
1 files changed, 219 insertions, 0 deletions
diff --git a/OpenSim/Region/CoreModules/Framework/Library/LibraryModule.cs b/OpenSim/Region/CoreModules/Framework/Library/LibraryModule.cs new file mode 100644 index 0000000..df7df46 --- /dev/null +++ b/OpenSim/Region/CoreModules/Framework/Library/LibraryModule.cs | |||
@@ -0,0 +1,219 @@ | |||
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 | |||
32 | using OpenSim.Framework; | ||
33 | using OpenSim.Framework.Communications; | ||
34 | using OpenSim.Framework.Communications.Cache; | ||
35 | using OpenSim.Region.CoreModules.Avatar.Inventory.Archiver; | ||
36 | using OpenSim.Region.Framework; | ||
37 | using OpenSim.Region.Framework.Interfaces; | ||
38 | using OpenSim.Region.Framework.Scenes; | ||
39 | using OpenSim.Services.Interfaces; | ||
40 | |||
41 | using OpenMetaverse; | ||
42 | using log4net; | ||
43 | using Nini.Config; | ||
44 | |||
45 | namespace OpenSim.Region.CoreModules.Framework.Library | ||
46 | { | ||
47 | public class LibraryModule : ISharedRegionModule | ||
48 | { | ||
49 | private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); | ||
50 | private static bool m_HasRunOnce = false; | ||
51 | |||
52 | private bool m_Enabled = false; | ||
53 | private string m_LibraryName = "OpenSim Library"; | ||
54 | private Scene m_Scene; | ||
55 | |||
56 | #region ISharedRegionModule | ||
57 | |||
58 | public void Initialise(IConfigSource config) | ||
59 | { | ||
60 | m_Enabled = config.Configs["Modules"].GetBoolean("LibraryModule", m_Enabled); | ||
61 | if (m_Enabled) | ||
62 | { | ||
63 | IConfig libConfig = config.Configs["LibraryModule"]; | ||
64 | if (libConfig != null) | ||
65 | m_LibraryName = libConfig.GetString("LibraryName", m_LibraryName); | ||
66 | } | ||
67 | } | ||
68 | |||
69 | public bool IsSharedModule | ||
70 | { | ||
71 | get { return true; } | ||
72 | } | ||
73 | |||
74 | public string Name | ||
75 | { | ||
76 | get { return "Library Module"; } | ||
77 | } | ||
78 | |||
79 | public Type ReplaceableInterface | ||
80 | { | ||
81 | get { return null; } | ||
82 | } | ||
83 | |||
84 | public void AddRegion(Scene scene) | ||
85 | { | ||
86 | if (!m_Enabled) | ||
87 | return; | ||
88 | |||
89 | // Store only the first scene | ||
90 | if (m_Scene == null) | ||
91 | { | ||
92 | m_Scene = scene; | ||
93 | } | ||
94 | } | ||
95 | |||
96 | public void RemoveRegion(Scene scene) | ||
97 | { | ||
98 | } | ||
99 | |||
100 | public void RegionLoaded(Scene scene) | ||
101 | { | ||
102 | if (!m_Enabled) | ||
103 | return; | ||
104 | |||
105 | // This will never run more than once, even if the region is restarted | ||
106 | if (!m_HasRunOnce) | ||
107 | { | ||
108 | LoadLibrariesFromArchives(); | ||
109 | //DumpLibrary(); | ||
110 | m_HasRunOnce = true; | ||
111 | } | ||
112 | } | ||
113 | |||
114 | public void PostInitialise() | ||
115 | { | ||
116 | } | ||
117 | |||
118 | public void Close() | ||
119 | { | ||
120 | m_Scene = null; | ||
121 | } | ||
122 | |||
123 | #endregion ISharedRegionModule | ||
124 | |||
125 | #region LoadLibraries | ||
126 | private string pathToLibraries = "Library"; | ||
127 | |||
128 | protected void LoadLibrariesFromArchives() | ||
129 | { | ||
130 | InventoryFolderImpl lib = m_Scene.CommsManager.UserProfileCacheService.LibraryRoot; | ||
131 | if (lib == null) | ||
132 | { | ||
133 | m_log.Debug("[LIBRARY MODULE]: No library."); | ||
134 | return; | ||
135 | } | ||
136 | |||
137 | lib.Name = m_LibraryName; | ||
138 | |||
139 | RegionInfo regInfo = new RegionInfo(); | ||
140 | Scene m_MockScene = new Scene(regInfo); | ||
141 | m_MockScene.CommsManager = m_Scene.CommsManager; | ||
142 | LocalInventoryService invService = new LocalInventoryService((LibraryRootFolder)lib); | ||
143 | m_MockScene.RegisterModuleInterface<IInventoryService>(invService); | ||
144 | m_MockScene.RegisterModuleInterface<IAssetService>(m_Scene.AssetService); | ||
145 | |||
146 | UserProfileData profile = new UserProfileData(); | ||
147 | profile.FirstName = "OpenSim"; | ||
148 | profile.ID = lib.Owner; | ||
149 | profile.SurName = "Library"; | ||
150 | CachedUserInfo uinfo = new CachedUserInfo(invService, profile); | ||
151 | |||
152 | foreach (string iarFileName in Directory.GetFiles(pathToLibraries, "*.iar")) | ||
153 | { | ||
154 | string simpleName = Path.GetFileNameWithoutExtension(iarFileName); | ||
155 | |||
156 | m_log.InfoFormat("[LIBRARY MODULE]: Loading library archive {0} ({1})...", iarFileName, simpleName); | ||
157 | simpleName = GetInventoryPathFromName(simpleName); | ||
158 | |||
159 | try | ||
160 | { | ||
161 | InventoryArchiveReadRequest archread = new InventoryArchiveReadRequest(m_MockScene, uinfo, simpleName, iarFileName); | ||
162 | List<InventoryNodeBase> nodes = archread.Execute(); | ||
163 | if (nodes.Count == 0) | ||
164 | { | ||
165 | // didn't find the subfolder with the given name; place it on the top | ||
166 | m_log.InfoFormat("[LIBRARY MODULE]: Didn't find {0} in library. Placing archive on the top level", simpleName); | ||
167 | archread.Close(); | ||
168 | archread = new InventoryArchiveReadRequest(m_MockScene, uinfo, "/", iarFileName); | ||
169 | archread.Execute(); | ||
170 | } | ||
171 | archread.Close(); | ||
172 | } | ||
173 | catch (Exception e) | ||
174 | { | ||
175 | m_log.DebugFormat("[LIBRARY MODULE]: Exception when processing archive {0}: {1}", iarFileName, e.Message); | ||
176 | } | ||
177 | |||
178 | } | ||
179 | |||
180 | } | ||
181 | |||
182 | private void DumpLibrary() | ||
183 | { | ||
184 | InventoryFolderImpl lib = m_Scene.CommsManager.UserProfileCacheService.LibraryRoot; | ||
185 | |||
186 | m_log.DebugFormat(" - folder {0}", lib.Name); | ||
187 | DumpFolder(lib); | ||
188 | } | ||
189 | |||
190 | private void DumpFolder(InventoryFolderImpl folder) | ||
191 | { | ||
192 | foreach (InventoryItemBase item in folder.Items.Values) | ||
193 | { | ||
194 | m_log.DebugFormat(" --> item {0}", item.Name); | ||
195 | } | ||
196 | foreach (InventoryFolderImpl f in folder.RequestListOfFolderImpls()) | ||
197 | { | ||
198 | m_log.DebugFormat(" - folder {0}", f.Name); | ||
199 | DumpFolder(f); | ||
200 | } | ||
201 | } | ||
202 | |||
203 | private string GetInventoryPathFromName(string name) | ||
204 | { | ||
205 | string[] parts = name.Split(new char[] { ' ' }); | ||
206 | if (parts.Length == 3) | ||
207 | { | ||
208 | name = string.Empty; | ||
209 | // cut the last part | ||
210 | for (int i = 0; i < parts.Length - 1; i++) | ||
211 | name = name + ' ' + parts[i]; | ||
212 | } | ||
213 | |||
214 | return name; | ||
215 | } | ||
216 | |||
217 | #endregion LoadLibraries | ||
218 | } | ||
219 | } | ||