fCraft.PlayerDB.Load C# (CSharp) Method

Load() static private method

static private Load ( ) : void
return void
        internal static void Load() {
            lock( SaveLoadLocker ) {
                if( File.Exists( Paths.PlayerDBFileName ) ) {
                    Stopwatch sw = Stopwatch.StartNew();
                    using( StreamReader reader = File.OpenText( Paths.PlayerDBFileName ) ) {

                        string header = reader.ReadLine();

                        if( header == null ) return; // if PlayerDB is an empty file

                        lock( AddLocker ) {
                            int version = IdentifyFormatVersion( header );

                            while( !reader.EndOfStream ) {
                                string[] fields = reader.ReadLine().Split( ',' );
                                if( fields.Length >= PlayerInfo.MinFieldCount ) {
#if !DEBUG
                                    try {
#endif
                                        PlayerInfo info;
                                        if( version == 0 ) {
                                            info = PlayerInfo.LoadOldFormat( fields, true );
                                        } else {
                                            info = PlayerInfo.Load( fields );
                                        }
                                        if( Trie.ContainsKey( info.Name ) ) {
                                            Logger.Log( "PlayerDB.Load: Duplicate record for player \"{0}\" skipped.", LogType.Error, info.Name );
                                        } else {
                                            Trie.Add( info.Name, info );
                                            List.Add( info );
                                        }
#if !DEBUG
                                    } catch( Exception ex ) {
                                        Logger.LogAndReportCrash( "Error while parsing PlayerInfo record", "fCraft", ex, false );
                                    }
#endif
                                } else {
                                    Logger.Log( "PlayerDB.Load: Unexpected field count ({0}), expecting at least {1} fields for a PlayerDB entry.", LogType.Error,
                                                fields.Length,
                                                PlayerInfo.MinFieldCount );
                                }
                            }
                        }
                    }
                    List.TrimExcess();
                    sw.Stop();
                    Logger.Log( "PlayerDB.Load: Done loading player DB ({0} records) in {1}ms.", LogType.Debug,
                                Trie.Count, sw.ElapsedMilliseconds );
                } else {
                    Logger.Log( "PlayerDB.Load: No player DB file found.", LogType.Warning );
                }
                UpdateCache();
                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);
        }