SLUtil.cs 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764
  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 OpenMetaverse;
  28. using System;
  29. using System.Collections.Generic;
  30. using System.Globalization;
  31. namespace OpenSim.Framework
  32. {
  33. public static class SLUtil
  34. {
  35. // private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  36. /// <summary>
  37. /// Asset types used only in OpenSim.
  38. /// To avoid clashing with the code numbers used in Second Life, use only negative numbers here.
  39. /// </summary>
  40. public enum OpenSimAssetType : sbyte
  41. {
  42. Material = -2
  43. }
  44. #region SL / file extension / content-type conversions
  45. /// <summary>
  46. /// Returns the Enum entry corresponding to the given code, regardless of whether it belongs
  47. /// to the AssetType or OpenSimAssetType enums.
  48. /// </summary>
  49. public static object AssetTypeFromCode(sbyte assetType)
  50. {
  51. if (Enum.IsDefined(typeof(OpenMetaverse.AssetType), assetType))
  52. return (OpenMetaverse.AssetType)assetType;
  53. else if (Enum.IsDefined(typeof(OpenSimAssetType), assetType))
  54. return (OpenSimAssetType)assetType;
  55. else
  56. return OpenMetaverse.AssetType.Unknown;
  57. }
  58. private class TypeMapping
  59. {
  60. private sbyte assetType;
  61. private sbyte inventoryType;
  62. private string contentType;
  63. private string contentType2;
  64. private string extension;
  65. public sbyte AssetTypeCode
  66. {
  67. get { return assetType; }
  68. }
  69. public object AssetType
  70. {
  71. get { return AssetTypeFromCode(assetType); }
  72. }
  73. public sbyte InventoryType
  74. {
  75. get { return inventoryType; }
  76. }
  77. public string ContentType
  78. {
  79. get { return contentType; }
  80. }
  81. public string ContentType2
  82. {
  83. get { return contentType2; }
  84. }
  85. public string Extension
  86. {
  87. get { return extension; }
  88. }
  89. private TypeMapping(sbyte assetType, sbyte inventoryType, string contentType, string contentType2, string extension)
  90. {
  91. this.assetType = assetType;
  92. this.inventoryType = inventoryType;
  93. this.contentType = contentType;
  94. this.contentType2 = contentType2;
  95. this.extension = extension;
  96. }
  97. public TypeMapping(AssetType assetType, sbyte inventoryType, string contentType, string contentType2, string extension)
  98. : this((sbyte)assetType, inventoryType, contentType, contentType2, extension)
  99. {
  100. }
  101. public TypeMapping(AssetType assetType, InventoryType inventoryType, string contentType, string contentType2, string extension)
  102. : this((sbyte)assetType, (sbyte)inventoryType, contentType, contentType2, extension)
  103. {
  104. }
  105. public TypeMapping(AssetType assetType, InventoryType inventoryType, string contentType, string extension)
  106. : this((sbyte)assetType, (sbyte)inventoryType, contentType, null, extension)
  107. {
  108. }
  109. public TypeMapping(AssetType assetType, FolderType inventoryType, string contentType, string extension)
  110. : this((sbyte)assetType, (sbyte)inventoryType, contentType, null, extension)
  111. {
  112. }
  113. public TypeMapping(OpenSimAssetType assetType, InventoryType inventoryType, string contentType, string extension)
  114. : this((sbyte)assetType, (sbyte)inventoryType, contentType, null, extension)
  115. {
  116. }
  117. }
  118. /// <summary>
  119. /// Maps between AssetType, InventoryType and Content-Type.
  120. /// Where more than one possibility exists, the first one takes precedence. E.g.:
  121. /// AssetType "AssetType.Texture" -> Content-Type "image-xj2c"
  122. /// Content-Type "image/x-j2c" -> InventoryType "InventoryType.Texture"
  123. /// </summary>
  124. private static TypeMapping[] MAPPINGS = new TypeMapping[] {
  125. new TypeMapping(AssetType.Unknown, InventoryType.Unknown, "application/octet-stream", "bin"),
  126. new TypeMapping(AssetType.Texture, InventoryType.Texture, "image/x-j2c", "image/jp2", "j2c"),
  127. new TypeMapping(AssetType.Texture, InventoryType.Snapshot, "image/x-j2c", "image/jp2", "j2c"),
  128. new TypeMapping(AssetType.TextureTGA, InventoryType.Texture, "image/tga", "tga"),
  129. new TypeMapping(AssetType.ImageTGA, InventoryType.Texture, "image/tga", "tga"),
  130. new TypeMapping(AssetType.ImageJPEG, InventoryType.Texture, "image/jpeg", "jpg"),
  131. new TypeMapping(AssetType.Sound, InventoryType.Sound, "audio/ogg", "application/ogg", "ogg"),
  132. new TypeMapping(AssetType.SoundWAV, InventoryType.Sound, "audio/x-wav", "wav"),
  133. new TypeMapping(AssetType.CallingCard, InventoryType.CallingCard, "application/vnd.ll.callingcard", "application/x-metaverse-callingcard", "callingcard"),
  134. new TypeMapping(AssetType.Landmark, InventoryType.Landmark, "application/vnd.ll.landmark", "application/x-metaverse-landmark", "landmark"),
  135. new TypeMapping(AssetType.Clothing, InventoryType.Wearable, "application/vnd.ll.clothing", "application/x-metaverse-clothing", "clothing"),
  136. new TypeMapping(AssetType.Object, InventoryType.Object, "application/vnd.ll.primitive", "application/x-metaverse-primitive", "primitive"),
  137. new TypeMapping(AssetType.Object, InventoryType.Attachment, "application/vnd.ll.primitive", "application/x-metaverse-primitive", "primitive"),
  138. new TypeMapping(AssetType.Notecard, InventoryType.Notecard, "application/vnd.ll.notecard", "application/x-metaverse-notecard", "notecard"),
  139. new TypeMapping(AssetType.LSLText, InventoryType.LSL, "application/vnd.ll.lsltext", "application/x-metaverse-lsl", "lsl"),
  140. new TypeMapping(AssetType.LSLBytecode, InventoryType.LSL, "application/vnd.ll.lslbyte", "application/x-metaverse-lso", "lso"),
  141. new TypeMapping(AssetType.Bodypart, InventoryType.Wearable, "application/vnd.ll.bodypart", "application/x-metaverse-bodypart", "bodypart"),
  142. new TypeMapping(AssetType.Animation, InventoryType.Animation, "application/vnd.ll.animation", "application/x-metaverse-animation", "animation"),
  143. new TypeMapping(AssetType.Gesture, InventoryType.Gesture, "application/vnd.ll.gesture", "application/x-metaverse-gesture", "gesture"),
  144. new TypeMapping(AssetType.Simstate, InventoryType.Snapshot, "application/x-metaverse-simstate", "simstate"),
  145. new TypeMapping(AssetType.Link, InventoryType.Unknown, "application/vnd.ll.link", "link"),
  146. new TypeMapping(AssetType.LinkFolder, InventoryType.Unknown, "application/vnd.ll.linkfolder", "linkfolder"),
  147. new TypeMapping(AssetType.Mesh, InventoryType.Mesh, "application/vnd.ll.mesh", "llm"),
  148. // The next few items are about inventory folders
  149. new TypeMapping(AssetType.Folder, FolderType.None, "application/vnd.ll.folder", "folder"),
  150. new TypeMapping(AssetType.Folder, FolderType.Root, "application/vnd.ll.rootfolder", "rootfolder"),
  151. new TypeMapping(AssetType.Folder, FolderType.Trash, "application/vnd.ll.trashfolder", "trashfolder"),
  152. new TypeMapping(AssetType.Folder, FolderType.Snapshot, "application/vnd.ll.snapshotfolder", "snapshotfolder"),
  153. new TypeMapping(AssetType.Folder, FolderType.LostAndFound, "application/vnd.ll.lostandfoundfolder", "lostandfoundfolder"),
  154. new TypeMapping(AssetType.Folder, FolderType.Favorites, "application/vnd.ll.favoritefolder", "favoritefolder"),
  155. new TypeMapping(AssetType.Folder, FolderType.CurrentOutfit, "application/vnd.ll.currentoutfitfolder", "currentoutfitfolder"),
  156. new TypeMapping(AssetType.Folder, FolderType.Outfit, "application/vnd.ll.outfitfolder", "outfitfolder"),
  157. new TypeMapping(AssetType.Folder, FolderType.MyOutfits, "application/vnd.ll.myoutfitsfolder", "myoutfitsfolder"),
  158. // This next mappping is an asset to inventory item mapping.
  159. // Note: LL stores folders as assets of type Folder = 8, and it has a corresponding InventoryType = 8
  160. // OpenSim doesn't store folders as assets, so this mapping should only be used when parsing things from the viewer to the server
  161. new TypeMapping(AssetType.Folder, InventoryType.Folder, "application/vnd.ll.folder", "folder"),
  162. // OpenSim specific
  163. new TypeMapping(OpenSimAssetType.Material, InventoryType.Unknown, "application/llsd+xml", "material")
  164. };
  165. private static Dictionary<sbyte, string> asset2Content;
  166. private static Dictionary<sbyte, string> asset2Extension;
  167. private static Dictionary<sbyte, string> inventory2Content;
  168. private static Dictionary<string, sbyte> content2Asset;
  169. private static Dictionary<string, sbyte> content2Inventory;
  170. private static Dictionary<string, AssetType> name2Asset = new Dictionary<string, AssetType>()
  171. {
  172. {"texture", AssetType.Texture },
  173. {"sound", AssetType.Sound},
  174. {"callcard", AssetType.CallingCard},
  175. {"landmark", AssetType.Landmark},
  176. {"script", (AssetType)4},
  177. {"clothing", AssetType.Clothing},
  178. {"object", AssetType.Object},
  179. {"notecard", AssetType.Notecard},
  180. {"category", AssetType.Folder},
  181. {"lsltext", AssetType.LSLText},
  182. {"lslbyte", AssetType.LSLBytecode},
  183. {"txtr_tga", AssetType.TextureTGA},
  184. {"bodypart", AssetType.Bodypart},
  185. {"snd_wav", AssetType.SoundWAV},
  186. {"img_tga", AssetType.ImageTGA},
  187. {"jpeg", AssetType.ImageJPEG},
  188. {"animatn", AssetType.Animation},
  189. {"gesture", AssetType.Gesture},
  190. {"simstate", AssetType.Simstate},
  191. {"mesh", AssetType.Mesh},
  192. {"settings", AssetType.Settings}
  193. };
  194. private static Dictionary<string, FolderType> name2Inventory = new Dictionary<string, FolderType>()
  195. {
  196. {"texture", FolderType.Texture},
  197. {"sound", FolderType.Sound},
  198. {"callcard", FolderType.CallingCard},
  199. {"landmark", FolderType.Landmark},
  200. {"script", (FolderType)4},
  201. {"clothing", FolderType.Clothing},
  202. {"object", FolderType.Object},
  203. {"notecard", FolderType.Notecard},
  204. {"root", FolderType.Root},
  205. {"lsltext", FolderType.LSLText},
  206. {"bodypart", FolderType.BodyPart},
  207. {"trash", FolderType.Trash},
  208. {"snapshot", FolderType.Snapshot},
  209. {"lostandfound", FolderType.LostAndFound},
  210. {"animatn", FolderType.Animation},
  211. {"gesture", FolderType.Gesture},
  212. {"favorites", FolderType.Favorites},
  213. {"currentoutfit", FolderType.CurrentOutfit},
  214. {"outfit", FolderType.Outfit},
  215. {"myoutfits", FolderType.MyOutfits},
  216. {"mesh", FolderType.Mesh},
  217. {"settings", FolderType.Settings},
  218. {"suitcase", FolderType.Suitcase}
  219. };
  220. static SLUtil()
  221. {
  222. asset2Content = new Dictionary<sbyte, string>();
  223. asset2Extension = new Dictionary<sbyte, string>();
  224. inventory2Content = new Dictionary<sbyte, string>();
  225. content2Asset = new Dictionary<string, sbyte>();
  226. content2Inventory = new Dictionary<string, sbyte>();
  227. foreach (TypeMapping mapping in MAPPINGS)
  228. {
  229. sbyte assetType = mapping.AssetTypeCode;
  230. if (!asset2Content.ContainsKey(assetType))
  231. asset2Content.Add(assetType, mapping.ContentType);
  232. if (!asset2Extension.ContainsKey(assetType))
  233. asset2Extension.Add(assetType, mapping.Extension);
  234. if (!inventory2Content.ContainsKey(mapping.InventoryType))
  235. inventory2Content.Add(mapping.InventoryType, mapping.ContentType);
  236. if (!content2Asset.ContainsKey(mapping.ContentType))
  237. content2Asset.Add(mapping.ContentType, assetType);
  238. if (!content2Inventory.ContainsKey(mapping.ContentType))
  239. content2Inventory.Add(mapping.ContentType, mapping.InventoryType);
  240. if (mapping.ContentType2 != null)
  241. {
  242. if (!content2Asset.ContainsKey(mapping.ContentType2))
  243. content2Asset.Add(mapping.ContentType2, assetType);
  244. if (!content2Inventory.ContainsKey(mapping.ContentType2))
  245. content2Inventory.Add(mapping.ContentType2, mapping.InventoryType);
  246. }
  247. }
  248. }
  249. public static AssetType SLAssetName2Type(string name)
  250. {
  251. if (name2Asset.TryGetValue(name, out AssetType type))
  252. return type;
  253. return AssetType.Unknown;
  254. }
  255. public static FolderType SLInvName2Type(string name)
  256. {
  257. if (name2Inventory.TryGetValue(name, out FolderType type))
  258. return type;
  259. return FolderType.None;
  260. }
  261. public static string SLAssetTypeToContentType(int assetType)
  262. {
  263. string contentType;
  264. if (!asset2Content.TryGetValue((sbyte)assetType, out contentType))
  265. contentType = asset2Content[(sbyte)AssetType.Unknown];
  266. return contentType;
  267. }
  268. public static string SLInvTypeToContentType(int invType)
  269. {
  270. string contentType;
  271. if (!inventory2Content.TryGetValue((sbyte)invType, out contentType))
  272. contentType = inventory2Content[(sbyte)InventoryType.Unknown];
  273. return contentType;
  274. }
  275. public static sbyte ContentTypeToSLAssetType(string contentType)
  276. {
  277. sbyte assetType;
  278. if (!content2Asset.TryGetValue(contentType, out assetType))
  279. assetType = (sbyte)AssetType.Unknown;
  280. return (sbyte)assetType;
  281. }
  282. public static sbyte ContentTypeToSLInvType(string contentType)
  283. {
  284. sbyte invType;
  285. if (!content2Inventory.TryGetValue(contentType, out invType))
  286. invType = (sbyte)InventoryType.Unknown;
  287. return (sbyte)invType;
  288. }
  289. public static string SLAssetTypeToExtension(int assetType)
  290. {
  291. string extension;
  292. if (!asset2Extension.TryGetValue((sbyte)assetType, out extension))
  293. extension = asset2Extension[(sbyte)AssetType.Unknown];
  294. return extension;
  295. }
  296. #endregion SL / file extension / content-type conversions
  297. static char[] seps = new char[] { '\t', '\n' };
  298. static char[] stringseps = new char[] { '|', '\n' };
  299. static byte[] moronize = new byte[16]
  300. {
  301. 60, 17, 94, 81, 4, 244, 82, 60, 159, 166, 152, 175, 241, 3, 71, 48
  302. };
  303. static int getField(string note, int start, string name, bool isString, out string value)
  304. {
  305. value = String.Empty;
  306. int end = -1;
  307. int limit = note.Length - start;
  308. if (limit > 64)
  309. limit = 64;
  310. int indx = note.IndexOf(name, start, limit);
  311. if (indx < 0)
  312. return -1;
  313. indx += name.Length + 1; // eat \t
  314. limit = note.Length - indx - 2;
  315. if (limit > 129)
  316. limit = 129;
  317. if (isString)
  318. end = note.IndexOfAny(stringseps, indx, limit);
  319. else
  320. end = note.IndexOfAny(seps, indx, limit);
  321. if (end < 0)
  322. return -1;
  323. value = note.Substring(indx, end - indx);
  324. return end;
  325. }
  326. private static UUID deMoronize(UUID id)
  327. {
  328. byte[] data = new byte[16];
  329. id.ToBytes(data,0);
  330. for(int i = 0; i < 16; ++i)
  331. data[i] ^= moronize[i];
  332. return new UUID(data,0);
  333. }
  334. public static InventoryItemBase GetEmbeddedItem(byte[] data, UUID itemID)
  335. {
  336. if(data == null || data.Length < 300)
  337. return null;
  338. string note = Util.UTF8.GetString(data);
  339. if (String.IsNullOrWhiteSpace(note))
  340. return null;
  341. // waste some time checking rigid versions
  342. string version = note.Substring(0,21);
  343. if (!version.Equals("Linden text version 2"))
  344. return null;
  345. version = note.Substring(24, 25);
  346. if (!version.Equals("LLEmbeddedItems version 1"))
  347. return null;
  348. int indx = note.IndexOf(itemID.ToString(), 100);
  349. if (indx < 0)
  350. return null;
  351. indx = note.IndexOf("permissions", indx, 100); // skip parentID
  352. if (indx < 0)
  353. return null;
  354. string valuestr;
  355. indx = getField(note, indx, "base_mask", false, out valuestr);
  356. if (indx < 0)
  357. return null;
  358. if (!uint.TryParse(valuestr, NumberStyles.HexNumber, Culture.NumberFormatInfo, out uint basemask))
  359. return null;
  360. indx = getField(note, indx, "owner_mask", false, out valuestr);
  361. if (indx < 0)
  362. return null;
  363. if (!uint.TryParse(valuestr, NumberStyles.HexNumber, Culture.NumberFormatInfo, out uint ownermask))
  364. return null;
  365. indx = getField(note, indx, "group_mask", false, out valuestr);
  366. if (indx < 0)
  367. return null;
  368. if (!uint.TryParse(valuestr, NumberStyles.HexNumber, Culture.NumberFormatInfo, out uint groupmask))
  369. return null;
  370. indx = getField(note, indx, "everyone_mask", false, out valuestr);
  371. if (indx < 0)
  372. return null;
  373. if (!uint.TryParse(valuestr, NumberStyles.HexNumber, Culture.NumberFormatInfo, out uint everyonemask))
  374. return null;
  375. indx = getField(note, indx, "next_owner_mask", false, out valuestr);
  376. if (indx < 0)
  377. return null;
  378. if (!uint.TryParse(valuestr, NumberStyles.HexNumber, Culture.NumberFormatInfo, out uint nextownermask))
  379. return null;
  380. indx = getField(note, indx, "creator_id", false, out valuestr);
  381. if (indx < 0)
  382. return null;
  383. if (!UUID.TryParse(valuestr, out UUID creatorID))
  384. return null;
  385. indx = getField(note, indx, "owner_id", false, out valuestr);
  386. if (indx < 0)
  387. return null;
  388. if (!UUID.TryParse(valuestr, out UUID ownerID))
  389. return null;
  390. int limit = note.Length - indx;
  391. if (limit > 120)
  392. limit = 120;
  393. indx = note.IndexOf('}', indx, limit); // last owner
  394. if (indx < 0)
  395. return null;
  396. int curindx = indx;
  397. UUID assetID = UUID.Zero;
  398. indx = getField(note, indx, "asset_id", false, out valuestr);
  399. if (indx < 0)
  400. {
  401. indx = getField(note, curindx, "shadow_id", false, out valuestr);
  402. if (indx < 0)
  403. return null;
  404. if (!UUID.TryParse(valuestr, out assetID))
  405. return null;
  406. assetID = deMoronize(assetID);
  407. }
  408. else
  409. {
  410. if (!UUID.TryParse(valuestr, out assetID))
  411. return null;
  412. }
  413. indx = getField(note, indx, "type", false, out valuestr);
  414. if (indx < 0)
  415. return null;
  416. AssetType assetType = SLAssetName2Type(valuestr);
  417. indx = getField(note, indx, "inv_type", false, out valuestr);
  418. if (indx < 0)
  419. return null;
  420. FolderType invType = SLInvName2Type(valuestr);
  421. indx = getField(note, indx, "flags", false, out valuestr);
  422. if (indx < 0)
  423. return null;
  424. if (!uint.TryParse(valuestr, NumberStyles.HexNumber, Culture.NumberFormatInfo, out uint flags))
  425. return null;
  426. limit = note.Length - indx;
  427. if (limit > 120)
  428. limit = 120;
  429. indx = note.IndexOf('}', indx, limit); // skip sale
  430. if (indx < 0)
  431. return null;
  432. indx = getField(note, indx, "name", true, out valuestr);
  433. if (indx < 0)
  434. return null;
  435. string name = valuestr;
  436. indx = getField(note, indx, "desc", true, out valuestr);
  437. if (indx < 0)
  438. return null;
  439. string desc = valuestr;
  440. InventoryItemBase item = new InventoryItemBase();
  441. item.AssetID = assetID;
  442. item.AssetType = (sbyte)assetType;
  443. item.BasePermissions = basemask;
  444. item.CreationDate = Util.UnixTimeSinceEpoch();
  445. item.CreatorData = "";
  446. item.CreatorId = creatorID.ToString();
  447. item.CurrentPermissions = ownermask;
  448. item.Description = desc;
  449. item.Flags = flags;
  450. item.Folder = UUID.Zero;
  451. item.GroupID = UUID.Zero;
  452. item.GroupOwned = false;
  453. item.GroupPermissions = groupmask;
  454. item.InvType = (sbyte)invType;
  455. item.Name = name;
  456. item.NextPermissions = nextownermask;
  457. item.Owner = ownerID;
  458. item.SalePrice = 0;
  459. item.SaleType = (byte)SaleType.Not;
  460. item.ID = UUID.Random();
  461. return item;
  462. }
  463. public static List<UUID> GetEmbeddedAssetIDs(byte[] data)
  464. {
  465. if (data == null || data.Length < 79)
  466. return null;
  467. string note = Util.UTF8.GetString(data);
  468. if (String.IsNullOrWhiteSpace(note))
  469. return null;
  470. // waste some time checking rigid versions
  471. string tmpStr = note.Substring(0, 21);
  472. if (!tmpStr.Equals("Linden text version 2"))
  473. return null;
  474. tmpStr = note.Substring(24, 25);
  475. if (!tmpStr.Equals("LLEmbeddedItems version 1"))
  476. return null;
  477. tmpStr = note.Substring(52,5);
  478. if (!tmpStr.Equals("count"))
  479. return null;
  480. int limit = note.Length - 57 - 2;
  481. if (limit > 8)
  482. limit = 8;
  483. int indx = note.IndexOfAny(seps, 57, limit);
  484. if(indx < 0)
  485. return null;
  486. if (!int.TryParse(note.Substring(57, indx - 57), out int count))
  487. return null;
  488. List<UUID> ids = new List<UUID>();
  489. while(count > 0)
  490. {
  491. string valuestr;
  492. UUID assetID = UUID.Zero;
  493. indx = note.IndexOf('}',indx); // skip to end of permissions
  494. if (indx < 0)
  495. return null;
  496. int curindx = indx;
  497. indx = getField(note, indx, "asset_id", false, out valuestr);
  498. if (indx < 0)
  499. {
  500. indx = getField(note, curindx, "shadow_id", false, out valuestr);
  501. if (indx < 0)
  502. return null;
  503. if (!UUID.TryParse(valuestr, out assetID))
  504. return null;
  505. assetID = deMoronize(assetID);
  506. }
  507. else
  508. {
  509. if (!UUID.TryParse(valuestr, out assetID))
  510. return null;
  511. }
  512. ids.Add(assetID);
  513. indx = note.IndexOf('}', indx); // skip to end of sale
  514. if (indx < 0)
  515. return null;
  516. indx = getField(note, indx, "name", false, out valuestr); // avoid name contents
  517. if (indx < 0)
  518. return null;
  519. indx = getField(note, indx, "desc", false, out valuestr); // avoid desc contents
  520. if (indx < 0)
  521. return null;
  522. if(count > 1)
  523. {
  524. indx = note.IndexOf("ext char index", indx); // skip to next
  525. if (indx < 0)
  526. return null;
  527. }
  528. --count;
  529. }
  530. indx = note.IndexOf("Text length",indx);
  531. if(indx > 0)
  532. {
  533. indx += 14;
  534. List<UUID> textIDs = Util.GetUUIDsOnString(ref note, indx, note.Length - indx);
  535. if(textIDs.Count > 0)
  536. ids.AddRange(textIDs);
  537. }
  538. if (ids.Count == 0)
  539. return null;
  540. return ids;
  541. }
  542. /// <summary>
  543. /// Parse a notecard in Linden format to a list of ordinary lines for LSL
  544. /// </summary>
  545. /// <param name="rawInput"></param>
  546. /// <returns></returns>
  547. public static string[] ParseNotecardToArray(byte[] data)
  548. {
  549. // check of a valid notecard
  550. if (data == null || data.Length < 79)
  551. return new string[0];
  552. //LSL can't read notecards with embedded items
  553. if (data[58] != '0' || data[59] != '\n')
  554. return new string[0];
  555. string note = Util.UTF8.GetString(data);
  556. if (String.IsNullOrWhiteSpace(note))
  557. return new string[0];
  558. // waste some time checking rigid versions
  559. string tmpStr = note.Substring(0, 21);
  560. if (!tmpStr.Equals("Linden text version 2"))
  561. return new string[0];
  562. tmpStr = note.Substring(24, 25);
  563. if (!tmpStr.Equals("LLEmbeddedItems version 1"))
  564. return new string[0];
  565. tmpStr = note.Substring(52, 5);
  566. if (!tmpStr.Equals("count"))
  567. return new string[0];
  568. int indx = note.IndexOf("Text length", 60);
  569. if(indx < 0)
  570. return new string[0];
  571. indx += 12;
  572. int end = indx + 1;
  573. for (; end < note.Length && note[end] != '\n'; ++end);
  574. if (note[end] != '\n')
  575. return new string[0];
  576. tmpStr = note.Substring(indx, end - indx);
  577. if (!int.TryParse(tmpStr, out int textLen) || textLen == 0)
  578. return new string[0];
  579. indx = end + 1;
  580. if (textLen + indx > data.Length)
  581. return new string[0];
  582. // yeackk
  583. note = Util.UTF8.GetString(data, indx, textLen);
  584. textLen = note.Length;
  585. indx = 0;
  586. var lines = new List<string>();
  587. while (indx < textLen)
  588. {
  589. end = indx;
  590. for (; end < textLen && note[end] != '\n'; ++end);
  591. if(end == indx)
  592. lines.Add(String.Empty);
  593. else
  594. lines.Add(note.Substring(indx, end - indx));
  595. indx = end + 1;
  596. }
  597. // notes only seem to have one text section
  598. if(lines.Count == 0)
  599. return new string[0];
  600. return lines.ToArray();
  601. }
  602. // libomv has old names on ATTACH_LEFT_PEC and ATTACH_RIGHT_PEC
  603. public static readonly string[] AttachmentPointNames = new string[]
  604. {
  605. string.Empty,
  606. "ATTACH_CHEST", // 1
  607. "ATTACH_HEAD", // 2
  608. "ATTACH_LSHOULDER", // 3
  609. "ATTACH_RSHOULDER", // 4
  610. "ATTACH_LHAND", // 5
  611. "ATTACH_RHAND", // 6
  612. "ATTACH_LFOOT", // 7
  613. "ATTACH_RFOOT", // 8
  614. "ATTACH_BACK", // 9
  615. "ATTACH_PELVIS", // 10
  616. "ATTACH_MOUTH", // 11
  617. "ATTACH_CHIN", // 12
  618. "ATTACH_LEAR", // 13
  619. "ATTACH_REAR", // 14
  620. "ATTACH_LEYE", // 15
  621. "ATTACH_REYE", // 16
  622. "ATTACH_NOSE", // 17
  623. "ATTACH_RUARM", // 18
  624. "ATTACH_RLARM", // 19
  625. "ATTACH_LUARM", // 20
  626. "ATTACH_LLARM", // 21
  627. "ATTACH_RHIP", // 22
  628. "ATTACH_RULEG", // 23
  629. "ATTACH_RLLEG", // 24
  630. "ATTACH_LHIP", // 25
  631. "ATTACH_LULEG", // 26
  632. "ATTACH_LLLEG", // 27
  633. "ATTACH_BELLY", // 28
  634. "ATTACH_LEFT_PEC", // 29
  635. "ATTACH_RIGHT_PEC", // 30
  636. "ATTACH_HUD_CENTER_2", // 31
  637. "ATTACH_HUD_TOP_RIGHT", // 32
  638. "ATTACH_HUD_TOP_CENTER", // 33
  639. "ATTACH_HUD_TOP_LEFT", // 34
  640. "ATTACH_HUD_CENTER_1", // 35
  641. "ATTACH_HUD_BOTTOM_LEFT", // 36
  642. "ATTACH_HUD_BOTTOM", // 37
  643. "ATTACH_HUD_BOTTOM_RIGHT", // 38
  644. "ATTACH_NECK", // 39
  645. "ATTACH_AVATAR_CENTER", // 40
  646. "ATTACH_LHAND_RING1", // 41
  647. "ATTACH_RHAND_RING1", // 42
  648. "ATTACH_TAIL_BASE", // 43
  649. "ATTACH_TAIL_TIP", // 44
  650. "ATTACH_LWING", // 45
  651. "ATTACH_RWING", // 46
  652. "ATTACH_FACE_JAW", // 47
  653. "ATTACH_FACE_LEAR", // 48
  654. "ATTACH_FACE_REAR", // 49
  655. "ATTACH_FACE_LEYE", // 50
  656. "ATTACH_FACE_REYE", // 51
  657. "ATTACH_FACE_TONGUE", // 52
  658. "ATTACH_GROIN", // 53
  659. "ATTACH_HIND_LFOOT", // 54
  660. "ATTACH_HIND_RFOOT" // 55
  661. };
  662. public static string GetAttachmentName(int point)
  663. {
  664. if(point < AttachmentPointNames.Length)
  665. return AttachmentPointNames[point];
  666. return "Unknown";
  667. }
  668. }
  669. }