<commit_before>﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Reflection;

namespace Kerobot
{
    static class ModuleLoader
    {
        private const string LogName = nameof(ModuleLoader);

        /// <summary>
        /// Given the instance configuration, loads all appropriate types from file specified in it.
        /// </summary>
        internal static ReadOnlyCollection<ModuleBase> Load(InstanceConfig conf, Kerobot k)
        {
            var path = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location) + Path.DirectorySeparatorChar;
            var modules = new List<ModuleBase>();

            try
            {
                foreach (var file in conf.EnabledAssemblies)
                {
                    var a = Assembly.LoadFile(path + file);
                    modules.AddRange(LoadModulesFromAssembly(a, k));
                }
                return modules.AsReadOnly();
            }
            catch (Exception ex)
            {
                // TODO better (not lazy) exception handling
                // Possible exceptions:
                // - Errors loading assemblies
                // - Errors finding module paths
                // - Errors creating module instances
                // - Unknown errors
                Console.WriteLine("Module load failed.");
                Console.WriteLine(ex.ToString());
                return null;
            }
        }

        static IEnumerable<ModuleBase> LoadModulesFromAssembly(Assembly asm, Kerobot k)
        {
            var eligibleTypes = from type in asm.GetTypes()
                                where !type.IsAssignableFrom(typeof(ModuleBase))
                                where type.GetCustomAttribute<KerobotModuleAttribute>() != null
                                select type;
            k.InstanceLogAsync(false, LogName,
                $"{asm.FullName} has {eligibleTypes.Count()} usable types:");

            var newmods = new List<ModuleBase>();
            foreach (var t in eligibleTypes)
            {
                var mod = Activator.CreateInstance(t, k);
                k.InstanceLogAsync(false, LogName,
                    $"---> Instance created: {t.FullName}");
                newmods.Add((ModuleBase)mod);
            }
            return newmods;
        }
    }
}<commit_msg>Better error handling on module load<commit_after>﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Reflection;

namespace Kerobot
{
    static class ModuleLoader
    {
        private const string LogName = nameof(ModuleLoader);

        /// <summary>
        /// Given the instance configuration, loads all appropriate types from file specified in it.
        /// </summary>
        internal static ReadOnlyCollection<ModuleBase> Load(InstanceConfig conf, Kerobot k)
        {
            var path = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location) + Path.DirectorySeparatorChar;
            var modules = new List<ModuleBase>();

            foreach (var file in conf.EnabledAssemblies)
            {
                Assembly a = null;
                try
                {
                    a = Assembly.LoadFile(path + file);
                }
                catch (Exception ex)
                {
                    Console.WriteLine("An error occurred when attempting to load a module assembly.");
                    Console.WriteLine($"File: {file}");
                    Console.WriteLine(ex.ToString());
                    Environment.Exit(2);
                }

                IEnumerable<ModuleBase> amods = null;
                try
                {
                    amods = LoadModulesFromAssembly(a, k);
                }
                catch (Exception ex)
                {
                    Console.WriteLine("An error occurred when attempting to create a module instance.");
                    Console.WriteLine(ex.ToString());
                    Environment.Exit(2);
                }
                modules.AddRange(LoadModulesFromAssembly(a, k));
            }
            return modules.AsReadOnly();
        }

        static IEnumerable<ModuleBase> LoadModulesFromAssembly(Assembly asm, Kerobot k)
        {
            var eligibleTypes = from type in asm.GetTypes()
                                where !type.IsAssignableFrom(typeof(ModuleBase))
                                where type.GetCustomAttribute<KerobotModuleAttribute>() != null
                                select type;
            k.InstanceLogAsync(false, LogName,
                $"{asm.GetName().Name} has {eligibleTypes.Count()} usable types");

            var newmods = new List<ModuleBase>();
            foreach (var t in eligibleTypes)
            {
                var mod = Activator.CreateInstance(t, k);
                k.InstanceLogAsync(false, LogName,
                    $"---> Instance created: {t.FullName}");
                newmods.Add((ModuleBase)mod);
            }
            return newmods;
        }
    }
}
{'fraction_non_alphanumeric': 0.0875388198757764, 'fraction_numerical': 0.00038819875776397513, 'mean_word_length': 1.5173424523693209, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 20, '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': '5751255', 'n_tokens_mistral': 1236, 'n_tokens_neox': 1170, 'n_words': 302}