Exemple #1
0
        IEnumerator Loop()
        {
            CoreLogger.LogDebug(LoggerModules.Tasks, string.Format("dispatcher {0} starting up!", name));

            while (true)
            {
                int count = 0;
                while (_news.Count > 0 && (count < actionsPerIteration || actionsPerIteration < 0))
                {
                    _post.Add(_news.Dequeue());
                    count++;
                }

                foreach (Entry entry in _post)
                {
                    entry.delay--;
                    if (entry.delay == 0)
                    {
                        try
                        {
                            entry.action();
                        } catch (System.Exception ex)
                        {
                            CoreLogger.LogError(LoggerModules.Tasks, string.Format("Error in Dispatcher Action: {0}\n Stack Trace:{1}", ex.Message, ex.StackTrace));
                        }
                    }
                }

                _post.RemoveAll(e => e.delay == 0);

                yield return(null);
            }
        }
        public TItemView AddItem()
        {
            TItemView itemView = null;

            GameObject itemGo = AddItemGo();

            if (itemGo == null)
            {
                return(itemView);
            }

            itemGo.transform.SetParent(transform, false);

            itemView = itemGo.GetComponentOrInterface <TItemView> ();

            if (itemView == null)
            {
                CoreLogger.LogError("GenericViewList", "AddItem " + itemGo + " missing component " + typeof(TItemView));
                Destroy(itemGo);
                return(itemView);
            }

            _itemList.Add(itemView);
            gameObjectList.Add(itemGo);

            OnItemAdded(itemView, itemGo);

            return(itemView);
        }
 //[Conditional("DEBUG")]
 public static void Assert(bool condition, string log = "")
 {
     if (!condition)
     {
         CoreLogger.LogError(log);
         throw new UnityException(log);
     }
 }
Exemple #4
0
        public TModule Get <TModule>()
            where TModule : IModule
        {
            IModule module;

            if (_modules.TryGetValue(typeof(TModule), out module))
            {
                return((TModule)module);
            }

            if (!_availableModules.TryGetValue(typeof(TModule), out module))
            {
                return(default(TModule));
            }

            if (_modulesInInitState.ContainsKey(module))
            {
                CoreLogger.LogError(LoggerModules.GameApplication, string.Format("module {0} is initializing and cannot be retrieved at the moment", module.GetType().Name));
                return(default(TModule));
            }

            ITask init = module.GetInitializer(this);

            if (init == null)
            {
                CoreLogger.LogDebug(LoggerModules.GameApplication, string.Format("unable to retrieve initializer for module {0} failed", module.GetType().Name));
                _failedModules[typeof(TModule)] = module;
                _availableModules.Remove(typeof(TModule));
                return(default(TModule));
            }

            _modulesInInitState[module] = init;
            TaskEnding result = init.Handle();

            _modulesInInitState.Remove(module);

            if (result.IsOk())
            {
                CoreLogger.LogDebug(LoggerModules.GameApplication, string.Format("successfully initialized module {0}", module.GetType().Name));
                _modules[typeof(TModule)] = module;
                _availableModules.Remove(typeof(TModule));
            }
            else
            {
                CoreLogger.LogDebug(LoggerModules.GameApplication, string.Format("failed to initialize module {0}", module.GetType().Name));
                _failedModules[typeof(TModule)] = module;
                _availableModules.Remove(typeof(TModule));
                return(default(TModule));
            }

            if (_started)
            {
                module.StartModule();
                CoreLogger.LogDebug(LoggerModules.GameApplication, string.Format("starting module {0}", module.GetType().Name));
            }

            return((TModule)module);
        }
