예제 #1
0
파일: Resource.cs 프로젝트: j1m1l0k0/server
        private void InvokeUpdateClientPackage()
        {
            if (ms_clientUpdateQueued)
            {
                return;
            }

            ms_clientUpdateQueued = true;

            Task.Run(async() =>
            {
                await Task.Delay(500);

                // go
                var runner = new Tasks.ResourceTaskRunner();
                runner.ExecuteTasks(this);

                // and unlock the lock
                ms_clientUpdateQueued = false;
            });
        }
예제 #2
0
        public async Task<bool> Start()
        {
            if (State == ResourceState.Running)
            {
                return true;
            }

            if (State != ResourceState.Stopped && State != ResourceState.Starting)
            {
                throw new InvalidOperationException("can not start a resource that is not stopped");
            }

            this.Log().Info("Starting resource {0} (last state: {1}).", Name, State);

            // as this is already done for us in this case
            if (State != ResourceState.Starting)
            {
                if (!Parse())
                {
                    State = ResourceState.Error;

                    return false;
                }

                // resolve dependencies
                foreach (var dep in Dependencies)
                {
                    var res = Manager.GetResource(dep);

                    if (res == null)
                    {
                        this.Log().Warn("Can't resolve dependency {0} from resource {1}.", dep, Name);
                        return false;
                    }

                    await res.Start();
                    res.AddDependant(Name);
                }

                // execute tasks
                var runner = new Tasks.ResourceTaskRunner();

                if (!await runner.ExecuteTasks(this))
                {
                    this.Log().Error("Executing tasks for resource {0} failed.", Name);

                    State = ResourceState.Stopped;
                    return false;
                }

                m_watcher = new FileSystemWatcher();

                // create script environment
                if (!EnsureScriptEnvironment())
                {
                    return false;
                }
            }

            State = ResourceState.Starting;

            m_scriptEnvironment.DoInitFile(false);

            // trigger event
            if (!Manager.TriggerEvent("onResourceStarting", -1, Name))
            {
                // how the h-
                if (State == ResourceState.Running)
                {
                    return true;
                }

                Stop();

                return false;
            }

            m_scriptEnvironment.LoadScripts();

            // TODO: add development mode check
            m_watcher.Path = Path;
            m_watcher.IncludeSubdirectories = true;
            m_watcher.NotifyFilter = NotifyFilters.LastWrite;
            m_watcher.Changed += (s, e) => InvokeUpdateClientPackage();
            m_watcher.Created += (s, e) => InvokeUpdateClientPackage();
            m_watcher.Deleted += (s, e) => InvokeUpdateClientPackage();
            m_watcher.Renamed += (s, e) => InvokeUpdateClientPackage();

            m_watcher.EnableRaisingEvents = true;

            State = ResourceState.Running;

            // trigger event
            if (!Manager.TriggerEvent("onResourceStart", -1, Name))
            {
                this.Log().Info("Resource start canceled by event.");

                Stop();

                return false;
            }

            // log that we started
            this.Log().Info("Started resource {0}.", Name);

            // broadcast to current clients
            var clients = ClientInstances.Clients.Where(c => c.Value.NetChannel != null).Select(c => c.Value);

            foreach (var client in clients)
            {
                client.SendReliableCommand(0xAFE4CD4A, Encoding.UTF8.GetBytes(Name)); // msgResStart
            }

            return true;
        }
예제 #3
0
파일: Resource.cs 프로젝트: j1m1l0k0/server
        public async Task <bool> Start()
        {
            if (State == ResourceState.Running)
            {
                return(true);
            }

            if (State != ResourceState.Stopped && State != ResourceState.Starting)
            {
                throw new InvalidOperationException("can not start a resource that is not stopped");
            }

            this.Log().Info("Starting resource {0} (last state: {1}).", Name, State);

            // as this is already done for us in this case
            if (State != ResourceState.Starting)
            {
                if (!Parse())
                {
                    State = ResourceState.Error;

                    return(false);
                }

                // resolve dependencies
                foreach (var dep in Dependencies)
                {
                    var res = Manager.GetResource(dep);

                    if (res == null)
                    {
                        this.Log().Warn("Can't resolve dependency {0} from resource {1}.", dep, Name);
                        return(false);
                    }

                    await res.Start();

                    res.AddDependant(Name);
                }

                // execute tasks
                var runner = new Tasks.ResourceTaskRunner();

                if (!await runner.ExecuteTasks(this))
                {
                    this.Log().Error("Executing tasks for resource {0} failed.", Name);

                    State = ResourceState.Stopped;
                    return(false);
                }

                m_watcher = new FileSystemWatcher();

                // create script environment
                if (!EnsureScriptEnvironment())
                {
                    return(false);
                }
            }

            State = ResourceState.Starting;

            m_scriptEnvironment.DoInitFile(false);

            // trigger event
            if (!Manager.TriggerEvent("onResourceStarting", -1, Name))
            {
                // how the h-
                if (State == ResourceState.Running)
                {
                    return(true);
                }

                Stop();

                return(false);
            }

            m_scriptEnvironment.LoadScripts();

            // TODO: add development mode check
            m_watcher.Path = Path;
            m_watcher.IncludeSubdirectories = true;
            m_watcher.NotifyFilter          = NotifyFilters.LastWrite;
            m_watcher.Changed += (s, e) => InvokeUpdateClientPackage();
            m_watcher.Created += (s, e) => InvokeUpdateClientPackage();
            m_watcher.Deleted += (s, e) => InvokeUpdateClientPackage();
            m_watcher.Renamed += (s, e) => InvokeUpdateClientPackage();

            m_watcher.EnableRaisingEvents = true;

            State = ResourceState.Running;

            // trigger event
            if (!Manager.TriggerEvent("onResourceStart", -1, Name))
            {
                this.Log().Info("Resource start canceled by event.");

                Stop();

                return(false);
            }

            // log that we started
            this.Log().Info("Started resource {0}.", Name);

            // broadcast to current clients
            var clients = ClientInstances.Clients.Where(c => c.Value.NetChannel != null).Select(c => c.Value);

            foreach (var client in clients)
            {
                client.SendReliableCommand(0xAFE4CD4A, Encoding.UTF8.GetBytes(Name)); // msgResStart
            }

            return(true);
        }
예제 #4
0
        private void InvokeUpdateClientPackage()
        {
            if (ms_clientUpdateQueued)
            {
                return;
            }

            ms_clientUpdateQueued = true;

            Task.Run(async () =>
            {
                await Task.Delay(500);

                // go
                var runner = new Tasks.ResourceTaskRunner();
                runner.ExecuteTasks(this);

                // and unlock the lock
                ms_clientUpdateQueued = false;
            });
        }