<commit_before>﻿/*
====================================================================================
This file is part of ListPlayers, the open-source S.T.A.L.K.E.R. multiplayer
statistics organizing tool for game server administrators.
Copyright (C) 2013 <NAME>.

You should have received a copy of the MIT License along with ListPlayers sources.
If not, see <http://www.opensource.org/licenses/mit-license.php>.

For support and more information about ListPlayers,
visit <http://mpnetworks.ru> or <https://github.com/nitrocaster/ListPlayers>
====================================================================================
*/

using System;
using System.IO;
using ListPlayers.PcdbModel;

namespace ListPlayers.Parsers
{
    public sealed class PcdbParser : ISpecificParser
    {
        private sealed class PcdbParserImpl : ParserBase
        {
            public PcdbParserImpl(HostParser host, PcdbFile database)
                : base(host)
            { Database = database; }

            public override void Parse(string path)
            {
                var src = PcdbFile.Open(path);
                src.OpenConnection();
                src.BeginTransaction();
                {
                    var srcGameVersion = src.GetGameVersion();
                    var selfGameVersion = Database.GetGameVersion();
                    if (srcGameVersion != selfGameVersion)
                    {
                        src.CommitTransaction();
                        src.CloseConnection();
                        return;
                    }
                    var progress = new Progress(100);
                    OnProgressChanged(progress);
                    var ids = src.SelectIds(DatabaseTableId.Hash);
                    var idCount = ids.Rows.Count;
                    OnFoundData(DatabaseTableId.Hash, idCount);
                    for (var idIndex = 0; idIndex < idCount; idIndex++)
                    {
                        if (Cancelled)
                            break;
                        var id = Convert.ToUInt32(ids.Rows[idIndex][0]);
                        var hashRow = src.Select(DatabaseTableId.Hash, id).Rows[0];
                        var hash = (string)hashRow.ItemArray[1];
                        var info = Convert.ToString(hashRow.ItemArray[2]);
                        if (Database.HashExist(hash) && !String.IsNullOrEmpty(info))
                            Database.UpdateHash(hash, info);
                        else
                            Database.AppendNew(hash, info);
                        // names
                        var names = src.Select(DatabaseTableId.Name, id);
                        OnFoundData(DatabaseTableId.Name, names.Rows.Count);
                        for (var i = 0; i < names.Rows.Count; i++)
                        {
                            var nameRow = names.Rows[i];
                            var name = (string)nameRow[1];
                            if (src.Revision >= (int)PcdbRevision.Rev1)
                                Database.AppendName(hash, name, (DateTime)nameRow[2]);
                            else
                                Database.AppendName(hash, name, PcdbFile.InvalidDateTime);
                        }
                        // ips
                        var ips = src.Select(DatabaseTableId.Ip, id);
                        OnFoundData(DatabaseTableId.Ip, ips.Rows.Count);
                        for (var i = 0; i < ips.Rows.Count; i++)
                        {
                            var ipRow = ips.Rows[i];
                            var ip = (string)ipRow[1];
                            if (src.Revision >= (int)PcdbRevision.Rev1)
                                Database.AppendIp(hash, ip, (DateTime)ipRow[2]);
                            else
                                Database.AppendIp(hash, ip, PcdbFile.InvalidDateTime);
                        }
                        // gsids
                        if (srcGameVersion == PcdbGameVersion.COP)
                        {
                            var gsids = src.Select(DatabaseTableId.Gsid, id);
                            OnFoundData(DatabaseTableId.Gsid, gsids.Rows.Count);
                            for (var i = 0; i < gsids.Rows.Count; i++)
                            {
                                var gsidRow = gsids.Rows[i];
                                var gsid = Convert.ToUInt32(gsidRow[1]);
                                if (src.Revision >= (int)PcdbRevision.Rev1)
                                    Database.AppendGsid(hash, gsid, (DateTime)gsidRow[2]);
                                else
                                    Database.AppendGsid(hash, gsid, PcdbFile.InvalidDateTime);
                            }
                        }
                        var newProgress = (uint)Math.Round(100.0*idIndex/idCount);
                        if (newProgress > progress.Current)
                        {
                            progress.Current = newProgress;
                            OnProgressChanged(progress);
                        }
                    }
                }
                src.CommitTransaction();
                src.CloseConnection();
            }
        }

