<commit_before>﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace Heroes.ReplayParser.MPQFiles
{
    /// <summary> Parses the replay.server.battlelobby file in the replay file. </summary>
    public class ReplayServerBattlelobby
    {
        public const string FileName = "replay.server.battlelobby";

        /// <summary> Parses the replay.server.battlelobby file in a replay file. </summary>
        /// <param name="replay"> The replay file to apply the parsed data to. </param>
        /// <param name="buffer"> The buffer containing the replay.server.battlelobby file. </param>
        public static void Parse(Replay replay, byte[] buffer)
        {
            using (var stream = new MemoryStream(buffer))
            {
                var bitReader = new BitReader(stream);

                // 52124 and 52381 are non-tested ptr builds
                if (replay.ReplayBuild < 47479 ||
                    replay.ReplayBuild < 47903 || replay.ReplayBuild == 52124 || replay.ReplayBuild == 52381 ||
                    replay.GameMode == GameMode.Unknown)
                {
                    GetBattleTags(replay, bitReader);
                    return;
                }

                uint dependenciesLength = bitReader.Read(6);
                for (int i = 0; i < dependenciesLength; i++)
                {
                    bitReader.ReadBlobPrecededWithLength(10);
                }

                // s2ma cache handles
                uint s2maCacheHandlesLength = bitReader.Read(6);
                for (int i = 0; i < s2maCacheHandlesLength; i++)
                {
                    bitReader.AlignToByte();
                    if (bitReader.ReadString(4) != "s2ma")
                        throw new DetailedParsedException($"s2ma cache");

                    bitReader.ReadBytes(36);
                }

                DetailedParse(bitReader, replay, s2maCacheHandlesLength);
            }        
        }

        private static void DetailedParse(BitReader bitReader, Replay replay, uint s2maCacheHandlesLength)
        {
            bitReader.AlignToByte();
            for (; ; )
            {
                // we're just going to skip all the way down to the s2mh 
                if (bitReader.ReadString(4) == "s2mh")
                {
                    bitReader.stream.Position -= 4;
                    break;
                }
                else
                    bitReader.stream.Position -= 3;
            }

            // bitReader.Read(???); // this depends on previous data (not byte aligned)

            // s2mh cache handles
            // uint s2mhCacheHandlesLength = bitReader.Read(6);
            // for (int i = 0; i < s2mhCacheHandlesLength; i++)
            for (var i = 0; i < s2maCacheHandlesLength; i++)
            {
                bitReader.AlignToByte();
                if (bitReader.ReadString(4) != "s2mh")
                    throw new Exception($"s2mh cache");

                bitReader.ReadBytes(36);
            }

            // Player collections - starting with HOTS 2.0 (live build 52860)
            // strings gone starting with build (ptr) 55929
            // --------------------------------------------------------------
            var playerCollection = new List<string>();

            int collectionSize = replay.ReplayBuild >= 48027 ? bitReader.ReadInt16() : bitReader.ReadInt32();

            if (collectionSize > 8000)
                throw new DetailedParsedException("collectionSize is an unusually large number");

            for (var i = 0; i < collectionSize; i++)
            {
                if (replay.ReplayBuild >= 55929)
                    bitReader.ReadBytes(8); // most likey an identifier for the item; first six bytes are 0x00
                else
                    playerCollection.Add(bitReader.ReadString(bitReader.ReadByte()));
            }

            // use to determine if the collection item is usable by the player (owns/free to play/internet cafe)
            if (bitReader.ReadInt32() != collectionSize)
                throw new DetailedParsedException("skinArrayLength not equal");

            for (var i = 0; i < collectionSize; i++)
            {
                for (var j = 0; j < 16; j++) // 16 is total player slots
                {
                    bitReader.ReadByte();

                    var num = bitReader.Read(8);

                    if (replay.ReplayBuild < 55929)
                    {
                        if (replay.ClientListByUserID[j] != null)
                        {
                            if (num > 0)
                                replay.ClientListByUserID[j].PlayerCollectionDictionary.Add(playerCollection[i], true);
                            else if (num == 0)
                                replay.ClientListByUserID[j].PlayerCollectionDictionary.Add(playerCollection[i], false);
                            else
                                throw new NotImplementedException();
                        }
                    }
                }
            }

            // Player info 
            // ------------------------

            // m_randomSeed, set it if it hasn't been set
            if (replay.RandomValue == 0)
                replay.RandomValue = (uint)bitReader.ReadInt32(); 
            else
                bitReader.ReadInt32();

            bitReader.ReadBytes(4);

            uint playerListLength = bitReader.Read(5);

            for (uint i = 0; i < playerListLength; i++)
            {
                bitReader.Read(32);

                bitReader.Read(5); // player index

                // toon
                bitReader.Read(8); // m_region
                if (bitReader.ReadStringFromBits(32, true) != "Hero") // m_programId
                    throw new DetailedParsedException("Not Hero");
                bitReader.Read(32); // m_realm
                bitReader.ReadLong(64); // m_id

                // internal toon
                bitReader.Read(8); // m_region
                if (bitReader.ReadStringFromBits(32, true) != "Hero") // m_programId
                    throw new DetailedParsedException("Not Hero");
                bitReader.Read(32); // m_realm

                int idLength = (int)bitReader.Read(7);

                bitReader.AlignToByte();
                if (bitReader.ReadString(2) != "T:")
                    throw new DetailedParsedException("Not T:");

                replay.ClientListByUserID[i].BattleNetTId = bitReader.ReadString(idLength);

                bitReader.Read(6);

                if (replay.ReplayBuild <= 47479)
                {
                    // internal toon repeat
                    bitReader.Read(8); // m_region
                    if (bitReader.ReadStringFromBits(32, true) != "Hero") // m_programId
                        throw new DetailedParsedException("Not Hero");
                    bitReader.Read(32); // m_realm

                    idLength = (int)bitReader.Read(7);
                    bitReader.AlignToByte();
                    if (bitReader.ReadString(2) != "T:")
                        throw new DetailedParsedException("Not T: (duplicate)");

                    if (replay.ClientListByUserID[i].BattleNetTId != bitReader.ReadString(idLength))
                        throw new DetailedParsedException("Duplicate TID does not match");
                }

                bitReader.Read(2);
                bitReader.ReadBytes(25);
                bitReader.Read(24);

                // bitReader.ReadBytes(8); ai games have 8 more bytes somewhere around here

                bitReader.Read(7);

                if (!bitReader.ReadBoolean())
                {
                    // repeat of the collection section above
                    if (replay.ReplayBuild >= 51609 || replay.ReplayBuild == 47903 || replay.ReplayBuild == 47479)<commit_msg>Updated to handle internal id (T: and TOON)<commit_after>﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace Heroes.ReplayParser.MPQFiles
{
    /// <summary> Parses the replay.server.battlelobby file in the replay file. </summary>
    public class ReplayServerBattlelobby
    {
        public const string FileName = "replay.server.battlelobby";

        /// <summary> Parses the replay.server.battlelobby file in a replay file. </summary>
        /// <param name="replay"> The replay file to apply the parsed data to. </param>
        /// <param name="buffer"> The buffer containing the replay.server.battlelobby file. </param>
        public static void Parse(Replay replay, byte[] buffer)
        {
            using (var stream = new MemoryStream(buffer))
            {
                var bitReader = new BitReader(stream);

                // 52124 and 52381 are non-tested ptr builds
                if (replay.ReplayBuild < 47479 ||
                    replay.ReplayBuild < 47903 || replay.ReplayBuild == 52124 || replay.ReplayBuild == 52381 ||
                    replay.GameMode == GameMode.Unknown)
                {
                    GetBattleTags(replay, bitReader);
                    return;
                }

                uint dependenciesLength = bitReader.Read(6);
                for (int i = 0; i < dependenciesLength; i++)
                {
                    bitReader.ReadBlobPrecededWithLength(10);
                }

                // s2ma cache handles
                uint s2maCacheHandlesLength = bitReader.Read(6);
                for (int i = 0; i < s2maCacheHandlesLength; i++)
                {
                    bitReader.AlignToByte();
                    if (bitReader.ReadString(4) != "s2ma")
                        throw new DetailedParsedException($"s2ma cache");

                    bitReader.ReadBytes(36);
                }

                DetailedParse(bitReader, replay, s2maCacheHandlesLength);
            }        
        }

        private static void DetailedParse(BitReader bitReader, Replay replay, uint s2maCacheHandlesLength)
        {
            bitReader.AlignToByte();
            for (; ; )
            {
                // we're just going to skip all the way down to the s2mh 
                if (bitReader.ReadString(4) == "s2mh")
                {
                    bitReader.stream.Position -= 4;
                    break;
                }
                else
                    bitReader.stream.Position -= 3;
            }

            // bitReader.Read(???); // this depends on previous data (not byte aligned)

            // s2mh cache handles
            // uint s2mhCacheHandlesLength = bitReader.Read(6);
            // for (int i = 0; i < s2mhCacheHandlesLength; i++)
            for (var i = 0; i < s2maCacheHandlesLength; i++)
            {
                bitReader.AlignToByte();
                if (bitReader.ReadString(4) != "s2mh")
                    throw new Exception($"s2mh cache");

                bitReader.ReadBytes(36);
            }

            // Player collections - starting with HOTS 2.0 (live build 52860)
            // strings gone starting with build (ptr) 55929
            // --------------------------------------------------------------
            var playerCollection = new List<string>();

            int collectionSize = replay.ReplayBuild >= 48027 ? bitReader.ReadInt16() : bitReader.ReadInt32();

            if (collectionSize > 8000)
                throw new DetailedParsedException("collectionSize is an unusually large number");

            for (var i = 0; i < collectionSize; i++)
            {
                if (replay.ReplayBuild >= 55929)
                    bitReader.ReadBytes(8); // most likey an identifier for the item; first six bytes are 0x00
                else
                    playerCollection.Add(bitReader.ReadString(bitReader.ReadByte()));
            }

            // use to determine if the collection item is usable by the player (owns/free to play/internet cafe)
            if (bitReader.ReadInt32() != collectionSize)
                throw new DetailedParsedException("skinArrayLength not equal");

            for (var i = 0; i < collectionSize; i++)
            {
                for (var j = 0; j < 16; j++) // 16 is total player slots
                {
                    bitReader.ReadByte();

                    var num = bitReader.Read(8);

                    if (replay.ReplayBuild < 55929)
                    {
                        if (replay.ClientListByUserID[j] != null)
                        {
                            if (num > 0)
                                replay.ClientListByUserID[j].PlayerCollectionDictionary.Add(playerCollection[i], true);
                            else if (num == 0)
                                replay.ClientListByUserID[j].PlayerCollectionDictionary.Add(playerCollection[i], false);
                            else
                                throw new NotImplementedException();
                        }
                    }
                }
            }

            // Player info 
            // ------------------------

            // m_randomSeed, set it if it hasn't been set
            if (replay.RandomValue == 0)
                replay.RandomValue = (uint)bitReader.ReadInt32(); 
            else
                bitReader.ReadInt32();

            bitReader.ReadBytes(4);

            uint playerListLength = bitReader.Read(5);

            for (uint i = 0; i < playerListLength; i++)
            {
                bitReader.Read(32);

                bitReader.Read(5); // player index

                // toon
                bitReader.Read(8); // m_region
                if (bitReader.ReadStringFromBits(32, true) != "Hero") // m_programId
                    throw new DetailedParsedException("Not Hero");
                bitReader.Read(32); // m_realm
                bitReader.ReadLong(64); // m_id

                // internal toon
                bitReader.Read(8); // m_region
                if (bitReader.ReadStringFromBits(32, true) != "Hero") // m_programId
                    throw new DetailedParsedException("Not Hero");
                bitReader.Read(32); // m_realm

                int idLength = (int)bitReader.Read(7) + 2;
                bitReader.AlignToByte();
                replay.ClientListByUserID[i].BattleNetTId = bitReader.ReadString(idLength);

                bitReader.Read(6);

                if (replay.ReplayBuild <= 47479)
                {
                    // internal toon repeat
                    bitReader.Read(8); // m_region
                    if (bitReader.ReadStringFromBits(32, true) != "Hero") // m_programId
                        throw new DetailedParsedException("Not Hero");
                    bitReader.Read(32); // m_realm

                    idLength = (int)bitReader.Read(7) + 2;
                    bitReader.AlignToByte();
                    if (replay.ClientListByUserID[i].BattleNetTId != bitReader.ReadString(idLength))
                        throw new DetailedParsedException("Duplicate internal id does not match");
                }

                bitReader.Read(2);
                bitReader.ReadBytes(25);
                bitReader.Read(24);

                // bitReader.ReadBytes(8); ai games have 8 more bytes somewhere around here

                bitReader.Read(7);

                if (!bitReader.ReadBoolean())
                {
                    // repeat of the collection section above
                    if (replay.ReplayBuild >= 51609 || replay.ReplayBuild == 47903 || replay.ReplayBuild == 47479)
{'fraction_non_alphanumeric': 0.10499840815027062, 'fraction_numerical': 0.022285896211397645, 'mean_word_length': 1.3431299418170968, 'pattern_counts': {'":': 0, '<': 45, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3628573', 'n_tokens_mistral': 4342, 'n_tokens_neox': 3787, 'n_words': 958}