<commit_before>﻿namespace Mappy.IO
{
    using System;
    using System.Collections.Generic;
    using System.Drawing;
    using System.Linq;

    using Mappy.Collections;
    using Mappy.Data;
    using Mappy.Models;

    using TAUtil;
    using TAUtil.Gdi.Bitmap;
    using TAUtil.Tdf;
    using TAUtil.Tnt;

    /// <summary>
    /// Provides methods for creating a MapModel instance
    /// from TNT and OTA files.
    /// </summary>
    public class MapModelFactory
    {
        public MapModel FromTntAndOta(ITntSource tnt, TdfNode ota)
        {
            var attrs = MapAttributes.Load(ota);
            var model = new MapModel(tnt.DataWidth, tnt.DataHeight, attrs);

            var schemaData = ota.Keys["GlobalHeader"].Keys["Schema 0"];
            if (schemaData.Keys.ContainsKey("features"))
            {
                var featureData = schemaData.Keys["features"];
                foreach (var data in featureData.Keys)
                {
                    TdfNode node = data.Value;
                    var x = TdfConvert.ToInt32(node.Entries["XPos"]);
                    var y = TdfConvert.ToInt32(node.Entries["ZPos"]);
                    var name = node.Entries["Featurename"];

                    var inst = new FeatureInstance(Guid.NewGuid(), name, x, y);
                    model.AddFeatureInstance(inst);
                }
            }

            return this.ReadTnt(tnt, model);
        }

        public MapModel FromTnt(ITntSource tnt)
        {
            MapModel m = new MapModel(tnt.DataWidth, tnt.DataHeight);

            return this.ReadTnt(tnt, m);
        }

        private static void ReadFeatures(ITntSource tnt, MapModel model, List<string> features)
        {
            var enumer = tnt.EnumerateAttrs().GetEnumerator();
            for (int y = 0; y < tnt.DataHeight * 2; y++)
            {
                for (int x = 0; x < tnt.DataWidth * 2; x++)
                {
                    enumer.MoveNext();
                    model.Tile.HeightGrid.Set(x, y, enumer.Current.Height);

                    switch (enumer.Current.Feature)
                    {
                        case TileAttr.FeatureNone:
                        case TileAttr.FeatureUnknown:
                            break;
                        case TileAttr.FeatureVoid:
                            model.Voids.Set(x, y, true);
                            break;
                        default:
                            var inst = new FeatureInstance(Guid.NewGuid(), features[enumer.Current.Feature], x, y);
                            model.AddFeatureInstance(inst);
                            break;
                    }
                }
            }
        }

        private static void ReadData(ITntSource tnt, MapModel model, List<Bitmap> tiles)
        {
            var enumer = tnt.EnumerateData().GetEnumerator();
            for (int y = 0; y < tnt.DataHeight; y++)
            {
                for (int x = 0; x < tnt.DataWidth; x++)
                {
                    enumer.MoveNext();
                    model.Tile.TileGrid.Set(x, y, tiles[enumer.Current]);
                }
            }
        }

        private MapModel ReadTnt(ITntSource tnt, MapModel model)
        {
            List<Bitmap> tiles = new List<Bitmap>(tnt.TileCount);
            tiles.AddRange(tnt.EnumerateTiles().Select(this.ToBitmap));

            ReadData(tnt, model, tiles);

            List<string> features = new List<string>(tnt.AnimCount);
            features.AddRange(tnt.EnumerateAnims());

            ReadFeatures(tnt, model, features);

            model.SeaLevel = tnt.SeaLevel;

            model.Minimap = this.ToBitmap(tnt.GetMinimap());

            return model;
        }

        private Bitmap ToBitmap(byte[] tile)
        {
            Bitmap bmp = BitmapConvert.ToBitmap(tile, MapConstants.TileWidth, MapConstants.TileHeight);
            return Globals.TileCache.GetOrAddBitmap(bmp);
        }

        private Bitmap ToBitmap(MinimapInfo minimap)
        {
            return BitmapConvert.ToBitmap(minimap.Data, minimap.Width, minimap.Height);
        }
    }
}
<commit_msg>Fix crash when loading East Indeez<commit_after>﻿namespace Mappy.IO
{
    using System;
    using System.Collections.Generic;
    using System.Drawing;
    using System.Linq;

    using Mappy.Collections;
    using Mappy.Data;
    using Mappy.Models;

    using TAUtil;
    using TAUtil.Gdi.Bitmap;
    using TAUtil.Tdf;
    using TAUtil.Tnt;

    /// <summary>
    /// Provides methods for creating a MapModel instance
    /// from TNT and OTA files.
    /// </summary>
    public class MapModelFactory
    {
        public MapModel FromTntAndOta(ITntSource tnt, TdfNode ota)
        {
            var attrs = MapAttributes.Load(ota);
            var model = new MapModel(tnt.DataWidth, tnt.DataHeight, attrs);

            this.ReadTnt(tnt, model);

            var schemaData = ota.Keys["GlobalHeader"].Keys["Schema 0"];
            if (schemaData.Keys.ContainsKey("features"))
            {
                var featureData = schemaData.Keys["features"];
                foreach (var data in featureData.Keys)
                {
                    TdfNode node = data.Value;
                    var x = TdfConvert.ToInt32(node.Entries["XPos"]);
                    var y = TdfConvert.ToInt32(node.Entries["ZPos"]);
                    var name = node.Entries["Featurename"];

                    if (!model.HasFeatureInstanceAt(x, y))
                    {
                        var inst = new FeatureInstance(Guid.NewGuid(), name, x, y);
                        model.AddFeatureInstance(inst);
                    }
                }
            }

            return model;
        }

        public MapModel FromTnt(ITntSource tnt)
        {
            MapModel m = new MapModel(tnt.DataWidth, tnt.DataHeight);

            return this.ReadTnt(tnt, m);
        }

        private static void ReadFeatures(ITntSource tnt, MapModel model, List<string> features)
        {
            var enumer = tnt.EnumerateAttrs().GetEnumerator();
            for (int y = 0; y < tnt.DataHeight * 2; y++)
            {
                for (int x = 0; x < tnt.DataWidth * 2; x++)
                {
                    enumer.MoveNext();
                    model.Tile.HeightGrid.Set(x, y, enumer.Current.Height);

                    switch (enumer.Current.Feature)
                    {
                        case TileAttr.FeatureNone:
                        case TileAttr.FeatureUnknown:
                            break;
                        case TileAttr.FeatureVoid:
                            model.Voids.Set(x, y, true);
                            break;
                        default:
                            var inst = new FeatureInstance(Guid.NewGuid(), features[enumer.Current.Feature], x, y);
                            model.AddFeatureInstance(inst);
                            break;
                    }
                }
            }
        }

        private static void ReadData(ITntSource tnt, MapModel model, List<Bitmap> tiles)
        {
            var enumer = tnt.EnumerateData().GetEnumerator();
            for (int y = 0; y < tnt.DataHeight; y++)
            {
                for (int x = 0; x < tnt.DataWidth; x++)
                {
                    enumer.MoveNext();
                    model.Tile.TileGrid.Set(x, y, tiles[enumer.Current]);
                }
            }
        }

        private MapModel ReadTnt(ITntSource tnt, MapModel model)
        {
            List<Bitmap> tiles = new List<Bitmap>(tnt.TileCount);
            tiles.AddRange(tnt.EnumerateTiles().Select(this.ToBitmap));

            ReadData(tnt, model, tiles);

            List<string> features = new List<string>(tnt.AnimCount);
            features.AddRange(tnt.EnumerateAnims());

            ReadFeatures(tnt, model, features);

            model.SeaLevel = tnt.SeaLevel;

            model.Minimap = this.ToBitmap(tnt.GetMinimap());

            return model;
        }

        private Bitmap ToBitmap(byte[] tile)
        {
            Bitmap bmp = BitmapConvert.ToBitmap(tile, MapConstants.TileWidth, MapConstants.TileHeight);
            return Globals.TileCache.GetOrAddBitmap(bmp);
        }

        private Bitmap ToBitmap(MinimapInfo minimap)
        {
            return BitmapConvert.ToBitmap(minimap.Data, minimap.Width, minimap.Height);
        }
    }
}

{'fraction_non_alphanumeric': 0.0958730888607886, 'fraction_numerical': 0.002529026324864927, 'mean_word_length': 1.331814526936478, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 19, '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': '4091778', 'n_tokens_mistral': 2432, 'n_tokens_neox': 2257, 'n_words': 483}