Exemple #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);
                }
            }
        }
Exemple #2
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);
        }
Exemple #3
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;
        }
Exemple #4
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;
            }
        }
Exemple #5
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);
                        }
                    }
                }
            }
        }
Exemple #6
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);
            }
        }
Exemple #7
0
        public MonoCommand()
        {
            _running = true;

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