fCraft.RankManager.ParseRank C# (CSharp) Method

ParseRank() public static method

Parses serialized rank. Accepts either the "name" or "name#ID" format. Uses legacy rank mapping table for unrecognized ranks. Does not autocomple.
public static ParseRank ( string name ) : Rank
name string Full rank name
return Rank
        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;
            }
        }

Usage Example

        public override void Validate(string value)
        {
            base.Validate(value);

            Rank rank;

            if (value.Length == 0)
            {
                rank = GetBlankValueSubstitute();
                if (rank == null)
                {
                    return;                // ranks must not have loaded yet; can't validate
                }
            }
            else
            {
                rank = RankManager.ParseRank(value);
                if (rank == null)
                {
                    throw new FormatException("Value cannot be parsed as a rank.");
                }
            }
            if (!CanBeLowest && rank == RankManager.LowestRank)
            {
                throw new FormatException("Value may not be the lowest rank.");
            }
            if (!CanBeHighest && rank == RankManager.HighestRank)
            {
                throw new FormatException("Value may not be the highest rank.");
            }
        }
All Usage Examples Of fCraft.RankManager::ParseRank