1
0

SLUtil.cs 18 KB

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