fix:AR相关修改
This commit is contained in:
1 parent
74ba5790f9
commit
c95f6b7c70
12 files changed
+382
-368
No files matched your search
+97
-58
@@ -5,6 +5,7 @@ using System.Threading.Tasks;
|
||||
using Assets.Scenes.Ride.Scripts;
|
||||
using Assets.Scripts.Apis.Models;
|
||||
using Cysharp.Threading.Tasks;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Networking;
|
||||
|
||||
namespace Assets.AR
|
||||
@@ -20,10 +21,46 @@ namespace Assets.AR
|
||||
private readonly string _serverARRoutePath;
|
||||
private readonly string _serverVideoPath;
|
||||
|
||||
public bool IsVideoDownloaded { get; private set; }
|
||||
public bool IsARDataDownloaded { get; private set; }
|
||||
public bool IsARRouteDownloaded { get; private set; }
|
||||
public float Total { get; private set; } = 0;
|
||||
private bool IsVideoNeedDownload { get; set; }
|
||||
private bool IsARDataNeedDownload { get; set; }
|
||||
private bool IsARRouteNeedDownload { get; set; }
|
||||
public bool IsDone => !IsVideoNeedDownload && !IsARDataNeedDownload && !IsARRouteNeedDownload;
|
||||
|
||||
private float _total { get; set; }
|
||||
private float _videoProgress;
|
||||
public float VideoProgress
|
||||
{
|
||||
get => _videoProgress;
|
||||
set
|
||||
{
|
||||
_videoProgress = value;
|
||||
ComputeProgress();
|
||||
}
|
||||
}
|
||||
|
||||
private float _dataProgress;
|
||||
public float DataProgress
|
||||
{
|
||||
get => _dataProgress;
|
||||
set
|
||||
{
|
||||
_dataProgress = value;
|
||||
ComputeProgress();
|
||||
}
|
||||
}
|
||||
|
||||
private float _routeProgress;
|
||||
public float RouteProgress
|
||||
{
|
||||
get => _routeProgress;
|
||||
set
|
||||
{
|
||||
_routeProgress = value;
|
||||
ComputeProgress();
|
||||
}
|
||||
}
|
||||
|
||||
private Action<float> ReportProgress { get; set; }
|
||||
|
||||
public ARDownloader(MapRoute route)
|
||||
{
|
||||
@@ -41,52 +78,73 @@ namespace Assets.AR
|
||||
_serverARRoutePath = _route.VideoRoute;
|
||||
_serverVideoPath = _route.Url;
|
||||
}
|
||||
|
||||
public async UniTask<UnityWebRequest> DownLoadVideoAsync(IProgress<float> progress = null,
|
||||
CancellationTokenSource cancellation = default(CancellationTokenSource))
|
||||
|
||||
private void ComputeProgress()
|
||||
{
|
||||
return await DownloadToFileAsync(_serverVideoPath, _localVideoPath, progress, cancellation);
|
||||
var progress = (_videoProgress + _routeProgress + _dataProgress) / _total;
|
||||
ReportProgress?.Invoke(progress);
|
||||
}
|
||||
|
||||
public async Task DownLoadDefault(Action<float> progress = null,CancellationTokenSource cancellation = default(CancellationTokenSource))
|
||||
{
|
||||
ReportProgress = progress;
|
||||
|
||||
if(IsVideoNeedDownload)
|
||||
await DownLoadVideoAsync(Progress.Create<float>(x => VideoProgress = x),cancellation);
|
||||
if(IsARRouteNeedDownload)
|
||||
await DownLoadARRouteAsync(Progress.Create<float>(x=> RouteProgress = x),cancellation);
|
||||
if(IsARDataNeedDownload)
|
||||
await DownLoadARDataAsync(Progress.Create<float>(x=> DataProgress = x),cancellation);
|
||||
}
|
||||
|
||||
public async UniTask<UnityWebRequest> DownLoadARRouteAsync(IProgress<float> progress = null,
|
||||
CancellationTokenSource cancellation = default(CancellationTokenSource))
|
||||
private async UniTask<UnityWebRequest> DownLoadVideoAsync(IProgress<float> progress = null,CancellationTokenSource cancellation = default(CancellationTokenSource))
|
||||
{
|
||||
return await DownloadToFileAsync(_serverARRoutePath, _localARRoutePath, progress, cancellation);
|
||||
var result = await DownloadToFileAsync(_serverVideoPath, _localVideoPath, progress, cancellation);
|
||||
IsVideoNeedDownload = !result.isDone;
|
||||
return result;
|
||||
}
|
||||
|
||||
public async UniTask<UnityWebRequest> DownLoadARDataAsync(IProgress<float> progress = null,
|
||||
private async UniTask<UnityWebRequest> DownLoadARRouteAsync(IProgress<float> progress = null,CancellationTokenSource cancellation = default(CancellationTokenSource))
|
||||
{
|
||||
var result = await DownloadToFileAsync(_serverARRoutePath, _localARRoutePath, progress, cancellation);
|
||||
IsARRouteNeedDownload = !result.isDone;
|
||||
return result;
|
||||
}
|
||||
|
||||
private async UniTask<UnityWebRequest> DownLoadARDataAsync(IProgress<float> progress = null,
|
||||
CancellationTokenSource cancellation = default(CancellationTokenSource))
|
||||
{
|
||||
return await DownloadToFileAsync(_serverARDataPath, _localARDataPath, progress, cancellation);
|
||||
var result = await DownloadToFileAsync(_serverARDataPath, _localARDataPath, progress, cancellation);
|
||||
IsARDataNeedDownload = !result.isDone;
|
||||
return result;
|
||||
}
|
||||
|
||||
public async Task<bool> CheckAllAsync()
|
||||
{
|
||||
IsARRouteDownloaded = await CheckARRouteAsync();
|
||||
IsARDataDownloaded = await CheckARDataAsync();
|
||||
IsVideoDownloaded = await CheckVideoAsync();
|
||||
IsARRouteNeedDownload = await CheckARRouteAsync();
|
||||
IsARDataNeedDownload = await CheckARDataAsync();
|
||||
IsVideoNeedDownload = await CheckVideoAsync();
|
||||
|
||||
if (IsVideoDownloaded)
|
||||
Total += 1f;
|
||||
if (IsARRouteDownloaded)
|
||||
Total += 1f;
|
||||
if (IsARDataDownloaded)
|
||||
Total += 1f;
|
||||
if (IsVideoNeedDownload)
|
||||
_total += 1f;
|
||||
if (IsARRouteNeedDownload)
|
||||
_total += 1f;
|
||||
if (IsARDataNeedDownload)
|
||||
_total += 1f;
|
||||
|
||||
return IsVideoDownloaded || IsARRouteDownloaded || IsARDataDownloaded;
|
||||
return IsVideoNeedDownload || IsARRouteNeedDownload || IsARDataNeedDownload;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// return true if the video is needed to download
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public async Task<bool> CheckVideoAsync()
|
||||
private async Task<bool> CheckVideoAsync()
|
||||
{
|
||||
try
|
||||
{
|
||||
if (!File.Exists(_localVideoPath)) return true;
|
||||
var result = await HeadFileAsync(_serverVideoPath, _localVideoPath);
|
||||
return result.responseCode != 304;
|
||||
return await HeadFileAsync(_serverVideoPath, _localVideoPath);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
@@ -95,35 +153,33 @@ namespace Assets.AR
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// return true if the video is needed to download
|
||||
/// return true if the video need to update or download
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public async Task<bool> CheckARRouteAsync()
|
||||
private async Task<bool> CheckARRouteAsync()
|
||||
{
|
||||
if (!File.Exists(_localARRoutePath)) return true;
|
||||
var result = await HeadFileAsync(_serverARRoutePath, _localARRoutePath);
|
||||
return result.responseCode != 304;
|
||||
return await HeadFileAsync(_serverARRoutePath, _localARRoutePath);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// return true if the video is needed to download
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public async Task<bool> CheckARDataAsync()
|
||||
private async Task<bool> CheckARDataAsync()
|
||||
{
|
||||
if (!File.Exists(_localARDataPath)) return true;
|
||||
var result = await HeadFileAsync(_serverARDataPath, _localARDataPath);
|
||||
return result.responseCode != 304;
|
||||
return await HeadFileAsync(_serverARDataPath, _localARDataPath);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 下载文件到本地(持续存入磁盘减少内存占用)
|
||||
/// </summary>
|
||||
/// <param name="downloadUrl">文件连接</param>
|
||||
/// <param name="fullPath">本地文件全路径</param>
|
||||
/// <param name="progress">下载进度</param>
|
||||
public static async UniTask<UnityWebRequest> DownloadToFileAsync(string downloadUrl, string fullPath,
|
||||
/// <param name="cancellation"></param>
|
||||
private static async UniTask<UnityWebRequest> DownloadToFileAsync(string downloadUrl, string fullPath,
|
||||
IProgress<float> progress = null, CancellationTokenSource cancellation = default(CancellationTokenSource))
|
||||
{
|
||||
var dh = new DownloadHandlerFile(fullPath)
|
||||
@@ -138,12 +194,8 @@ namespace Assets.AR
|
||||
var result = await request.SendWebRequest().ToUniTask(progress, PlayerLoopTiming.Update,
|
||||
(cancellation?.Token ?? default(CancellationToken)));
|
||||
|
||||
var etag = result.GetResponseHeader("ETag");
|
||||
var etag = result.GetResponseHeader("x-oss-hash-crc64ecma");
|
||||
var path = fullPath + ".etag";
|
||||
if (!File.Exists(path))
|
||||
{
|
||||
File.Create(path);
|
||||
}
|
||||
File.WriteAllText(path, etag);
|
||||
|
||||
return result;
|
||||
@@ -155,7 +207,7 @@ namespace Assets.AR
|
||||
/// <param name="downloadUrl">download url</param>
|
||||
/// <param name="fullPath">local path for storage</param>
|
||||
/// <returns></returns>
|
||||
public static async UniTask<UnityWebRequest> HeadFileAsync(string downloadUrl, string fullPath,
|
||||
private static async Task<bool> HeadFileAsync(string downloadUrl, string fullPath,
|
||||
IProgress<float> progress = null, CancellationTokenSource cancellation = default(CancellationTokenSource))
|
||||
{
|
||||
try
|
||||
@@ -168,26 +220,13 @@ namespace Assets.AR
|
||||
}
|
||||
|
||||
var request = UnityWebRequest.Head(downloadUrl);
|
||||
if (!string.IsNullOrEmpty(etag))
|
||||
request.SetRequestHeader("If-None-Match", etag);
|
||||
|
||||
var result =
|
||||
await request
|
||||
.SendWebRequest().ToUniTask(progress, PlayerLoopTiming.Update,
|
||||
(cancellation?.Token ?? default(CancellationToken)));
|
||||
|
||||
if (result.responseCode != 304)
|
||||
{
|
||||
etag = result.GetResponseHeader("ETag");
|
||||
File.WriteAllText(fullPath + ".etag", etag);
|
||||
}
|
||||
|
||||
return result;
|
||||
|
||||
var result = await request.SendWebRequest().ToUniTask(progress, PlayerLoopTiming.Update, (cancellation?.Token ?? default(CancellationToken)));
|
||||
var responseTag = result.GetResponseHeader("x-oss-hash-crc64ecma");
|
||||
return !responseTag.Equals(etag);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
return null;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user