fCraft.IPBanList.Load C# (CSharp) Method

Load() static private method

static private Load ( ) : void
return void
        internal static void Load()
        {
            if ( File.Exists( Paths.IPBanListFileName ) ) {
                using ( StreamReader reader = File.OpenText( Paths.IPBanListFileName ) ) {
                    string headerText = reader.ReadLine();
                    if ( headerText == null ) {
                        Logger.Log( LogType.Warning, "IPBanList.Load: IP ban file is empty." );
                        return;
                    }

                    int version = ParseHeader( headerText );
                    if ( version > FormatVersion ) {
                        Logger.Log( LogType.Warning,
                                    "IPBanList.Load: Attempting to load unsupported IPBanList format ({0}). Errors may occur.",
                                    version );
                    } else if ( version < FormatVersion ) {
                        Logger.Log( LogType.Warning,
                                    "IPBanList.Load: Converting IPBanList to a newer format (version {0} to {1}).",
                                    version, FormatVersion );
                    }

                    while ( !reader.EndOfStream ) {
                        string line = reader.ReadLine();
                        if ( line == null )
                            break;
                        string[] fields = line.Split( ',' );
                        if ( fields.Length == IPBanInfo.FieldCount ) {
                            try {
                                IPBanInfo ban;
                                switch ( version ) {
                                    case 0:
                                        ban = IPBanInfo.LoadFormat0( fields, true );
                                        break;

                                    case 1:
                                        ban = IPBanInfo.LoadFormat1( fields );
                                        break;

                                    case 2:
                                        ban = IPBanInfo.LoadFormat2( fields );
                                        break;

                                    default:
                                        return;
                                }

                                if ( ban.Address.Equals( IPAddress.Any ) || ban.Address.Equals( IPAddress.None ) ) {
                                    Logger.Log( LogType.Warning,
                                                "IPBanList.Load: Invalid IP address skipped." );
                                } else {
                                    Bans.Add( ban.Address.ToString(), ban );
                                }
                            } catch ( IOException ex ) {
                                Logger.Log( LogType.Error,
                                            "IPBanList.Load: Error while trying to read from file: {0}", ex.Message );
                            } catch ( Exception ex ) {
                                Logger.Log( LogType.Error,
                                            "IPBanList.Load: Could not parse a record: {0}", ex.Message );
                            }
                        } else {
                            Logger.Log( LogType.Error,
                                        "IPBanList.Load: Corrupt record skipped ({0} fields instead of {1}): {2}",
                                        fields.Length, IPBanInfo.FieldCount, String.Join( ",", fields ) );
                        }
                    }
                }

                Logger.Log( LogType.Debug,
                            "IPBanList.Load: Done loading IP ban list ({0} records).", Bans.Count );
            } else {
                Logger.Log( LogType.Warning,
                            "IPBanList.Load: No IP ban file found." );
            }
            IsLoaded = true;
        }

Usage Example

Example #1
0
        public bool Init()
        {
            log.Init("fCraft.log");
            if (!config.Load("config.xml"))
            {
                return(false);
            }
            config.ApplyConfig();
            config.Save("config.xml");

            // allocate player list
            players = new Player[config.GetInt("MaxPlayers") + 1];
            tasks.Init();

            // load player DB
            db.Load();
            bans.Load();

            return(true);
        }