30 using System.Collections.Generic;
 
   32 namespace OpenSim.Framework
 
   34     public static class SLUtil
 
   48         #region SL / file extension / content-type conversions 
   54         public static object AssetTypeFromCode(sbyte assetType)
 
   56             if (Enum.IsDefined(typeof(OpenMetaverse.AssetType), assetType))
 
   57                 return (OpenMetaverse.AssetType)assetType;
 
   61                 return OpenMetaverse.AssetType.Unknown;
 
   64         private class TypeMapping
 
   66             private sbyte assetType;
 
   67             private sbyte inventoryType;
 
   68             private string contentType;
 
   69             private string contentType2;
 
   70             private string extension;
 
   72             public sbyte AssetTypeCode
 
   74                 get { 
return assetType; }
 
   77             public object AssetType
 
   79                 get { 
return AssetTypeFromCode(assetType); }
 
   82             public sbyte InventoryType
 
   84                 get { 
return inventoryType; }
 
   87             public string ContentType
 
   89                 get { 
return contentType; }
 
   92             public string ContentType2
 
   94                 get { 
return contentType2; }
 
   97             public string Extension
 
   99                 get { 
return extension; }
 
  102             private TypeMapping(sbyte assetType, sbyte inventoryType, 
string contentType, 
string contentType2, 
string extension)
 
  104                 this.assetType = assetType;
 
  105                 this.inventoryType = inventoryType;
 
  106                 this.contentType = contentType;
 
  107                 this.contentType2 = contentType2;
 
  108                 this.extension = extension;
 
  111             public TypeMapping(AssetType assetType, sbyte inventoryType, 
string contentType, 
string contentType2, 
string extension)
 
  112                 : this((sbyte)assetType, inventoryType, contentType, contentType2, extension)
 
  116             public TypeMapping(AssetType assetType, InventoryType inventoryType, 
string contentType, 
string contentType2, 
string extension)
 
  117                 : this((sbyte)assetType, (sbyte)inventoryType, contentType, contentType2, extension)
 
  121             public TypeMapping(AssetType assetType, InventoryType inventoryType, 
string contentType, 
string extension)
 
  122                 : this((sbyte)assetType, (sbyte)inventoryType, contentType, null, extension)
 
  126             public TypeMapping(AssetType assetType, FolderType inventoryType, 
string contentType, 
string extension)
 
  127                 : this((sbyte)assetType, (sbyte)inventoryType, contentType, null, extension)
 
  131             public TypeMapping(
OpenSimAssetType assetType, InventoryType inventoryType, 
string contentType, 
string extension)
 
  132                 : this((sbyte)assetType, (sbyte)inventoryType, contentType, null, extension)
 
  143         private static TypeMapping[] MAPPINGS = 
