public static bool DeleteRank(Rank deletedRank, Rank replacementRank) { if (deletedRank == null) { throw new ArgumentNullException("deletedRank"); } if (replacementRank == null) { throw new ArgumentNullException("replacementRank"); } if (PlayerDB.IsLoaded) { throw new InvalidOperationException("You may not add ranks after PlayerDB has already been loaded."); } bool rankLimitsChanged = false; Ranks.Remove(deletedRank); RanksByName.Remove(deletedRank.Name.ToLower()); RanksByID.Remove(deletedRank.ID); RanksByFullName.Remove(deletedRank.GetFullName()); LegacyRankMapping.Add(deletedRank.ID, replacementRank.ID); foreach (Rank rank in Ranks) { for (int i = 0; i < rank.PermissionLimits.Length; i++) { if (rank.GetLimit((Permission)i) == deletedRank) { rank.ResetLimit((Permission)i); rankLimitsChanged = true; } } } RebuildIndex(); return(rankLimitsChanged); }
/// <summary> Removes the specified rank from the list of available ranks </summary> /// <param name="deletedRank"> Rank to be deleted. </param> /// <param name="replacementRank"> Rank that will replace the deleted rank. </param> /// <returns> Whether or not the rank was succesfully deleted/replaced. </returns> /// <exception cref="ArgumentNullException"> If deletedRank or replacementRank is null. </exception> /// <exception cref="InvalidOperationException"> If PlayerDB is already loaded. </exception> public static bool DeleteRank([NotNull] Rank deletedRank, [NotNull] Rank replacementRank) { if (deletedRank == null) { throw new ArgumentNullException("deletedRank"); } if (replacementRank == null) { throw new ArgumentNullException("replacementRank"); } CheckIfPlayerDBLoaded(); bool rankLimitsChanged = false; Ranks.Remove(deletedRank); RanksByName.Remove(deletedRank.Name.ToLower()); RanksByID.Remove(deletedRank.ID); RanksByFullName.Remove(deletedRank.FullName); LegacyRankMapping.Add(deletedRank.ID, replacementRank.ID); foreach (Rank rank in Ranks) { for (int i = 0; i < rank.PermissionLimits.Length; i++) { if (rank.GetLimit((Permission)i) == deletedRank) { rank.ResetLimit((Permission)i); rankLimitsChanged = true; } } } RebuildIndex(); return(rankLimitsChanged); }
/// <summary> Parses serialized rank. Accepts either the "name" or "name#ID" format. /// Uses legacy rank mapping table for unrecognized ranks. Does not autocomple. </summary> /// <param name="name"> Full rank name </param> /// <returns> If name could be parsed, returns the corresponding Rank object. Otherwise returns null. </returns> public static Rank ParseRank(string name) { if (name == null) { return(null); } if (RanksByFullName.ContainsKey(name)) { return(RanksByFullName[name]); } if (name.Contains("#")) { // new format string id = name.Substring(name.IndexOf("#") + 1); if (RanksByID.ContainsKey(id)) { // current class return(RanksByID[id]); } else { // unknown class int tries = 0; while (LegacyRankMapping.ContainsKey(id)) { id = LegacyRankMapping[id]; if (RanksByID.ContainsKey(id)) { return(RanksByID[id]); } // avoid infinite loops due to recursive definitions tries++; if (tries > 100) { throw new RankDefinitionException("Recursive legacy rank definition"); } } // try to fall back to name-only name = name.Substring(0, name.IndexOf('#')).ToLower(); return(RanksByName.ContainsKey(name) ? RanksByName[name] : null); } } else if (RanksByName.ContainsKey(name.ToLower())) { // old format return(RanksByName[name.ToLower()]); // LEGACY } else { // totally unknown rank return(null); } }