<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.IO;
using UnityEngine;

namespace BuildingThemes
{
    public class Configuration
    {
        [XmlArray(ElementName = "Themes")]
        [XmlArrayItem(ElementName = "Theme")]
        public List<Theme> themes = new List<Theme>();

        public Theme getTheme(string name)
        {
            foreach (Theme theme in themes)
            {
                if (theme.name == name) return theme;
            }
            return null;
        }

        public class Theme
        {
            [XmlAttribute("name")]
            public string name;

            [XmlIgnoreAttribute]
            public bool isBuiltIn = false;

            [XmlArray(ElementName = "Buildings")]
            [XmlArrayItem(ElementName = "Building")]
            public List<Building> buildings = new List<Building>();

            public void addAll(string[] buildingNames, bool builtIn)
            {
                foreach (string b in buildingNames)
                {
                    if(!containsBuilding(b))
                    {
                        buildings.Add(new Building(b, builtIn));
                    }
                }
            }
            public bool containsBuilding(string name)
            {
                foreach (Building building in buildings)
                {
                    if (building.name == name) return true;
                }
                return false;
            }

            public Theme(string name)
            {
                this.name = name;
            }

            public Theme()
            {
            }
        }

        public class Building
        {
            [XmlAttribute("name")]
            public string name;

            [XmlIgnoreAttribute]
            public bool isBuiltIn = false;

            public Building(string name)
            {
                this.name = name;
            }

            public Building(string name, bool builtIn)
            {
                this.name = name;
                this.isBuiltIn = true;
            }

            public Building()
            {
            }
        }

        public static Configuration Deserialize(string filename)
        {
            if (!File.Exists(filename)) return null;

            XmlSerializer xmlSerializer = new XmlSerializer(typeof(Configuration));
            try
            {
                using (System.IO.StreamReader streamReader = new System.IO.StreamReader(filename))
                {
                    return (Configuration)xmlSerializer.Deserialize(streamReader);
                }
            }
            catch (Exception e)
            {
                Debug.Log("Couldn't load configuration (XML malformed?)");
                throw e;
            }
        }

        public static void Serialize(string filename, Configuration config)
        {
            XmlSerializer xmlSerializer = new XmlSerializer(typeof(Configuration));
            try
            {
                using (System.IO.StreamWriter streamWriter = new System.IO.StreamWriter(filename))
                {
                    Configuration configCopy = new Configuration();
                    foreach (var theme in config.themes)
                    {
                        if (!theme.isBuiltIn)
                        {
                            Theme newTheme = new Theme(theme.name);

                            foreach (var building in theme.buildings)
                            {
                                if (!building.isBuiltIn) 
                                {
                                    newTheme.buildings.Add(building);
                                }
                            }
                            
                            configCopy.themes.Add(newTheme);
                        }
                    }

                    xmlSerializer.Serialize(streamWriter, configCopy);
                }
            }
            catch (Exception e)
            {
                Debug.Log("Couldn't create configuration file at \"" + Directory.GetCurrentDirectory() + "\"");
                throw e;
            }
        }

        public static void addBuiltInEuropeanTheme(Configuration config)
        {
            var theme = config.getTheme("European");

            if(theme == null) 
            {
                theme = new Theme("European");
                //theme.isBuiltIn = true;
                config.themes.Add(theme);
            }
            theme.addAll(sharedBuildings, true);
            theme.addAll(euroOnlyBuildings, true);
        }

        public static void addBuiltInInternationalTheme(Configuration config)
        {
            var theme = config.getTheme("International");

            if (theme == null)
            {
                theme = new Theme("International");
                //theme.isBuiltIn = true;
                config.themes.Add(theme);
            }
            //intTheme.isBuiltIn = true;
            theme.addAll(sharedBuildings, true);
            theme.addAll(intOnlyBuildings, true);
        }

