<commit_before>﻿using System.IO;
using DataTool.Flag;
using TankLib;
using TankLib.ExportFormats;
using TankLib.STU;
using TankLib.STU.Types;
using static DataTool.Helper.IO;
using static DataTool.Helper.STUHelper;
using static DataTool.Helper.Logger;
using System;
using DataTool.DataModels;

namespace DataTool.SaveLogic {
    public static class Map {
        /// <summary>
        /// OWMAP format
        /// </summary>
        public class OverwatchMap : IExportFormat {
            public string Extension => "owmap";

            public string Name;

            public FindLogic.Combo.ComboInfo Info;

            public teMapPlaceableData SingleModels;
            public teMapPlaceableData ModelGroups;
            public teMapPlaceableData Models;
            public teMapPlaceableData Entities;
            public teMapPlaceableData Lights;
            public teMapPlaceableData Sounds;
            public teMapPlaceableData Effects;

            public OverwatchMap(
                string name, FindLogic.Combo.ComboInfo info, teMapPlaceableData singleModels,
                teMapPlaceableData modelGroups, teMapPlaceableData models, teMapPlaceableData entities,
                teMapPlaceableData lights, teMapPlaceableData sounds, teMapPlaceableData effects) {
                Name = name;
                Info = info;

                SingleModels = singleModels ?? new teMapPlaceableData();
                ModelGroups = modelGroups ?? new teMapPlaceableData();
                Models = models ?? new teMapPlaceableData();
                Entities = entities ?? new teMapPlaceableData();
                Lights = lights ?? new teMapPlaceableData();
                Sounds = sounds ?? new teMapPlaceableData();
                Effects = effects ?? new teMapPlaceableData();
            }

            private string GetModelLookMatPath(FindLogic.Combo.ModelAsset modelInfo, FindLogic.Combo.ModelLookAsset modelLookAsset) {
                return Path.Combine("Models", modelInfo.GetName(), "ModelLooks", modelLookAsset.GetNameIndex() + ".owmat");
            }

            private string GetModelPath(FindLogic.Combo.ModelAsset modelInfo) {
                return Path.Combine("Models", modelInfo.GetName(), modelInfo.GetNameIndex() + ".owmdl");
            }

