corrade-vassal – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 vero 1 /*
2 * Copyright (c) 2006-2014, openmetaverse.org
3 * All rights reserved.
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 *
8 * - Redistributions of source code must retain the above copyright notice, this
9 * list of conditions and the following disclaimer.
10 * - Neither the name of the openmetaverse.org nor the names
11 * of its contributors may be used to endorse or promote products derived from
12 * this software without specific prior written permission.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
18 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24 * POSSIBILITY OF SUCH DAMAGE.
25 */
26  
27 using System;
28 using System.Collections.Generic;
29 using System.IO;
30 using System.Reflection;
31 using System.Xml;
32 using OpenMetaverse;
33  
34 namespace OpenMetaverse.Assets
35 {
36 /// <summary>
37 /// Archives assets
38 /// </summary>
39 public class AssetsArchiver
40 {
41 ///// <value>
42 ///// Post a message to the log every x assets as a progress bar
43 ///// </value>
44 //static int LOG_ASSET_LOAD_NOTIFICATION_INTERVAL = 50;
45  
46 /// <summary>
47 /// Archive assets
48 /// </summary>
49 protected IDictionary<UUID, Asset> m_assets;
50  
51 public AssetsArchiver(IDictionary<UUID, Asset> assets)
52 {
53 m_assets = assets;
54 }
55  
56 /// <summary>
57 /// Archive the assets given to this archiver to the given archive.
58 /// </summary>
59 /// <param name="archive"></param>
60 public void Archive(TarArchiveWriter archive)
61 {
62 //WriteMetadata(archive);
63 WriteData(archive);
64 }
65  
66 /// <summary>
67 /// Write an assets metadata file to the given archive
68 /// </summary>
69 /// <param name="archive"></param>
70 protected void WriteMetadata(TarArchiveWriter archive)
71 {
72 StringWriter sw = new StringWriter();
73 using (XmlTextWriter xtw = new XmlTextWriter(sw))
74 {
75 xtw.Formatting = Formatting.Indented;
76 xtw.WriteStartDocument();
77  
78 xtw.WriteStartElement("assets");
79  
80 foreach (UUID uuid in m_assets.Keys)
81 {
82 Asset asset = m_assets[uuid];
83  
84 if (asset != null)
85 {
86 xtw.WriteStartElement("asset");
87  
88 string extension = string.Empty;
89  
90 if (ArchiveConstants.ASSET_TYPE_TO_EXTENSION.ContainsKey(asset.AssetType))
91 {
92 extension = ArchiveConstants.ASSET_TYPE_TO_EXTENSION[asset.AssetType];
93 }
94  
95 xtw.WriteElementString("filename", uuid.ToString() + extension);
96  
97 xtw.WriteElementString("name", uuid.ToString());
98 xtw.WriteElementString("description", String.Empty);
99 xtw.WriteElementString("asset-type", asset.AssetType.ToString());
100  
101 xtw.WriteEndElement();
102 }
103 }
104  
105 xtw.WriteEndElement();
106 xtw.WriteEndDocument();
107 archive.WriteFile("assets.xml", sw.ToString());
108 }
109 }
110  
111 /// <summary>
112 /// Write asset data files to the given archive
113 /// </summary>
114 /// <param name="archive"></param>
115 protected void WriteData(TarArchiveWriter archive)
116 {
117 // It appears that gtar, at least, doesn't need the intermediate directory entries in the tar
118 //archive.AddDir("assets");
119  
120 int assetsAdded = 0;
121  
122 foreach (UUID uuid in m_assets.Keys)
123 {
124 Asset asset = m_assets[uuid];
125  
126 string extension = string.Empty;
127  
128 if (ArchiveConstants.ASSET_TYPE_TO_EXTENSION.ContainsKey(asset.AssetType))
129 {
130 extension = ArchiveConstants.ASSET_TYPE_TO_EXTENSION[asset.AssetType];
131 }
132 else
133 {
134 Logger.Log(String.Format(
135 "Unrecognized asset type {0} with uuid {1}. This asset will be saved but not reloaded",
136 asset.AssetType, asset.AssetID), Helpers.LogLevel.Warning);
137 }
138  
139 asset.Encode();
140  
141 archive.WriteFile(
142 ArchiveConstants.ASSETS_PATH + uuid.ToString() + extension,
143 asset.AssetData);
144  
145 assetsAdded++;
146 }
147 }
148 }
149 }