<commit_before>using System;
using System.Collections.Generic;
using UnityEngine;

namespace ModestTree.Zenject
{
    public class GameObjectInstantiator
    {
        public event Action<GameObject> GameObjectInstantiated = delegate { };
        readonly DiContainer _container;

        CompositionRoot _compRoot;

        public GameObjectInstantiator(DiContainer container)
        {
            _container = container;
            _compRoot = container.Resolve<CompositionRoot>();
        }

        public Transform RootParent
        {
            get
            {
                return _compRoot.transform;
            }
        }

        // Add a monobehaviour to an existing game object
        // Note: gameobject here is not a prefab prototype, it is an instance
        public TContract AddMonobehaviour<TContract>(GameObject gameObject, params object[] args) where TContract : Component
        {
            return (TContract)AddMonobehaviour(typeof(TContract), gameObject, args);
        }

        // Add a monobehaviour to an existing game object, using Type rather than a generic
        // Note: gameobject here is not a prefab prototype, it is an instance
        public Component AddMonobehaviour(Type behaviourType, GameObject gameObject, params object[] args)
        {
            using (new LookupInProgressAdder(_container, behaviourType))
            {
                List<object> additional = new List<object>();
                if (args != null)
                {
                    additional.AddRange(args);
                }

                var injecter = new PropertiesInjecter(_container, additional);

                var component = gameObject.AddComponent(behaviourType);
                injecter.Inject(component);

                return component;
            }
        }

        // Create a new game object from a given prefab 
        // Without returning any particular monobehaviour
        public GameObject Instantiate(GameObject template, params object[] args)
        {
            var gameObj = (GameObject)GameObject.Instantiate(template);

            // By default parent to comp root
            // This is good so that the entire object graph is 
            // contained underneath it, which is useful for cases
            // where you need to delete the entire object graph
            gameObj.transform.parent = _compRoot.transform;

            gameObj.SetActive(true);

            List<object> additional = new List<object>();
            if (args != null)
            {
                additional.AddRange(args);
            }

            var injecter = new PropertiesInjecter(_container, additional);
            var components = gameObj.GetComponentsInChildren<MonoBehaviour>();
            foreach (var t in components)
            {
                Assert.That(t != null, "Undefined monobehaviour in template '" + template.name + "'");
                injecter.Inject(t);
            }

            GameObjectInstantiated(gameObj);

            return gameObj;
        }

        // Create from prefab and customize name
        // Return specific monobehaviour
        public T Instantiate<T>(GameObject template, string name) where T : MonoBehaviour
        {
            var component = Instantiate<T>(template);
            component.gameObject.name = name;
            return component;
        }

        // Create from prefab
        // Return specific monobehaviour
        public T Instantiate<T>(GameObject template) where T : MonoBehaviour
        {
            Assert.That(template != null, "Null template found when instantiating game object");

            using (new LookupInProgressAdder(_container, typeof(T)))
            {
                var obj = Instantiate(template);

                var component = obj.GetComponent<T>();
                Assert.That(component != null, "Could not find component with type '" + typeof(T) + "' when instantiating template");

                return component;
            }
        }

        public object Instantiate(Type type, string name)
        {
            Assert.That(typeof(Component).IsAssignableFrom(type));

            var gameObj = new GameObject(name);
            gameObj.transform.parent = _compRoot.transform;

            var component = gameObj.AddComponent(type);

            using (new LookupInProgressAdder(_container, type))
            {
                var injecter = new PropertiesInjecter(_container);
                injecter.Inject(component);
            }

            GameObjectInstantiated(gameObj);

            return component;
        }

        public T Instantiate<T>(string name) where T : Component
        {
            return (T)Instantiate(typeof(T), name);
        }

        public GameObject Instantiate(string name)
        {
            var gameObj = new GameObject(name);
            gameObj.transform.parent = _compRoot.transform;

            GameObjectInstantiated(gameObj);

            return gameObj;
        }
    }
}
<commit_msg>Changed to allow the returned monobehaviour to exist anywhere in the prefab heirarchy rather than just the root<commit_after>using System;
using System.Collections.Generic;
using UnityEngine;

