예제 #1
0
        protected void FinishCommand(bool success = true)
        {
            if (_running)
            {
                _running = false;
                _success = success;

                MonoLog.Log(MonoLogChannel.Core, "Finishing command " + this.GetType().Name);
                OnFinishCommand(success);

                if (_started)
                {
                    OnReleaseResources();
                    _resourcesReleased = true;
                }

                if (this.Scope == CommandScope.Application)
                {
                    UnityEngine.Object.Destroy(this.gameObject);
                }
                else
                {
                    MonoLog.Log(MonoLogChannel.Core, "Destroing component " + this.GetType().Name);

                    UnityEngine.Object.Destroy(this);
                }
            }
        }
예제 #2
0
        private void StopAllCommands()
        {
            int maxCalls = _commands.Count;

            while (_commands.Count > 0)
            {
                maxCalls--;

                _commands[0].Terminate();

                if (maxCalls == 0)
                {
                    break;
                }
            }

            if (_commands.Count != 0)
            {
                MonoLog.LogWarning(MonoLogChannel.Exceptions, "State machine does not finished all commands");
            }

            /*for(int i = _commands.Count-1; i >= 0; i--)
             * {
             *      _commands[i].Terminate();
             * }*/
        }
예제 #3
0
        public T Get(string key)
        {
            if (!_items.ContainsKey(key))
            {
                MonoLog.LogWarning(MonoLogChannel.Core, string.Format("Object with key {0} does not exists", key));
            }

            return(_items[key]);
        }
예제 #4
0
        public static TCommand Execute <TCommand>(params object[] args)
            where TCommand : MonoCommand, new()
        {
            GameObject mgGameObject = MonoSingleton.GetMGGameObject();

            object[] attibutes = typeof(TCommand).GetCustomAttributes(true);

            bool         oneItemOnScene = false;
            CommandScope scope          = CommandScope.Application;

            foreach (Attribute eachAttribute in attibutes)
            {
                if (eachAttribute is SingletoneAttribute)
                {
                    oneItemOnScene = true;
                }
                else if (eachAttribute is MonoCommandScopeAttribute)
                {
                    MonoCommandScopeAttribute monoCommandScopeAttribute = (MonoCommandScopeAttribute)eachAttribute;

                    scope = monoCommandScopeAttribute.IsApplication ? CommandScope.Application : CommandScope.Scene;
                }
            }

            if (oneItemOnScene)
            {
                TCommand command = (TCommand)UnityEngine.Object.FindObjectOfType(typeof(TCommand));

                if (command != null)
                {
                    MonoLog.Log(MonoLogChannel.Core, "Found existing command " + command);

                    return(command);
                }
            }

            GameObject target = null;

            if (scope == CommandScope.Application)
            {
                target = new GameObject(typeof(TCommand).Name);
                target.transform.parent = mgGameObject.transform;

                UnityEngine.Object.DontDestroyOnLoad(target);
            }
            else
            {
                target = UIManager.CurrentSceneController.gameObject;
            }

            TCommand result = ExecuteOn <TCommand>(target, args);

            result.Scope = scope;

            return(result);
        }
예제 #5
0
        protected virtual void OnDestroy()
        {
#if UNITY_EDITOR
            if (this.GetType().GetCustomAttributes(typeof(ShowDestroyAttribute), true).Length == 1)
            {
                MonoLog.Log(MonoLogChannel.Leaks, string.Format("{0} has been destroyed", this.GetType().FullName));
            }
#endif
            if (!_released)
            {
                OnReleaseResources();
            }

            _released = true;
        }
예제 #6
0
        protected override sealed void OnDestroy()
        {
            MonoLog.Log(MonoLogChannel.Core, "Command " + this.GetType().Name + " has beed destroyed");

            if (!_resourcesReleased && _started)
            {
                try
                {
                    OnReleaseResources();
                }
                catch (Exception e)
                {
                    MonoLog.Log(MonoLogChannel.Core, "Error during release command resources", e);
                }

                _resourcesReleased = true;
            }
        }
예제 #7
0
        protected override sealed void Start()
        {
            if (_running)
            {
                _started = true;

                MonoLog.Log(MonoLogChannel.Core, String.Format("Executing command {0}", this.GetType().Name));

                try
                {
                    OnStart(_args);
                }
                catch (Exception e)
                {
                    MonoLog.Log(MonoLogChannel.Core, String.Format("Unable to execute command {0}", this.GetType().Name), e);

                    FinishCommand(false);
                    return;
                }

                object[] attributes = this.GetType().GetCustomAttributes(true);

                foreach (object attribute in attributes)
                {
                    if (attribute is MonoCommandTriggerAttribute)
                    {
                        MonoCommandTriggerAttribute trigger = (MonoCommandTriggerAttribute)attribute;

                        try
                        {
                            trigger.OnCommandStart(this, _args);
                        }
                        catch (Exception e)
                        {
                            MonoLog.Log(MonoLogChannel.Core, String.Format("Unable to execute trigger {0}", trigger.GetType().Name), e);
                        }
                    }
                }
            }
        }
예제 #8
0
        protected IEnumerator LoadURL(string url)
        {
            _www = new WWW(url);

            yield return(_www);

            if (String.IsNullOrEmpty(_www.error))
            {
                _file.Load(_www);
                _file.IsLoaded = true;

                if (!System.IO.File.Exists(_file.Path))
                {
                    try
                    {
                        using (FileStream stream = System.IO.File.OpenWrite(_file.Path))
                        {
                            stream.Write(_www.bytes, 0, _www.bytes.Length);
                        }
                    }
                    catch (Exception e)
                    {
                        MonoLog.Log(MonoLogChannel.Core, "Unable to store file to path " + _file.Path, e);
                    }
                }

                FinishCommand();
            }
            else
            {
                MonoLog.Log(MonoLogChannel.Core, "Unable to load url " + url + ". Error:" + _www.error);

                _file.IsLoaded = false;

                FinishCommand(false);
            }
        }
예제 #9
0
        public MonoCommand()
        {
            _running = true;

            MonoLog.Log(MonoLogChannel.Core, String.Format("Created command {0}", this.GetType().Name));
        }