SLUtil.cs 30 KB

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