SLUtil.cs 16 KB

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