        private static string[] sharedBuildings = {
                                         "Agricultural 1x1 processing 1",
                                         "Agricultural 2x2 processing 2",
                                         "Agricultural 3x2 processing 2",
                                         "Agricultural 3x3 Processing 03",
                                         "Agricultural 3x3 Processing 04",
                                         "Agricultural 4x4 Processing 01",
                                         "Agricultural 4x4 Processing 02",
                                         "agricultural_building_05",
                                         "cargoyard",
                                         "Farming 4x4 Farm",
                                         "Farming 4x4 Farm 2",
                                         "Farming 4x4 Farm 3",
                                         "Farming2x2",
                                         "Farming3x2",
                                         "Farming4x4",
                                         "Farming4x4_02",
                                         "Farming4x4_03",
                                         "Forestry 1x1",
                                         "Forestry 2x2",
                                         "Forestry 3x3 Extractor",
                                         "Forestry 3x3 Forest",
                                         "Forestry 3x3 Processing",
                                         "Forestry 3x3 Processing 2",<commit_msg>Fix my PR comments<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.IO;
using UnityEngine;

namespace BuildingThemes
{
    public class Configuration
    {
        [XmlArray(ElementName = "Themes")]
        [XmlArrayItem(ElementName = "Theme")]
        public List<Theme> themes = new List<Theme>();

        public Theme getTheme(string name)
        {
            foreach (Theme theme in themes)
            {
                if (theme.name == name) return theme;
            }
            return null;
        }

        public class Theme
        {
            [XmlAttribute("name")]
            public string name;

            [XmlIgnoreAttribute]
            public bool isBuiltIn = false;

            [XmlArray(ElementName = "Buildings")]
            [XmlArrayItem(ElementName = "Building")]
            public List<Building> buildings = new List<Building>();

            public void addAll(string[] buildingNames, bool builtIn)
            {
                foreach (string b in buildingNames)
                {
                    if (!containsBuilding(b))
                    {
                        buildings.Add(new Building(b, builtIn));
                    }
                }
            }
            public bool containsBuilding(string name)
            {
                foreach (Building building in buildings)
                {
                    if (building.name == name) return true;
                }
                return false;
            }

            public Theme(string name)
            {
                this.name = name;
            }

            public Theme()
            {
            }
        }

        public class Building
        {
            [XmlAttribute("name")]
            public string name;

            [XmlIgnoreAttribute]
            public bool isBuiltIn = false;

            public Building(string name)
            {
                this.name = name;
            }

            public Building(string name, bool builtIn)
            {
                this.name = name;
                this.isBuiltIn = true;
            }

            public Building()
            {
            }
        }

        public static Configuration Deserialize(string filename)
        {
            if (!File.Exists(filename)) return null;

            XmlSerializer xmlSerializer = new XmlSerializer(typeof(Configuration));
            try
            {
                using (System.IO.StreamReader streamReader = new System.IO.StreamReader(filename))
                {
                    return (Configuration)xmlSerializer.Deserialize(streamReader);
                }
            }
            catch (Exception e)
            {
                Debug.Log("Couldn't load configuration (XML malformed?)");
                throw e;
            }
        }

        public static void Serialize(string filename, Configuration config)
        {
            var xmlSerializer = new XmlSerializer(typeof(Configuration));
            try
            {
                using (System.IO.StreamWriter streamWriter = new System.IO.StreamWriter(filename))
                {
                    var configCopy = new Configuration();
                    foreach (var theme in config.themes)
                    {
                        var newTheme = new Theme(theme.name);
                        foreach (var building in theme.buildings.Where(building => !theme.isBuiltIn || !building.isBuiltIn))
                        {
                            newTheme.buildings.Add(building);
                        }

                        configCopy.themes.Add(newTheme);
                    }

                    xmlSerializer.Serialize(streamWriter, configCopy);
                }
            }
            catch (Exception e)
            {
                Debug.Log("Couldn't create configuration file at \"" + Directory.GetCurrentDirectory() + "\"");
                throw e;
            }
        }

        public static void addBuiltInEuropeanTheme(Configuration config)
        {
            addBuiltInTheme(config, "European", euroOnlyBuildings);
        }

        public static void addBuiltInInternationalTheme(Configuration config)
        {
            addBuiltInTheme(config, "International", intOnlyBuildings);
        }

        private static void addBuiltInTheme(Configuration config, string themeName, string[] specificBuildings)
        {
            var theme = config.getTheme(themeName);

            if (theme == null)
            {
                theme = new Theme(themeName);
                //theme.isBuiltIn = true;
                config.themes.Add(theme);
            }
            theme.addAll(sharedBuildings, true);
            theme.addAll(specificBuildings, true);
        }

        private static string[] sharedBuildings = {
                                         "Agricultural 1x1 processing 1",
                                         "Agricultural 2x2 processing 2",
                                         "Agricultural 3x2 processing 2",
                                         "Agricultural 3x3 Processing 03",
                                         "Agricultural 3x3 Processing 04",
                                         "Agricultural 4x4 Processing 01",
                                         "Agricultural 4x4 Processing 02",
                                         "agricultural_building_05",
                                         "cargoyard",
                                         "Farming 4x4 Farm",
                                         "Farming 4x4 Farm 2",
                                         "Farming 4x4 Farm 3",
                                         "Farming2x2",
                                         "Farming3x2",
                                         "Farming4x4",
                                         "Farming4x4_02",
                                         "Farming4x4_03",
                                         "Forestry 1x1",
                                         "Forestry 2x2",
                                         "Forestry 3x3 Extractor",
                                         "Forestry 3x3 Forest",
                                         "Forestry 3x3 Processing",
                                         "Forestry 3x3 Processing 2",
{'fraction_non_alphanumeric': 0.07189045264359072, 'fraction_numerical': 0.009433244579688095, 'mean_word_length': 0.9885040084707306, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 8}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 94, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '794152', 'n_tokens_mistral': 2916, 'n_tokens_neox': 2639, 'n_words': 651}