1
0

AvatarCreationModule.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  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. else if ((templateItems != null) && (templateItems.Count == 0))
  231. {
  232. // m_log.Info("[AvatarAppearance]Folder being cloned was empty");
  233. success = true;
  234. }
  235. List<InventoryFolderBase> subFolders = FindSubFolders(templateFolder.ID.Guid, templateFolders);
  236. foreach (InventoryFolderBase subFolder in subFolders)
  237. {
  238. if (subFolder.Name.ToLower() != "trash")
  239. {
  240. success = CloneFolder(avatarInventory, avID, toFolder.ID, appearance, subFolder, templateFolders, modifyPermissions);
  241. }
  242. }
  243. }
  244. else
  245. {
  246. m_log.Info("[AvatarAppearance] Failed to find the template folder");
  247. }
  248. return success;
  249. }
  250. private UUID CloneInventoryItem(UUID avatarID, UUID avatarFolder, InventoryItemBase item, bool modifyPerms)
  251. {
  252. if (avatarFolder != UUID.Zero)
  253. {
  254. InventoryItemBase clonedItem = new InventoryItemBase();
  255. clonedItem.Owner = avatarID;
  256. clonedItem.AssetID = item.AssetID;
  257. clonedItem.AssetType = item.AssetType;
  258. clonedItem.BasePermissions = item.BasePermissions;
  259. clonedItem.CreationDate = item.CreationDate;
  260. clonedItem.CreatorId = item.CreatorId;
  261. clonedItem.CreatorIdAsUuid = item.CreatorIdAsUuid;
  262. clonedItem.CurrentPermissions = item.CurrentPermissions;
  263. clonedItem.Description = item.Description;
  264. clonedItem.EveryOnePermissions = item.EveryOnePermissions;
  265. clonedItem.Flags = item.Flags;
  266. clonedItem.Folder = avatarFolder;
  267. clonedItem.GroupID = item.GroupID;
  268. clonedItem.GroupOwned = item.GroupOwned;
  269. clonedItem.GroupPermissions = item.GroupPermissions;
  270. clonedItem.ID = UUID.Random();
  271. clonedItem.InvType = item.InvType;
  272. clonedItem.Name = item.Name;
  273. clonedItem.NextPermissions = item.NextPermissions;
  274. clonedItem.SalePrice = item.SalePrice;
  275. clonedItem.SaleType = item.SaleType;
  276. if (modifyPerms)
  277. {
  278. ModifyPermissions(ref clonedItem);
  279. }
  280. SynchronousRestObjectRequester.MakeRequest<InventoryItemBase, bool>(
  281. "POST", m_inventoryServerUrl + "AddNewItem/", clonedItem);
  282. return clonedItem.ID;
  283. }
  284. return UUID.Zero;
  285. }
  286. // TODO: unused
  287. // private void UpdateAvatarAppearance(UUID avatarID, int wearableType, UUID itemID, UUID assetID)
  288. // {
  289. // AvatarAppearance appearance = GetAppearance(avatarID);
  290. // appearance.Wearables[wearableType].AssetID = assetID;
  291. // appearance.Wearables[wearableType].ItemID = itemID;
  292. // m_userDataBaseService.UpdateUserAppearance(avatarID, appearance);
  293. // }
  294. // TODO: unused
  295. // private void UpdateAvatarAttachment(UUID avatarID, int attachmentPoint, UUID itemID, UUID assetID)
  296. // {
  297. // AvatarAppearance appearance = GetAppearance(avatarID);
  298. // appearance.SetAttachment(attachmentPoint, itemID, assetID);
  299. // m_userDataBaseService.UpdateUserAppearance(avatarID, appearance);
  300. // }
  301. private void RemoveClothesAndAttachments(UUID avatarID)
  302. {
  303. AvatarAppearance appearance = GetAppearance(avatarID);
  304. appearance.ClearWearables();
  305. appearance.ClearAttachments();
  306. m_userDataBaseService.UpdateUserAppearance(avatarID, appearance);
  307. }
  308. private AvatarAppearance GetAppearance(UUID avatarID)
  309. {
  310. AvatarAppearance appearance = m_userDataBaseService.GetUserAppearance(avatarID);
  311. if (appearance == null)
  312. {
  313. appearance = CreateDefaultAppearance(avatarID);
  314. }
  315. return appearance;
  316. }
  317. // TODO: unused
  318. // private UUID FindFolderID(string name, List<InventoryFolderBase> folders)
  319. // {
  320. // foreach (InventoryFolderBase folder in folders)
  321. // {
  322. // if (folder.Name == name)
  323. // {
  324. // return folder.ID;
  325. // }
  326. // }
  327. // return UUID.Zero;
  328. // }
  329. // TODO: unused
  330. // private InventoryFolderBase FindFolder(string name, List<InventoryFolderBase> folders)
  331. // {
  332. // foreach (InventoryFolderBase folder in folders)
  333. // {
  334. // if (folder.Name == name)
  335. // {
  336. // return folder;
  337. // }
  338. // }
  339. // return null;
  340. // }
  341. private InventoryFolderBase FindFolder(string name, Guid parentFolderID, List<InventoryFolderBase> folders)
  342. {
  343. foreach (InventoryFolderBase folder in folders)
  344. {
  345. if ((folder.Name == name) && (folder.ParentID.Guid == parentFolderID))
  346. {
  347. return folder;
  348. }
  349. }
  350. return null;
  351. }
  352. // TODO: unused
  353. // private InventoryItemBase GetItem(string itemName, List<InventoryItemBase> items)
  354. // {
  355. // foreach (InventoryItemBase item in items)
  356. // {
  357. // if (item.Name.ToLower() == itemName.ToLower())
  358. // {
  359. // return item;
  360. // }
  361. // }
  362. // return null;
  363. // }
  364. private List<InventoryFolderBase> FindSubFolders(Guid parentFolderID, List<InventoryFolderBase> folders)
  365. {
  366. List<InventoryFolderBase> subFolders = new List<InventoryFolderBase>();
  367. foreach (InventoryFolderBase folder in folders)
  368. {
  369. if (folder.ParentID.Guid == parentFolderID)
  370. {
  371. subFolders.Add(folder);
  372. }
  373. }
  374. return subFolders;
  375. }
  376. protected virtual void ModifyPermissions(ref InventoryItemBase item)
  377. {
  378. // Propagate Permissions
  379. item.BasePermissions = item.BasePermissions & item.NextPermissions;
  380. item.CurrentPermissions = item.BasePermissions;
  381. item.EveryOnePermissions = item.EveryOnePermissions & item.NextPermissions;
  382. item.GroupPermissions = item.GroupPermissions & item.NextPermissions;
  383. }
  384. private AvatarAppearance CreateDefaultAppearance(UUID avatarId)
  385. {
  386. AvatarAppearance appearance = null;
  387. AvatarWearable[] wearables;
  388. byte[] visualParams;
  389. GetDefaultAvatarAppearance(out wearables, out visualParams);
  390. appearance = new AvatarAppearance(avatarId, wearables, visualParams);
  391. return appearance;
  392. }
  393. private static void GetDefaultAvatarAppearance(out AvatarWearable[] wearables, out byte[] visualParams)
  394. {
  395. visualParams = GetDefaultVisualParams();
  396. wearables = AvatarWearable.DefaultWearables;
  397. }
  398. private static byte[] GetDefaultVisualParams()
  399. {
  400. byte[] visualParams;
  401. visualParams = new byte[218];
  402. for (int i = 0; i < 218; i++)
  403. {
  404. visualParams[i] = 100;
  405. }
  406. return visualParams;
  407. }
  408. private int ItemIsPartOfAppearance(InventoryItemBase item, AvatarAppearance appearance)
  409. {
  410. if (appearance != null)
  411. {
  412. if (appearance.BodyItem == item.ID)
  413. return (int)WearableType.Shape;
  414. if (appearance.EyesItem == item.ID)
  415. return (int)WearableType.Eyes;
  416. if (appearance.GlovesItem == item.ID)
  417. return (int)WearableType.Gloves;
  418. if (appearance.HairItem == item.ID)
  419. return (int)WearableType.Hair;
  420. if (appearance.JacketItem == item.ID)
  421. return (int)WearableType.Jacket;
  422. if (appearance.PantsItem == item.ID)
  423. return (int)WearableType.Pants;
  424. if (appearance.ShirtItem == item.ID)
  425. return (int)WearableType.Shirt;
  426. if (appearance.ShoesItem == item.ID)
  427. return (int)WearableType.Shoes;
  428. if (appearance.SkinItem == item.ID)
  429. return (int)WearableType.Skin;
  430. if (appearance.SkirtItem == item.ID)
  431. return (int)WearableType.Skirt;
  432. if (appearance.SocksItem == item.ID)
  433. return (int)WearableType.Socks;
  434. if (appearance.UnderPantsItem == item.ID)
  435. return (int)WearableType.Underpants;
  436. if (appearance.UnderShirtItem == item.ID)
  437. return (int)WearableType.Undershirt;
  438. }
  439. return -1;
  440. }
  441. #endregion
  442. public enum PermissionMask
  443. {
  444. None = 0,
  445. Transfer = 8192,
  446. Modify = 16384,
  447. Copy = 32768,
  448. Move = 524288,
  449. Damage = 1048576,
  450. All = 2147483647,
  451. }
  452. public enum WearableType
  453. {
  454. Shape = 0,
  455. Skin = 1,
  456. Hair = 2,
  457. Eyes = 3,
  458. Shirt = 4,
  459. Pants = 5,
  460. Shoes = 6,
  461. Socks = 7,
  462. Jacket = 8,
  463. Gloves = 9,
  464. Undershirt = 10,
  465. Underpants = 11,
  466. Skirt = 12,
  467. }
  468. public class ClothesAttachment
  469. {
  470. public int Type;
  471. public UUID ItemID;
  472. public UUID AssetID;
  473. public ClothesAttachment(int type, UUID itemID, UUID assetID)
  474. {
  475. Type = type;
  476. ItemID = itemID;
  477. AssetID = assetID;
  478. }
  479. }
  480. }
  481. }