Exemple #5
0
        public void Init(int fromStage, int toStage, System.Action handler)
        {
            CoreLogger.LogDebug(LoggerModules.GameApplication, string.Format("requesgted non blocking initialization of modules stages {0} to {1}; total available: {2}", fromStage, toStage, _availableModules.Count));

            int pending = 0;

            ICollection <ITask> tasks = new List <ITask>();

            IList <KeyValuePair <Type, IModule> > modulesToRemove = new List <KeyValuePair <Type, IModule> >();

            foreach (KeyValuePair <Type, IModule> kvp in _availableModules.Where(kvp => (kvp.Value.Stage >= fromStage) && (kvp.Value.Stage <= toStage)))
            {
                //always remember to create a local-scoped variable within foreach loops that use lambdas!!!
                IModule localModule = kvp.Value;

                if (!_modules.ContainsKey(localModule.GetType()))
                {
                    ITask init = localModule.GetInitializer(this);
                    if (init != null)
                    {
                        tasks.Add(init);

                        CoreLogger.LogDebug(LoggerModules.GameApplication, string.Format("located module {0} - starting init", localModule.GetType().Name));

                        init.Done += result => {
                            if (result.IsOk())
                            {
                                CoreLogger.LogDebug(LoggerModules.GameApplication, string.Format("successfully done with module {0} ({1}); {2} modules remaining", localModule.GetType().Name, result, pending));
                                _modules[localModule.GetType()] = localModule;
                            }
                            else
                            {
                                CoreLogger.LogDebug(LoggerModules.GameApplication, string.Format("failed with module {0} ({1}); {2} modules remaining", localModule.GetType().Name, result, pending));
                                _failedModules[localModule.GetType()] = localModule;
                            }
                        };
                    }
                    else
                    {
                        CoreLogger.LogError(LoggerModules.GameApplication, string.Format("failed to retrieve initializer for module {0}", localModule.GetType().Name));
                        _failedModules[localModule.GetType()] = localModule;
                    }
                }
            }

            _taskFactory.Parallelize(tasks).Start(result => {
                handler();
            });

            foreach (KeyValuePair <Type, IModule> toRemove in modulesToRemove)
            {
                _availableModules.Remove(toRemove);
            }
        }
Exemple #6
0
        void ImplementToggle(GameObject itemGo)
        {
            Toggle toggle = itemGo.GetComponent <Toggle>();

            if (toggle == null)
            {
                CoreLogger.LogError("ToggleViewList", "ImplementToggle missing Toggle component");
                return;
            }

            toggle.group = _toggleGroup;
            toggle.isOn  = false;
            toggle.onValueChanged.AddListener(OnToggleValueChanged);
        }
        public void LoadPrefab(string prefabPath)
        {
            if (prefabPath == prefabData.itemPrefabPath && prefabData.itemPrefab != null)
            {
                return;
            }

            _assetManager = GameApplication.Instance.AssetManager;

            prefabData.itemPrefabPath = prefabPath;
            prefabData.itemPrefab     = _assetManager.GetResource <GameObject> (prefabPath);
            if (prefabData.itemPrefab == null)
            {
                CoreLogger.LogError("GenericViewList", "LoadPrefab cannot find prefab " + prefabData.itemPrefabPath);
            }
        }
Exemple #8
0
        /// <summary>
        /// This initializes the GameApplication singleton as a blocking call - it means
        /// that access to Instance is valied immediately as this method returns.
        /// </summary>
        public static GameApplication CreateInstanceSync()
        {
#if UNITY_EDITOR
            s_requestingScene = "";
#endif

            if (s_instance != null)
            {
                CoreLogger.LogError(LoggerModules.GameApplication, "calling GameApplication sync init while in async init - this is a serious error, unless in a special debug mode!!");
                return(s_instance);
            }

            CoreLogger.LogDebug(LoggerModules.GameApplication, "creating GameApplication instance synchronously");

            GameApplication instance = CreateOrGet();
            if (instance == null)
            {
                CoreLogger.LogCritical(LoggerModules.GameApplication, "unable to obtain kernel instance!");
                return(null);
            }

            if (!instance.AllowSynchronousCreation)
            {
                if (instance.synchronousCreationFallbackScene != null && instance.synchronousCreationFallbackScene != "")
                {
                    CoreLogger.LogDebug(LoggerModules.GameApplication, string.Format("synchronous creation not allowed in this game - " +
                                                                                     "reverting to scene {0}", instance.synchronousCreationFallbackScene));
                    string scene = instance.synchronousCreationFallbackScene;
                    DestroyObject(instance.gameObject);
#if UNITY_EDITOR
                    GameApplication.Init(scene);
#endif
                    return(null);
                }

                CoreLogger.LogCritical(LoggerModules.GameApplication, "synchrnous creation not allowed in this Game!!!");
                return(null);
            }

            IEnumerator steps = instance.Init(true);
            while (steps.MoveNext())
            {
            }

            return(instance);
        }
