<commit_before>using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.Rendering;
using Votyra.Core.Models;
using Votyra.Core.Pooling;
using Votyra.Core.Profiling;
using Votyra.Core.TerrainMeshes;
using Votyra.Core.Utils;

namespace Votyra.Core.MeshUpdaters
{
    public class TerrainMeshUpdater<TKey> : IMeshUpdater<TKey>
    {
        private readonly IProfiler _profiler;
        private readonly Func<GameObject> _gameObjectFactory;
        private SetDictionary<TKey, MeshUnityData> _meshFilters = new SetDictionary<TKey, MeshUnityData>();

        public TerrainMeshUpdater(Func<GameObject> gameObjectFactory, IProfiler profiler)
        {
            _gameObjectFactory = gameObjectFactory;
            _profiler = profiler;
        }

        public IReadOnlySet<TKey> ExistingGroups => _meshFilters;

        public void UpdateMesh(IReadOnlyDictionary<TKey, UnityMesh> terrainMeshes, IReadOnlySet<TKey> toKeepGroups)
        {
            if (terrainMeshes != null)
            {
                using (_profiler.Start("Setting Mesh"))
                {
                    var toDeleteGroups = _meshFilters.Keys.Except(terrainMeshes.Keys).Except(toKeepGroups).ToList();

                    int meshIndex = 0;
                    foreach (var terrainMesh in terrainMeshes)
                    {
                        if (terrainMesh.Value == null || terrainMesh.Value.VertexCount == 0)
                        {
                            if (_meshFilters.ContainsKey(terrainMesh.Key))
                            {
                                _meshFilters[terrainMesh.Key]?.Dispose();
                            }
                            _meshFilters[terrainMesh.Key] = null;
                            continue;
                        }
                        var unityData = _meshFilters.TryGetValue(terrainMesh.Key);
                        if (unityData == null)
                        {
                            if (toDeleteGroups.Count > 0)
                            {
                                int toDeleteIndex = toDeleteGroups.Count - 1;
                                var toDeleteKey = toDeleteGroups[toDeleteIndex];
                                unityData = _meshFilters[toDeleteKey];
                                _meshFilters.Remove(toDeleteKey);
                                _meshFilters[terrainMesh.Key] = unityData;

                                toDeleteGroups.RemoveAt(toDeleteIndex);
                            }
                            if (unityData == null)
                            {
                                unityData = CreateMeshObject();
                                _meshFilters[terrainMesh.Key] = unityData;
                            }
                        }

                        var triangleMesh = terrainMesh.Value;
                        UpdateMesh(triangleMesh, unityData.MeshFilter.sharedMesh);

                        unityData.MeshCollider.sharedMesh = null;
                        unityData.MeshCollider.sharedMesh = unityData.MeshFilter.sharedMesh;

                        meshIndex++;
                    }

                    foreach (var toDeleteGroup in toDeleteGroups)
                    {
                        _meshFilters[toDeleteGroup]?.Dispose();
                        _meshFilters.Remove(toDeleteGroup);
                    }
                }
            }
        }

        private void UpdateMesh(UnityMesh triangleMesh, Mesh mesh)
        {
            SetMeshFormat(mesh, triangleMesh.VertexCount);

            bool reinitializeMesh = mesh.vertexCount != triangleMesh.VertexCount;
            if (reinitializeMesh)
            {
                mesh.Clear();
            }

            mesh.vertices = triangleMesh.Vertices;
            mesh.normals = triangleMesh.Normals;

            if (reinitializeMesh)
            {
                mesh.uv = triangleMesh.UV;
                mesh.SetTriangles(triangleMesh.Indices, 0, false);
                mesh.bounds = triangleMesh.MeshBounds;
            }
        }