        public ParserBase GetParser(HostParser host, PcdbFile database)
        { return new PcdbParserImpl(host, database); }

        public string AcceptedFileExtension
        { get { return ".pcdb"; } }

        public bool CheckFormat(string path)
        { return (Path.GetExtension(path).ToLowerInvariant() == AcceptedFileExtension); }

        #region Singleton implementation

        private PcdbParser() {}

        private static readonly PcdbParser instance = new PcdbParser();
<commit_msg>Split parser implementations for different pcdb revisions.<commit_after>﻿/*
====================================================================================
This file is part of ListPlayers, the open-source S.T.A.L.K.E.R. multiplayer
statistics organizing tool for game server administrators.
Copyright (C) 2013 <NAME>.

You should have received a copy of the MIT License along with ListPlayers sources.
If not, see <http://www.opensource.org/licenses/mit-license.php>.

For support and more information about ListPlayers,
visit <http://mpnetworks.ru> or <https://github.com/nitrocaster/ListPlayers>
====================================================================================
*/

using System;
using System.IO;
using ListPlayers.PcdbModel;

namespace ListPlayers.Parsers
{
    public sealed class PcdbParser : ISpecificParser
    {
        private sealed class PcdbParserImpl : ParserBase
        {
            public PcdbParserImpl(HostParser host, PcdbFile database)
                : base(host)
            { Database = database; }

            public override void Parse(string path)
            {
                var src = PcdbFile.Open(path);
                src.OpenConnection();
                src.BeginTransaction();
                switch (src.Revision)
                {
                case (int)PcdbRevision.Rev0: ParseRev0(src); break;
                case (int)PcdbRevision.Rev1: ParseRev1(src); break;
                case (int)PcdbRevision.Rev2: ParseRev2(src); break;
                }
                src.CommitTransaction();
                src.CloseConnection();
            }

            private void ParseRev0(PcdbFile src)
            {
                var srcGameVersion = src.GetGameVersion();
                var selfGameVersion = Database.GetGameVersion();
                if (srcGameVersion != selfGameVersion)
                    return;
                var progress = new Progress(100);
                OnProgressChanged(progress);
                var ids = src.SelectIds(DatabaseTableId.Hash);
                var idCount = ids.Rows.Count;
                OnFoundData(DatabaseTableId.Hash, idCount);
                for (var idIndex = 0; idIndex < idCount; idIndex++)
                {
                    if (Cancelled)
                        break;
                    var id = Convert.ToUInt32(ids.Rows[idIndex][0]);
                    var hashRow = src.Select(DatabaseTableId.Hash, id).Rows[0];
                    var hash = (string)hashRow.ItemArray[1];
                    var info = Convert.ToString(hashRow.ItemArray[2]);
                    if (Database.HashExist(hash) && !String.IsNullOrEmpty(info))
                        Database.UpdateHash(hash, info);
                    else
                        Database.AppendNew(hash, info);
                    // names
                    var names = src.Select(DatabaseTableId.Name, id);
                    OnFoundData(DatabaseTableId.Name, names.Rows.Count);
                    for (var i = 0; i < names.Rows.Count; i++)
                    {
                        var nameRow = names.Rows[i];
                        var name = (string)nameRow[1];
                        Database.AppendName(hash, name, PcdbFile.InvalidDateTime);
                    }
                    // ips
                    var ips = src.Select(DatabaseTableId.Ip, id);
                    OnFoundData(DatabaseTableId.Ip, ips.Rows.Count);
                    for (var i = 0; i < ips.Rows.Count; i++)
                    {
                        var ipRow = ips.Rows[i];
                        var ip = (string)ipRow[1];
                        Database.AppendIp(hash, ip, PcdbFile.InvalidDateTime);
                    }
                    // gsids
                    if (srcGameVersion == PcdbGameVersion.COP)
                    {
                        var gsids = src.Select(DatabaseTableId.Gsid, id);
                        OnFoundData(DatabaseTableId.Gsid, gsids.Rows.Count);
                        for (var i = 0; i < gsids.Rows.Count; i++)
                        {
                            var gsidRow = gsids.Rows[i];
                            var gsid = Convert.ToUInt32(gsidRow[1]);
                            Database.AppendGsid(hash, gsid, PcdbFile.InvalidDateTime);
                        }
                    }
                    var newProgress = (uint)Math.Round(100.0*idIndex/idCount);
                    if (newProgress > progress.Current)
                    {
                        progress.Current = newProgress;
                        OnProgressChanged(progress);
                    }
                }
            }

