<commit_before>﻿using FactorioModBuilder.Build.Data;
using FactorioModBuilder.Build.Messages;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Json;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace FactorioModBuilder.Build.Extensions
{
    public enum ExtensionType
    {
        None,
        Project,
        FactorioInfo,
        FactorioDependencies,
        FactorioControl,
        FactorioData,
        FactorioLocale,
        Prototypes,
        PrototypeEntities,
        PrototypeEquipment,
        PrototypeFluids,
        PrototypeGroups,
        PrototypeItems,
        PrototypeRecipes,
        PrototypeTechnologies,
        PrototypeTiles
    }

    public abstract class ExtensionBase<T> : ICompilerExtension where T : DataUnit
    {
        public ExtensionType Extension { get; private set; }
        public IEnumerable<ExtensionType> Dependencies { get; private set; }

        private Compiler _compiler;

        protected string ProjectName 
        { 
            get { return _compiler.ProjectName; }
            set { _compiler.ProjectName = value; }
        }

        protected string TemporaryDirectory
        {
            get { return _compiler.TemporaryDirectory; }
            set { _compiler.TemporaryDirectory = value; }
        }

        protected string OutputDirectory
        {
            get { return _compiler.OutputDirectory; }
            set { _compiler.OutputDirectory = value; }
        }

        protected HashSet<string> GroupNames { get { return _compiler.GroupNames; } }

        protected HashSet<string> SubGroupNames { get { return _compiler.SubGroupNames; } }

        protected HashSet<string> ItemNames { get { return _compiler.ItemNames; } }

        public ExtensionBase(ExtensionType extension, 
            params ExtensionType[] dependencies)
        {
            this.Extension = extension;
            if (dependencies != null)
                this.Dependencies = dependencies.ToList();
            else
                this.Dependencies = new List<ExtensionType>();
        }

        public void AttachToCompiler(Compiler c)
        {
            this._compiler = c;
        }

        public bool BuildUnit(IEnumerable<DataUnit> units)
        {
            if(units == null)
                throw new ArgumentNullException("units");

            try
            {
                return this.BuildUnit(units.Cast<T>());
            }
            catch(InvalidCastException)
            {
                this.Error("Expected {0}, recieved {1}", typeof(T).Name, 
                    units.Any() ? units.First().GetType().Name : "null");
                return false;
            }
        }

        protected abstract bool BuildUnit(IEnumerable<T> units);

        protected void Info(string format, params object[] args)
        {
            if (_compiler != null)
                _compiler.BuildMessages.Add(
                    new InfoMessage(format, args));
        }

        protected void Warning(WarningLevel level, string format, params object[] args)
        {
            if (_compiler != null)
                _compiler.BuildMessages.Add(
                    new WarningMessage(level, format, args));
        }

        protected void Error(string format, params object[] args)
        {
            if (_compiler != null)
                _compiler.BuildMessages.Add(
                    new ErrorMessage(format, args));
        }

        protected void Fatal(string format, params object[] args)
        {
            if (_compiler != null)
                _compiler.BuildMessages.Add(
                    new FatalMessage(format, args));
        }

        protected ICompilerExtension GetCompilerExtension(ExtensionType type)
        {
            ICompilerExtension ext;
            if (!_compiler.TryGetExtension(type, out ext))
                throw new Exception("Could not resolve compiler extension for type: " + ext.GetType().Name);
            return ext;
        }
    }
}
<commit_msg>Added second catch block to remove the need for extension implementers to catch exceptions<commit_after>﻿using FactorioModBuilder.Build.Data;
using FactorioModBuilder.Build.Messages;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Json;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace FactorioModBuilder.Build.Extensions
{
    public enum ExtensionType
    {
        None,
        Project,
        FactorioInfo,
        FactorioDependencies,
        FactorioControl,
        FactorioData,
        FactorioLocale,
        Prototypes,
        PrototypeEntities,
        PrototypeEquipment,
        PrototypeFluids,
        PrototypeGroups,
        PrototypeItems,
        PrototypeRecipes,
        PrototypeTechnologies,
        PrototypeTiles
    }

    public abstract class ExtensionBase<T> : ICompilerExtension where T : DataUnit
    {
        public ExtensionType Extension { get; private set; }
        public IEnumerable<ExtensionType> Dependencies { get; private set; }

        private Compiler _compiler;

        protected string ProjectName 
        { 
            get { return _compiler.ProjectName; }
            set { _compiler.ProjectName = value; }
        }

        protected string TemporaryDirectory
        {
            get { return _compiler.TemporaryDirectory; }
            set { _compiler.TemporaryDirectory = value; }
        }

        protected string OutputDirectory
        {
            get { return _compiler.OutputDirectory; }
            set { _compiler.OutputDirectory = value; }
        }

        protected HashSet<string> GroupNames { get { return _compiler.GroupNames; } }

        protected HashSet<string> SubGroupNames { get { return _compiler.SubGroupNames; } }

        protected HashSet<string> ItemNames { get { return _compiler.ItemNames; } }

        public ExtensionBase(ExtensionType extension, 
            params ExtensionType[] dependencies)
        {
            this.Extension = extension;
            if (dependencies != null)
                this.Dependencies = dependencies.ToList();
            else
                this.Dependencies = new List<ExtensionType>();
        }

        public void AttachToCompiler(Compiler c)
        {
            this._compiler = c;
        }

        public bool BuildUnit(IEnumerable<DataUnit> units)
        {
            if(units == null)
                throw new ArgumentNullException("units");

            try
            {
                return this.BuildUnit(units.Cast<T>());
            }
            catch(InvalidCastException)
            {
                this.Error("Expected {0}, recieved {1}", typeof(T).Name, 
                    units.Any() ? units.First().GetType().Name : "null");
                return false;
            }
            catch(Exception e)
            {
                this.Fatal("Encountered an exception in {0} building data unit: {1}",
                    this.GetType().Name, e.Message);
                return false;
            }
        }

        protected abstract bool BuildUnit(IEnumerable<T> units);

        protected void Info(string format, params object[] args)
        {
            if (_compiler != null)
                _compiler.BuildMessages.Add(
                    new InfoMessage(format, args));
        }

        protected void Warning(WarningLevel level, string format, params object[] args)
        {
            if (_compiler != null)
                _compiler.BuildMessages.Add(
                    new WarningMessage(level, format, args));
        }

        protected void Error(string format, params object[] args)
        {
            if (_compiler != null)
                _compiler.BuildMessages.Add(
                    new ErrorMessage(format, args));
        }

        protected void Fatal(string format, params object[] args)
        {
            if (_compiler != null)
                _compiler.BuildMessages.Add(
                    new FatalMessage(format, args));
        }

        protected ICompilerExtension GetCompilerExtension(ExtensionType type)
        {
            ICompilerExtension ext;
            if (!_compiler.TryGetExtension(type, out ext))
                throw new Exception("Could not resolve compiler extension for type: " + ext.GetType().Name);
            return ext;
        }
    }
}

{'fraction_non_alphanumeric': 0.07954411937492657, 'fraction_numerical': 0.0007049700387733521, 'mean_word_length': 1.830728300631859, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, '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': '3396194', 'n_tokens_mistral': 2104, 'n_tokens_neox': 1993, 'n_words': 510}