namespace ModestTree.Zenject
{
    public class GameObjectInstantiator
    {
        public event Action<GameObject> GameObjectInstantiated = delegate { };
        readonly DiContainer _container;

        CompositionRoot _compRoot;

        public GameObjectInstantiator(DiContainer container)
        {
            _container = container;
            _compRoot = container.Resolve<CompositionRoot>();
        }

        public Transform RootParent
        {
            get
            {
                return _compRoot.transform;
            }
        }

        // Add a monobehaviour to an existing game object
        // Note: gameobject here is not a prefab prototype, it is an instance
        public TContract AddMonobehaviour<TContract>(GameObject gameObject, params object[] args) where TContract : Component
        {
            return (TContract)AddMonobehaviour(typeof(TContract), gameObject, args);
        }

        // Add a monobehaviour to an existing game object, using Type rather than a generic
        // Note: gameobject here is not a prefab prototype, it is an instance
        public Component AddMonobehaviour(Type behaviourType, GameObject gameObject, params object[] args)
        {
            using (new LookupInProgressAdder(_container, behaviourType))
            {
                List<object> additional = new List<object>();
                if (args != null)
                {
                    additional.AddRange(args);
                }

                var injecter = new PropertiesInjecter(_container, additional);

                var component = gameObject.AddComponent(behaviourType);
                injecter.Inject(component);

                return component;
            }
        }

        // Create a new game object from a given prefab
        // Without returning any particular monobehaviour
        public GameObject Instantiate(GameObject template, params object[] args)
        {
            var gameObj = (GameObject)GameObject.Instantiate(template);

            // By default parent to comp root
            // This is good so that the entire object graph is
            // contained underneath it, which is useful for cases
            // where you need to delete the entire object graph
            gameObj.transform.parent = _compRoot.transform;

            gameObj.SetActive(true);

            List<object> additional = new List<object>();
            if (args != null)
            {
                additional.AddRange(args);
            }

            var injecter = new PropertiesInjecter(_container, additional);
            var components = gameObj.GetComponentsInChildren<MonoBehaviour>();
            foreach (var t in components)
            {
                Assert.That(t != null, "Undefined monobehaviour in template '" + template.name + "'");
                injecter.Inject(t);
            }

            GameObjectInstantiated(gameObj);

            return gameObj;
        }

        // Create from prefab and customize name
        // Return specific monobehaviour
        public T Instantiate<T>(GameObject template, string name) where T : MonoBehaviour
        {
            var component = Instantiate<T>(template);
            component.gameObject.name = name;
            return component;
        }

        // Create from prefab
        // Return specific monobehaviour
        public T Instantiate<T>(GameObject template) where T : MonoBehaviour
        {
            Assert.That(template != null, "Null template found when instantiating game object");

            using (new LookupInProgressAdder(_container, typeof(T)))
            {
                var obj = Instantiate(template);

                var component = obj.GetComponentInChildren<T>();
                Assert.That(component != null, "Could not find component with type '" + typeof(T) + "' when instantiating template");

                return component;
            }
        }

        public object Instantiate(Type type, string name)
        {
            Assert.That(typeof(Component).IsAssignableFrom(type));

            var gameObj = new GameObject(name);
            gameObj.transform.parent = _compRoot.transform;

            var component = gameObj.AddComponent(type);

            using (new LookupInProgressAdder(_container, type))
            {
                var injecter = new PropertiesInjecter(_container);
                injecter.Inject(component);
            }

            GameObjectInstantiated(gameObj);

            return component;
        }

        public T Instantiate<T>(string name) where T : Component
        {
            return (T)Instantiate(typeof(T), name);
        }

        public GameObject Instantiate(string name)
        {
            var gameObj = new GameObject(name);
            gameObj.transform.parent = _compRoot.transform;

            GameObjectInstantiated(gameObj);

            return gameObj;
        }
    }
}

{'fraction_non_alphanumeric': 0.0737091052424123, 'fraction_numerical': 0.0, 'mean_word_length': 1.8604847801578355, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 29, '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': '4822851', 'n_tokens_mistral': 2458, 'n_tokens_neox': 2317, 'n_words': 697}