SLUtil.cs 16 KB

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