AvatarCreationModule.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  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;
  29. using System.Collections.Generic;
  30. using System.Reflection;
  31. using System.Threading;
  32. using log4net;
  33. using Nwc.XmlRpc;
  34. using OpenMetaverse;
  35. using OpenSim.Framework;
  36. using OpenSim.Framework.Console;
  37. using OpenSim.Framework.Communications;
  38. using OpenSim.Framework.Servers;
  39. using OpenSim.Framework.Servers.HttpServer;
  40. using OpenSim.Grid.Framework;
  41. namespace OpenSim.Grid.UserServer.Modules
  42. {
  43. public class AvatarCreationModule
  44. {
  45. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  46. private UserDataBaseService m_userDataBaseService;
  47. // private BaseHttpServer m_httpServer;
  48. // TODO: unused: private UserConfig m_config;
  49. private string m_inventoryServerUrl;
  50. private IInterServiceInventoryServices m_inventoryService;
  51. public AvatarCreationModule(UserDataBaseService userDataBaseService, UserConfig config, IInterServiceInventoryServices inventoryService)
  52. {
  53. // TODO: unused: m_config = config;
  54. m_userDataBaseService = userDataBaseService;
  55. m_inventoryService = inventoryService;
  56. m_inventoryServerUrl = config.InventoryUrl.OriginalString;
  57. }
  58. public void Initialise(IGridServiceCore core)
  59. {
  60. CommandConsole console;
  61. if (core.TryGet<CommandConsole>(out console))
  62. {
  63. console.Commands.AddCommand("userserver", false, "clone avatar",
  64. "clone avatar <TemplateAvatarFirstName> <TemplateAvatarLastName> <TargetAvatarFirstName> <TargetAvatarLastName>",
  65. "Clone the template avatar's inventory into a target avatar", RunCommand);
  66. }
  67. }
  68. public void PostInitialise()
  69. {
  70. }
  71. public void RegisterHandlers(BaseHttpServer httpServer)
  72. {
  73. }
  74. public void RunCommand(string module, string[] cmd)
  75. {
  76. if ((cmd.Length == 6) && (cmd[0] == "clone") && (cmd[1] == "avatar"))
  77. {
  78. try
  79. {
  80. string tFirst = cmd[2];
  81. string tLast = cmd[3];
  82. string nFirst = cmd[4];
  83. string nLast = cmd[5];
  84. UserProfileData templateAvatar = m_userDataBaseService.GetUserProfile(tFirst, tLast);
  85. UserProfileData newAvatar = m_userDataBaseService.GetUserProfile(nFirst, nLast);
  86. if (templateAvatar == null)
  87. {
  88. m_log.ErrorFormat("[AvatarAppearance] Clone Avatar: Could not find template avatar {0} , {1}", tFirst, tLast);
  89. return;
  90. }
  91. if (newAvatar == null)
  92. {
  93. m_log.ErrorFormat("[AvatarAppearance] Clone Avatar: Could not find target avatar {0} , {1}", nFirst, nLast);
  94. return;
  95. }
  96. Guid avatar = newAvatar.ID.Guid;
  97. Guid template = templateAvatar.ID.Guid;
  98. CloneAvatar(avatar, template, true, true);
  99. }
  100. catch (Exception e)
  101. {
  102. m_log.Error("Error: " + e.ToString());
  103. }
  104. }
  105. }
  106. #region Avatar Appearance Creation
  107. public bool CloneAvatar(Guid avatarID, Guid templateID, bool modifyPermissions, bool removeTargetsClothes)
  108. {
  109. m_log.InfoFormat("[AvatarAppearance] Starting to clone avatar {0} inventory to avatar {1}", templateID.ToString(), avatarID.ToString());
  110. // TODO: unused: Guid bodyFolder = Guid.Empty;
  111. // TODO: unused: Guid clothesFolder = Guid.Empty;
  112. bool success = false;
  113. UUID avID = new UUID(avatarID);
  114. List<InventoryFolderBase> avatarInventory = m_inventoryService.GetInventorySkeleton(avID);
  115. if ((avatarInventory == null) || (avatarInventory.Count == 0))
  116. {
  117. m_log.InfoFormat("[AvatarAppearance] No inventory found for user {0} , so creating it", avID.ToString());
  118. m_inventoryService.CreateNewUserInventory(avID);
  119. Thread.Sleep(5000);
  120. avatarInventory = m_inventoryService.GetInventorySkeleton(avID);
  121. }
  122. if ((avatarInventory != null) && (avatarInventory.Count > 0))
  123. {
  124. UUID tempOwnID = new UUID(templateID);
  125. AvatarAppearance appearance = m_userDataBaseService.GetUserAppearance(tempOwnID);
  126. if (removeTargetsClothes)
  127. {
  128. //remove clothes and attachments from target avatar so that the end result isn't a merger of its existing clothes
  129. // and the clothes from the template avatar.
  130. RemoveClothesAndAttachments(avID);
  131. }
  132. List<InventoryFolderBase> templateInventory = m_inventoryService.GetInventorySkeleton(tempOwnID);
  133. if ((templateInventory != null) && (templateInventory.Count != 0))
  134. {
  135. for (int i = 0; i < templateInventory.Count; i++)
  136. {
  137. if (templateInventory[i].ParentID == UUID.Zero)
  138. {
  139. success = CloneFolder(avatarInventory, avID, UUID.Zero, appearance, templateInventory[i], templateInventory, modifyPermissions);
  140. break;
  141. }
  142. }
  143. }
  144. else
  145. {
  146. m_log.InfoFormat("[AvatarAppearance] Failed to find the template owner's {0} inventory", tempOwnID);
  147. }
  148. }
  149. m_log.InfoFormat("[AvatarAppearance] finished cloning avatar with result: {0}", success);
  150. return success;
  151. }
  152. private bool CloneFolder(List<InventoryFolderBase> avatarInventory, UUID avID, UUID parentFolder, AvatarAppearance appearance, InventoryFolderBase templateFolder, List<InventoryFolderBase> templateFolders, bool modifyPermissions)
  153. {
  154. bool success = false;
  155. UUID templateFolderId = templateFolder.ID;
  156. if (templateFolderId != UUID.Zero)
  157. {
  158. InventoryFolderBase toFolder = FindFolder(templateFolder.Name, parentFolder.Guid, avatarInventory);
  159. if (toFolder == null)
  160. {
  161. //create new folder
  162. toFolder = new InventoryFolderBase();
  163. toFolder.ID = UUID.Random();
  164. toFolder.Name = templateFolder.Name;
  165. toFolder.Owner = avID;
  166. toFolder.Type = templateFolder.Type;
  167. toFolder.Version = 1;
  168. toFolder.ParentID = parentFolder;
  169. if (!SynchronousRestObjectRequester.MakeRequest<InventoryFolderBase, bool>(
  170. "POST", m_inventoryServerUrl + "CreateFolder/", toFolder))
  171. {
  172. m_log.InfoFormat("[AvatarApperance] Couldn't make new folder {0} in users inventory", toFolder.Name);
  173. return false;
  174. }
  175. else
  176. {
  177. // m_log.InfoFormat("made new folder {0} in users inventory", toFolder.Name);
  178. }
  179. }
  180. List<InventoryItemBase> templateItems = SynchronousRestObjectRequester.MakeRequest<Guid, List<InventoryItemBase>>(
  181. "POST", m_inventoryServerUrl + "GetItems/", templateFolderId.Guid);
  182. if ((templateItems != null) && (templateItems.Count > 0))
  183. {
  184. List<ClothesAttachment> wornClothes = new List<ClothesAttachment>();
  185. List<ClothesAttachment> attachedItems = new List<ClothesAttachment>();
  186. foreach (InventoryItemBase item in templateItems)
  187. {
  188. UUID clonedItemId = CloneInventoryItem(avID, toFolder.ID, item, modifyPermissions);
  189. if (clonedItemId != UUID.Zero)
  190. {
  191. int appearanceType = ItemIsPartOfAppearance(item, appearance);
  192. if (appearanceType >= 0)
  193. {
  194. // UpdateAvatarAppearance(avID, appearanceType, clonedItemId, item.AssetID);
  195. wornClothes.Add(new ClothesAttachment(appearanceType, clonedItemId, item.AssetID));
  196. }
  197. if (appearance != null)
  198. {
  199. int attachment = appearance.GetAttachpoint(item.ID);
  200. if (attachment > 0)
  201. {
  202. //UpdateAvatarAttachment(avID, attachment, clonedItemId, item.AssetID);
  203. attachedItems.Add(new ClothesAttachment(attachment, clonedItemId, item.AssetID));
  204. }
  205. }
  206. success = true;
  207. }
  208. }
  209. if ((wornClothes.Count > 0) || (attachedItems.Count > 0))
  210. {
  211. //Update the worn clothes and attachments
  212. AvatarAppearance targetAppearance = GetAppearance(avID);
  213. if (targetAppearance != null)
  214. {
  215. foreach (ClothesAttachment wornItem in wornClothes)
  216. {
  217. targetAppearance.Wearables[wornItem.Type].AssetID = wornItem.AssetID;
  218. targetAppearance.Wearables[wornItem.Type].ItemID = wornItem.ItemID;
  219. }
  220. foreach (ClothesAttachment wornItem in attachedItems)
  221. {
  222. targetAppearance.SetAttachment(wornItem.Type, wornItem.ItemID, wornItem.AssetID);
  223. }
  224. m_userDataBaseService.UpdateUserAppearance(avID, targetAppearance);
  225. wornClothes.Clear();
  226. attachedItems.Clear();
  227. }
  228. }
  229. }
  230. List<InventoryFolderBase> subFolders = FindSubFolders(templateFolder.ID.Guid, templateFolders);
  231. foreach (InventoryFolderBase subFolder in subFolders)
  232. {
  233. if (subFolder.Name.ToLower() != "trash")
  234. {
  235. success = CloneFolder(avatarInventory, avID, toFolder.ID, appearance, subFolder, templateFolders, modifyPermissions);
  236. }
  237. }
  238. }
  239. else
  240. {
  241. m_log.Info("[AvatarAppearance] Failed to find the template folder");
  242. }
  243. return success;
  244. }
  245. private UUID CloneInventoryItem(UUID avatarID, UUID avatarFolder, InventoryItemBase item, bool modifyPerms)
  246. {
  247. if (avatarFolder != UUID.Zero)
  248. {
  249. InventoryItemBase clonedItem = new InventoryItemBase();
  250. clonedItem.Owner = avatarID;
  251. clonedItem.AssetID = item.AssetID;
  252. clonedItem.AssetType = item.AssetType;
  253. clonedItem.BasePermissions = item.BasePermissions;
  254. clonedItem.CreationDate = item.CreationDate;
  255. clonedItem.CreatorId = item.CreatorId;
  256. clonedItem.CreatorIdAsUuid = item.CreatorIdAsUuid;
  257. clonedItem.CurrentPermissions = item.CurrentPermissions;
  258. clonedItem.Description = item.Description;
  259. clonedItem.EveryOnePermissions = item.EveryOnePermissions;
  260. clonedItem.Flags = item.Flags;
  261. clonedItem.Folder = avatarFolder;
  262. clonedItem.GroupID = item.GroupID;
  263. clonedItem.GroupOwned = item.GroupOwned;
  264. clonedItem.GroupPermissions = item.GroupPermissions;
  265. clonedItem.ID = UUID.Random();
  266. clonedItem.InvType = item.InvType;
  267. clonedItem.Name = item.Name;
  268. clonedItem.NextPermissions = item.NextPermissions;
  269. clonedItem.SalePrice = item.SalePrice;
  270. clonedItem.SaleType = item.SaleType;
  271. if (modifyPerms)
  272. {
  273. ModifyPermissions(ref clonedItem);
  274. }
  275. SynchronousRestObjectRequester.MakeRequest<InventoryItemBase, bool>(
  276. "POST", m_inventoryServerUrl + "AddNewItem/", clonedItem);
  277. return clonedItem.ID;
  278. }
  279. return UUID.Zero;
  280. }
  281. // TODO: unused
  282. // private void UpdateAvatarAppearance(UUID avatarID, int wearableType, UUID itemID, UUID assetID)
  283. // {
  284. // AvatarAppearance appearance = GetAppearance(avatarID);
  285. // appearance.Wearables[wearableType].AssetID = assetID;
  286. // appearance.Wearables[wearableType].ItemID = itemID;
  287. // m_userDataBaseService.UpdateUserAppearance(avatarID, appearance);
  288. // }
  289. // TODO: unused
  290. // private void UpdateAvatarAttachment(UUID avatarID, int attachmentPoint, UUID itemID, UUID assetID)
  291. // {
  292. // AvatarAppearance appearance = GetAppearance(avatarID);
  293. // appearance.SetAttachment(attachmentPoint, itemID, assetID);
  294. // m_userDataBaseService.UpdateUserAppearance(avatarID, appearance);
  295. // }
  296. private void RemoveClothesAndAttachments(UUID avatarID)
  297. {
  298. AvatarAppearance appearance = GetAppearance(avatarID);
  299. appearance.ClearWearables();
  300. appearance.ClearAttachments();
  301. m_userDataBaseService.UpdateUserAppearance(avatarID, appearance);
  302. }
  303. private AvatarAppearance GetAppearance(UUID avatarID)
  304. {
  305. AvatarAppearance appearance = m_userDataBaseService.GetUserAppearance(avatarID);
  306. if (appearance == null)
  307. {
  308. appearance = CreateDefaultAppearance(avatarID);
  309. }
  310. return appearance;
  311. }
  312. // TODO: unused
  313. // private UUID FindFolderID(string name, List<InventoryFolderBase> folders)
  314. // {
  315. // foreach (InventoryFolderBase folder in folders)
  316. // {
  317. // if (folder.Name == name)
  318. // {
  319. // return folder.ID;
  320. // }
  321. // }
  322. // return UUID.Zero;
  323. // }
  324. // TODO: unused
  325. // private InventoryFolderBase FindFolder(string name, List<InventoryFolderBase> folders)
  326. // {
  327. // foreach (InventoryFolderBase folder in folders)
  328. // {
  329. // if (folder.Name == name)
  330. // {
  331. // return folder;
  332. // }
  333. // }
  334. // return null;
  335. // }
  336. private InventoryFolderBase FindFolder(string name, Guid parentFolderID, List<InventoryFolderBase> folders)
  337. {
  338. foreach (InventoryFolderBase folder in folders)
  339. {
  340. if ((folder.Name == name) && (folder.ParentID.Guid == parentFolderID))
  341. {
  342. return folder;
  343. }
  344. }
  345. return null;
  346. }
  347. // TODO: unused
  348. // private InventoryItemBase GetItem(string itemName, List<InventoryItemBase> items)
  349. // {
  350. // foreach (InventoryItemBase item in items)
  351. // {
  352. // if (item.Name.ToLower() == itemName.ToLower())
  353. // {
  354. // return item;
  355. // }
  356. // }
  357. // return null;
  358. // }
  359. private List<InventoryFolderBase> FindSubFolders(Guid parentFolderID, List<InventoryFolderBase> folders)
  360. {
  361. List<InventoryFolderBase> subFolders = new List<InventoryFolderBase>();
  362. foreach (InventoryFolderBase folder in folders)
  363. {
  364. if (folder.ParentID.Guid == parentFolderID)
  365. {
  366. subFolders.Add(folder);
  367. }
  368. }
  369. return subFolders;
  370. }
  371. protected virtual void ModifyPermissions(ref InventoryItemBase item)
  372. {
  373. // Propagate Permissions
  374. item.BasePermissions = item.BasePermissions & item.NextPermissions;
  375. item.CurrentPermissions = item.BasePermissions;
  376. item.EveryOnePermissions = item.EveryOnePermissions & item.NextPermissions;
  377. item.GroupPermissions = item.GroupPermissions & item.NextPermissions;
  378. }
  379. private AvatarAppearance CreateDefaultAppearance(UUID avatarId)
  380. {
  381. AvatarAppearance appearance = null;
  382. AvatarWearable[] wearables;
  383. byte[] visualParams;
  384. GetDefaultAvatarAppearance(out wearables, out visualParams);
  385. appearance = new AvatarAppearance(avatarId, wearables, visualParams);
  386. return appearance;
  387. }
  388. private static void GetDefaultAvatarAppearance(out AvatarWearable[] wearables, out byte[] visualParams)
  389. {
  390. visualParams = GetDefaultVisualParams();
  391. wearables = AvatarWearable.DefaultWearables;
  392. }
  393. private static byte[] GetDefaultVisualParams()
  394. {
  395. byte[] visualParams;
  396. visualParams = new byte[218];
  397. for (int i = 0; i < 218; i++)
  398. {
  399. visualParams[i] = 100;
  400. }
  401. return visualParams;
  402. }
  403. private int ItemIsPartOfAppearance(InventoryItemBase item, AvatarAppearance appearance)
  404. {
  405. if (appearance != null)
  406. {
  407. if (appearance.BodyItem == item.ID)
  408. return (int)WearableType.Shape;
  409. if (appearance.EyesItem == item.ID)
  410. return (int)WearableType.Eyes;
  411. if (appearance.GlovesItem == item.ID)
  412. return (int)WearableType.Gloves;
  413. if (appearance.HairItem == item.ID)
  414. return (int)WearableType.Hair;
  415. if (appearance.JacketItem == item.ID)
  416. return (int)WearableType.Jacket;
  417. if (appearance.PantsItem == item.ID)
  418. return (int)WearableType.Pants;
  419. if (appearance.ShirtItem == item.ID)
  420. return (int)WearableType.Shirt;
  421. if (appearance.ShoesItem == item.ID)
  422. return (int)WearableType.Shoes;
  423. if (appearance.SkinItem == item.ID)
  424. return (int)WearableType.Skin;
  425. if (appearance.SkirtItem == item.ID)
  426. return (int)WearableType.Skirt;
  427. if (appearance.SocksItem == item.ID)
  428. return (int)WearableType.Socks;
  429. if (appearance.UnderPantsItem == item.ID)
  430. return (int)WearableType.Underpants;
  431. if (appearance.UnderShirtItem == item.ID)
  432. return (int)WearableType.Undershirt;
  433. }
  434. return -1;
  435. }
  436. #endregion
  437. public enum PermissionMask
  438. {
  439. None = 0,
  440. Transfer = 8192,
  441. Modify = 16384,
  442. Copy = 32768,
  443. Move = 524288,
  444. Damage = 1048576,
  445. All = 2147483647,
  446. }
  447. public enum WearableType
  448. {
  449. Shape = 0,
  450. Skin = 1,
  451. Hair = 2,
  452. Eyes = 3,
  453. Shirt = 4,
  454. Pants = 5,
  455. Shoes = 6,
  456. Socks = 7,
  457. Jacket = 8,
  458. Gloves = 9,
  459. Undershirt = 10,
  460. Underpants = 11,
  461. Skirt = 12,
  462. }
  463. public class ClothesAttachment
  464. {
  465. public int Type;
  466. public UUID ItemID;
  467. public UUID AssetID;
  468. public ClothesAttachment(int type, UUID itemID, UUID assetID)
  469. {
  470. Type = type;
  471. ItemID = itemID;
  472. AssetID = assetID;
  473. }
  474. }
  475. }
  476. }