PageRenderTime 54ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/OpenSim/Region/CoreModules/World/Objects/BuySell/BuySellModule.cs

https://github.com/UbitUmarov/Ubit-opensim
C# | 262 lines | 190 code | 45 blank | 27 comment | 33 complexity | 30fe9b2e2c5e829641f2bd012af36c18 MD5 | raw file
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSimulator Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. using System;
  28. using System.Collections.Generic;
  29. using System.Reflection;
  30. using log4net;
  31. using Mono.Addins;
  32. using Nini.Config;
  33. using OpenMetaverse;
  34. using OpenMetaverse.Packets;
  35. using OpenSim.Framework;
  36. using OpenSim.Region.Framework;
  37. using OpenSim.Region.Framework.Interfaces;
  38. using OpenSim.Region.Framework.Scenes;
  39. using OpenSim.Region.Framework.Scenes.Serialization;
  40. namespace OpenSim.Region.CoreModules.World.Objects.BuySell
  41. {
  42. [Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule", Id = "BuySellModule")]
  43. public class BuySellModule : IBuySellModule, INonSharedRegionModule
  44. {
  45. // private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  46. protected Scene m_scene = null;
  47. protected IDialogModule m_dialogModule;
  48. public string Name { get { return "Object BuySell Module"; } }
  49. public Type ReplaceableInterface { get { return null; } }
  50. public void Initialise(IConfigSource source) {}
  51. public void AddRegion(Scene scene)
  52. {
  53. m_scene = scene;
  54. m_scene.RegisterModuleInterface<IBuySellModule>(this);
  55. m_scene.EventManager.OnNewClient += SubscribeToClientEvents;
  56. }
  57. public void RemoveRegion(Scene scene)
  58. {
  59. m_scene.EventManager.OnNewClient -= SubscribeToClientEvents;
  60. }
  61. public void RegionLoaded(Scene scene)
  62. {
  63. m_dialogModule = scene.RequestModuleInterface<IDialogModule>();
  64. }
  65. public void Close()
  66. {
  67. RemoveRegion(m_scene);
  68. }
  69. public void SubscribeToClientEvents(IClientAPI client)
  70. {
  71. client.OnObjectSaleInfo += ObjectSaleInfo;
  72. }
  73. protected void ObjectSaleInfo(
  74. IClientAPI client, UUID agentID, UUID sessionID, uint localID, byte saleType, int salePrice)
  75. {
  76. SceneObjectPart part = m_scene.GetSceneObjectPart(localID);
  77. if (part == null)
  78. return;
  79. if (part.ParentGroup.IsDeleted)
  80. return;
  81. if (part.OwnerID != client.AgentId && (!m_scene.Permissions.IsGod(client.AgentId)))
  82. return;
  83. part = part.ParentGroup.RootPart;
  84. part.ObjectSaleType = saleType;
  85. part.SalePrice = salePrice;
  86. part.ParentGroup.HasGroupChanged = true;
  87. part.SendPropertiesToClient(client);
  88. }
  89. public bool BuyObject(IClientAPI remoteClient, UUID categoryID, uint localID, byte saleType, int salePrice)
  90. {
  91. SceneObjectPart part = m_scene.GetSceneObjectPart(localID);
  92. if (part == null)
  93. return false;
  94. SceneObjectGroup group = part.ParentGroup;
  95. switch (saleType)
  96. {
  97. case 1: // Sell as original (in-place sale)
  98. uint effectivePerms = group.GetEffectivePermissions();
  99. if ((effectivePerms & (uint)PermissionMask.Transfer) == 0)
  100. {
  101. if (m_dialogModule != null)
  102. m_dialogModule.SendAlertToUser(remoteClient, "This item doesn't appear to be for sale");
  103. return false;
  104. }
  105. group.SetOwnerId(remoteClient.AgentId);
  106. group.SetRootPartOwner(part, remoteClient.AgentId, remoteClient.ActiveGroupId);
  107. if (m_scene.Permissions.PropagatePermissions())
  108. {
  109. foreach (SceneObjectPart child in group.Parts)
  110. {
  111. child.Inventory.ChangeInventoryOwner(remoteClient.AgentId);
  112. child.TriggerScriptChangedEvent(Changed.OWNER);
  113. child.ApplyNextOwnerPermissions();
  114. }
  115. }
  116. part.ObjectSaleType = 0;
  117. part.SalePrice = 10;
  118. group.HasGroupChanged = true;
  119. part.SendPropertiesToClient(remoteClient);
  120. part.TriggerScriptChangedEvent(Changed.OWNER);
  121. group.ResumeScripts();
  122. part.ScheduleFullUpdate();
  123. break;
  124. case 2: // Sell a copy
  125. Vector3 inventoryStoredPosition = new Vector3
  126. (((group.AbsolutePosition.X > (int)Constants.RegionSize)
  127. ? 250
  128. : group.AbsolutePosition.X)
  129. ,
  130. (group.AbsolutePosition.X > (int)Constants.RegionSize)
  131. ? 250
  132. : group.AbsolutePosition.X,
  133. group.AbsolutePosition.Z);
  134. Vector3 originalPosition = group.AbsolutePosition;
  135. group.AbsolutePosition = inventoryStoredPosition;
  136. string sceneObjectXml = SceneObjectSerializer.ToOriginalXmlFormat(group);
  137. group.AbsolutePosition = originalPosition;
  138. uint perms = group.GetEffectivePermissions();
  139. if ((perms & (uint)PermissionMask.Transfer) == 0)
  140. {
  141. if (m_dialogModule != null)
  142. m_dialogModule.SendAlertToUser(remoteClient, "This item doesn't appear to be for sale");
  143. return false;
  144. }
  145. AssetBase asset = m_scene.CreateAsset(
  146. group.GetPartName(localID),
  147. group.GetPartDescription(localID),
  148. (sbyte)AssetType.Object,
  149. Utils.StringToBytes(sceneObjectXml),
  150. group.OwnerID);
  151. m_scene.AssetService.Store(asset);
  152. InventoryItemBase item = new InventoryItemBase();
  153. item.CreatorId = part.CreatorID.ToString();
  154. item.CreatorData = part.CreatorData;
  155. item.ID = UUID.Random();
  156. item.Owner = remoteClient.AgentId;
  157. item.AssetID = asset.FullID;
  158. item.Description = asset.Description;
  159. item.Name = asset.Name;
  160. item.AssetType = asset.Type;
  161. item.InvType = (int)InventoryType.Object;
  162. item.Folder = categoryID;
  163. uint nextPerms=(perms & 7) << 13;
  164. if ((nextPerms & (uint)PermissionMask.Copy) == 0)
  165. perms &= ~(uint)PermissionMask.Copy;
  166. if ((nextPerms & (uint)PermissionMask.Transfer) == 0)
  167. perms &= ~(uint)PermissionMask.Transfer;
  168. if ((nextPerms & (uint)PermissionMask.Modify) == 0)
  169. perms &= ~(uint)PermissionMask.Modify;
  170. item.BasePermissions = perms & part.NextOwnerMask;
  171. item.CurrentPermissions = perms & part.NextOwnerMask;
  172. item.NextPermissions = part.NextOwnerMask;
  173. item.EveryOnePermissions = part.EveryoneMask &
  174. part.NextOwnerMask;
  175. item.GroupPermissions = part.GroupMask &
  176. part.NextOwnerMask;
  177. item.Flags |= (uint)InventoryItemFlags.ObjectSlamPerm;
  178. item.CreationDate = Util.UnixTimeSinceEpoch();
  179. if (m_scene.AddInventoryItem(item))
  180. {
  181. remoteClient.SendInventoryItemCreateUpdate(item, 0);
  182. }
  183. else
  184. {
  185. if (m_dialogModule != null)
  186. m_dialogModule.SendAlertToUser(remoteClient, "Cannot buy now. Your inventory is unavailable");
  187. return false;
  188. }
  189. break;
  190. case 3: // Sell contents
  191. List<UUID> invList = part.Inventory.GetInventoryList();
  192. bool okToSell = true;
  193. foreach (UUID invID in invList)
  194. {
  195. TaskInventoryItem item1 = part.Inventory.GetInventoryItem(invID);
  196. if ((item1.CurrentPermissions &
  197. (uint)PermissionMask.Transfer) == 0)
  198. {
  199. okToSell = false;
  200. break;
  201. }
  202. }
  203. if (!okToSell)
  204. {
  205. if (m_dialogModule != null)
  206. m_dialogModule.SendAlertToUser(
  207. remoteClient, "This item's inventory doesn't appear to be for sale");
  208. return false;
  209. }
  210. if (invList.Count > 0)
  211. m_scene.MoveTaskInventoryItems(remoteClient.AgentId, part.Name, part, invList);
  212. break;
  213. }
  214. return true;
  215. }
  216. }
  217. }