1
0

SLUtil.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. using System;
  2. using System.Collections.Generic;
  3. using OpenMetaverse;
  4. namespace OpenSim.Framework
  5. {
  6. public static class SLUtil
  7. {
  8. #region SL / file extension / content-type conversions
  9. public static string SLAssetTypeToContentType(int assetType)
  10. {
  11. switch ((AssetType)assetType)
  12. {
  13. case AssetType.Texture:
  14. return "image/x-j2c";
  15. case AssetType.Sound:
  16. return "application/ogg";
  17. case AssetType.CallingCard:
  18. return "application/vnd.ll.callingcard";
  19. case AssetType.Landmark:
  20. return "application/vnd.ll.landmark";
  21. case AssetType.Clothing:
  22. return "application/vnd.ll.clothing";
  23. case AssetType.Object:
  24. return "application/vnd.ll.primitive";
  25. case AssetType.Notecard:
  26. return "application/vnd.ll.notecard";
  27. case AssetType.Folder:
  28. return "application/vnd.ll.folder";
  29. case AssetType.RootFolder:
  30. return "application/vnd.ll.rootfolder";
  31. case AssetType.LSLText:
  32. return "application/vnd.ll.lsltext";
  33. case AssetType.LSLBytecode:
  34. return "application/vnd.ll.lslbyte";
  35. case AssetType.TextureTGA:
  36. case AssetType.ImageTGA:
  37. return "image/tga";
  38. case AssetType.Bodypart:
  39. return "application/vnd.ll.bodypart";
  40. case AssetType.TrashFolder:
  41. return "application/vnd.ll.trashfolder";
  42. case AssetType.SnapshotFolder:
  43. return "application/vnd.ll.snapshotfolder";
  44. case AssetType.LostAndFoundFolder:
  45. return "application/vnd.ll.lostandfoundfolder";
  46. case AssetType.SoundWAV:
  47. return "audio/x-wav";
  48. case AssetType.ImageJPEG:
  49. return "image/jpeg";
  50. case AssetType.Animation:
  51. return "application/vnd.ll.animation";
  52. case AssetType.Gesture:
  53. return "application/vnd.ll.gesture";
  54. case AssetType.Simstate:
  55. return "application/x-metaverse-simstate";
  56. case AssetType.Unknown:
  57. default:
  58. return "application/octet-stream";
  59. }
  60. }
  61. public static sbyte ContentTypeToSLAssetType(string contentType)
  62. {
  63. switch (contentType)
  64. {
  65. case "image/x-j2c":
  66. case "image/jp2":
  67. return (sbyte)AssetType.Texture;
  68. case "application/ogg":
  69. return (sbyte)AssetType.Sound;
  70. case "application/vnd.ll.callingcard":
  71. case "application/x-metaverse-callingcard":
  72. return (sbyte)AssetType.CallingCard;
  73. case "application/vnd.ll.landmark":
  74. case "application/x-metaverse-landmark":
  75. return (sbyte)AssetType.Landmark;
  76. case "application/vnd.ll.clothing":
  77. case "application/x-metaverse-clothing":
  78. return (sbyte)AssetType.Clothing;
  79. case "application/vnd.ll.primitive":
  80. case "application/x-metaverse-primitive":
  81. return (sbyte)AssetType.Object;
  82. case "application/vnd.ll.notecard":
  83. case "application/x-metaverse-notecard":
  84. return (sbyte)AssetType.Notecard;
  85. case "application/vnd.ll.folder":
  86. return (sbyte)AssetType.Folder;
  87. case "application/vnd.ll.rootfolder":
  88. return (sbyte)AssetType.RootFolder;
  89. case "application/vnd.ll.lsltext":
  90. case "application/x-metaverse-lsl":
  91. return (sbyte)AssetType.LSLText;
  92. case "application/vnd.ll.lslbyte":
  93. case "application/x-metaverse-lso":
  94. return (sbyte)AssetType.LSLBytecode;
  95. case "image/tga":
  96. // Note that AssetType.TextureTGA will be converted to AssetType.ImageTGA
  97. return (sbyte)AssetType.ImageTGA;
  98. case "application/vnd.ll.bodypart":
  99. case "application/x-metaverse-bodypart":
  100. return (sbyte)AssetType.Bodypart;
  101. case "application/vnd.ll.trashfolder":
  102. return (sbyte)AssetType.TrashFolder;
  103. case "application/vnd.ll.snapshotfolder":
  104. return (sbyte)AssetType.SnapshotFolder;
  105. case "application/vnd.ll.lostandfoundfolder":
  106. return (sbyte)AssetType.LostAndFoundFolder;
  107. case "audio/x-wav":
  108. return (sbyte)AssetType.SoundWAV;
  109. case "image/jpeg":
  110. return (sbyte)AssetType.ImageJPEG;
  111. case "application/vnd.ll.animation":
  112. case "application/x-metaverse-animation":
  113. return (sbyte)AssetType.Animation;
  114. case "application/vnd.ll.gesture":
  115. case "application/x-metaverse-gesture":
  116. return (sbyte)AssetType.Gesture;
  117. case "application/x-metaverse-simstate":
  118. return (sbyte)AssetType.Simstate;
  119. case "application/octet-stream":
  120. default:
  121. return (sbyte)AssetType.Unknown;
  122. }
  123. }
  124. public static sbyte ContentTypeToSLInvType(string contentType)
  125. {
  126. switch (contentType)
  127. {
  128. case "image/x-j2c":
  129. case "image/jp2":
  130. case "image/tga":
  131. case "image/jpeg":
  132. return (sbyte)InventoryType.Texture;
  133. case "application/ogg":
  134. case "audio/x-wav":
  135. return (sbyte)InventoryType.Sound;
  136. case "application/vnd.ll.callingcard":
  137. case "application/x-metaverse-callingcard":
  138. return (sbyte)InventoryType.CallingCard;
  139. case "application/vnd.ll.landmark":
  140. case "application/x-metaverse-landmark":
  141. return (sbyte)InventoryType.Landmark;
  142. case "application/vnd.ll.clothing":
  143. case "application/x-metaverse-clothing":
  144. case "application/vnd.ll.bodypart":
  145. case "application/x-metaverse-bodypart":
  146. return (sbyte)InventoryType.Wearable;
  147. case "application/vnd.ll.primitive":
  148. case "application/x-metaverse-primitive":
  149. return (sbyte)InventoryType.Object;
  150. case "application/vnd.ll.notecard":
  151. case "application/x-metaverse-notecard":
  152. return (sbyte)InventoryType.Notecard;
  153. case "application/vnd.ll.folder":
  154. return (sbyte)InventoryType.Folder;
  155. case "application/vnd.ll.rootfolder":
  156. return (sbyte)InventoryType.RootCategory;
  157. case "application/vnd.ll.lsltext":
  158. case "application/x-metaverse-lsl":
  159. case "application/vnd.ll.lslbyte":
  160. case "application/x-metaverse-lso":
  161. return (sbyte)InventoryType.LSL;
  162. case "application/vnd.ll.trashfolder":
  163. case "application/vnd.ll.snapshotfolder":
  164. case "application/vnd.ll.lostandfoundfolder":
  165. return (sbyte)InventoryType.Folder;
  166. case "application/vnd.ll.animation":
  167. case "application/x-metaverse-animation":
  168. return (sbyte)InventoryType.Animation;
  169. case "application/vnd.ll.gesture":
  170. case "application/x-metaverse-gesture":
  171. return (sbyte)InventoryType.Gesture;
  172. case "application/x-metaverse-simstate":
  173. return (sbyte)InventoryType.Snapshot;
  174. case "application/octet-stream":
  175. default:
  176. return (sbyte)InventoryType.Unknown;
  177. }
  178. }
  179. #endregion SL / file extension / content-type conversions
  180. /// <summary>
  181. /// Parse a notecard in Linden format to a string of ordinary text.
  182. /// </summary>
  183. /// <param name="rawInput"></param>
  184. /// <returns></returns>
  185. public static string ParseNotecardToString(string rawInput)
  186. {
  187. return string.Join("\n", ParseNotecardToList(rawInput).ToArray());
  188. }
  189. /// <summary>
  190. /// Parse a notecard in Linden format to a list of ordinary lines.
  191. /// </summary>
  192. /// <param name="rawInput"></param>
  193. /// <returns></returns>
  194. public static List<string> ParseNotecardToList(string rawInput)
  195. {
  196. string[] input = rawInput.Replace("\r", "").Split('\n');
  197. int idx = 0;
  198. int level = 0;
  199. List<string> output = new List<string>();
  200. string[] words;
  201. while (idx < input.Length)
  202. {
  203. if (input[idx] == "{")
  204. {
  205. level++;
  206. idx++;
  207. continue;
  208. }
  209. if (input[idx]== "}")
  210. {
  211. level--;
  212. idx++;
  213. continue;
  214. }
  215. switch (level)
  216. {
  217. case 0:
  218. words = input[idx].Split(' '); // Linden text ver
  219. // Notecards are created *really* empty. Treat that as "no text" (just like after saving an empty notecard)
  220. if (words.Length < 3)
  221. return output;
  222. int version = int.Parse(words[3]);
  223. if (version != 2)
  224. return output;
  225. break;
  226. case 1:
  227. words = input[idx].Split(' ');
  228. if (words[0] == "LLEmbeddedItems")
  229. break;
  230. if (words[0] == "Text")
  231. {
  232. int len = int.Parse(words[2]);
  233. idx++;
  234. int count = -1;
  235. while (count < len)
  236. {
  237. // int l = input[idx].Length;
  238. string ln = input[idx];
  239. int need = len-count-1;
  240. if (ln.Length > need)
  241. ln = ln.Substring(0, need);
  242. output.Add(ln);
  243. count += ln.Length + 1;
  244. idx++;
  245. }
  246. return output;
  247. }
  248. break;
  249. case 2:
  250. words = input[idx].Split(' '); // count
  251. if (words[0] == "count")
  252. {
  253. int c = int.Parse(words[1]);
  254. if (c > 0)
  255. return output;
  256. break;
  257. }
  258. break;
  259. }
  260. idx++;
  261. }
  262. return output;
  263. }
  264. }
  265. }