diff options
Diffstat (limited to 'OpenSim/Region/Environment/Modules/Avatar/Inventory/InventoryArchiveReadRequest.cs')
-rw-r--r-- | OpenSim/Region/Environment/Modules/Avatar/Inventory/InventoryArchiveReadRequest.cs | 238 |
1 files changed, 238 insertions, 0 deletions
diff --git a/OpenSim/Region/Environment/Modules/Avatar/Inventory/InventoryArchiveReadRequest.cs b/OpenSim/Region/Environment/Modules/Avatar/Inventory/InventoryArchiveReadRequest.cs new file mode 100644 index 0000000..5dbaa19 --- /dev/null +++ b/OpenSim/Region/Environment/Modules/Avatar/Inventory/InventoryArchiveReadRequest.cs | |||
@@ -0,0 +1,238 @@ | |||
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 OpenSim 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.IO.Compression; | ||
32 | using System.Reflection; | ||
33 | using System.Xml; | ||
34 | using OpenMetaverse; | ||
35 | using OpenSim.Region.Environment.Scenes; | ||
36 | using OpenSim.Region.Environment.Modules.World.Archiver; | ||
37 | using OpenSim.Framework; | ||
38 | using OpenSim.Framework.Communications; | ||
39 | using OpenSim.Framework.Communications.Cache; | ||
40 | using log4net; | ||
41 | using OpenSim.Region.Environment.Modules.World.Serialiser; | ||
42 | |||
43 | |||
44 | namespace OpenSim.Region.Environment.Modules.Avatar.Inventory | ||
45 | { | ||
46 | public class InventoryArchiveReadRequest | ||
47 | { | ||
48 | protected Scene scene; | ||
49 | protected TarArchiveReader archive; | ||
50 | private static System.Text.ASCIIEncoding m_asciiEncoding = new System.Text.ASCIIEncoding(); | ||
51 | ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); | ||
52 | CachedUserInfo userInfo; | ||
53 | UserProfileData userProfile; | ||
54 | CommunicationsManager commsManager; | ||
55 | string loadPath; | ||
56 | |||
57 | public InventoryArchiveReadRequest(Scene currentScene, CommunicationsManager commsManager) | ||
58 | { | ||
59 | List<string> serialisedObjects = new List<string>(); | ||
60 | scene = currentScene; | ||
61 | this.commsManager = commsManager; | ||
62 | } | ||
63 | |||
64 | protected InventoryItemBase loadInvItem(string path, string contents) | ||
65 | { | ||
66 | InventoryItemBase item = new InventoryItemBase(); | ||
67 | StringReader sr = new StringReader(contents); | ||
68 | XmlTextReader reader = new XmlTextReader(sr); | ||
69 | |||
70 | if (contents.Equals("")) return null; | ||
71 | |||
72 | reader.ReadStartElement("InventoryObject"); | ||
73 | reader.ReadStartElement("Name"); | ||
74 | item.Name = reader.ReadString(); | ||
75 | reader.ReadEndElement(); | ||
76 | reader.ReadStartElement("ID"); | ||
77 | item.ID = UUID.Parse(reader.ReadString()); | ||
78 | reader.ReadEndElement(); | ||
79 | reader.ReadStartElement("InvType"); | ||
80 | item.InvType = System.Convert.ToInt32(reader.ReadString()); | ||
81 | reader.ReadEndElement(); | ||
82 | reader.ReadStartElement("CreatorUUID"); | ||
83 | item.Creator = UUID.Parse(reader.ReadString()); | ||
84 | item.Creator = userProfile.ID; | ||
85 | reader.ReadEndElement(); | ||
86 | reader.ReadStartElement("CreationDate"); | ||
87 | item.CreationDate = System.Convert.ToInt32(reader.ReadString()); | ||
88 | reader.ReadEndElement(); | ||
89 | reader.ReadStartElement("Owner"); | ||
90 | item.Owner = UUID.Parse(reader.ReadString()); | ||
91 | item.Owner = userProfile.ID; | ||
92 | reader.ReadEndElement(); | ||
93 | //No description would kill it | ||
94 | if (reader.IsEmptyElement) | ||
95 | { | ||
96 | reader.ReadStartElement("Description"); | ||
97 | } | ||
98 | else | ||
99 | { | ||
100 | reader.ReadStartElement("Description"); | ||
101 | item.Description = reader.ReadString(); | ||
102 | reader.ReadEndElement(); | ||
103 | } | ||
104 | reader.ReadStartElement("AssetType"); | ||
105 | item.AssetType = System.Convert.ToInt32(reader.ReadString()); | ||
106 | reader.ReadEndElement(); | ||
107 | reader.ReadStartElement("AssetID"); | ||
108 | item.AssetID = UUID.Parse(reader.ReadString()); | ||
109 | reader.ReadEndElement(); | ||
110 | reader.ReadStartElement("SaleType"); | ||
111 | item.SaleType = System.Convert.ToByte(reader.ReadString()); | ||
112 | reader.ReadEndElement(); | ||
113 | reader.ReadStartElement("SalePrice"); | ||
114 | item.SalePrice = System.Convert.ToInt32(reader.ReadString()); | ||
115 | reader.ReadEndElement(); | ||
116 | reader.ReadStartElement("BasePermissions"); | ||
117 | item.BasePermissions = System.Convert.ToUInt32(reader.ReadString()); | ||
118 | reader.ReadEndElement(); | ||
119 | reader.ReadStartElement("CurrentPermissions"); | ||
120 | item.CurrentPermissions = System.Convert.ToUInt32(reader.ReadString()); | ||
121 | reader.ReadEndElement(); | ||
122 | reader.ReadStartElement("EveryOnePermssions"); | ||
123 | item.EveryOnePermissions = System.Convert.ToUInt32(reader.ReadString()); | ||
124 | reader.ReadEndElement(); | ||
125 | reader.ReadStartElement("NextPermissions"); | ||
126 | item.NextPermissions = System.Convert.ToUInt32(reader.ReadString()); | ||
127 | reader.ReadEndElement(); | ||
128 | reader.ReadStartElement("Flags"); | ||
129 | item.Flags = System.Convert.ToUInt32(reader.ReadString()); | ||
130 | reader.ReadEndElement(); | ||
131 | reader.ReadStartElement("GroupID"); | ||
132 | item.GroupID = UUID.Parse(reader.ReadString()); | ||
133 | reader.ReadEndElement(); | ||
134 | reader.ReadStartElement("GroupOwned"); | ||
135 | item.GroupOwned = System.Convert.ToBoolean(reader.ReadString()); | ||
136 | reader.ReadEndElement(); | ||
137 | //reader.ReadStartElement("ParentFolderID"); | ||
138 | //item.Folder = UUID.Parse(reader.ReadString()); | ||
139 | //reader.ReadEndElement(); | ||
140 | //reader.ReadEndElement(); | ||
141 | |||
142 | return item; | ||
143 | } | ||
144 | |||
145 | public void execute(string[] cmdparams) | ||
146 | { | ||
147 | string filePath = "ERROR"; | ||
148 | int successfulAssetRestores = 0; | ||
149 | int failedAssetRestores = 0; | ||
150 | |||
151 | string firstName = cmdparams[0]; | ||
152 | string lastName = cmdparams[1]; | ||
153 | string invPath = cmdparams[2]; | ||
154 | loadPath = (cmdparams.Length > 3 ? cmdparams[3] : "inventory.tar.gz"); | ||
155 | |||
156 | archive | ||
157 | = new TarArchiveReader(new GZipStream( | ||
158 | new FileStream(loadPath, FileMode.Open), CompressionMode.Decompress)); | ||
159 | |||
160 | |||
161 | userProfile = commsManager.UserService.GetUserProfile(firstName, lastName); | ||
162 | userInfo = commsManager.UserProfileCacheService.GetUserDetails(userProfile.ID); | ||
163 | |||
164 | byte[] data; | ||
165 | while ((data = archive.ReadEntry(out filePath)) != null) | ||
166 | { | ||
167 | if (filePath.StartsWith(ArchiveConstants.ASSETS_PATH)) | ||
168 | { | ||
169 | if (LoadAsset(filePath, data)) | ||
170 | successfulAssetRestores++; | ||
171 | else | ||
172 | failedAssetRestores++; | ||
173 | } | ||
174 | else | ||
175 | { | ||
176 | //Load the item | ||
177 | InventoryItemBase item = loadInvItem(filePath, m_asciiEncoding.GetString(data)); | ||
178 | if (item != null) userInfo.AddItem(item); | ||
179 | } | ||
180 | } | ||
181 | |||
182 | archive.Close(); | ||
183 | |||
184 | m_log.InfoFormat("[ARCHIVER]: Restored {0} assets", successfulAssetRestores); | ||
185 | } | ||
186 | |||
187 | /// <summary> | ||
188 | /// Load an asset | ||
189 | /// </summary> | ||
190 | /// <param name="assetFilename"></param> | ||
191 | /// <param name="data"></param> | ||
192 | /// <returns>true if asset was successfully loaded, false otherwise</returns> | ||
193 | private bool LoadAsset(string assetPath, byte[] data) | ||
194 | { | ||
195 | IRegionSerialiser serialiser = scene.RequestModuleInterface<IRegionSerialiser>(); | ||
196 | // Right now we're nastily obtaining the UUID from the filename | ||
197 | string filename = assetPath.Remove(0, ArchiveConstants.ASSETS_PATH.Length); | ||
198 | int i = filename.LastIndexOf(ArchiveConstants.ASSET_EXTENSION_SEPARATOR); | ||
199 | |||
200 | if (i == -1) | ||
201 | { | ||
202 | m_log.ErrorFormat( | ||
203 | "[ARCHIVER]: Could not find extension information in asset path {0} since it's missing the separator {1}. Skipping", | ||
204 | assetPath, ArchiveConstants.ASSET_EXTENSION_SEPARATOR); | ||
205 | |||
206 | return false; | ||
207 | } | ||
208 | |||
209 | string extension = filename.Substring(i); | ||
210 | string uuid = filename.Remove(filename.Length - extension.Length); | ||
211 | |||
212 | if (ArchiveConstants.EXTENSION_TO_ASSET_TYPE.ContainsKey(extension)) | ||
213 | { | ||
214 | sbyte assetType = ArchiveConstants.EXTENSION_TO_ASSET_TYPE[extension]; | ||
215 | |||
216 | m_log.DebugFormat("[ARCHIVER]: Importing asset {0}, type {1}", uuid, assetType); | ||
217 | |||
218 | AssetBase asset = new AssetBase(new UUID(uuid), "RandomName"); | ||
219 | |||
220 | asset.Type = assetType; | ||
221 | asset.Data = data; | ||
222 | |||
223 | scene.AssetCache.AddAsset(asset); | ||
224 | |||
225 | |||
226 | return true; | ||
227 | } | ||
228 | else | ||
229 | { | ||
230 | m_log.ErrorFormat( | ||
231 | "[ARCHIVER]: Tried to dearchive data with path {0} with an unknown type extension {1}", | ||
232 | assetPath, extension); | ||
233 | |||
234 | return false; | ||
235 | } | ||
236 | } | ||
237 | } | ||
238 | } \ No newline at end of file | ||