SLUtil.cs 16 KB

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