        private void SetMeshFormat(Mesh mesh, int vertexCount)
        {
            if (vertexCount > 65000 && mesh.indexFormat != IndexFormat.UInt32)
            {
                mesh.indexFormat = IndexFormat.UInt32;
            }
            else if (vertexCount < 65000 && mesh.indexFormat != IndexFormat.UInt16)<commit_msg>simple: fix for missing groups<commit_after>using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.Rendering;
using Votyra.Core.Models;
using Votyra.Core.Pooling;
using Votyra.Core.Profiling;
using Votyra.Core.TerrainMeshes;
using Votyra.Core.Utils;

namespace Votyra.Core.MeshUpdaters
{
    public class TerrainMeshUpdater<TKey> : IMeshUpdater<TKey>
    {
        private readonly IProfiler _profiler;
        private readonly Func<GameObject> _gameObjectFactory;
        private SetDictionary<TKey, MeshUnityData> _meshFilters = new SetDictionary<TKey, MeshUnityData>();

        public TerrainMeshUpdater(Func<GameObject> gameObjectFactory, IProfiler profiler)
        {
            _gameObjectFactory = gameObjectFactory;
            _profiler = profiler;
        }

        public IReadOnlySet<TKey> ExistingGroups => _meshFilters;

        public void UpdateMesh(IReadOnlyDictionary<TKey, UnityMesh> terrainMeshes, IReadOnlySet<TKey> toKeepGroups)
        {
            if (terrainMeshes != null)
            {
                using (_profiler.Start("Setting Mesh"))
                {
                    var toDeleteGroups = _meshFilters.Keys.Except(terrainMeshes.Keys).Except(toKeepGroups).ToList();

                    int meshIndex = 0;
                    foreach (var terrainMesh in terrainMeshes)
                    {
                        if (terrainMesh.Value == null || terrainMesh.Value.VertexCount == 0)
                        {
                            if (_meshFilters.ContainsKey(terrainMesh.Key))
                            {
                                _meshFilters[terrainMesh.Key]?.Dispose();
                            }
                            _meshFilters[terrainMesh.Key] = null;
                            continue;
                        }
                        var unityData = _meshFilters.TryGetValue(terrainMesh.Key);
                        if (unityData == null)
                        {
                            if (toDeleteGroups.Count > 0)
                            {
                                int toDeleteIndex = toDeleteGroups.Count - 1;
                                var toDeleteKey = toDeleteGroups[toDeleteIndex];
                                unityData = _meshFilters[toDeleteKey];
                                _meshFilters.Remove(toDeleteKey);
                                _meshFilters[terrainMesh.Key] = unityData;

                                toDeleteGroups.RemoveAt(toDeleteIndex);
                            }
                            if (unityData == null)
                            {
                                unityData = CreateMeshObject();
                                _meshFilters[terrainMesh.Key] = unityData;
                            }
                        }

                        var triangleMesh = terrainMesh.Value;
                        UpdateMesh(triangleMesh, unityData.MeshFilter.sharedMesh);

                        unityData.MeshCollider.sharedMesh = null;
                        unityData.MeshCollider.sharedMesh = unityData.MeshFilter.sharedMesh;

                        meshIndex++;
                    }

                    foreach (var toDeleteGroup in toDeleteGroups)
                    {
                        _meshFilters[toDeleteGroup]?.Dispose();
                        _meshFilters.Remove(toDeleteGroup);
                    }
                }
            }
        }

        private void UpdateMesh(UnityMesh triangleMesh, Mesh mesh)
        {
            SetMeshFormat(mesh, triangleMesh.VertexCount);

            bool reinitializeMesh = mesh.vertexCount != triangleMesh.VertexCount;
            if (reinitializeMesh)
            {
                mesh.Clear();
            }

            mesh.vertices = triangleMesh.Vertices;
            mesh.normals = triangleMesh.Normals;
            mesh.bounds = triangleMesh.MeshBounds;

            if (reinitializeMesh)
            {
                mesh.uv = triangleMesh.UV;
                mesh.SetTriangles(triangleMesh.Indices, 0, false);
            }
        }

        private void SetMeshFormat(Mesh mesh, int vertexCount)
        {
            if (vertexCount > 65000 && mesh.indexFormat != IndexFormat.UInt32)
            {
                mesh.indexFormat = IndexFormat.UInt32;
            }
            else if (vertexCount < 65000 && mesh.indexFormat != IndexFormat.UInt16)
{'fraction_non_alphanumeric': 0.07201622900935423, 'fraction_numerical': 0.004733461061647695, 'mean_word_length': 1.3175763907025333, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4552950', 'n_tokens_mistral': 2231, 'n_tokens_neox': 2112, 'n_words': 367}