<commit_before>﻿using Neon.Serialization;
using Neon.Utilities;
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;

namespace Neon.Entities.Serialization {
    /// <summary>
    /// Serialization specification for an EntityManager state (a level).
    /// </summary>
    public class SerializedLevel {
        /// <summary>
        /// The relative paths of DLLs to inject.
        /// </summary>
        public List<string> DllInjections;

        /// <summary>
        /// The types of ISystemProviders that should be used to inject systems.
        /// </summary>
        public List<string> SystemProviders;

        /// <summary>
        /// The update number that the level is currently on.
        /// </summary>
        public int CurrentUpdateNumber;

        /// <summary>
        /// The templates that are used in this level.
        /// </summary>
        public List<SerializedTemplate> Templates;

        /// <summary>
        /// Singleton entity that the EntityManager owns
        /// </summary>
        public EntityJson SingletonEntity;

        /// <summary>
        /// All entities that are being simulated (at the current update).
        /// </summary>
        public List<EntityJson> Entities;

        /// <summary>
        /// Saved system states.
        /// </summary>
        public List<SerializedSystem> SavedSystemStates;

        private void InjectDlls() {
            // load the dlls
            foreach (var dllInjectionPath in DllInjections) {
                string dllPath = Path.GetFullPath(dllInjectionPath);
                Console.WriteLine("Loading DLL " + dllPath);
                Assembly.LoadFile(dllPath);
            }
        }

        private List<ISystemProvider> GetSystemProviders() {
            List<ISystemProvider> providers = new List<ISystemProvider>();
            foreach (var providerName in SystemProviders) {
                Type providerType = TypeCache.FindType(providerName);
                object instance = Activator.CreateInstance(providerType);

                Console.WriteLine("Injecting systems from provider " + instance + " (an instance of type " + providerType + ")");

                if (instance is ISystemProvider == false) {
                    throw new Exception("System provider " + providerName + " has to derive from ISystemProvider, but it does not");
                }
                providers.Add((ISystemProvider)instance);
            }

            return providers;
        }

        /// <summary>
        /// Returns all systems that are loaded by the level (from the registered ISystemProviders)
        /// that have also had their states restored.
        /// </summary>
        private List<ISystem> GetRestoredSystems() {
            // create our systems from the system providers
            List<ISystem> systems = new List<ISystem>();
            foreach (var provider in GetSystemProviders()) {
                foreach (var system in provider.GetSystems()) {
                    Console.WriteLine("Adding system " + system);
                    systems.Add(system);
                }
            }

            // restore them
            foreach (SerializedSystem systemJson in SavedSystemStates) {
                bool found = false;
                foreach (var system in systems) {
                    if (system is IRestoredSystem) {
                        IRestoredSystem restorableSystem = (IRestoredSystem)system;
                        if (restorableSystem.RestorationGUID == systemJson.RestorationGUID) {<commit_msg>Simplify system provider retrieval<commit_after>﻿using Neon.Serialization;
using Neon.Utilities;
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;

namespace Neon.Entities.Serialization {
    /// <summary>
    /// Serialization specification for an EntityManager state (a level).
    /// </summary>
    public class SerializedLevel {
        /// <summary>
        /// The relative paths of DLLs to inject.
        /// </summary>
        public List<string> DllInjections;

        /// <summary>
        /// The types of ISystemProviders that should be used to inject systems.
        /// </summary>
        public List<string> SystemProviders;

        /// <summary>
        /// The update number that the level is currently on.
        /// </summary>
        public int CurrentUpdateNumber;

        /// <summary>
        /// The templates that are used in this level.
        /// </summary>
        public List<SerializedTemplate> Templates;

        /// <summary>
        /// Singleton entity that the EntityManager owns
        /// </summary>
        public EntityJson SingletonEntity;

        /// <summary>
        /// All entities that are being simulated (at the current update).
        /// </summary>
        public List<EntityJson> Entities;

        /// <summary>
        /// Saved system states.
        /// </summary>
        public List<SerializedSystem> SavedSystemStates;

        private void InjectDlls() {
            // load the dlls
            foreach (var dllInjectionPath in DllInjections) {
                string dllPath = Path.GetFullPath(dllInjectionPath);
                Console.WriteLine("Loading DLL " + dllPath);
                Assembly.LoadFile(dllPath);
            }
        }

        private List<ISystemProvider> GetSystemProviders() {
            List<ISystemProvider> providers = new List<ISystemProvider>();
            foreach (var providerName in SystemProviders) {
                Type providerType = TypeCache.FindType(providerName);
                ISystemProvider instance = Activator.CreateInstance(providerType) as ISystemProvider;
                if (instance == null) {
                    throw new InvalidOperationException("System provider " + providerName +
                        " has to derive from ISystemProvider, but it does not");
                }
                providers.Add(instance);
            }

            return providers;
        }

        /// <summary>
        /// Returns all systems that are loaded by the level (from the registered ISystemProviders)
        /// that have also had their states restored.
        /// </summary>
        private List<ISystem> GetRestoredSystems() {
            // create our systems from the system providers
            List<ISystem> systems = new List<ISystem>();
            foreach (var provider in GetSystemProviders()) {
                foreach (var system in provider.GetSystems()) {
                    Console.WriteLine("Adding system " + system);
                    systems.Add(system);
                }
            }

            // restore them
            foreach (SerializedSystem systemJson in SavedSystemStates) {
                bool found = false;
                foreach (var system in systems) {
                    if (system is IRestoredSystem) {
                        IRestoredSystem restorableSystem = (IRestoredSystem)system;
                        if (restorableSystem.RestorationGUID == systemJson.RestorationGUID) {
{'fraction_non_alphanumeric': 0.09229466553767993, 'fraction_numerical': 0.0, 'mean_word_length': 1.7781262250098, 'pattern_counts': {'":': 0, '<': 61, '<?xml version=': 0, '>': 61, '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': '4209755', 'n_tokens_mistral': 1694, 'n_tokens_neox': 1629, 'n_words': 511}