123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575 |
-
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.IO;
- using UnityEngine;
- using UnityEngine.Networking;
- public interface IUpdater
- {
- void OnStart();
- void OnMessage(string msg);
- void OnProgress(float progress);
- void OnVersion(string ver);
- void OnClear();
- }
- public class Updater : MonoBehaviour, IUpdater
- {
- enum Step
- {
- Wait,
- Copy,
- Coping,
- Versions,
- Prepared,
- Download,
- }
- private Step _step;
- [SerializeField] private string baseURL = "http://127.0.0.1:7888/DLC/";
- [SerializeField] private string gameScene = "Game.unity";
- [SerializeField] private bool enableVFS = true;
- public bool development;
- public IUpdater listener { get; set; }
- private string _platform;
- private string _savePath;
- private IEnumerator _checking;
- private void Start()
- {
- _savePath = string.Format("{0}/DLC/", UnityEngine.Application.persistentDataPath);
- _platform = GetPlatformForAssetBundles(UnityEngine.Application.platform);
- _step = Step.Wait;
- LoadModule.updatePath = _savePath;
- }
- public void StartUpdate()
- {
- GameLog.Log("StartUpdate.Development:" + development);
- #if UNITY_EDITOR
- if (development)
- {
- LoadModule.runtimeMode = false;
- StartCoroutine(LoadGameScene());
- return;
- }
- #endif
- OnStart();
- if (_checking != null)
- {
- StopCoroutine(_checking);
- }
- _checking = Checking();
- StartCoroutine(_checking);
- }
- private IEnumerator LoadGameScene()
- {
- OnMessage("正在初始化");
- var init = LoadModule.Initialize();
- yield return init;
- if (string.IsNullOrEmpty(init.error))
- {
- LoadModule.AddSearchPath("Assets/_Scenes");
- init.Release();
- OnProgress(0);
- OnMessage("加载游戏场景");
- //var scene = LoadModule.LoadSceneAsync(gameScene, false);
- //while (!scene.isDone)
- //{
- // OnProgress(scene.progress);
- // yield return null;
- //}
- }
- else
- {
- init.Release();
- //var mb = MessageBox.Show("提示", "初始化异常错误:" + init.error + "请联系技术支持");
- //yield return mb;
- Quit();
- }
- }
- private void OnComplete()
- {
- //if (enableVFS)
- //{
- // string dataPath = _savePath + Versions.Dataname;
- // var downloads = _downloader.downloads;
- // if (downloads.Count > 0 && File.Exists(dataPath))
- // {
- // OnMessage("更新本地版本信息");
- // var files = new List<VFile>(downloads.Count);
- // foreach (var download in downloads)
- // {
- // files.Add(new VFile
- // {
- // name = download.name,
- // hash = download.hash,
- // len = download.len,
- // });
- // }
- // var file = files[0];
- // if (!file.name.Equals(Versions.Dataname))
- // Versions.UpdateDisk(dataPath, files);
- // }
- // Versions.LoadDisk(dataPath);
- //}
- //OnProgress(1);
- //OnMessage("更新完成");
- //var version = Versions.LoadVersion(_savePath + Versions.Filename);
- //if (version > 0)
- // OnVersion(version.ToString());
- StartCoroutine(LoadGameScene());
- }
- public void Clear()
- {
- }
- private void Quit()
- {
- #if UNITY_EDITOR
- UnityEditor.EditorApplication.isPlaying = false;
- #else
- Application.Quit();
- #endif
- }
- private void OnDestroy()
- {
- }
- //private void AddDownload(VFile item)
- //{
- // //_downloader.AddDownload(GetDownloadURL(item.name), item.name, _savePath + item.name, item.hash, item.len);
- //}
- //private void PrepareDownloads()
- //{
- // if (enableVFS)
- // {
- // var path = string.Format("{0}{1}", _savePath, Versions.Dataname);
- // if (!File.Exists(path))
- // {
- // //AddDownload(_versions[0]);
- // return;
- // }
- // Versions.LoadDisk(path);
- // }
- // for (var i = 1; i < _versions.Count; i++)
- // {
- // var item = _versions[i];
- // if (Versions.IsNew(string.Format("{0}{1}", _savePath, item.name), item.len, item.hash))
- // // AddDownload(item);
- // }
- //}
- #region Get
- private static string GetPlatformForAssetBundles(RuntimePlatform target)
- {
- // ReSharper disable once SwitchStatementMissingSomeCases
- switch (target)
- {
- case RuntimePlatform.Android:
- return "Android";
- case RuntimePlatform.IPhonePlayer:
- return "iOS";
- case RuntimePlatform.WebGLPlayer:
- return "WebGL";
- case RuntimePlatform.WindowsPlayer:
- case RuntimePlatform.WindowsEditor:
- return "Windows";
- case RuntimePlatform.OSXEditor:
- case RuntimePlatform.OSXPlayer:
- return "iOS"; // OSX
- default:
- return null;
- }
- }
- private string GetDownloadURL(string filename)
- {
- return string.Format("{0}{1}/{2}", baseURL, _platform, filename);
- }
- private static string GetStreamingAssetsPath()
- {
- if (UnityEngine.Application.platform == RuntimePlatform.Android)
- return UnityEngine.Application.streamingAssetsPath;
- if (UnityEngine.Application.platform == RuntimePlatform.WindowsPlayer ||
- UnityEngine.Application.platform == RuntimePlatform.WindowsEditor)
- return "file:///" + UnityEngine.Application.streamingAssetsPath;
- return "file://" + UnityEngine.Application.streamingAssetsPath;
- }
- #endregion
- #region Step
- /// <summary>
- /// 主协程,状态推进器
- /// </summary>
- /// <returns></returns>
- private IEnumerator Checking()
- {
- if (!Directory.Exists(_savePath))
- {
- Directory.CreateDirectory(_savePath);
- }
- if (_step == Step.Wait)
- {
- if (enableVFS)
- _step = Step.Copy;
- else
- {
- yield return null;
- _step = Step.Copy;
- }
- }
- if (_step == Step.Copy)
- {
- // yield return RequestCopy();
- }
- if (_step == Step.Coping)
- {
- //var path = _savePath + Versions.Filename + ".tmp";
- //List<VFile> versions = Versions.LoadVersions(path);
- //var basePath = GetStreamingAssetsPath() + "/";
- //yield return UpdateCopy(versions, basePath);
- _step = Step.Versions;
- }
- if (_step == Step.Versions)
- {
- // yield return RequestVersions();
- }
- if (_step == Step.Prepared)
- {
- //OnMessage("正在检查版本信息...");
- //var totalSize = _downloader.size;
- //if (totalSize > 0)
- //{
- // var tips = string.Format("发现内容更新,总计需要下载 {0} 内容", Downloader.GetDisplaySize(totalSize));
- // var mb = MessageBox.Show("提示", tips, "下载", "退出");
- // yield return mb;
- // if (mb.isOk)
- // {
- // _downloader.StartDownload();
- _step = Step.Download;
- // }
- // else
- // {
- // Quit();
- // }
- }
- else
- {
- OnComplete();
- }
- }
- //private IEnumerator RequestVFS()
- //{
- // var mb = MessageBox.Show("提示", "是否开启VFS?开启有助于提升IO性能和数据安全。", "开启");
- // yield return mb;
- // enableVFS = mb.isOk;
- //}
- //private IEnumerator RequestCopy()
- //{
- // var v1 = Versions.LoadVersion(_savePath + Versions.Filename);
- // var basePath = GetStreamingAssetsPath() + "/";
- // var request = UnityWebRequest.Get(basePath + Versions.Filename);
- // var path = _savePath + Versions.Filename + ".tmp";
- // request.downloadHandler = new DownloadHandlerFile(path);
- // // 请求版本文件
- // yield return request.SendWebRequest();
- // if (string.IsNullOrEmpty(request.error))
- // {
- // int v2 = Versions.LoadVersion(path);
- // // 服务器版本号大于本地版本号==》解压
- // if (v2 > v1)
- // {
- // var mb = MessageBox.Show("提示", "是否将资源解压到本地?", "解压", "跳过");
- // yield return mb;
- // _step = mb.isOk ? Step.Coping : Step.Versions;
- // }
- // else
- // {
- // Versions.LoadVersions(path);
- // _step = Step.Versions;
- // }
- // }
- // else
- // _step = Step.Versions;
- // request.Dispose();
- //}
- //private IEnumerator UpdateCopy(IList<VFile> versions, string basePath)
- //{
- // VFile version = versions[0];
- // if (version.name.Equals(Versions.Dataname))
- // {
- // var request = UnityWebRequest.Get(basePath + version.name);
- // request.downloadHandler = new DownloadHandlerFile(_savePath + version.name);
- // var req = request.SendWebRequest();
- // while (!req.isDone)
- // {
- // OnMessage("正在复制文件");
- // OnProgress(req.progress);
- // yield return null;
- // }
- // request.Dispose();
- // }
- // else
- // {
- // for (var index = 0; index < versions.Count; index++)
- // {
- // var item = versions[index];
- // var request = UnityWebRequest.Get(basePath + item.name);
- // request.downloadHandler = new DownloadHandlerFile(_savePath + item.name);
- // yield return request.SendWebRequest();
- // request.Dispose();
- // OnMessage(string.Format("正在复制文件:{0}/{1}", index, versions.Count));
- // OnProgress(index * 1f / versions.Count);
- // }
- // }
- //}
- /// <summary>
- /// 获取版本信息
- /// </summary>
- /// <returns></returns>
- //private IEnumerator RequestVersions()
- //{
- // OnMessage("正在获取版本信息...");
- // if (Application.internetReachability == NetworkReachability.NotReachable)
- // {
- // var mb = MessageBox.Show("提示", "请检查网络连接状态", "重试", "退出");
- // yield return mb;
- // if (mb.isOk)
- // StartUpdate();
- // else
- // Quit();
- // yield break;
- // }
- // var url = GetDownloadURL(Versions.Filename);
- // GameLog.Log(url);
- // var request = UnityWebRequest.Get(url);
- // request.downloadHandler = new DownloadHandlerFile(_savePath + Versions.Filename);
- // yield return request.SendWebRequest();
- // var error = request.error;
- // request.Dispose();
- // if (!string.IsNullOrEmpty(error))
- // {
- // var mb = MessageBox.Show("提示", string.Format("获取服务器版本失败:{0}", error), "重试", "退出");
- // yield return mb;
- // if (mb.isOk)
- // StartUpdate();
- // else
- // Quit();
- // yield break;
- // }
- // try
- // {
- // _versions = Versions.LoadVersions(_savePath + Versions.Filename, true);
- // if (_versions.Count > 0)
- // {
- // //PrepareDownloads();
- // _step = Step.Prepared;
- // }
- // else
- // OnComplete();
- // }
- // catch (Exception e)
- // {
- // GameLog.LogException(e);
- // MessageBox.Show("提示", "版本文件加载失败", "重试", "退出").onComplete +=
- // delegate (MessageBox.EventId id)
- // {
- // if (id == MessageBox.EventId.Ok)
- // StartUpdate();
- // else
- // Quit();
- // };
- // }
- //}
- #endregion
- #region IUpdater
- public void OnMessage(string msg)
- {
- if (listener != null)
- {
- listener.OnMessage(msg);
- }
- }
- public void OnProgress(float progress)
- {
- if (listener != null)
- {
- listener.OnProgress(progress);
- }
- }
- public void OnVersion(string ver)
- {
- if (listener != null)
- {
- listener.OnVersion(ver);
- }
- }
- //private void OnUpdate(long progress, long size, float speed)
- //{
- // //OnMessage(string.Format("下载中...{0}/{1}, 速度:{2}",
- // // Downloader.GetDisplaySize(progress),
- // // Downloader.GetDisplaySize(size),
- // // Downloader.GetDisplaySpeed(speed)));
- // //OnProgress(progress * 1f / size);
- //}
- public void OnStart()
- {
- if (listener != null)
- {
- listener.OnStart();
- }
- }
- public void OnClear()
- {
- OnMessage("数据清除完毕");
- OnProgress(0);
- //_versions.Clear();
- //_downloader.Clear();
- _step = Step.Wait;
- _reachabilityChanged = false;
- LoadModule.Clear();
- if (listener != null)
- {
- listener.OnClear();
- }
- if (Directory.Exists(_savePath))
- {
- Directory.Delete(_savePath, true);
- }
- }
- #endregion
- #region INetworkMonitorListener
- private bool _reachabilityChanged;
- //public void OnReachablityChanged(NetworkReachability reachability)
- //{
- // if (_step == Step.Wait)
- // return;
- // _reachabilityChanged = true;
- // if (_step == Step.Download)
- // //_downloader.Stop();
- // if (reachability == NetworkReachability.NotReachable)
- // {
- // MessageBox.Show("提示!", "找不到网络,请确保手机已经联网", "确定", "退出").onComplete += delegate (MessageBox.EventId id)
- // {
- // if (id == MessageBox.EventId.Ok)
- // {
- // if (_step == Step.Download)
- // _downloader.Restart();
- // else
- // StartUpdate();
- // _reachabilityChanged = false;
- // }
- // else
- // Quit();
- // };
- // }
- // else
- // {
- // if (_step == Step.Download)
- // _downloader.Restart();
- // else
- // StartUpdate();
- // _reachabilityChanged = false;
- // MessageBox.CloseAll();
- // }
- //}
- private void OnApplicationFocus(bool hasFocus)
- {
- if (_reachabilityChanged || _step == Step.Wait)
- return;
- if (hasFocus)
- {
- //MessageBox.CloseAll();
- if (_step == Step.Download)
- {
- }
- // _downloader.Restart();
- else
- StartUpdate();
- }
- else
- {
- if (_step == Step.Download)
- {
- }
- //_downloader.Stop();
- }
- }
- #endregion
- }
|