<commit_before>﻿using System.IO;

namespace OpenSage.Data.Map
{
    public sealed class SidesList : Asset
    {
        public const string AssetName = "SidesList";

        public Player[] Players { get; private set; }
        public Team[] Teams { get; private set; }
        public PlayerScriptsList PlayerScripts { get; private set; }

        internal static SidesList Parse(BinaryReader reader, MapParseContext context)
        {
            return ParseAsset(reader, context, version =>
            {
                if (version >= 6)
                {
                    var unknown = reader.ReadByte();
                    if (unknown != 1)
                    {
                        throw new InvalidDataException();
                    }
                }

                var numPlayers = reader.ReadUInt32();
                var players = new Player[numPlayers];

                for (var i = 0; i < numPlayers; i++)
                {
                    players[i] = Player.Parse(reader, context);
                }

                if (version >= 5)
                {
                    // Above version 5, teams and scripts are in separate top-level chunks.
                    return new SidesList
                    {
                        Players = players
                    };
                }

                var numTeams = reader.ReadUInt32();
                var teams = new Team[numTeams];

                for (var i = 0; i < numTeams; i++)
                {
                    teams[i] = Team.Parse(reader, context);
                }

                PlayerScriptsList playerScripts = null;

                ParseAssets(reader, context, assetName =>
                {
                    if (assetName != PlayerScriptsList.AssetName)
                    {
                        throw new InvalidDataException();
                    }

                    if (playerScripts != null)
                    {
                        throw new InvalidDataException();
                    }

                    playerScripts = PlayerScriptsList.Parse(reader, context);
                });

                return new SidesList
                {
                    Players = players,
                    Teams = teams,
                    PlayerScripts = playerScripts
                };
            });
        }

        internal void WriteTo(BinaryWriter writer, AssetNameCollection assetNames)
        {
            WriteAssetTo(writer, () =>
            {
                if (Version >= 6)
                {
                    writer.Write((byte) 1);
                }

                writer.Write((uint) Players.Length);

                foreach (var player in Players)
                {
                    player.WriteTo(writer, assetNames);
                }

                if (Version >= 5)
                {
                    return;
                }

                writer.Write((uint) Teams.Length);<commit_msg>Fix SidesList parsing for BFME maps<commit_after>﻿using System.IO;

namespace OpenSage.Data.Map
{
    public sealed class SidesList : Asset
    {
        public const string AssetName = "SidesList";

        public byte Unknown { get; private set; }

        public Player[] Players { get; private set; }
        public Team[] Teams { get; private set; }
        public PlayerScriptsList PlayerScripts { get; private set; }

        internal static SidesList Parse(BinaryReader reader, MapParseContext context)
        {
            return ParseAsset(reader, context, version =>
            {
                var unknown = (byte) 0;
                if (version >= 6)
                {
                    unknown = reader.ReadByte();
                    if (unknown != 0 && unknown != 1)
                    {
                        throw new InvalidDataException();
                    }
                }

                var numPlayers = reader.ReadUInt32();
                var players = new Player[numPlayers];

                for (var i = 0; i < numPlayers; i++)
                {
                    players[i] = Player.Parse(reader, context);
                }

                if (version >= 5)
                {
                    // Above version 5, teams and scripts are in separate top-level chunks.
                    return new SidesList
                    {
                        Unknown = unknown,
                        Players = players
                    };
                }

                var numTeams = reader.ReadUInt32();
                var teams = new Team[numTeams];

                for (var i = 0; i < numTeams; i++)
                {
                    teams[i] = Team.Parse(reader, context);
                }

                PlayerScriptsList playerScripts = null;

                ParseAssets(reader, context, assetName =>
                {
                    if (assetName != PlayerScriptsList.AssetName)
                    {
                        throw new InvalidDataException();
                    }

                    if (playerScripts != null)
                    {
                        throw new InvalidDataException();
                    }

                    playerScripts = PlayerScriptsList.Parse(reader, context);
                });

                return new SidesList
                {
                    Players = players,
                    Teams = teams,
                    PlayerScripts = playerScripts
                };
            });
        }

        internal void WriteTo(BinaryWriter writer, AssetNameCollection assetNames)
        {
            WriteAssetTo(writer, () =>
            {
                if (Version >= 6)
                {
                    writer.Write(Unknown);
                }

                writer.Write((uint) Players.Length);

                foreach (var player in Players)
                {
                    player.WriteTo(writer, assetNames);
                }

                if (Version >= 5)
                {
                    return;
                }

                writer.Write((uint) Teams.Length);
{'fraction_non_alphanumeric': 0.07766672128461412, 'fraction_numerical': 0.0044240537440602985, 'mean_word_length': 0.9696676347208777, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 17, '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': '397889', 'n_tokens_mistral': 1476, 'n_tokens_neox': 1277, 'n_words': 346}