Update samples

This commit is contained in:
hevinci
2022-07-18 14:59:15 +08:00
parent df5f0b9c13
commit 95e6921a4e
357 changed files with 0 additions and 8 deletions

View File

@@ -0,0 +1,26 @@

/// <summary>
/// 用户层反馈的操作方式
/// </summary>
public enum EPatchOperation
{
/// <summary>
/// 开始下载网络文件
/// </summary>
BeginDownloadWebFiles,
/// <summary>
/// 尝试再次更新静态版本
/// </summary>
TryUpdateStaticVersion,
/// <summary>
/// 尝试再次更新补丁清单
/// </summary>
TryUpdatePatchManifest,
/// <summary>
/// 尝试再次下载网络文件
/// </summary>
TryDownloadWebFiles,
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 219b7f0e5d04432429b5fcb2207fa23b
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,31 @@

/// <summary>
/// 补丁系统更新状态
/// </summary>
public enum EPatchStates
{
/// <summary>
/// 更新静态的资源版本
/// </summary>
UpdateStaticVersion,
/// <summary>
/// 更新补丁清单
/// </summary>
UpdateManifest,
/// <summary>
/// 创建下载器
/// </summary>
CreateDownloader,
/// <summary>
/// 下载远端文件
/// </summary>
DownloadWebFiles,
/// <summary>
/// 补丁流程完毕
/// </summary>
PatchDone,
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 6f1f35b77d2285045b35f588be8f7236
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 22a43ec58b9da1743bfa629be08ef1ac
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,46 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using YooAsset;
public class FsmCreateDownloader : IFsmNode
{
public string Name { private set; get; } = nameof(FsmCreateDownloader);
void IFsmNode.OnEnter()
{
PatchEventDispatcher.SendPatchStepsChangeMsg(EPatchStates.CreateDownloader);
BootScene.Instance.StartCoroutine(CreateDownloader());
}
void IFsmNode.OnUpdate()
{
}
void IFsmNode.OnExit()
{
}
IEnumerator CreateDownloader()
{
yield return new WaitForSecondsRealtime(0.5f);
Debug.Log("创建补丁下载器.");
int downloadingMaxNum = 10;
int failedTryAgain = 3;
PatchUpdater.Downloader = YooAssets.CreatePatchDownloader(downloadingMaxNum, failedTryAgain);
if (PatchUpdater.Downloader.TotalDownloadCount == 0)
{
Debug.Log("没有发现需要下载的资源");
FsmManager.Transition(nameof(FsmPatchDone));
}
else
{
Debug.Log($"一共发现了{PatchUpdater.Downloader.TotalDownloadCount}个资源需要更新下载。");
// 发现新更新文件后,挂起流程系统
// 注意:开发者需要在下载前检测磁盘空间不足
int totalDownloadCount = PatchUpdater.Downloader.TotalDownloadCount;
long totalDownloadBytes = PatchUpdater.Downloader.TotalDownloadBytes;
PatchEventDispatcher.SendFoundUpdateFilesMsg(totalDownloadCount, totalDownloadBytes);
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 8b104741ebcb72946abe282c1da73360
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,36 @@
using System.Collections;
using YooAsset;
public class FsmDownloadWebFiles : IFsmNode
{
public string Name { private set; get; } = nameof(FsmDownloadWebFiles);
void IFsmNode.OnEnter()
{
PatchEventDispatcher.SendPatchStepsChangeMsg(EPatchStates.DownloadWebFiles);
BootScene.Instance.StartCoroutine(BeginDownload());
}
void IFsmNode.OnUpdate()
{
}
void IFsmNode.OnExit()
{
}
private IEnumerator BeginDownload()
{
var downloader = PatchUpdater.Downloader;
// 注册下载回调
downloader.OnDownloadErrorCallback = PatchEventDispatcher.SendWebFileDownloadFailedMsg;
downloader.OnDownloadProgressCallback = PatchEventDispatcher.SendDownloadProgressUpdateMsg;
downloader.BeginDownload();
yield return downloader;
// 检测下载结果
if (downloader.Status != EOperationStatus.Succeed)
yield break;
FsmManager.Transition(nameof(FsmPatchDone));
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: a3fae22040fe2d541ab4582c7c1c71fb
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,22 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
internal class FsmPatchDone : IFsmNode
{
public string Name { private set; get; } = nameof(FsmPatchDone);
void IFsmNode.OnEnter()
{
PatchEventDispatcher.SendPatchStepsChangeMsg(EPatchStates.PatchDone);
Debug.Log("补丁流程更新完毕!");
YooAsset.YooAssets.LoadSceneAsync("Scene/Game1");
}
void IFsmNode.OnUpdate()
{
}
void IFsmNode.OnExit()
{
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 6cf80cfbed474c34b892eaeda7fcb054
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,30 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
internal class FsmPatchInit : IFsmNode
{
public string Name { private set; get; } = nameof(FsmPatchInit);
void IFsmNode.OnEnter()
{
// 加载更新面板
var go = Resources.Load<GameObject>("PatchWindow");
GameObject.Instantiate(go);
BootScene.Instance.StartCoroutine(Begin());
}
void IFsmNode.OnUpdate()
{
}
void IFsmNode.OnExit()
{
}
private IEnumerator Begin()
{
yield return new WaitForSecondsRealtime(0.5f);
FsmManager.Transition(nameof(FsmUpdateStaticVersion));
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 8998f67b4187d404eb26190f5cd5ac86
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,40 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using YooAsset;
public class FsmUpdateManifest : IFsmNode
{
public string Name { private set; get; } = nameof(FsmUpdateManifest);
void IFsmNode.OnEnter()
{
PatchEventDispatcher.SendPatchStepsChangeMsg(EPatchStates.UpdateManifest);
BootScene.Instance.StartCoroutine(UpdateManifest());
}
void IFsmNode.OnUpdate()
{
}
void IFsmNode.OnExit()
{
}
private IEnumerator UpdateManifest()
{
yield return new WaitForSecondsRealtime(0.5f);
// 更新补丁清单
var operation = YooAssets.UpdateManifestAsync(PatchUpdater.ResourceVersion, 30);
yield return operation;
if(operation.Status == EOperationStatus.Succeed)
{
FsmManager.Transition(nameof(FsmCreateDownloader));
}
else
{
Debug.LogWarning(operation.Error);
PatchEventDispatcher.SendPatchManifestUpdateFailedMsg();
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: b4b6ef49759dcf14d80c5aa2d360f597
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,42 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using YooAsset;
internal class FsmUpdateStaticVersion : IFsmNode
{
public string Name { private set; get; } = nameof(FsmUpdateStaticVersion);
void IFsmNode.OnEnter()
{
PatchEventDispatcher.SendPatchStepsChangeMsg(EPatchStates.UpdateStaticVersion);
BootScene.Instance.StartCoroutine(GetStaticVersion());
}
void IFsmNode.OnUpdate()
{
}
void IFsmNode.OnExit()
{
}
private IEnumerator GetStaticVersion()
{
yield return new WaitForSecondsRealtime(0.5f);
// 更新资源版本号
var operation = YooAssets.UpdateStaticVersionAsync(30);
yield return operation;
if (operation.Status == EOperationStatus.Succeed)
{
Debug.Log($"Found static version : {operation.ResourceVersion}");
PatchUpdater.ResourceVersion = operation.ResourceVersion;
FsmManager.Transition(nameof(FsmUpdateManifest));
}
else
{
Debug.LogWarning(operation.Error);
PatchEventDispatcher.SendStaticVersionUpdateFailedMsg();
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: f5ea76dfe0fc52b47a00a3bf68a54d09
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,43 @@

public static class PatchEventDispatcher
{
public static void SendPatchStepsChangeMsg(EPatchStates currentStates)
{
PatchEventMessageDefine.PatchStatesChange msg = new PatchEventMessageDefine.PatchStatesChange();
msg.CurrentStates = currentStates;
EventManager.SendMessage(msg);
}
public static void SendFoundUpdateFilesMsg(int totalCount, long totalSizeBytes)
{
PatchEventMessageDefine.FoundUpdateFiles msg = new PatchEventMessageDefine.FoundUpdateFiles();
msg.TotalCount = totalCount;
msg.TotalSizeBytes = totalSizeBytes;
EventManager.SendMessage(msg);
}
public static void SendDownloadProgressUpdateMsg(int totalDownloadCount, int currentDownloadCount, long totalDownloadSizeBytes, long currentDownloadSizeBytes)
{
PatchEventMessageDefine.DownloadProgressUpdate msg = new PatchEventMessageDefine.DownloadProgressUpdate();
msg.TotalDownloadCount = totalDownloadCount;
msg.CurrentDownloadCount = currentDownloadCount;
msg.TotalDownloadSizeBytes = totalDownloadSizeBytes;
msg.CurrentDownloadSizeBytes = currentDownloadSizeBytes;
EventManager.SendMessage(msg);
}
public static void SendStaticVersionUpdateFailedMsg()
{
PatchEventMessageDefine.StaticVersionUpdateFailed msg = new PatchEventMessageDefine.StaticVersionUpdateFailed();
EventManager.SendMessage(msg);
}
public static void SendPatchManifestUpdateFailedMsg()
{
PatchEventMessageDefine.PatchManifestUpdateFailed msg = new PatchEventMessageDefine.PatchManifestUpdateFailed();
EventManager.SendMessage(msg);
}
public static void SendWebFileDownloadFailedMsg(string fileName, string error)
{
PatchEventMessageDefine.WebFileDownloadFailed msg = new PatchEventMessageDefine.WebFileDownloadFailed();
msg.FileName = fileName;
msg.Error = error;
EventManager.SendMessage(msg);
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: fe1971e74e4881641b59485f977219dc
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,54 @@

public class PatchEventMessageDefine
{
/// <summary>
/// 补丁流程步骤改变
/// </summary>
public class PatchStatesChange : IEventMessage
{
public EPatchStates CurrentStates;
}
/// <summary>
/// 发现更新文件
/// </summary>
public class FoundUpdateFiles : IEventMessage
{
public int TotalCount;
public long TotalSizeBytes;
}
/// <summary>
/// 下载进度更新
/// </summary>
public class DownloadProgressUpdate : IEventMessage
{
public int TotalDownloadCount;
public int CurrentDownloadCount;
public long TotalDownloadSizeBytes;
public long CurrentDownloadSizeBytes;
}
/// <summary>
/// 资源版本号更新失败
/// </summary>
public class StaticVersionUpdateFailed : IEventMessage
{
}
/// <summary>
/// 补丁清单更新失败
/// </summary>
public class PatchManifestUpdateFailed : IEventMessage
{
}
/// <summary>
/// 网络文件下载失败
/// </summary>
public class WebFileDownloadFailed : IEventMessage
{
public string FileName;
public string Error;
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 7bb954fb42c50874b8897756e8b5dae5
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,73 @@
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using YooAsset;
public static class PatchUpdater
{
private static bool _isRun = false;
/// <summary>
/// 下载器
/// </summary>
public static PatchDownloaderOperation Downloader { set; get; }
/// <summary>
/// 资源版本
/// </summary>
public static int ResourceVersion { set; get; }
/// <summary>
/// 开启初始化流程
/// </summary>
public static void Run()
{
if (_isRun == false)
{
_isRun = true;
Debug.Log("开始补丁更新...");
// 注意:按照先后顺序添加流程节点
FsmManager.AddNode(new FsmPatchInit());
FsmManager.AddNode(new FsmUpdateStaticVersion());
FsmManager.AddNode(new FsmUpdateManifest());
FsmManager.AddNode(new FsmCreateDownloader());
FsmManager.AddNode(new FsmDownloadWebFiles());
FsmManager.AddNode(new FsmPatchDone());
FsmManager.Run(nameof(FsmPatchInit));
}
else
{
Debug.LogWarning("补丁更新已经正在进行中!");
}
}
/// <summary>
/// 处理请求操作
/// </summary>
public static void HandleOperation(EPatchOperation operation)
{
if (operation == EPatchOperation.BeginDownloadWebFiles)
{
FsmManager.Transition(nameof(FsmDownloadWebFiles));
}
else if(operation == EPatchOperation.TryUpdateStaticVersion)
{
FsmManager.Transition(nameof(FsmUpdateStaticVersion));
}
else if (operation == EPatchOperation.TryUpdatePatchManifest)
{
FsmManager.Transition(nameof(FsmUpdateManifest));
}
else if (operation == EPatchOperation.TryDownloadWebFiles)
{
FsmManager.Transition(nameof(FsmCreateDownloader));
}
else
{
throw new NotImplementedException($"{operation}");
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 4c1b4cadd806a1a409db720564a44a83
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,185 @@
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class PatchWindow : MonoBehaviour
{
/// <summary>
/// 对话框封装类
/// </summary>
private class MessageBox
{
private GameObject _cloneObject;
private Text _content;
private Button _btnOK;
private System.Action _clickOK;
public bool ActiveSelf
{
get
{
return _cloneObject.activeSelf;
}
}
public void Create(GameObject cloneObject)
{
_cloneObject = cloneObject;
_content = cloneObject.transform.Find("txt_content").GetComponent<Text>();
_btnOK = cloneObject.transform.Find("btn_ok").GetComponent<Button>();
_btnOK.onClick.AddListener(OnClickYes);
}
public void Show(string content, System.Action clickOK)
{
_content.text = content;
_clickOK = clickOK;
_cloneObject.SetActive(true);
_cloneObject.transform.SetAsLastSibling();
}
public void Hide()
{
_content.text = string.Empty;
_clickOK = null;
_cloneObject.SetActive(false);
}
private void OnClickYes()
{
_clickOK?.Invoke();
Hide();
}
}
private readonly EventGroup _eventGroup = new EventGroup();
private readonly List<MessageBox> _msgBoxList = new List<MessageBox>();
// UGUI相关
private GameObject _messageBoxObj;
private Slider _slider;
private Text _tips;
void Awake()
{
_slider = transform.Find("UIWindow/Slider").GetComponent<Slider>();
_tips = transform.Find("UIWindow/Slider/txt_tips").GetComponent<Text>();
_tips.text = "Initializing the game world !";
_messageBoxObj = transform.Find("UIWindow/MessgeBox").gameObject;
_messageBoxObj.SetActive(false);
_eventGroup.AddListener<PatchEventMessageDefine.PatchStatesChange>(OnHandleEvent);
_eventGroup.AddListener<PatchEventMessageDefine.FoundUpdateFiles>(OnHandleEvent);
_eventGroup.AddListener<PatchEventMessageDefine.DownloadProgressUpdate>(OnHandleEvent);
_eventGroup.AddListener<PatchEventMessageDefine.StaticVersionUpdateFailed>(OnHandleEvent);
_eventGroup.AddListener<PatchEventMessageDefine.PatchManifestUpdateFailed>(OnHandleEvent);
_eventGroup.AddListener<PatchEventMessageDefine.WebFileDownloadFailed>(OnHandleEvent);
}
void OnDestroy()
{
_eventGroup.RemoveAllListener();
}
/// <summary>
/// 接收事件
/// </summary>
private void OnHandleEvent(IEventMessage msg)
{
if (msg is PatchEventMessageDefine.PatchStatesChange)
{
var message = msg as PatchEventMessageDefine.PatchStatesChange;
if (message.CurrentStates == EPatchStates.UpdateStaticVersion)
_tips.text = "Update static version.";
else if (message.CurrentStates == EPatchStates.UpdateManifest)
_tips.text = "Update patch manifest.";
else if (message.CurrentStates == EPatchStates.CreateDownloader)
_tips.text = "Check download contents.";
else if (message.CurrentStates == EPatchStates.DownloadWebFiles)
_tips.text = "Downloading patch files.";
else if (message.CurrentStates == EPatchStates.PatchDone)
_tips.text = "Welcome to game world !";
else
throw new NotImplementedException(message.CurrentStates.ToString());
}
else if (msg is PatchEventMessageDefine.FoundUpdateFiles)
{
var message = msg as PatchEventMessageDefine.FoundUpdateFiles;
System.Action callback = () =>
{
PatchUpdater.HandleOperation(EPatchOperation.BeginDownloadWebFiles);
};
float sizeMB = message.TotalSizeBytes / 1048576f;
sizeMB = Mathf.Clamp(sizeMB, 0.1f, float.MaxValue);
string totalSizeMB = sizeMB.ToString("f1");
ShowMessageBox($"Found update patch files, Total count {message.TotalCount} Total szie {totalSizeMB}MB", callback);
}
else if (msg is PatchEventMessageDefine.DownloadProgressUpdate)
{
var message = msg as PatchEventMessageDefine.DownloadProgressUpdate;
_slider.value = (float)message.CurrentDownloadCount / message.TotalDownloadCount;
string currentSizeMB = (message.CurrentDownloadSizeBytes / 1048576f).ToString("f1");
string totalSizeMB = (message.TotalDownloadSizeBytes / 1048576f).ToString("f1");
_tips.text = $"{message.CurrentDownloadCount}/{message.TotalDownloadCount} {currentSizeMB}MB/{totalSizeMB}MB";
}
else if (msg is PatchEventMessageDefine.StaticVersionUpdateFailed)
{
System.Action callback = () =>
{
PatchUpdater.HandleOperation(EPatchOperation.TryUpdateStaticVersion);
};
ShowMessageBox($"Failed to update static version, please check the network status.", callback);
}
else if (msg is PatchEventMessageDefine.PatchManifestUpdateFailed)
{
System.Action callback = () =>
{
PatchUpdater.HandleOperation(EPatchOperation.TryUpdatePatchManifest);
};
ShowMessageBox($"Failed to update patch manifest, please check the network status.", callback);
}
else if (msg is PatchEventMessageDefine.WebFileDownloadFailed)
{
var message = msg as PatchEventMessageDefine.WebFileDownloadFailed;
System.Action callback = () =>
{
PatchUpdater.HandleOperation(EPatchOperation.TryDownloadWebFiles);
};
ShowMessageBox($"Failed to download file : {message.FileName}", callback);
}
else
{
throw new System.NotImplementedException($"{msg.GetType()}");
}
}
/// <summary>
/// 显示对话框
/// </summary>
private void ShowMessageBox(string content, System.Action ok)
{
// 尝试获取一个可用的对话框
MessageBox msgBox = null;
for (int i = 0; i < _msgBoxList.Count; i++)
{
var item = _msgBoxList[i];
if (item.ActiveSelf == false)
{
msgBox = item;
break;
}
}
// 如果没有可用的对话框,则创建一个新的对话框
if (msgBox == null)
{
msgBox = new MessageBox();
var cloneObject = GameObject.Instantiate(_messageBoxObj, _messageBoxObj.transform.parent);
msgBox.Create(cloneObject);
_msgBoxList.Add(msgBox);
}
// 显示对话框
msgBox.Show(content, ok);
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: c569e2dd13155914aa41a50e0e588b6d
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: