Exemplo n.º 1
0
        public void Initialize()
        {
            Assert.That(!_hasInitialized);
            _hasInitialized = true;

            _initializables = _initializables.OrderBy(x => x.Priority).ToList();

#if UNITY_EDITOR
            foreach (var initializable in _initializables.Select(x => x.Initializable).GetDuplicates())
            {
                Assert.That(false, "Found duplicate IInitializable with type '{0}'".Fmt(initializable.GetType()));
            }
#endif

            foreach (var initializable in _initializables)
            {
                try
                {
#if UNITY_EDITOR
                    using (ProfileBlock.Start("{0}.Initialize()", initializable.Initializable.GetType()))
#endif
                    {
                        initializable.Initializable.Initialize();
                    }
                }
                catch (Exception e)
                {
                    throw Assert.CreateException(
                              e, "Error occurred while initializing IInitializable with type '{0}'", initializable.Initializable.GetType());
                }
            }
        }
Exemplo n.º 2
0
        public void Initialize()
        {
            _initializables = _initializables.OrderBy(x => x.Priority).ToList();

            foreach (var initializable in _initializables.Select(x => x.Initializable).GetDuplicates())
            {
                Assert.That(false, "Found duplicate IInitializable with type '{0}'".Fmt(initializable.GetType()));
            }

            foreach (var initializable in _initializables)
            {
                //Log.Info("Initializing initializable with type '" + initializable.GetType() + "'");

                try
                {
                    using (ProfileBlock.Start("{0}.Initialize()", initializable.Initializable.GetType().Name()))
                    {
                        initializable.Initializable.Initialize();
                    }
                }
                catch (Exception e)
                {
                    throw new ZenjectException(
                              "Error occurred while initializing IInitializable with type '{0}'".Fmt(initializable.Initializable.GetType().Name()), e);
                }
            }
        }
Exemplo n.º 3
0
        public void Initialize()
        {
            _initializables = _initializables.OrderBy(x => x.Priority).ToList();

            foreach (var initializable in _initializables.Select(x => x.Initializable).GetDuplicates())
            {
                Assert.That(false, "Found duplicate IInitializable with type '{0}'".Fmt(initializable.GetType()));
            }

            foreach (var initializable in _initializables)
            {
                Log.Debug("Initializing '" + initializable.Initializable.GetType() + "'");

                try
                {
#if PROFILING_ENABLED
                    using (ProfileBlock.Start("{0}.Initialize()", initializable.Initializable.GetType().Name()))
#endif
                    {
                        initializable.Initializable.Initialize();
                    }
                }
                catch (Exception e)
                {
                    throw Assert.CreateException(
                              e, "Error occurred while initializing IInitializable with type '{0}'", initializable.Initializable.GetType().Name());
                }
            }
        }
        public static ZenjectTypeInfo GetInfo(Type type)
        {
#if UNITY_EDITOR
            using (ProfileBlock.Start("Zenject Reflection"))
#endif
            {
                Assert.That(!type.IsAbstract(),
                            "Tried to analyze abstract type '{0}'.  This is not currently allowed.", type);

                ZenjectTypeInfo info;

#if ZEN_MULTITHREADING
                lock (_typeInfo)
#endif
                {
                    if (!_typeInfo.TryGetValue(type, out info))
                    {
                        info = CreateTypeInfo(type);
                        _typeInfo.Add(type, info);
                    }
                }

                return(info);
            }
        }
Exemplo n.º 5
0
 void UpdateTickable(ITickable tickable)
 {
     using (ProfileBlock.Start("{0}.Tick()".Fmt(tickable.GetType().Name())))
     {
         tickable.Tick();
     }
 }
Exemplo n.º 6
0
        // Same as Instantiate except you can pass in null value
        // however the type for each parameter needs to be explicitly provided in this case
        public object InstantiateExplicit(
            Type concreteType, List <TypeValuePair> extraArgMap, InjectContext currentContext, string concreteIdentifier, bool autoInject)
        {
#if PROFILING_ENABLED
            using (ProfileBlock.Start("Zenject.Instantiate({0})", concreteType))
#endif
            {
                if (CheckForCircularDependencies)
                {
                    if (_instantiatesInProgress.Contains(concreteType))
                    {
                        throw new ZenjectResolveException(
                                  "Circular dependency detected! \nObject graph:\n" + concreteType.Name() + "\n" + currentContext.GetObjectGraphString());
                    }

                    _instantiatesInProgress.Push(concreteType);
                    try
                    {
                        return(InstantiateInternal(concreteType, extraArgMap, currentContext, concreteIdentifier, autoInject));
                    }
                    finally
                    {
                        Assert.That(_instantiatesInProgress.Peek() == concreteType);
                        _instantiatesInProgress.Pop();
                    }
                }
                else
                {
                    return(InstantiateInternal(concreteType, extraArgMap, currentContext, concreteIdentifier, autoInject));
                }
            }
        }
