using System; namespace etoy { abstract class Command : IContextSetter, ICommand { bool _completed; bool _running; float _progress; Exception _exception; void IContextSetter.SetContext(Context context) => Context = context; bool ICommand.Start => _running; bool ICommand.Done => _completed; float ICommand.Progress => _progress; Exception ICommand.Exception => _exception; void ICommand.Execute() { if (!_running) { _running = true; OnProcess(); } } /// /// 上下文 /// protected Context Context { get; private set; } /// /// 异步完成 /// protected void Completed() { _progress = 1.0f; _completed = true; } protected void SetException(Exception e) { _exception = e; } /// /// 设置进度 /// /// protected void SetProgress(float progress) { if (progress >= 1.0f) _progress = 1.0f; else if (progress < 0.0f) _progress = 0.0f; else _progress = progress; } /// /// 处理回调 /// protected abstract void OnProcess(); /// /// 命令描述, 用于显示在控制台上 /// public abstract string Description { get; } } }