AccountInfo.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. using Server.MirNetwork;
  2. using Server.MirEnvir;
  3. using Server.Utils;
  4. using C = ClientPackets;
  5. namespace Server.MirDatabase
  6. {
  7. public class AccountInfo
  8. {
  9. protected static Envir Envir
  10. {
  11. get { return Envir.Main; }
  12. }
  13. protected static MessageQueue MessageQueue => MessageQueue.Instance;
  14. public int Index;
  15. public string AccountID = string.Empty;
  16. private string password = string.Empty;
  17. public string Password
  18. {
  19. get { return password; }
  20. set
  21. {
  22. Salt = Crypto.GenerateSalt();
  23. password = Crypto.HashPassword(value, Salt);
  24. }
  25. }
  26. public byte[] Salt = new byte[24];
  27. private string storagePassword = string.Empty;
  28. public string StoragePassword
  29. {
  30. get { return storagePassword; }
  31. set
  32. {
  33. StorageSalt = Crypto.GenerateSalt();
  34. storagePassword = Crypto.HashPassword(value, StorageSalt);
  35. }
  36. }
  37. public byte[] StorageSalt = new byte[24];
  38. public bool HasStoragePassword => !string.IsNullOrEmpty(storagePassword);
  39. public DateTime StoragePasswordLastSet = DateTime.MinValue;
  40. public string UserName = string.Empty;
  41. public DateTime BirthDate;
  42. public string SecretQuestion = string.Empty;
  43. public string SecretAnswer = string.Empty;
  44. public string EMailAddress = string.Empty;
  45. public string CreationIP = string.Empty;
  46. public DateTime CreationDate;
  47. public bool Banned;
  48. public bool RequirePasswordChange;
  49. public string BanReason = string.Empty;
  50. public DateTime ExpiryDate;
  51. public int WrongPasswordCount;
  52. public string LastIP = string.Empty;
  53. public DateTime LastDate;
  54. public List<CharacterInfo> Characters = new List<CharacterInfo>();
  55. public UserItem[] Storage = new UserItem[80];
  56. public bool HasExpandedStorage;
  57. public DateTime ExpandedStorageExpiryDate;
  58. public uint Gold;
  59. public uint Credit;
  60. public MirConnection Connection;
  61. public LinkedList<AuctionInfo> Auctions = new LinkedList<AuctionInfo>();
  62. public bool AdminAccount;
  63. public AccountInfo()
  64. {
  65. }
  66. public AccountInfo(C.NewAccount p)
  67. {
  68. AccountID = p.AccountID;
  69. Password = p.Password;
  70. UserName = p.UserName;
  71. SecretQuestion = p.SecretQuestion;
  72. SecretAnswer = p.SecretAnswer;
  73. EMailAddress = p.EMailAddress;
  74. BirthDate = p.BirthDate;
  75. CreationDate = Envir.Now;
  76. }
  77. public AccountInfo(BinaryReader reader)
  78. {
  79. Index = reader.ReadInt32();
  80. AccountID = reader.ReadString();
  81. if (Envir.LoadVersion < 94)
  82. Password = reader.ReadString();
  83. else
  84. password = reader.ReadString();
  85. if (Envir.LoadVersion > 93)
  86. Salt = reader.ReadBytes(reader.ReadInt32());
  87. if (Envir.LoadVersion > 97)
  88. RequirePasswordChange = reader.ReadBoolean();
  89. if (Envir.LoadVersion >= 117)
  90. {
  91. storagePassword = reader.ReadString();
  92. StorageSalt = reader.ReadBytes(reader.ReadInt32());
  93. StoragePasswordLastSet = DateTime.FromBinary(reader.ReadInt64());
  94. }
  95. UserName = reader.ReadString();
  96. BirthDate = DateTime.FromBinary(reader.ReadInt64());
  97. SecretQuestion = reader.ReadString();
  98. SecretAnswer = reader.ReadString();
  99. EMailAddress = reader.ReadString();
  100. CreationIP = reader.ReadString();
  101. CreationDate = DateTime.FromBinary(reader.ReadInt64());
  102. Banned = reader.ReadBoolean();
  103. BanReason = reader.ReadString();
  104. ExpiryDate = DateTime.FromBinary(reader.ReadInt64());
  105. LastIP = reader.ReadString();
  106. LastDate = DateTime.FromBinary(reader.ReadInt64());
  107. int count = reader.ReadInt32();
  108. for (int i = 0; i < count; i++)
  109. {
  110. var info = new CharacterInfo(reader, Envir.LoadVersion, Envir.LoadCustomVersion) { AccountInfo = this };
  111. if (info.Deleted && info.DeleteDate.AddMonths(Settings.ArchiveDeletedCharacterAfterMonths) <= Envir.Now)
  112. {
  113. MessageQueue.Enqueue(GameLanguage.ServerTextMap.GetLocalization((ServerTextKeys.PlayerArchivedAfterDeletionMonths), info.Name, Settings.ArchiveDeletedCharacterAfterMonths));
  114. Envir.SaveArchivedCharacter(info);
  115. continue;
  116. }
  117. if (info.LastLoginDate == DateTime.MinValue && info.CreationDate.AddMonths(Settings.ArchiveInactiveCharacterAfterMonths) <= Envir.Now)
  118. {
  119. MessageQueue.Enqueue(GameLanguage.ServerTextMap.GetLocalization((ServerTextKeys.PlayerArchivedAfterNoLoginMonths), info.Name, Settings.ArchiveInactiveCharacterAfterMonths));
  120. Envir.SaveArchivedCharacter(info);
  121. continue;
  122. }
  123. if (info.LastLoginDate > DateTime.MinValue && info.LastLoginDate.AddMonths(Settings.ArchiveInactiveCharacterAfterMonths) <= Envir.Now)
  124. {
  125. MessageQueue.Enqueue(GameLanguage.ServerTextMap.GetLocalization((ServerTextKeys.PlayerArchivedAfterInactivityMonths), info.Name, Settings.ArchiveInactiveCharacterAfterMonths));
  126. Envir.SaveArchivedCharacter(info);
  127. continue;
  128. }
  129. Characters.Add(info);
  130. }
  131. if (Envir.LoadVersion > 75)
  132. {
  133. HasExpandedStorage = reader.ReadBoolean();
  134. ExpandedStorageExpiryDate = DateTime.FromBinary(reader.ReadInt64());
  135. }
  136. Gold = reader.ReadUInt32();
  137. if (Envir.LoadVersion >= 63) Credit = reader.ReadUInt32();
  138. count = reader.ReadInt32();
  139. Array.Resize(ref Storage, count);
  140. for (int i = 0; i < count; i++)
  141. {
  142. if (!reader.ReadBoolean()) continue;
  143. UserItem item = new UserItem(reader, Envir.LoadVersion, Envir.LoadCustomVersion);
  144. if (Envir.BindItem(item) && i < Storage.Length)
  145. Storage[i] = item;
  146. }
  147. if (Envir.LoadVersion >= 10) AdminAccount = reader.ReadBoolean();
  148. if (!AdminAccount)
  149. {
  150. for (int i = 0; i < Characters.Count; i++)
  151. {
  152. if (Characters[i] == null) continue;
  153. if (Characters[i].Deleted) continue;
  154. if ((Envir.Now - Characters[i].LastLogoutDate).TotalDays > 13) continue;
  155. Envir.CheckRankUpdate(Characters[i]);
  156. }
  157. }
  158. }
  159. public void Save(BinaryWriter writer)
  160. {
  161. writer.Write(Index);
  162. writer.Write(AccountID);
  163. writer.Write(Password);
  164. writer.Write(Salt.Length);
  165. writer.Write(Salt);
  166. writer.Write(RequirePasswordChange);
  167. writer.Write(storagePassword);
  168. writer.Write(StorageSalt.Length);
  169. writer.Write(StorageSalt);
  170. writer.Write(StoragePasswordLastSet.ToBinary());
  171. writer.Write(UserName);
  172. writer.Write(BirthDate.ToBinary());
  173. writer.Write(SecretQuestion);
  174. writer.Write(SecretAnswer);
  175. writer.Write(EMailAddress);
  176. writer.Write(CreationIP);
  177. writer.Write(CreationDate.ToBinary());
  178. writer.Write(Banned);
  179. writer.Write(BanReason);
  180. writer.Write(ExpiryDate.ToBinary());
  181. writer.Write(LastIP);
  182. writer.Write(LastDate.ToBinary());
  183. writer.Write(Characters.Count);
  184. for (int i = 0; i < Characters.Count; i++)
  185. {
  186. Characters[i].Save(writer);
  187. }
  188. writer.Write(HasExpandedStorage);
  189. writer.Write(ExpandedStorageExpiryDate.ToBinary());
  190. writer.Write(Gold);
  191. writer.Write(Credit);
  192. writer.Write(Storage.Length);
  193. for (int i = 0; i < Storage.Length; i++)
  194. {
  195. writer.Write(Storage[i] != null);
  196. if (Storage[i] == null) continue;
  197. Storage[i].Save(writer);
  198. }
  199. writer.Write(AdminAccount);
  200. }
  201. public List<SelectInfo> GetSelectInfo()
  202. {
  203. List<SelectInfo> list = new List<SelectInfo>();
  204. for (int i = 0; i < Characters.Count; i++)
  205. {
  206. if (Characters[i].Deleted) continue;
  207. list.Add(Characters[i].ToSelectInfo());
  208. if (list.Count >= Globals.MaxCharacterCount) break;
  209. }
  210. return list;
  211. }
  212. public int ExpandStorage()
  213. {
  214. if (!HasExpandedStorage)
  215. {
  216. if (Storage.Length == Globals.StorageGridSize)
  217. Array.Resize(ref Storage, Storage.Length + Globals.StorageGridSize);
  218. }
  219. return Storage.Length;
  220. }
  221. public bool IsValidStorageIndex(int index)
  222. {
  223. if (index >= Globals.StorageGridSize)
  224. {
  225. var level = index / Globals.StorageGridSize;
  226. if (level > (HasExpandedStorage ? 1 : 0))
  227. return false;
  228. }
  229. return true;
  230. }
  231. public bool ValidateStoragePassword(string rawPassword)
  232. {
  233. if (string.IsNullOrEmpty(storagePassword)) return false;
  234. var hashed = Crypto.HashPassword(rawPassword, StorageSalt);
  235. return string.CompareOrdinal(storagePassword, hashed) == 0;
  236. }
  237. public void ClearStoragePassword()
  238. {
  239. storagePassword = string.Empty;
  240. StoragePasswordLastSet = DateTime.MinValue;
  241. }
  242. }
  243. }