            public void Write(Stream output) {
                using (BinaryWriter writer = new BinaryWriter(output)) {
                    writer.Write((ushort) 1); // version major
                    writer.Write((ushort) 2); // version minor

                    if (Name.Length == 0) {
                        writer.Write((byte) 0);
                    } else {
                        writer.Write(Name);
                    }

                    writer.Write(ModelGroups.Header.PlaceableCount); // nr objects

                    int entitiesWithModelCount = 0;
                    STUModelComponent[] modelComponents = new STUModelComponent[Entities.Header.PlaceableCount];

                    for (int i = 0; i < Entities.Header.PlaceableCount; i++) {

                        teMapPlaceableEntity entity = (teMapPlaceableEntity) Entities.Placeables[i];
                        FindLogic.Combo.Find(Info, entity.Header.EntityDefinition);

                        var modelComponent = GetInstance<STUModelComponent>(entity.Header.EntityDefinition);
                        modelComponents[i] = modelComponent;
                        if (modelComponent != null) {
                            entitiesWithModelCount += 1;
                        }

                        foreach (STUComponentInstanceData instanceData in entity.InstanceData) {
                            if (instanceData is STUStatescriptComponentInstanceData statescriptComponentInstanceData) {
                                if (statescriptComponentInstanceData.m_6D10093E != null) {
                                    foreach (STUStatescriptGraphWithOverrides graphWithOverrides in statescriptComponentInstanceData.m_6D10093E) {
                                        FindLogic.Combo.Find(Info, graphWithOverrides);
                                    }
                                }

                                if (statescriptComponentInstanceData.m_2D9815BA != null) {
                                    // todo: ??
                                }
                            }
                        }<commit_msg>fix map model entities with no model<commit_after>﻿using System.IO;
using DataTool.Flag;
using TankLib;
using TankLib.ExportFormats;
using TankLib.STU;
using TankLib.STU.Types;
using static DataTool.Helper.IO;
using static DataTool.Helper.STUHelper;
using static DataTool.Helper.Logger;
using System;
using DataTool.DataModels;

namespace DataTool.SaveLogic {
    public static class Map {
        /// <summary>
        /// OWMAP format
        /// </summary>
        public class OverwatchMap : IExportFormat {
            public string Extension => "owmap";

            public string Name;

            public FindLogic.Combo.ComboInfo Info;

            public teMapPlaceableData SingleModels;
            public teMapPlaceableData ModelGroups;
            public teMapPlaceableData Models;
            public teMapPlaceableData Entities;
            public teMapPlaceableData Lights;
            public teMapPlaceableData Sounds;
            public teMapPlaceableData Effects;

            public OverwatchMap(
                string name, FindLogic.Combo.ComboInfo info, teMapPlaceableData singleModels,
                teMapPlaceableData modelGroups, teMapPlaceableData models, teMapPlaceableData entities,
                teMapPlaceableData lights, teMapPlaceableData sounds, teMapPlaceableData effects) {
                Name = name;
                Info = info;

                SingleModels = singleModels ?? new teMapPlaceableData();
                ModelGroups = modelGroups ?? new teMapPlaceableData();
                Models = models ?? new teMapPlaceableData();
                Entities = entities ?? new teMapPlaceableData();
                Lights = lights ?? new teMapPlaceableData();
                Sounds = sounds ?? new teMapPlaceableData();
                Effects = effects ?? new teMapPlaceableData();
            }

            private string GetModelLookMatPath(FindLogic.Combo.ModelAsset modelInfo, FindLogic.Combo.ModelLookAsset modelLookAsset) {
                return Path.Combine("Models", modelInfo.GetName(), "ModelLooks", modelLookAsset.GetNameIndex() + ".owmat");
            }

            private string GetModelPath(FindLogic.Combo.ModelAsset modelInfo) {
                return Path.Combine("Models", modelInfo.GetName(), modelInfo.GetNameIndex() + ".owmdl");
            }

            public void Write(Stream output) {
                using (BinaryWriter writer = new BinaryWriter(output)) {
                    writer.Write((ushort) 1); // version major
                    writer.Write((ushort) 2); // version minor

                    if (Name.Length == 0) {
                        writer.Write((byte) 0);
                    } else {
                        writer.Write(Name);
                    }

                    writer.Write(ModelGroups.Header.PlaceableCount); // nr objects

                    int entitiesWithModelCount = 0;
                    STUModelComponent[] modelComponents = new STUModelComponent[Entities.Header.PlaceableCount];

                    for (int i = 0; i < Entities.Header.PlaceableCount; i++) {

                        teMapPlaceableEntity entity = (teMapPlaceableEntity) Entities.Placeables[i];
                        FindLogic.Combo.Find(Info, entity.Header.EntityDefinition);

                        var modelComponent = GetInstance<STUModelComponent>(entity.Header.EntityDefinition);

                        if (modelComponent != null && modelComponent.m_model != 0) {
                            modelComponents[i] = modelComponent;
                            entitiesWithModelCount += 1;
                        }

                        foreach (STUComponentInstanceData instanceData in entity.InstanceData) {
                            if (instanceData is STUStatescriptComponentInstanceData statescriptComponentInstanceData) {
                                if (statescriptComponentInstanceData.m_6D10093E != null) {
                                    foreach (STUStatescriptGraphWithOverrides graphWithOverrides in statescriptComponentInstanceData.m_6D10093E) {
                                        FindLogic.Combo.Find(Info, graphWithOverrides);
                                    }
                                }

                                if (statescriptComponentInstanceData.m_2D9815BA != null) {
                                    // todo: ??
                                }
                            }
                        }
{'fraction_non_alphanumeric': 0.07400293752118405, 'fraction_numerical': 0.005536097616088578, 'mean_word_length': 1.6783661119515885, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4087988', 'n_tokens_mistral': 2148, 'n_tokens_neox': 2005, 'n_words': 456}