new TypeMapping[] {
 
  144             new TypeMapping(AssetType.Unknown, InventoryType.Unknown, 
"application/octet-stream", 
"bin"),
 
  145             new TypeMapping(AssetType.Texture, InventoryType.Texture, 
"image/x-j2c", 
"image/jp2", 
"j2c"),
 
  146             new TypeMapping(AssetType.Texture, InventoryType.Snapshot, 
"image/x-j2c", 
"image/jp2", 
"j2c"),
 
  147             new TypeMapping(AssetType.TextureTGA, InventoryType.Texture, 
"image/tga", 
"tga"),
 
  148             new TypeMapping(AssetType.ImageTGA, InventoryType.Texture, 
"image/tga", 
"tga"),
 
  149             new TypeMapping(AssetType.ImageJPEG, InventoryType.Texture, 
"image/jpeg", 
"jpg"),
 
  150             new TypeMapping(AssetType.Sound, InventoryType.Sound, 
"audio/ogg", 
"application/ogg", 
"ogg"),
 
  151             new TypeMapping(AssetType.SoundWAV, InventoryType.Sound, 
"audio/x-wav", 
"wav"),
 
  152             new TypeMapping(AssetType.CallingCard, InventoryType.CallingCard, 
"application/vnd.ll.callingcard", 
"application/x-metaverse-callingcard", 
"callingcard"),
 
  153             new TypeMapping(AssetType.Landmark, InventoryType.Landmark, 
"application/vnd.ll.landmark", 
"application/x-metaverse-landmark", 
"landmark"),
 
  154             new TypeMapping(AssetType.Clothing, InventoryType.Wearable, 
"application/vnd.ll.clothing", 
"application/x-metaverse-clothing", 
"clothing"),
 
  155             new TypeMapping(AssetType.Object, InventoryType.Object, 
"application/vnd.ll.primitive", 
"application/x-metaverse-primitive", 
"primitive"),
 
  156             new TypeMapping(AssetType.Object, InventoryType.Attachment, 
"application/vnd.ll.primitive", 
"application/x-metaverse-primitive", 
"primitive"),
 
  157             new TypeMapping(AssetType.Notecard, InventoryType.Notecard, 
"application/vnd.ll.notecard", 
"application/x-metaverse-notecard", 
"notecard"),
 
  158             new TypeMapping(AssetType.LSLText, InventoryType.LSL, 
"application/vnd.ll.lsltext", 
"application/x-metaverse-lsl", 
"lsl"),
 
  159             new TypeMapping(AssetType.LSLBytecode, InventoryType.LSL, 
"application/vnd.ll.lslbyte", 
"application/x-metaverse-lso", 
"lso"),
 
  160             new TypeMapping(AssetType.Bodypart, InventoryType.Wearable, 
"application/vnd.ll.bodypart", 
"application/x-metaverse-bodypart", 
"bodypart"),
 
  161             new TypeMapping(AssetType.Animation, InventoryType.Animation, 
"application/vnd.ll.animation", 
"application/x-metaverse-animation", 
"animation"),
 
  162             new TypeMapping(AssetType.Gesture, InventoryType.Gesture, 
"application/vnd.ll.gesture", 
"application/x-metaverse-gesture", 
"gesture"),
 
  163             new TypeMapping(AssetType.Simstate, InventoryType.Snapshot, 
"application/x-metaverse-simstate", 
"simstate"),
 
  164             new TypeMapping(AssetType.Link, InventoryType.Unknown, 
"application/vnd.ll.link", 
"link"),
 
  165             new TypeMapping(AssetType.LinkFolder, InventoryType.Unknown, 
"application/vnd.ll.linkfolder", 
"linkfolder"),
 
  166             new TypeMapping(AssetType.Mesh, InventoryType.Mesh, 
"application/vnd.ll.mesh", 
"llm"),
 
  169             new TypeMapping(AssetType.Folder, FolderType.None, 
"application/vnd.ll.folder", 
"folder"),
 
  170             new TypeMapping(AssetType.Folder, FolderType.Root, 
"application/vnd.ll.rootfolder", 
"rootfolder"),
 
  171             new TypeMapping(AssetType.Folder, FolderType.Trash, 
"application/vnd.ll.trashfolder", 
"trashfolder"),
 
  172             new TypeMapping(AssetType.Folder, FolderType.Snapshot, 
"application/vnd.ll.snapshotfolder", 
"snapshotfolder"),
 
  173             new TypeMapping(AssetType.Folder, FolderType.LostAndFound, 
"application/vnd.ll.lostandfoundfolder", 
"lostandfoundfolder"),
 
  174             new TypeMapping(AssetType.Folder, FolderType.Favorites, 
"application/vnd.ll.favoritefolder", 
"favoritefolder"),
 
  175             new TypeMapping(AssetType.Folder, FolderType.CurrentOutfit, 
"application/vnd.ll.currentoutfitfolder", 
"currentoutfitfolder"),
 
  176             new TypeMapping(AssetType.Folder, FolderType.Outfit, 
"application/vnd.ll.outfitfolder", 
"outfitfolder"),
 
  177             new TypeMapping(AssetType.Folder, FolderType.MyOutfits, 
"application/vnd.ll.myoutfitsfolder", 
"myoutfitsfolder"),
 
  182             new TypeMapping(AssetType.Folder, InventoryType.Folder, 
"application/vnd.ll.folder", 
"folder"),
 
  185             new TypeMapping(
OpenSimAssetType.Material, InventoryType.Unknown, 
"application/llsd+xml", 
"material")
 
  188         private static Dictionary<sbyte, string> asset2Content;
 
  189         private static Dictionary<sbyte, string> asset2Extension;
 
  190         private static Dictionary<sbyte, string> inventory2Content;
 
  191         private static Dictionary<string, sbyte> content2Asset;
 
  192         private static Dictionary<string, sbyte> content2Inventory;
 
  196             asset2Content = 
new Dictionary<sbyte, string>();
 
  197             asset2Extension = 
new Dictionary<sbyte, string>();
 
  198             inventory2Content = 
new Dictionary<sbyte, string>();
 
  199             content2Asset = 
new Dictionary<string, sbyte>();
 
  200             content2Inventory = 
new Dictionary<string, sbyte>();
 
  202             foreach (TypeMapping mapping 
in MAPPINGS)
 
  204                 sbyte assetType = mapping.AssetTypeCode;
 
  205                 if (!asset2Content.ContainsKey(assetType))
 
  206                     asset2Content.Add(assetType, mapping.ContentType);
 
  208                 if (!asset2Extension.ContainsKey(assetType))
 
  209                     asset2Extension.Add(assetType, mapping.Extension);
 
  211                 if (!inventory2Content.ContainsKey(mapping.InventoryType))
 
  212                     inventory2Content.Add(mapping.InventoryType, mapping.ContentType);
 
  214                 if (!content2Asset.ContainsKey(mapping.ContentType))
 
  215                     content2Asset.Add(mapping.ContentType, assetType);
 
  217                 if (!content2Inventory.ContainsKey(mapping.ContentType))
 
  218                     content2Inventory.Add(mapping.ContentType, mapping.InventoryType);
 
  220                 if (mapping.ContentType2 != null)
 
  222                     if (!content2Asset.ContainsKey(mapping.ContentType2))
 
  223                         content2Asset.Add(mapping.ContentType2, assetType);
 
  224                     if (!content2Inventory.ContainsKey(mapping.ContentType2))
 
  225                         content2Inventory.Add(mapping.ContentType2, mapping.InventoryType);
 
  230         public static string SLAssetTypeToContentType(
int assetType)
 
  233             if (!asset2Content.TryGetValue((sbyte)assetType, out contentType))
 
  234                 contentType = asset2Content[(sbyte)AssetType.Unknown];
 
  238         public static string SLInvTypeToContentType(
int invType)
 
  241             if (!inventory2Content.TryGetValue((sbyte)invType, out contentType))
 
  242                 contentType = inventory2Content[(sbyte)InventoryType.Unknown];
 
  246         public static sbyte ContentTypeToSLAssetType(
string contentType)
 
  249             if (!content2Asset.TryGetValue(contentType, out assetType))
 
  250                 assetType = (sbyte)AssetType.Unknown;
 
  251             return (sbyte)assetType;
 
  254         public static sbyte ContentTypeToSLInvType(
string contentType)
 
  257             if (!content2Inventory.TryGetValue(contentType, out invType))
 
  258                 invType = (sbyte)InventoryType.Unknown;
 
  259             return (sbyte)invType;
 
  262         public static string SLAssetTypeToExtension(
int assetType)
 
  265             if (!asset2Extension.TryGetValue((sbyte)assetType, out extension))
 
  266                 extension = asset2Extension[(sbyte)AssetType.Unknown];
 
  270         #endregion SL / file extension / content-type conversions 
  272         private class NotecardReader
 
  274             private string rawInput;
 
  275             private int lineNumber;
 
  277             public int LineNumber
 
  285             public NotecardReader(
string _rawInput)
 
  287                 rawInput = (string)_rawInput.Clone();
 
  291             public string getLine()
 
  293                 if(rawInput.Length == 0)
 
  295                     throw new NotANotecardFormatException(lineNumber + 1);
 
  298                 int pos = rawInput.IndexOf(
'\n');
 
  301                     pos = rawInput.Length;
 
  306                 string line = rawInput.Substring(0, pos);
 
  307                 if (pos + 1 >= rawInput.Length)
 
  309                     rawInput = string.Empty;
 
  313                     rawInput = rawInput.Substring(pos + 1);
 
  316                 line = line.Replace(
"\t", 
" ");
 
  317                 while(line.IndexOf(
"  ") >= 0)
 
  319                     line = line.Replace(
"  ", 
" ");
 
  321                 return line.Replace(
"\r", 
"").Trim();
 
  324             public string getBlock(
int length)
 
  327                 if(length > rawInput.Length)
 
  329                     throw new NotANotecardFormatException(lineNumber);
 
  331                 string line = rawInput.Substring(0, length);
 
  332                 rawInput = rawInput.Substring(length);
 
  343                lineNumber = _lineNumber;
 
  347         private static void skipSection(NotecardReader reader)
 
  349             if (reader.getLine() != 
"{")
 
  350                 throw new NotANotecardFormatException(reader.LineNumber);
 
  353             while ((line = reader.getLine()) != 
"}")
 
  355                 if(line.IndexOf(
'{')>=0)
 
  357                     throw new NotANotecardFormatException(reader.LineNumber);
 
  362         private static void skipInventoryItem(NotecardReader reader)
 
  364             if (reader.getLine() != 
"{")
 
  365                 throw new NotANotecardFormatException(reader.LineNumber);
 
  368             while((line = reader.getLine()) != 
"}")
 
  370                 string[] data = line.Split(
' ');
 
  375                 if(data[0] == 
"permissions")
 
  379                 else if(data[0] == 
"sale_info")
 
  383                 else if (line.IndexOf(
'{') >= 0)
 
  385                     throw new NotANotecardFormatException(reader.LineNumber);
 
  390         private static void skipInventoryItems(NotecardReader reader)
 
  392             if(reader.getLine() != 
"{")
 
  394                 throw new NotANotecardFormatException(reader.LineNumber);
 
  398             while((line = reader.getLine()) != 
"}")
 
  400                 string[] data = line.Split(
' ');
 
  406                 if(data[0] == 
"inv_item")
 
  408                     skipInventoryItem(reader);
 
  410                 else if (line.IndexOf(
'{') >= 0)
 
  412                     throw new NotANotecardFormatException(reader.LineNumber);
 
  418         private static void skipInventory(NotecardReader reader)
 
  420             if (reader.getLine() != 
"{")
 
  421                 throw new NotANotecardFormatException(reader.LineNumber);
 
  424             while((line = reader.getLine()) != 
"}")
 
  426                 string[] data = line.Split(
' ');
 
  427                 if(data[0] == 
"count")
 
  429                     int count = Int32.Parse(data[1]);
 
  430                     for(
int i = 0; i < count; ++i)
 
  432                         skipInventoryItems(reader);
 
  435                 else if (line.IndexOf(
'{') >= 0)
 
  437                     throw new NotANotecardFormatException(reader.LineNumber);
 
  442         private static string readNotecardText(NotecardReader reader)
 
  444             if (reader.getLine() != 
"{")
 
  445                 throw new NotANotecardFormatException(reader.LineNumber);
 
  447             string notecardString = string.Empty;
 
  449             while((line = reader.getLine()) != 
"}")
 
  451                 string[] data = line.Split(
' ');
 
  452                 if (data.Length == 0)
 
  457                 if (data[0] == 
"LLEmbeddedItems")
 
  459                     skipInventory(reader);
 
  461                 else if(data[0] == 
"Text" && data.Length == 3)
 
  463                     int length = Int32.Parse(data[2]);
 
  464                     notecardString = reader.getBlock(length);
 
  466                 else if (line.IndexOf(
'{') >= 0)
 
  468                     throw new NotANotecardFormatException(reader.LineNumber);
 
  472             return notecardString;
 
  475         private static string readNotecard(byte[] rawInput)
 
  477             string rawIntermedInput = string.Empty;
 
  480             foreach(byte c 
in rawInput)
 
  483                 rawIntermedInput += d;
 
  486             NotecardReader reader = 
new NotecardReader(rawIntermedInput);
 
  490                 line = reader.getLine();
 
  494                 return System.Text.Encoding.UTF8.GetString(rawInput);
 
  496             string[] versioninfo = line.Split(
' ');
 
  497             if(versioninfo.Length < 3)
 
  499                 return System.Text.Encoding.UTF8.GetString(rawInput);
 
  501             else if(versioninfo[0] != 
"Linden" || versioninfo[1] != 
"text")
 
  503                 return System.Text.Encoding.UTF8.GetString(rawInput);
 
  508                 string o = readNotecardText(reader);
 
  509                 byte[] a = 
new byte[o.Length];
 
  510                 for(
int i = 0; i < o.Length; ++i)
 
  514                 return System.Text.Encoding.UTF8.GetString(a);
 
  523         public static string ParseNotecardToString(byte[] rawInput)
 
  525             return readNotecard(rawInput);
 
  533         public static string[] ParseNotecardToArray(byte[] rawInput)
 
  535             return readNotecard(rawInput).Replace(
"\r", 
"").Split(
'\n');
 
OpenSim.Framework.SLUtil.OpenSimAssetType OpenSimAssetType
 
Material
Material type for a primitive