Exemple #9
0
        /// <summary>
        /// Go over all existing modules, and initialize them, if possible immediately.
        /// </summary>
        public void Init(int fromStage, int toStage)
        {
            CoreLogger.LogDebug(LoggerModules.GameApplication, string.Format("requested blocking initialization of modules stages {0} to {1}", fromStage, toStage));

            IList <KeyValuePair <Type, IModule> > modulesToRemove = new List <KeyValuePair <Type, IModule> >();

            foreach (KeyValuePair <Type, IModule> kvp in _availableModules.Where(kvp => (kvp.Value.Stage >= fromStage) && (kvp.Value.Stage <= toStage)))
            {
                IModule module = kvp.Value;

                CoreLogger.LogDebug(LoggerModules.GameApplication, string.Format("located module {0}", module.GetType().Name));
                modulesToRemove.Add(kvp);

                if (!_modules.ContainsKey(module.GetType()))
                {
                    ITask init = module.GetInitializer(this);
                    if (init != null)
                    {
                        TaskEnding result = init.Handle();
                        if (result.IsOk())
                        {
                            CoreLogger.LogDebug(LoggerModules.GameApplication, string.Format("successfully initialized module {0}", module.GetType().Name));
                            _modules[module.GetType()] = module;
                        }
                        else
                        {
                            CoreLogger.LogDebug(LoggerModules.GameApplication, string.Format("failed to initalize module {0}: {1}", module.GetType().Name, result));
                            _failedModules[module.GetType()] = module;
                        }
                    }
                    else
                    {
                        CoreLogger.LogError(LoggerModules.GameApplication, string.Format("failed to retrieve initializer for module {0}", module.GetType().Name));
                        _failedModules[module.GetType()] = module;
                    }
                }
            }

            foreach (KeyValuePair <Type, IModule> toRemove in modulesToRemove)
            {
                _availableModules.Remove(toRemove);
            }
        }
Exemple #10
0
        protected override IEnumerator InitService(IServiceResolver resolver)
        {
            GameObject dialogCanvas = (GameApplication.Instance as GameApplication).gameObject.GetComponentInChildren <Canvas>().gameObject;

            _modalsParent = dialogCanvas;
            if (_modalsParent != null)
            {
                _modalsCanvas = _modalsParent.GetComponentOrInterface <IModalsCanvas> ();
                if (_modalsCanvas != null)
                {
                    _modalsCanvas.MaskClickEvent += OnMaskClickEvent;
                }
            }
            else
            {
                CoreLogger.LogError("ModalsService", "cannot find parent canvas in scene");
            }

            yield break;
        }
        protected GameObject AddItemGo()
        {
            GameObject itemPrefab;

            if (_itemList.Count == 0 && prefabDataFirst.itemPrefab != null)
            {
                itemPrefab = prefabDataFirst.itemPrefab;
            }
            else
            {
                itemPrefab = prefabData.itemPrefab;
            }

            if (itemPrefab == null)
            {
                CoreLogger.LogError("GenericViewList", "AddItemGo missing prefab");
                return(null);
            }
            return(Instantiate(itemPrefab) as GameObject);
        }