            private void ParseRev1(PcdbFile src)
            {
                var srcGameVersion = src.GetGameVersion();
                var selfGameVersion = Database.GetGameVersion();
                if (srcGameVersion != selfGameVersion)
                    return;
                var progress = new Progress(100);
                OnProgressChanged(progress);
                var ids = src.SelectIds(DatabaseTableId.Hash);
                var idCount = ids.Rows.Count;
                OnFoundData(DatabaseTableId.Hash, idCount);
                for (var idIndex = 0; idIndex < idCount; idIndex++)
                {
                    if (Cancelled)
                        break;
                    var id = Convert.ToUInt32(ids.Rows[idIndex][0]);
                    var hashRow = src.Select(DatabaseTableId.Hash, id).Rows[0];
                    var hash = (string)hashRow.ItemArray[1];
                    var info = Convert.ToString(hashRow.ItemArray[2]);
                    if (Database.HashExist(hash) && !String.IsNullOrEmpty(info))
                        Database.UpdateHash(hash, info);
                    else
                        Database.AppendNew(hash, info);
                    // names
                    var names = src.Select(DatabaseTableId.Name, id);
                    OnFoundData(DatabaseTableId.Name, names.Rows.Count);
                    for (var i = 0; i < names.Rows.Count; i++)
                    {
                        var nameRow = names.Rows[i];
                        var name = (string)nameRow[1];
                        Database.AppendName(hash, name, (DateTime)nameRow[2]);
                    }
                    // ips
                    var ips = src.Select(DatabaseTableId.Ip, id);
                    OnFoundData(DatabaseTableId.Ip, ips.Rows.Count);
                    for (var i = 0; i < ips.Rows.Count; i++)
                    {
                        var ipRow = ips.Rows[i];
                        var ip = (string)ipRow[1];
                        Database.AppendIp(hash, ip, (DateTime)ipRow[2]);
                    }
                    // gsids
                    if (srcGameVersion == PcdbGameVersion.COP)
                    {
                        var gsids = src.Select(DatabaseTableId.Gsid, id);
                        OnFoundData(DatabaseTableId.Gsid, gsids.Rows.Count);
                        for (var i = 0; i < gsids.Rows.Count; i++)
                        {
                            var gsidRow = gsids.Rows[i];
                            var gsid = Convert.ToUInt32(gsidRow[1]);
                            Database.AppendGsid(hash, gsid, (DateTime)gsidRow[2]);
                        }
                    }
                    var newProgress = (uint)Math.Round(100.0*idIndex/idCount);
                    if (newProgress > progress.Current)
                    {
                        progress.Current = newProgress;
                        OnProgressChanged(progress);
                    }
                }
            }

            private void ParseRev2(PcdbFile src)
            { ParseRev1(src); }
        }

        public ParserBase GetParser(HostParser host, PcdbFile database)
        { return new PcdbParserImpl(host, database); }

        public string AcceptedFileExtension
        { get { return ".pcdb"; } }

        public bool CheckFormat(string path)
        { return (Path.GetExtension(path).ToLowerInvariant() == AcceptedFileExtension); }

        #region Singleton implementation

        private PcdbParser() {}

        private static readonly PcdbParser instance = new PcdbParser();

{'fraction_non_alphanumeric': 0.1110873440285205, 'fraction_numerical': 0.0066310160427807486, 'mean_word_length': 1.2659127625201938, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 17, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6169046', 'n_tokens_mistral': 3549, 'n_tokens_neox': 3252, 'n_words': 709}