using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using UniFramework.Machine; using UniFramework.Event; using YooAsset; public class PatchOperation : GameAsyncOperation { private enum ESteps { None, Update, Done, } private readonly EventGroup _eventGroup = new EventGroup(); private readonly StateMachine _machine; private ESteps _steps = ESteps.None; public PatchOperation(string packageName, string buildPipeline, EPlayMode playMode) { // 注册监听事件 _eventGroup.AddListener(OnHandleEventMessage); _eventGroup.AddListener(OnHandleEventMessage); _eventGroup.AddListener(OnHandleEventMessage); _eventGroup.AddListener(OnHandleEventMessage); _eventGroup.AddListener(OnHandleEventMessage); // 创建状态机 _machine = new StateMachine(this); _machine.AddNode(); _machine.AddNode(); _machine.AddNode(); _machine.AddNode(); _machine.AddNode(); _machine.AddNode(); _machine.AddNode(); _machine.AddNode(); _machine.SetBlackboardValue("PackageName", packageName); _machine.SetBlackboardValue("PlayMode", playMode); _machine.SetBlackboardValue("BuildPipeline", buildPipeline); } protected override void OnStart() { _steps = ESteps.Update; _machine.Run(); } protected override void OnUpdate() { if (_steps == ESteps.None || _steps == ESteps.Done) return; if(_steps == ESteps.Update) { _machine.Update(); if(_machine.CurrentNode == typeof(FsmUpdaterDone).FullName) { _eventGroup.RemoveAllListener(); Status = EOperationStatus.Succeed; _steps = ESteps.Done; } } } protected override void OnAbort() { } /// /// 接收事件 /// private void OnHandleEventMessage(IEventMessage message) { if (message is UserEventDefine.UserTryInitialize) { _machine.ChangeState(); } else if (message is UserEventDefine.UserBeginDownloadWebFiles) { _machine.ChangeState(); } else if (message is UserEventDefine.UserTryUpdatePackageVersion) { _machine.ChangeState(); } else if (message is UserEventDefine.UserTryUpdatePatchManifest) { _machine.ChangeState(); } else if (message is UserEventDefine.UserTryDownloadWebFiles) { _machine.ChangeState(); } else { throw new System.NotImplementedException($"{message.GetType()}"); } } }