Exemple #12
0
        public void Start()
        {
            if (_started)
            {
                CoreLogger.LogWarning(LoggerModules.GameApplication, "Start called twice!");
                return;
            }

            foreach (IModule module in _modules.Values)
            {
                CoreLogger.LogDebug(LoggerModules.GameApplication, string.Format("starting module {0}", module.GetType().Name));
                try
                {
                    module.StartModule();
                } catch (Exception ex)
                {
                    CoreLogger.LogError(LoggerModules.GameApplication, string.Format("failed to initialize module {0}:{1}", module.GetType().Name, ex));
                }
            }

            _started = true;
        }
        public static bool IsTouchingUGUI()
        {
            try {
                if (EventSystem.current.IsPointerOverGameObject())
                {
                    return(true);
                }

                if (Input.touchCount > 0)
                {
                    foreach (Touch item in Input.touches)
                    {
                        if (EventSystem.current.IsPointerOverGameObject(item.fingerId))
                        {
                            return(true);
                        }
                    }
                }
            } catch (System.Exception ex) {
                CoreLogger.LogError(ex.ToString());
            }

            return(false);
        }
Exemple #14
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);
                }
            }
        }
Exemple #15
0
        void Awake()
        {
            //At this point we want to initialize the SDK's
            //FIXME: we may want to reconsider this method - right now the concept
            //is to have the GameApplication object initialize what it can, and then
            //have the GameApplication object initialized through this object when running
            //in full "game" mode, and on its own automatically when running in "scene" mode
            // - that is, a single scene called from the unity editor
            //the downside to this is that we have to re-init all the services again and again
            //when debugging - it may be benefitial to create an "external" singleton to retain
            //this object - one that "lives" through unity editor run sessions (its lifecycle
            //is an editor session, not a game session)
            //note that it's possible to do that later on, but the downside to that is that
            //we will need some method to "reset" it from the editor
            string nextScene = openingScene;

#if UNITY_EDITOR
            SceneController.s_jumpToInitOnTestScenes = false;

            if (GameApplication.RequestingScene != "")
            {
                nextScene = GameApplication.RequestingScene;
            }
            else
            {
                GameApplication.RequestingScene = nextScene;
            }
#endif

            //find the SplashScreen, so we can signal it off later
            SplashScreen       splashScreen       = gameObject.GetComponentInChildren <SplashScreen>();
            GameApplication    gameApplication    = GameApplication.CreateOrGet();
            ServiceInitializer serviceInitializer = new ServiceInitializer();
            if (splashScreen == null)
            {
                GameApplication.InitDone += () => {
                    GameApplication.Instance.SceneManager.SwitchScene(nextScene);
                };

                //iff we finish the pre-init phase, we can start the kernel
                serviceInitializer.Run(GetComponentsInChildren(typeof(IApplicationPreInit)).Cast <IApplicationPreInit>(), result => {
                    if (result)
                    {
                        StartCoroutine(gameApplication.Init(false));
                    }
                    else
                    {
                        CoreLogger.LogError("Init", "failed to start the application");
                        Application.Quit();
                    }
                });
            }
            else
            {
                serviceInitializer.Run(GetComponentsInChildren(typeof(IApplicationPreInit)).Cast <IApplicationPreInit>(), result => {
                    //iff we finish the pre-init phase, we can ask the splash screen to start the kernel
                    if (result)
                    {
                        splashScreen.Enqueue(() => gameApplication.Init(false), "Build Game Application");

                        splashScreen.Done += () => {
                            CoreLogger.LogDebug("GameApplication.Instance == " + GameApplication.Instance);
                            CoreLogger.LogDebug("GameApplication.Instance.SceneManager ==" + GameApplication.Instance.SceneManager);

                            GameApplication.Instance.SceneManager.SwitchScene(nextScene);
                        };
                    }
                    else
                    {
                        CoreLogger.LogError("Init", "failed to start the application");
                        Application.Quit();
                    }
                });
            }


#if UNITY_EDITOR
#else
            Debugger.Assert(GameApplication.Instance == null, "GameApplication initialization called twice!");
#endif
        }