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  
28 using System;
29 using System.Collections.Generic;
30 using System.IO;
31 using System.Reflection;
32 using System.Xml;
33 using log4net;
34 using OpenMetaverse;
35  
36 namespace OpenSim.Framework
37 {
38 public static class SLUtil
39 {
40 // private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
41  
42 #region SL / file extension / content-type conversions
43  
44 private class TypeMapping
45 {
46 private sbyte assetType;
47 private InventoryType inventoryType;
48 private string contentType;
49 private string contentType2;
50 private string extension;
51  
52 public sbyte AssetTypeCode
53 {
54 get { return assetType; }
55 }
56  
57 public object AssetType
58 {
59 get {
60 if (Enum.IsDefined(typeof(OpenMetaverse.AssetType), assetType))
61 return (OpenMetaverse.AssetType)assetType;
62 else
63 return OpenMetaverse.AssetType.Unknown;
64 }
65 }
66  
67 public InventoryType InventoryType
68 {
69 get { return inventoryType; }
70 }
71  
72 public string ContentType
73 {
74 get { return contentType; }
75 }
76  
77 public string ContentType2
78 {
79 get { return contentType2; }
80 }
81  
82 public string Extension
83 {
84 get { return extension; }
85 }
86  
87 private TypeMapping(sbyte assetType, InventoryType inventoryType, string contentType, string contentType2, string extension)
88 {
89 this.assetType = assetType;
90 this.inventoryType = inventoryType;
91 this.contentType = contentType;
92 this.contentType2 = contentType2;
93 this.extension = extension;
94 }
95  
96 public TypeMapping(AssetType assetType, InventoryType inventoryType, string contentType, string contentType2, string extension)
97 : this((sbyte)assetType, inventoryType, contentType, contentType2, extension)
98 {
99 }
100  
101 public TypeMapping(AssetType assetType, InventoryType inventoryType, string contentType, string extension)
102 : this((sbyte)assetType, inventoryType, contentType, null, extension)
103 {
104 }
105 }
106  
107 /// <summary>
108 /// Maps between AssetType, InventoryType and Content-Type.
109 /// Where more than one possibility exists, the first one takes precedence. E.g.:
110 /// AssetType "AssetType.Texture" -> Content-Type "image-xj2c"
111 /// Content-Type "image/x-j2c" -> InventoryType "InventoryType.Texture"
112 /// </summary>
113 private static TypeMapping[] MAPPINGS = new TypeMapping[] {
114 new TypeMapping(AssetType.Unknown, InventoryType.Unknown, "application/octet-stream", "bin"),
115 new TypeMapping(AssetType.Texture, InventoryType.Texture, "image/x-j2c", "image/jp2", "j2c"),
116 new TypeMapping(AssetType.Texture, InventoryType.Snapshot, "image/x-j2c", "image/jp2", "j2c"),
117 new TypeMapping(AssetType.TextureTGA, InventoryType.Texture, "image/tga", "tga"),
118 new TypeMapping(AssetType.ImageTGA, InventoryType.Texture, "image/tga", "tga"),
119 new TypeMapping(AssetType.ImageJPEG, InventoryType.Texture, "image/jpeg", "jpg"),
120 new TypeMapping(AssetType.Sound, InventoryType.Sound, "audio/ogg", "application/ogg", "ogg"),
121 new TypeMapping(AssetType.SoundWAV, InventoryType.Sound, "audio/x-wav", "wav"),
122 new TypeMapping(AssetType.CallingCard, InventoryType.CallingCard, "application/vnd.ll.callingcard", "application/x-metaverse-callingcard", "callingcard"),
123 new TypeMapping(AssetType.Landmark, InventoryType.Landmark, "application/vnd.ll.landmark", "application/x-metaverse-landmark", "landmark"),
124 new TypeMapping(AssetType.Clothing, InventoryType.Wearable, "application/vnd.ll.clothing", "application/x-metaverse-clothing", "clothing"),
125 new TypeMapping(AssetType.Object, InventoryType.Object, "application/vnd.ll.primitive", "application/x-metaverse-primitive", "primitive"),
126 new TypeMapping(AssetType.Object, InventoryType.Attachment, "application/vnd.ll.primitive", "application/x-metaverse-primitive", "primitive"),
127 new TypeMapping(AssetType.Notecard, InventoryType.Notecard, "application/vnd.ll.notecard", "application/x-metaverse-notecard", "notecard"),
128 new TypeMapping(AssetType.Folder, InventoryType.Folder, "application/vnd.ll.folder", "folder"),
129 new TypeMapping(AssetType.RootFolder, InventoryType.RootCategory, "application/vnd.ll.rootfolder", "rootfolder"),
130 new TypeMapping(AssetType.LSLText, InventoryType.LSL, "application/vnd.ll.lsltext", "application/x-metaverse-lsl", "lsl"),
131 new TypeMapping(AssetType.LSLBytecode, InventoryType.LSL, "application/vnd.ll.lslbyte", "application/x-metaverse-lso", "lso"),
132 new TypeMapping(AssetType.Bodypart, InventoryType.Wearable, "application/vnd.ll.bodypart", "application/x-metaverse-bodypart", "bodypart"),
133 new TypeMapping(AssetType.TrashFolder, InventoryType.Folder, "application/vnd.ll.trashfolder", "trashfolder"),
134 new TypeMapping(AssetType.SnapshotFolder, InventoryType.Folder, "application/vnd.ll.snapshotfolder", "snapshotfolder"),
135 new TypeMapping(AssetType.LostAndFoundFolder, InventoryType.Folder, "application/vnd.ll.lostandfoundfolder", "lostandfoundfolder"),
136 new TypeMapping(AssetType.Animation, InventoryType.Animation, "application/vnd.ll.animation", "application/x-metaverse-animation", "animation"),
137 new TypeMapping(AssetType.Gesture, InventoryType.Gesture, "application/vnd.ll.gesture", "application/x-metaverse-gesture", "gesture"),
138 new TypeMapping(AssetType.Simstate, InventoryType.Snapshot, "application/x-metaverse-simstate", "simstate"),
139 new TypeMapping(AssetType.FavoriteFolder, InventoryType.Unknown, "application/vnd.ll.favoritefolder", "favoritefolder"),
140 new TypeMapping(AssetType.Link, InventoryType.Unknown, "application/vnd.ll.link", "link"),
141 new TypeMapping(AssetType.LinkFolder, InventoryType.Unknown, "application/vnd.ll.linkfolder", "linkfolder"),
142 new TypeMapping(AssetType.CurrentOutfitFolder, InventoryType.Unknown, "application/vnd.ll.currentoutfitfolder", "currentoutfitfolder"),
143 new TypeMapping(AssetType.OutfitFolder, InventoryType.Unknown, "application/vnd.ll.outfitfolder", "outfitfolder"),
144 new TypeMapping(AssetType.MyOutfitsFolder, InventoryType.Unknown, "application/vnd.ll.myoutfitsfolder", "myoutfitsfolder"),
145 new TypeMapping(AssetType.Mesh, InventoryType.Mesh, "application/vnd.ll.mesh", "llm")
146 };
147  
148 private static Dictionary<sbyte, string> asset2Content;
149 private static Dictionary<sbyte, string> asset2Extension;
150 private static Dictionary<InventoryType, string> inventory2Content;
151 private static Dictionary<string, sbyte> content2Asset;
152 private static Dictionary<string, InventoryType> content2Inventory;
153  
154 static SLUtil()
155 {
156 asset2Content = new Dictionary<sbyte, string>();
157 asset2Extension = new Dictionary<sbyte, string>();
158 inventory2Content = new Dictionary<InventoryType, string>();
159 content2Asset = new Dictionary<string, sbyte>();
160 content2Inventory = new Dictionary<string, InventoryType>();
161  
162 foreach (TypeMapping mapping in MAPPINGS)
163 {
164 sbyte assetType = mapping.AssetTypeCode;
165 if (!asset2Content.ContainsKey(assetType))
166 asset2Content.Add(assetType, mapping.ContentType);
167 if (!asset2Extension.ContainsKey(assetType))
168 asset2Extension.Add(assetType, mapping.Extension);
169 if (!inventory2Content.ContainsKey(mapping.InventoryType))
170 inventory2Content.Add(mapping.InventoryType, mapping.ContentType);
171 if (!content2Asset.ContainsKey(mapping.ContentType))
172 content2Asset.Add(mapping.ContentType, assetType);
173 if (!content2Inventory.ContainsKey(mapping.ContentType))
174 content2Inventory.Add(mapping.ContentType, mapping.InventoryType);
175  
176 if (mapping.ContentType2 != null)
177 {
178 if (!content2Asset.ContainsKey(mapping.ContentType2))
179 content2Asset.Add(mapping.ContentType2, assetType);
180 if (!content2Inventory.ContainsKey(mapping.ContentType2))
181 content2Inventory.Add(mapping.ContentType2, mapping.InventoryType);
182 }
183 }
184 }
185  
186 public static string SLAssetTypeToContentType(int assetType)
187 {
188 string contentType;
189 if (!asset2Content.TryGetValue((sbyte)assetType, out contentType))
190 contentType = asset2Content[(sbyte)AssetType.Unknown];
191 return contentType;
192 }
193  
194 public static string SLInvTypeToContentType(int invType)
195 {
196 string contentType;
197 if (!inventory2Content.TryGetValue((InventoryType)invType, out contentType))
198 contentType = inventory2Content[InventoryType.Unknown];
199 return contentType;
200 }
201  
202 public static sbyte ContentTypeToSLAssetType(string contentType)
203 {
204 sbyte assetType;
205 if (!content2Asset.TryGetValue(contentType, out assetType))
206 assetType = (sbyte)AssetType.Unknown;
207 return (sbyte)assetType;
208 }
209  
210 public static sbyte ContentTypeToSLInvType(string contentType)
211 {
212 InventoryType invType;
213 if (!content2Inventory.TryGetValue(contentType, out invType))
214 invType = InventoryType.Unknown;
215 return (sbyte)invType;
216 }
217  
218 public static string SLAssetTypeToExtension(int assetType)
219 {
220 string extension;
221 if (!asset2Extension.TryGetValue((sbyte)assetType, out extension))
222 extension = asset2Extension[(sbyte)AssetType.Unknown];
223 return extension;
224 }
225  
226 #endregion SL / file extension / content-type conversions
227  
228 /// <summary>
229 /// Parse a notecard in Linden format to a string of ordinary text.
230 /// </summary>
231 /// <param name="rawInput"></param>
232 /// <returns></returns>
233 public static string ParseNotecardToString(string rawInput)
234 {
235 string[] output = ParseNotecardToList(rawInput).ToArray();
236  
237 // foreach (string line in output)
238 // m_log.DebugFormat("[PARSE NOTECARD]: ParseNotecardToString got line {0}", line);
239  
240 return string.Join("\n", output);
241 }
242  
243 /// <summary>
244 /// Parse a notecard in Linden format to a list of ordinary lines.
245 /// </summary>
246 /// <param name="rawInput"></param>
247 /// <returns></returns>
248 public static List<string> ParseNotecardToList(string rawInput)
249 {
250 string[] input = rawInput.Replace("\r", "").Split('\n');
251 int idx = 0;
252 int level = 0;
253 List<string> output = new List<string>();
254 string[] words;
255  
256 while (idx < input.Length)
257 {
258 if (input[idx] == "{")
259 {
260 level++;
261 idx++;
262 continue;
263 }
264  
265 if (input[idx]== "}")
266 {
267 level--;
268 idx++;
269 continue;
270 }
271  
272 switch (level)
273 {
274 case 0:
275 words = input[idx].Split(' '); // Linden text ver
276 // Notecards are created *really* empty. Treat that as "no text" (just like after saving an empty notecard)
277 if (words.Length < 3)
278 return output;
279  
280 int version = int.Parse(words[3]);
281 if (version != 2)
282 return output;
283 break;
284 case 1:
285 words = input[idx].Split(' ');
286 if (words[0] == "LLEmbeddedItems")
287 break;
288 if (words[0] == "Text")
289 {
290 int len = int.Parse(words[2]);
291 idx++;
292  
293 int count = -1;
294  
295 while (count < len && idx < input.Length)
296 {
297 // int l = input[idx].Length;
298 string ln = input[idx];
299  
300 int need = len-count-1;
301 if (ln.Length > need)
302 ln = ln.Substring(0, need);
303  
304 // m_log.DebugFormat("[PARSE NOTECARD]: Adding line {0}", ln);
305 output.Add(ln);
306 count += ln.Length + 1;
307 idx++;
308 }
309  
310 return output;
311 }
312 break;
313 case 2:
314 words = input[idx].Split(' '); // count
315 if (words[0] == "count")
316 {
317 int c = int.Parse(words[1]);
318 if (c > 0)
319 return output;
320 break;
321 }
322 break;
323 }
324 idx++;
325 }
326  
327 return output;
328 }
329 }
330 }