clockwerk-opensim-stable – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 vero 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  
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 using OpenSim.Server.Base;
41  
42 using OpenMetaverse;
43 using log4net;
44 using Mono.Addins;
45 using Nini.Config;
46 using PermissionMask = OpenSim.Framework.PermissionMask;
47  
48 namespace OpenSim.Region.CoreModules.Framework.Library
49 {
50 [Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule", Id = "LibraryModule")]
51 public class LibraryModule : ISharedRegionModule
52 {
53 private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
54 private static bool m_HasRunOnce = false;
55  
56 private bool m_Enabled = false;
57 // private string m_LibraryName = "OpenSim Library";
58 private Scene m_Scene;
59  
60 private ILibraryService m_Library;
61  
62 #region ISharedRegionModule
63  
64 public void Initialise(IConfigSource config)
65 {
66 m_Enabled = config.Configs["Modules"].GetBoolean("LibraryModule", m_Enabled);
67 if (m_Enabled)
68 {
69 IConfig libConfig = config.Configs["LibraryService"];
70 if (libConfig != null)
71 {
72 string dllName = libConfig.GetString("LocalServiceModule", string.Empty);
73 m_log.Debug("[LIBRARY MODULE]: Library service dll is " + dllName);
74 if (dllName != string.Empty)
75 {
76 Object[] args = new Object[] { config };
77 m_Library = ServerUtils.LoadPlugin<ILibraryService>(dllName, args);
78 }
79 }
80 }
81 if (m_Library == null)
82 {
83 m_log.Warn("[LIBRARY MODULE]: No local library service. Module will be disabled.");
84 m_Enabled = false;
85 }
86 }
87  
88 public bool IsSharedModule
89 {
90 get { return true; }
91 }
92  
93 public string Name
94 {
95 get { return "Library Module"; }
96 }
97  
98 public Type ReplaceableInterface
99 {
100 get { return null; }
101 }
102  
103 public void AddRegion(Scene scene)
104 {
105 if (!m_Enabled)
106 return;
107  
108 // Store only the first scene
109 if (m_Scene == null)
110 {
111 m_Scene = scene;
112 }
113 scene.RegisterModuleInterface<ILibraryService>(m_Library);
114 }
115  
116 public void RemoveRegion(Scene scene)
117 {
118 if (!m_Enabled)
119 return;
120  
121 scene.UnregisterModuleInterface<ILibraryService>(m_Library);
122 }
123  
124 public void RegionLoaded(Scene scene)
125 {
126 if (!m_Enabled)
127 return;
128  
129 // This will never run more than once, even if the region is restarted
130 if (!m_HasRunOnce)
131 {
132 LoadLibrariesFromArchives();
133 //DumpLibrary();
134 m_HasRunOnce = true;
135 }
136 }
137  
138 public void PostInitialise()
139 {
140 }
141  
142 public void Close()
143 {
144 m_Scene = null;
145 }
146  
147 #endregion ISharedRegionModule
148  
149 #region LoadLibraries
150 private string pathToLibraries = "Library";
151  
152 protected void LoadLibrariesFromArchives()
153 {
154 InventoryFolderImpl lib = m_Library.LibraryRootFolder;
155 if (lib == null)
156 {
157 m_log.Debug("[LIBRARY MODULE]: No library. Ignoring Library Module");
158 return;
159 }
160  
161 RegionInfo regInfo = new RegionInfo();
162 Scene m_MockScene = new Scene(regInfo);
163 LocalInventoryService invService = new LocalInventoryService(lib);
164 m_MockScene.RegisterModuleInterface<IInventoryService>(invService);
165 m_MockScene.RegisterModuleInterface<IAssetService>(m_Scene.AssetService);
166  
167 UserAccount uinfo = new UserAccount(lib.Owner);
168 uinfo.FirstName = "OpenSim";
169 uinfo.LastName = "Library";
170 uinfo.ServiceURLs = new Dictionary<string, object>();
171  
172 foreach (string iarFileName in Directory.GetFiles(pathToLibraries, "*.iar"))
173 {
174 string simpleName = Path.GetFileNameWithoutExtension(iarFileName);
175  
176 m_log.InfoFormat("[LIBRARY MODULE]: Loading library archive {0} ({1})...", iarFileName, simpleName);
177 simpleName = GetInventoryPathFromName(simpleName);
178  
179 InventoryArchiveReadRequest archread = new InventoryArchiveReadRequest(m_MockScene.InventoryService, m_MockScene.AssetService, m_MockScene.UserAccountService, uinfo, simpleName, iarFileName, false);
180 try
181 {
182 HashSet<InventoryNodeBase> nodes = archread.Execute();
183 if (nodes != null && nodes.Count == 0)
184 {
185 // didn't find the subfolder with the given name; place it on the top
186 m_log.InfoFormat("[LIBRARY MODULE]: Didn't find {0} in library. Placing archive on the top level", simpleName);
187 archread.Close();
188 archread = new InventoryArchiveReadRequest(m_MockScene.InventoryService, m_MockScene.AssetService, m_MockScene.UserAccountService, uinfo, "/", iarFileName, false);
189 archread.Execute();
190 }
191  
192 foreach (InventoryNodeBase node in nodes)
193 FixPerms(node);
194 }
195 catch (Exception e)
196 {
197 m_log.DebugFormat("[LIBRARY MODULE]: Exception when processing archive {0}: {1}", iarFileName, e.StackTrace);
198 }
199 finally
200 {
201 archread.Close();
202 }
203 }
204 }
205  
206 private void FixPerms(InventoryNodeBase node)
207 {
208 m_log.DebugFormat("[LIBRARY MODULE]: Fixing perms for {0} {1}", node.Name, node.ID);
209  
210 if (node is InventoryItemBase)
211 {
212 InventoryItemBase item = (InventoryItemBase)node;
213 item.BasePermissions = (uint)PermissionMask.All;
214 item.EveryOnePermissions = (uint)PermissionMask.All - (uint)PermissionMask.Modify;
215 item.CurrentPermissions = (uint)PermissionMask.All;
216 item.NextPermissions = (uint)PermissionMask.All;
217 }
218 }
219  
220 // private void DumpLibrary()
221 // {
222 // InventoryFolderImpl lib = m_Library.LibraryRootFolder;
223 //
224 // m_log.DebugFormat(" - folder {0}", lib.Name);
225 // DumpFolder(lib);
226 // }
227 //
228 // private void DumpLibrary()
229 // {
230 // InventoryFolderImpl lib = m_Scene.CommsManager.UserProfileCacheService.LibraryRoot;
231 //
232 // m_log.DebugFormat(" - folder {0}", lib.Name);
233 // DumpFolder(lib);
234 // }
235  
236 private void DumpFolder(InventoryFolderImpl folder)
237 {
238 foreach (InventoryItemBase item in folder.Items.Values)
239 {
240 m_log.DebugFormat(" --> item {0}", item.Name);
241 }
242 foreach (InventoryFolderImpl f in folder.RequestListOfFolderImpls())
243 {
244 m_log.DebugFormat(" - folder {0}", f.Name);
245 DumpFolder(f);
246 }
247 }
248  
249 private string GetInventoryPathFromName(string name)
250 {
251 string[] parts = name.Split(new char[] { ' ' });
252 if (parts.Length == 3)
253 {
254 name = string.Empty;
255 // cut the last part
256 for (int i = 0; i < parts.Length - 1; i++)
257 name = name + ' ' + parts[i];
258 }
259  
260 return name;
261 }
262  
263 #endregion LoadLibraries
264 }
265 }