Exemplo n.º 1
0
        public IEnumerator Init(bool block)
        {
            if (s_initInProgress)
            {
                CoreLogger.LogError(LoggerModules.GameApplication, "attempt to initialize kernel while already doing so!");
                yield break;
            }

            Application.targetFrameRate = this.targetFrameRate;

            s_instance = this;

            s_initInProgress = true;

            _taskQueue        = gameObject.AddMissingComponent <TaskQueue>();
            _taskQueue.Active = false;

            EnqueueCreateResolvers();

            EnqueueAddProviders();

            EnqueuePreStageServiceResolution(block);
            _taskQueue.Enqueue(() => {
                LoadLoggerConfig();
            });
            EnqueueInitModules(int.MinValue, -1, block);

            EnqueueServiceResolution(block);
            EnqueueInitModules(0, 0, block);

            EnqueuePostStageServiceResolution(block);
            EnqueueInitModules(1, int.MaxValue, block);

            _taskQueue.Enqueue(() => {
                LoadLoggerConfig();
            });

            //register to the event that tells us when the queue is empty,
            //which implies we have finished initializing
            bool initDone = false;

            //when the task queue empties, we will know we are done
            _taskQueue.Enqueue(() => {
                //at this point we can set the static instance safely
                s_earlyAccess = false;

                //init is done
                s_initInProgress = false;
                initDone         = true;
            });

            //now start the actual initialization, by activating the queue
            float initStart = Time.time;

            //start running the init tasks
            _taskQueue.Active = true;

            if (block)
            {
                //the synchronous version:
                _taskQueue.HandleAll();

                //at this point in time, the singleton is ready for use
                _gameModules.Start();

                yield break;
            }
            else
            {
                //the async. version:

                //while(Time.time - initStart < initTimeout)
                while (true)
                {
                    if (Time.time - initStart > initTimeout)
                    {
                        CoreLogger.LogWarning(LoggerModules.GameApplication, "timeout in creating GameApplication object");
                    }

                    if (initDone)
                    {
                        //at this point in time, the singleton is ready for use
                        _gameModules.Start();

                        yield return(StartCoroutine(WaitForStrangeServiceProviderInit()));

                        InitDone();
                        CoreLogger.LogDebug(LoggerModules.GameApplication, "GameApplication succesfully finished Init!");
                        yield break;
                    }

                    yield return(null);
                }
            }
        }