Exemplo n.º 7
0
        void UpdateTickable(ITickable tickable)
        {
#if PROFILING_ENABLED
            using (ProfileBlock.Start("{0}.Tick()".Fmt(tickable.GetType().Name())))
#endif
            {
                tickable.Tick();
            }
        }
Exemplo n.º 8
0
        protected override void UpdateItem(IFixedTickable task)
        {
#if PROFILING_ENABLED
            using (ProfileBlock.Start("{0}.FixedTick()".Fmt(task.GetType().Name())))
#endif
            {
                task.FixedTick();
            }
        }
Exemplo n.º 9
0
        protected override void UpdateItem(IFixedTickable task)
        {
#if UNITY_EDITOR
            using (ProfileBlock.Start("{0}.FixedTick()", task.GetType()))
#endif
            {
                task.FixedTick();
            }
        }
Exemplo n.º 10
0
        protected override void UpdateItem(ILateTickable task)
        {
#if UNITY_EDITOR && ZEN_PROFILING_ENABLED
            using (ProfileBlock.Start("{0}.LateTick()", task.GetType()))
#endif
            {
                task.LateTick();
            }
        }
Exemplo n.º 11
0
        protected override void InternalExecute(THandler handler, object[] args)
        {
            Assert.That(args.IsEmpty());

#if UNITY_EDITOR && ZEN_PROFILING_ENABLED
            using (ProfileBlock.Start(_method.ToDebugString()))
#endif
            {
                _method(handler);
            }
        }
Exemplo n.º 12
0
 public object InstantiateExplicit(
     Type concreteType, List <TypeValuePair> extraArgMap)
 {
     using (ProfileBlock.Start("Zenject.Instantiate({0})", concreteType))
     {
         using (_container.PushLookup(concreteType))
         {
             return(InstantiateInternal(concreteType, extraArgMap));
         }
     }
 }
Exemplo n.º 13
0
        static void ResetStaticValues()
        {
            if (!UnityEditor.EditorSettings.enterPlayModeOptionsEnabled)
            {
                return;
            }

            _instance = new ProfileBlock();
            _nameCache.Clear();
            _blockCount = 0;
        }
Exemplo n.º 14
0
        public override void Execute(object[] args)
        {
            Assert.That(args.IsEmpty());

#if UNITY_EDITOR && ZEN_PROFILING_ENABLED
            using (ProfileBlock.Start(_method.ToDebugString()))
#endif
            {
                _method();
            }
        }
Exemplo n.º 15
0
        protected override void UpdateItem(IFixedTickable task)
        {
#if ZEN_INTERNAL_PROFILING
            using (ProfileTimers.CreateTimedBlock("User Code"))
#endif
#if UNITY_EDITOR
            using (ProfileBlock.Start("{0}.FixedTick()", task.GetType()))
#endif
            {
                task.FixedTick();
            }
        }
Exemplo n.º 16
0
        protected override void InternalExecute(THandler handler, object[] args)
        {
            Assert.That(args.IsEmpty());

            var method = _methodGetter(handler);
#if UNITY_EDITOR
            using (ProfileBlock.Start(method.ToDebugString()))
#endif
            {
                method();
            }
        }
Exemplo n.º 17
0
        object InstantiateInternal(
            Type concreteType, IEnumerable <TypeValuePair> extraArgMapParam, InjectContext currentContext)
        {
#if !ZEN_NOT_UNITY3D
            Assert.That(!concreteType.DerivesFrom <UnityEngine.Component>(),
                        "Error occurred while instantiating object of type '{0}'. Instantiator should not be used to create new mono behaviours.  Must use GameObjectInstantiator, GameObjectFactory, or GameObject.Instantiate.", concreteType.Name());
#endif

            var typeInfo = TypeAnalyzer.GetInfo(concreteType);

            if (typeInfo.InjectConstructor == null)
            {
                throw new ZenjectResolveException(
                          "More than one (or zero) constructors found for type '{0}' when creating dependencies.  Use one [Inject] attribute to specify which to use.".Fmt(concreteType));
            }

            // Make a copy since we remove from it below
            var extraArgMap = extraArgMapParam.ToList();
            var paramValues = new List <object>();

            foreach (var injectInfo in typeInfo.ConstructorInjectables)
            {
                object value;

                if (!InstantiateUtil.PopValueWithType(extraArgMap, injectInfo.MemberType, out value))
                {
                    value = Resolve(injectInfo.CreateInjectContext(this, currentContext, null));
                }

                paramValues.Add(value);
            }

            object newObj;

            try
            {
                using (ProfileBlock.Start("{0}.{0}()", concreteType))
                {
                    newObj = typeInfo.InjectConstructor.Invoke(paramValues.ToArray());
                }
            }
            catch (Exception e)
            {
                throw new ZenjectResolveException(
                          "Error occurred while instantiating object with type '{0}'".Fmt(concreteType.Name()), e);
            }

            Inject(newObj, extraArgMap, true, typeInfo, currentContext);

            return(newObj);
        }
Exemplo n.º 18
0
        public void Fire(TParam1 p1)
        {
#if UNITY_EDITOR
            using (ProfileBlock.Start("Signal '{0}'", this.GetType().Name))
#endif
            {
                var wasHandled = Manager.Trigger(SignalId, new object[] { p1 });

                wasHandled |= (_listeners.Count > 0);

                // Iterate over _tempListeners in case the
                // listener removes themselves in the callback
                // (we use _tempListeners to avoid memory allocs)
                _tempListeners.Clear();

                for (int i = 0; i < _listeners.Count; i++)
                {
                    _tempListeners.Add(_listeners[i]);
                }

                for (int i = 0; i < _tempListeners.Count; i++)
                {
                    var listener = _tempListeners[i];

#if UNITY_EDITOR
                    using (ProfileBlock.Start(listener.ToDebugString()))
#endif
                    {
                        listener(p1);
                    }
                }

#if ZEN_SIGNALS_ADD_UNIRX
                wasHandled |= _observable.HasObservers;
#if UNITY_EDITOR
                using (ProfileBlock.Start("UniRx Stream"))
#endif
                {
                    _observable.OnNext(p1);
                }
#endif

                if (Settings.RequiresHandler && !wasHandled)
                {
                    throw Assert.CreateException(
                              "Signal '{0}' was fired but no handlers were attached and the signal is marked to require a handler!", SignalId);
                }
            }
        }
Exemplo n.º 19
0
        public void TriggerOnSpawned()
        {
            Assert.That(!_isSpawned);
            _isSpawned = true;

            for (int i = 0; i < _poolables.Count; i++)
            {
#if UNITY_EDITOR && ZEN_PROFILING_ENABLED
                using (ProfileBlock.Start("{0}.OnSpawned", _poolables[i].GetType()))
#endif
                {
                    _poolables[i].OnSpawned();
                }
            }
        }
Exemplo n.º 20
0
        public TValue Spawn()
        {
            TValue item = GetInternal();

            if (!Container.IsValidating)
            {
#if UNITY_EDITOR
                using (ProfileBlock.Start("{0}.Reinitialize", this.GetType()))
#endif
                {
                    Reinitialize(item);
                }
            }
            return(item);
        }
Exemplo n.º 21
0
        internal static void Inject(
            DiContainer container, object injectable,
            IEnumerable <TypeValuePair> extraArgMapParam, bool shouldUseAll, ZenjectTypeInfo typeInfo)
        {
            Assert.IsEqual(typeInfo.TypeAnalyzed, injectable.GetType());
            Assert.That(injectable != null);

            // Make a copy since we remove from it below
            var extraArgMap = extraArgMapParam.ToList();

            foreach (var injectInfo in typeInfo.FieldInjectables.Concat(typeInfo.PropertyInjectables))
            {
                object value;

                if (InstantiateUtil.PopValueWithType(extraArgMap, injectInfo.ContractType, out value))
                {
                    injectInfo.Setter(injectable, value);
                }
                else
                {
                    value = container.Resolve(injectInfo, injectable);

                    if (injectInfo.Optional && value == null)
                    {
                        // Do not override in this case so it retains the hard-coded value
                    }
                    else
                    {
                        injectInfo.Setter(injectable, value);
                    }
                }
            }

            if (shouldUseAll && !extraArgMap.IsEmpty())
            {
                throw new ZenjectResolveException(
                          "Passed unnecessary parameters when injecting into type '{0}'. \nExtra Parameters: {1}\nObject graph:\n{2}"
                          .With(injectable.GetType().Name(), String.Join(",", extraArgMap.Select(x => x.Type.Name()).ToArray()), DiContainer.GetCurrentObjectGraph()));
            }

            foreach (var methodInfo in typeInfo.PostInjectMethods)
            {
                using (ProfileBlock.Start("{0}.{1}()", injectable.GetType(), methodInfo.Name))
                {
                    methodInfo.Invoke(injectable, new object[0]);
                }
            }
        }
Exemplo n.º 22
0
        public void TriggerOnDespawned()
        {
            Assert.That(_isSpawned);
            _isSpawned = false;

            // Call OnDespawned in the reverse order just like how dispose works
            for (int i = _poolables.Count - 1; i >= 0; i--)
            {
#if UNITY_EDITOR && ZEN_PROFILING_ENABLED
                using (ProfileBlock.Start("{0}.OnDespawned", _poolables[i].GetType()))
#endif
                {
                    _poolables[i].OnDespawned();
                }
            }
        }
Exemplo n.º 23
0
        public TValue Spawn()
        {
            var item = GetInternal();

            if (!Container.IsValidating)
            {
#if ZEN_INTERNAL_PROFILING
                using (ProfileTimers.CreateTimedBlock("User Code"))
#endif
#if UNITY_EDITOR
                using (ProfileBlock.Start("{0}.Reinitialize", this.GetType()))
#endif
                {
                    Reinitialize(item);
                }
            }
            return(item);
        }
Exemplo n.º 24
0
        public void TriggerOnSpawned()
        {
            Assert.That(!_isSpawned);
            _isSpawned = true;

            for (int i = 0; i < _poolables.Count; i++)
            {
#if ZEN_INTERNAL_PROFILING
                using (ProfileTimers.CreateTimedBlock("User Code"))
#endif
#if UNITY_EDITOR
                using (ProfileBlock.Start("{0}.OnSpawned", _poolables[i].GetType()))
#endif
                {
                    _poolables[i].OnSpawned();
                }
            }
        }
Exemplo n.º 25
0
        protected override void RunInternal()
        {
            // We always want to initialize ProjectContext as early as possible
            ProjectContext.Instance.EnsureIsInitialized();

#if UNITY_EDITOR
            using (ProfileBlock.Start("SceneContext.Install"))
#endif
            {
                Install();
            }
#if UNITY_EDITOR
            using (ProfileBlock.Start("SceneContext.Resolve"))
#endif
            {
                Resolve();
            }
        }
Exemplo n.º 26
0
        public static InjectTypeInfo TryGetInfo(Type type)
        {
            InjectTypeInfo info;

#if ZEN_MULTITHREADING
            lock (_typeInfo)
#endif
            {
                if (_typeInfo.TryGetValue(type, out info))
                {
                    return(info);
                }
            }

#if UNITY_EDITOR
            using (ProfileBlock.Start("Zenject Reflection"))
#endif
            {
                info = GetInfoInternal(type);
            }

            if (info != null)
            {
                Assert.IsEqual(info.Type, type);
                Assert.IsNull(info.BaseTypeInfo);

                var baseType = type.BaseType();

                if (baseType != null && !ShouldSkipTypeAnalysis(baseType))
                {
                    info.BaseTypeInfo = TryGetInfo(baseType);
                }
            }

#if ZEN_MULTITHREADING
            lock (_typeInfo)
#endif
            {
                _typeInfo[type] = info;
            }

            return(info);
        }
Exemplo n.º 27
0
        public void TriggerOnDespawned()
        {
            Assert.That(_isSpawned);
            _isSpawned = false;

            // Call OnDespawned in the reverse order just like how dispose works
            for (int i = _poolables.Count - 1; i >= 0; i--)
            {
#if ZEN_INTERNAL_PROFILING
                using (ProfileTimers.CreateTimedBlock("User Code"))
#endif
#if UNITY_EDITOR
                using (ProfileBlock.Start("{0}.OnDespawned", _poolables[i].GetType()))
#endif
                {
                    _poolables[i].OnDespawned();
                }
            }
        }
Exemplo n.º 28
0
        public void LazyInjectAll()
        {
#if UNITY_EDITOR && ZEN_PROFILING_ENABLED
            using (ProfileBlock.Start("LazyInstanceInjector.LazyInjectAll"))
#endif
            {
                var tempList = new List <object>();
                while (!_instancesToInject.IsEmpty())
                {
                    tempList.Clear();
                    tempList.AddRange(_instancesToInject);
                    _instancesToInject.Clear();
                    foreach (var instance in tempList)
                    {
                        _container.Inject(instance);
                    }
                }
            }
        }
Exemplo n.º 29
0
        public void OnGui()
        {
            foreach (var renderable in _renderables)
            {
                try
                {
#if UNITY_EDITOR && ZEN_PROFILING_ENABLED
                    using (ProfileBlock.Start("{0}.GuiRender()", renderable.Renderable.GetType()))
#endif
                    {
                        renderable.Renderable.GuiRender();
                    }
                }
                catch (Exception e)
                {
                    throw Assert.CreateException(
                              e, "Error occurred while initializing IGuiRenderable with type '{0}'", renderable.Renderable.GetType());
                }
            }
        }
Exemplo n.º 30
0
        protected override void RunInternal()
        {
            // We always want to initialize ProjectContext as early as possible
            ProjectContext.Instance.EnsureIsInitialized();

            Assert.That(!IsValidating);

#if UNITY_EDITOR && ZEN_PROFILING_ENABLED
            using (ProfileBlock.Start("SceneContext.Install"))
#endif
            {
                Install();
            }
#if UNITY_EDITOR && ZEN_PROFILING_ENABLED
            using (ProfileBlock.Start("SceneContext.Resolve"))
#endif
            {
                Resolve();
            }
        }