Advertisement
Guest User

Untitled

a guest
May 24th, 2017
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.49 KB | None | 0 0
  1. using ProtoBuf;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8.  
  9. namespace AboHanaa.Network.GamePackets
  10. {
  11. public sealed class CoatStorage
  12. {
  13. public CoatStorage() { }
  14. public bool Read(byte[] packet)
  15. {
  16. using (var memoryStream = new MemoryStream(packet))
  17. {
  18. Info = Serializer.DeserializeWithLengthPrefix<CoatStorageProto>(memoryStream, PrefixStyle.Fixed32);
  19. }
  20. return true;
  21. }
  22. public void Handle(Client.GameState client)
  23. {
  24. switch (Info.ActionId)
  25. {
  26. case Action.Equipcheck:
  27.  
  28. if (Illegal(Info, client))
  29. return;
  30.  
  31. MsgItemInfo myItem;
  32. if (client.Inventory.TryGetItem(Info.ItemGuid, out myItem))
  33. {
  34. var packet = FinalizeProtoBuf(new CoatStorageProto()
  35. {
  36. ActionId = Info.ActionId,
  37. ItemGuid = Info.ItemGuid,
  38. });
  39. client.Send(packet);
  40. }
  41. break;
  42. case Action.Addtowardrobe:
  43.  
  44. if (Illegal(Info, client))
  45. return;
  46. if (client.Inventory.TryGetItem(Info.ItemGuid, out myItem) ||
  47. client.Player.StorageItems.TryGetValue(Info.ItemGuid, out myItem))
  48. {
  49.  
  50. client.Send(FinalizeProtoBuf(new CoatStorageProto()
  51. {
  52. ActionId = Info.ActionId,
  53. ItemGuid = Info.ItemGuid,
  54. ItemId = (int)myItem.ID
  55. }));
  56.  
  57. if (!client.Player.StorageItems.ContainsKey(myItem.UID))
  58. {
  59. client.Player.StorageItems.Add(myItem.UID, myItem);
  60. myItem.InWardrobe = true;
  61. Database.ConquerItemTable.UpdateWardrobe(myItem.InWardrobe, myItem.UID);
  62. }
  63.  
  64.  
  65. foreach (var i in client.Player.StorageItems.Values)
  66. if (i.Position != 0 && i.Position == (byte)ItemHandler.GetPositionFromID(myItem.ID))
  67. {
  68. i.Position = 0;
  69. Database.ConquerItemTable.UpdateLocation(i, client);
  70. }
  71. myItem.Position = (byte)ItemHandler.GetPositionFromID(myItem.ID);
  72.  
  73.  
  74. if (!client.Equipment.Add(myItem))
  75. {
  76. client.Equipment.Remove((byte)myItem.Position, true);
  77. if (!client.Equipment.Add(myItem))
  78. throw new Exception();
  79. }
  80.  
  81.  
  82. var iu = new MsgItem(true);
  83. iu.ID = MsgItem.Mode.Unknown5;
  84. iu.UID = myItem.UID;
  85. iu.dwParam = myItem.Position;
  86. client.Send(iu.ToArray());
  87.  
  88.  
  89. MsgItemEquip equips = new MsgItemEquip();
  90. equips.DoEquips(client);
  91.  
  92. if ((equips.Garment == 0 || equips.Garment > 0 && equips.Garment != myItem.UID) && myItem.Position != 17)
  93. equips.Garment = myItem.UID;
  94.  
  95. client.Send(equips.ToArray());
  96.  
  97. Database.ConquerItemTable.UpdateLocation(myItem, client);
  98. client.Equipment.UpdateEntityPacket();
  99.  
  100. }
  101. break;
  102. case Action.Takeoff:
  103. if (client.Inventory.TryGetItem(Info.ItemGuid, out myItem) ||
  104. client.Player.StorageItems.TryGetValue(Info.ItemGuid, out myItem) || client.Equipment.TryGetItem(Info.ItemGuid, out myItem))
  105. {
  106. client.Send(FinalizeProtoBuf(new CoatStorageProto()
  107. {
  108. ActionId = Info.ActionId,
  109. ItemGuid = Info.ItemGuid,
  110. ItemId = (int)myItem.ID
  111. }));
  112.  
  113. var pos = (byte)ItemHandler.GetPositionFromID(myItem.ID);
  114.  
  115.  
  116. MsgItemEquip equips = new MsgItemEquip();
  117. equips.DoEquips(client);
  118. if (pos == 17)
  119. equips.SteedArmor = 0;
  120. else equips.Garment = 0;
  121. client.Send(equips.ToArray());
  122.  
  123. if (myItem != null && !client.Player.StorageItems.ContainsKey(myItem.UID))
  124. client.Player.StorageItems.Add(myItem.UID, myItem);
  125.  
  126. }
  127. break;
  128. case Action.Retrieve:
  129. if (client.Player.StorageItems.TryGetValue(Info.ItemGuid, out myItem))
  130. {
  131. client.Send(FinalizeProtoBuf(new CoatStorageProto()
  132. {
  133. ActionId = Info.ActionId,
  134. ItemGuid = Info.ItemGuid,
  135. ItemId = (int)myItem.ID
  136. }));
  137.  
  138. client.Player.StorageItems.Remove(myItem.UID);
  139. myItem.InWardrobe = false;
  140. var pos = (byte)ItemHandler.GetPositionFromID(myItem.ID);
  141.  
  142. if (client.Inventory.ContainsUID(myItem.UID))
  143. {
  144. client.Inventory.Remove(myItem, Game.Enums.ItemUse.Move);
  145. if (client.Equipment.TryGetItem(pos) != null && client.Equipment.TryGetItem(pos).UID == myItem.UID)
  146. client.Equipment.Remove(pos);
  147. else client.Inventory.Add(myItem, Game.Enums.ItemUse.Move);
  148. }
  149. else
  150. {
  151. if (client.Equipment.TryGetItem(pos) != null && client.Equipment.TryGetItem(pos).UID == myItem.UID)
  152. client.Equipment.Remove(pos);
  153. else client.Inventory.Add(myItem, Game.Enums.ItemUse.Move);
  154. }
  155.  
  156.  
  157. Database.ConquerItemTable.UpdateWardrobe(myItem.InWardrobe, myItem.UID);
  158.  
  159. }
  160. break;
  161. }
  162. }
  163.  
  164. private bool Illegal(CoatStorageProto Info, Client.GameState client)
  165. {
  166. MsgItemInfo myItem;
  167. if (client.Inventory.TryGetItem(Info.ItemGuid, out myItem) ||
  168. client.Player.StorageItems.TryGetValue(Info.ItemGuid, out myItem) ||
  169. client.Equipment.TryGetItem(Info.ItemGuid, out myItem))
  170. {
  171. var dbInfo = Database.ConquerItemInformation.BaseInformations.ContainsKey(myItem.ID) ?
  172. Database.ConquerItemInformation.BaseInformations[myItem.ID] : null;
  173. if (dbInfo == null)
  174. return true;
  175. var charSex = (client.Player.Body == 1003 || client.Player.Body == 1004) ? "Male" : "Female";
  176. if (dbInfo.Gender == 1 ? charSex != "Male" : dbInfo.Gender == 0 ? false : charSex != "Female")
  177. return true;
  178. }
  179. else return true;
  180. return false;
  181. }
  182.  
  183. private byte[] FinalizeProtoBuf(CoatStorageProto coatStorageProto)
  184. {
  185. using (var memoryStream = new MemoryStream())
  186. {
  187. Serializer.SerializeWithLengthPrefix(memoryStream, coatStorageProto, PrefixStyle.Fixed32);
  188. var pkt = new byte[8 + memoryStream.Length];
  189. memoryStream.ToArray().CopyTo(pkt, 0);
  190. Writer.WriteUshort((ushort)memoryStream.Length, 0, pkt);
  191. Writer.WriteUshort((ushort)3300, 2, pkt);
  192.  
  193. return pkt;
  194. }
  195. }
  196. public void Login(Client.GameState client)
  197. {
  198. var pkt = new CoatStorageProto();
  199. foreach (var item in client.Player.StorageItems.Values)
  200. {
  201. pkt.AddItem(item,
  202. client.Player.StorageItems.Values.Where(i => i.ID == item.ID).Count());
  203. client.Send(FinalizeProtoBuf(pkt));
  204.  
  205. if (item.Position != 0)
  206. {
  207.  
  208. client.Equipment.Add(item);
  209.  
  210. var iu = new MsgItem(true);
  211. iu.ID = MsgItem.Mode.Unknown5;
  212. iu.UID = item.UID;
  213. iu.dwParam = item.Position;
  214. client.Send(iu.ToArray());
  215.  
  216. MsgItemEquip equips = new MsgItemEquip();
  217. equips.DoEquips(client);
  218. client.Send(equips.ToArray());
  219.  
  220. Database.ConquerItemTable.UpdateLocation(item, client);
  221. client.Equipment.UpdateEntityPacket();
  222. }
  223.  
  224. }
  225. var currentGarment = client.Equipment.TryGetItem((byte)ItemHandler.Positions.Garment);
  226. if (currentGarment != null && !client.Player.StorageItems.ContainsKey(currentGarment.UID))
  227. {
  228. client.Player.StorageItems.Add(currentGarment.UID, currentGarment);
  229. pkt.AddItem(currentGarment,
  230. client.Player.StorageItems.Values.Where(i => i.ID == currentGarment.ID).Count());
  231. pkt.Item.Equipped = true;
  232. client.Send(FinalizeProtoBuf(pkt));
  233. }
  234. var currentMountArmor = client.Equipment.TryGetItem((byte)ItemHandler.Positions.SteedArmor);
  235. if (currentMountArmor != null && !client.Player.StorageItems.ContainsKey(currentMountArmor.UID))
  236. {
  237. client.Player.StorageItems.Add(currentMountArmor.UID, currentMountArmor);
  238. pkt.AddItem(currentMountArmor,
  239. client.Player.StorageItems.Values.Where(i => i.ID == currentMountArmor.ID).Count());
  240. pkt.Item.Equipped = true;
  241. client.Send(FinalizeProtoBuf(pkt));
  242. }
  243. }
  244.  
  245.  
  246. public CoatStorageProto Info;
  247.  
  248. public enum Action : int
  249. {
  250. /// <summary>
  251. /// Load items in storage ...
  252. /// </summary>
  253. Login = 0,
  254. Equipcheck = 1,
  255. Retrieve = 2,
  256. Addtowardrobe = 5,
  257. Takeoff = 6,
  258. }
  259. }
  260. [ProtoContract]
  261. public class CoatStorageProto
  262. {
  263. [ProtoMember(1, IsRequired = true)]
  264. public CoatStorage.Action ActionId;
  265. [ProtoMember(2, IsRequired = true)]
  266. public uint ItemGuid;
  267. [ProtoMember(3, IsRequired = true)]
  268. public int ItemId;
  269. [ProtoMember(4, IsRequired = true)]
  270. public int Junk;
  271. [ProtoMember(5, IsRequired = true)]
  272. public ItemStorage Item;
  273. public void AddItem(MsgItemInfo item, int stack)
  274. {
  275. Item = new ItemStorage();
  276. Item.ItemUID = item.UID;
  277. Item.ItemID = (int)item.ID;
  278. Item.MaximumDurability = Item.MinimumDurability = item.MaximDurability;
  279. Item.Stack = stack;
  280. Item.FirstSocket = (int)item.SocketOne;
  281. Item.SecondSocket = (int)item.SocketTwo;
  282. Item.Plus = item.Plus;
  283. Item.Protection = item.Bless;
  284. Item.Bound = item.Bound;
  285. Item.Health = item.Enchant;
  286. Item.SocketProgress = (int)item.SocketProgress;
  287. Item.Effect = item.Effect;
  288. Item.Color = item.Color;
  289. Item.CraftProgress = (int)item.PlusProgress;
  290. Item.Locked = item.Lock == 1 ? true : false;
  291. Item.Suspicious = false;
  292. Item.Inscribed = false;
  293. Item.dwParam7 = 0;
  294. Item.Equipped = item.Position != 0;
  295. Item.dwParam15 = 0;
  296. Item.Time = 0;
  297. Item.SubTime = 0;
  298. }
  299.  
  300. }
  301. [ProtoContract]
  302. public class ItemStorage
  303. {
  304. [ProtoMember(1, IsRequired = true)]
  305. public uint ItemUID;
  306. [ProtoMember(2, IsRequired = true)]
  307. public int ItemID;
  308. [ProtoMember(3, IsRequired = true)]
  309. public int SocketProgress;
  310. [ProtoMember(4, IsRequired = true)]
  311. public int FirstSocket;
  312. [ProtoMember(5, IsRequired = true)]
  313. public int SecondSocket;
  314. [ProtoMember(6, IsRequired = true)]
  315. public AboHanaa.Game.Enums.ItemEffect Effect;
  316. [ProtoMember(7, IsRequired = true)]
  317. public int dwParam7;
  318. [ProtoMember(8, IsRequired = true)]
  319. public int Plus;
  320. [ProtoMember(9, IsRequired = true)]
  321. public int Protection;
  322. [ProtoMember(10, IsRequired = true)]
  323. public bool Bound;
  324. [ProtoMember(11, IsRequired = true)]
  325. public int Health;
  326. [ProtoMember(12, IsRequired = true)]
  327. public bool Equipped;
  328. [ProtoMember(13, IsRequired = true)]
  329. public bool Suspicious;
  330. [ProtoMember(14, IsRequired = true)]
  331. public bool Locked;
  332. [ProtoMember(15, IsRequired = true)]
  333. public int dwParam15;
  334. [ProtoMember(16, IsRequired = true)]
  335. public AboHanaa.Game.Enums.Color Color;
  336. [ProtoMember(17, IsRequired = true)]
  337. public int CraftProgress;
  338. /// <summary>
  339. /// Inscribed in guild arsenal
  340. /// This class is for wardrobe items which are garments or mount armors so this filed is always false
  341. /// </summary>
  342. [ProtoMember(18, IsRequired = true)]
  343. public bool Inscribed;
  344. /// <summary>
  345. /// Time left in seconds !
  346. /// </summary>
  347. [ProtoMember(19, IsRequired = true)]
  348. public int Time;
  349. /// <summary>
  350. /// Time left in minutes (if item not activated only)
  351. /// </summary>
  352. [ProtoMember(20, IsRequired = true)]
  353. public int SubTime;
  354. [ProtoMember(21, IsRequired = true)]
  355. public int Stack;
  356. [ProtoMember(22, IsRequired = true)]
  357. public int MinimumDurability;
  358. [ProtoMember(23, IsRequired = true)]
  359. public int MaximumDurability;
  360. }
  361. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement