|
|
@@ -21,8 +21,8 @@ namespace ETModel
|
|
|
/// </summary>
|
|
|
public class BundleDownloaderComponent : Component
|
|
|
{
|
|
|
- public VersionConfig VersionConfig { get; private set; }
|
|
|
-
|
|
|
+ private VersionConfig remoteVersionConfig;
|
|
|
+
|
|
|
public Queue<string> bundles;
|
|
|
|
|
|
public long TotalSize;
|
|
|
@@ -33,81 +33,100 @@ namespace ETModel
|
|
|
|
|
|
public UnityWebRequestAsync webRequest;
|
|
|
|
|
|
- public TaskCompletionSource<bool> Tcs;
|
|
|
-
|
|
|
public async Task StartAsync()
|
|
|
{
|
|
|
- using (UnityWebRequestAsync webRequestAsync = ComponentFactory.Create<UnityWebRequestAsync>())
|
|
|
+ // 获取远程的Version.txt
|
|
|
+ string versionUrl = "";
|
|
|
+ try
|
|
|
{
|
|
|
- string versionUrl = GlobalConfigComponent.Instance.GlobalProto.GetUrl() + "StreamingAssets/" + "Version.txt";
|
|
|
- //Log.Debug(versionUrl);
|
|
|
- await webRequestAsync.DownloadAsync(versionUrl);
|
|
|
- this.VersionConfig = JsonHelper.FromJson<VersionConfig>(webRequestAsync.Request.downloadHandler.text);
|
|
|
- //Log.Debug(JsonHelper.ToJson(this.VersionConfig));
|
|
|
- }
|
|
|
+ using (UnityWebRequestAsync webRequestAsync = ComponentFactory.Create<UnityWebRequestAsync>())
|
|
|
+ {
|
|
|
+ versionUrl = GlobalConfigComponent.Instance.GlobalProto.GetUrl() + "StreamingAssets/" + "Version.txt";
|
|
|
+ //Log.Debug(versionUrl);
|
|
|
+ await webRequestAsync.DownloadAsync(versionUrl);
|
|
|
+ remoteVersionConfig = JsonHelper.FromJson<VersionConfig>(webRequestAsync.Request.downloadHandler.text);
|
|
|
+ //Log.Debug(JsonHelper.ToJson(this.VersionConfig));
|
|
|
+ }
|
|
|
|
|
|
+ }
|
|
|
+ catch (Exception e)
|
|
|
+ {
|
|
|
+ throw new Exception($"url: {versionUrl}");
|
|
|
+ }
|
|
|
|
|
|
- VersionConfig localVersionConfig;
|
|
|
- // 对比本地的Version.txt
|
|
|
- string versionPath = Path.Combine(PathHelper.AppHotfixResPath, "Version.txt");
|
|
|
- if (File.Exists(versionPath))
|
|
|
+ // 获取streaming目录的Version.txt
|
|
|
+ VersionConfig streamingVersionConfig;
|
|
|
+ string versionPath = Path.Combine(PathHelper.AppResPath4Web, "Version.txt");
|
|
|
+ using (UnityWebRequestAsync request = ComponentFactory.Create<UnityWebRequestAsync>())
|
|
|
{
|
|
|
- localVersionConfig = JsonHelper.FromJson<VersionConfig>(File.ReadAllText(versionPath));
|
|
|
+ await request.DownloadAsync(versionPath);
|
|
|
+ streamingVersionConfig = JsonHelper.FromJson<VersionConfig>(request.Request.downloadHandler.text);
|
|
|
}
|
|
|
- else
|
|
|
+
|
|
|
+ // 删掉远程不存在的文件
|
|
|
+ DirectoryInfo directoryInfo = new DirectoryInfo(PathHelper.AppHotfixResPath);
|
|
|
+ if (directoryInfo.Exists)
|
|
|
{
|
|
|
- versionPath = Path.Combine(PathHelper.AppResPath4Web, "Version.txt");
|
|
|
- using (UnityWebRequestAsync request = ComponentFactory.Create<UnityWebRequestAsync>())
|
|
|
+ FileInfo[] fileInfos = directoryInfo.GetFiles();
|
|
|
+ foreach (FileInfo fileInfo in fileInfos)
|
|
|
{
|
|
|
- await request.DownloadAsync(versionPath);
|
|
|
- localVersionConfig = JsonHelper.FromJson<VersionConfig>(request.Request.downloadHandler.text);
|
|
|
+ if (remoteVersionConfig.FileInfoDict.ContainsKey(fileInfo.Name))
|
|
|
+ {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ fileInfo.Delete();
|
|
|
}
|
|
|
}
|
|
|
+ else
|
|
|
+ {
|
|
|
+ directoryInfo.Create();
|
|
|
+ }
|
|
|
|
|
|
-
|
|
|
- // 先删除服务器端没有的ab
|
|
|
- foreach (FileVersionInfo fileVersionInfo in localVersionConfig.FileInfoDict.Values)
|
|
|
+ // 对比MD5
|
|
|
+ foreach (FileVersionInfo fileVersionInfo in remoteVersionConfig.FileInfoDict.Values)
|
|
|
{
|
|
|
- if (this.VersionConfig.FileInfoDict.ContainsKey(fileVersionInfo.File))
|
|
|
+ // 对比md5
|
|
|
+ string localFileMD5 = BundleHelper.GetBundleMD5(streamingVersionConfig, fileVersionInfo.File);
|
|
|
+ if (fileVersionInfo.MD5 == localFileMD5)
|
|
|
{
|
|
|
continue;
|
|
|
}
|
|
|
- string abPath = Path.Combine(PathHelper.AppHotfixResPath, fileVersionInfo.File);
|
|
|
- File.Delete(abPath);
|
|
|
+ this.bundles.Enqueue(fileVersionInfo.File);
|
|
|
+ this.TotalSize += fileVersionInfo.Size;
|
|
|
}
|
|
|
+ }
|
|
|
|
|
|
- // 再下载
|
|
|
- foreach (FileVersionInfo fileVersionInfo in this.VersionConfig.FileInfoDict.Values)
|
|
|
+ public int Progress
|
|
|
+ {
|
|
|
+ get
|
|
|
{
|
|
|
- FileVersionInfo localVersionInfo;
|
|
|
- if (localVersionConfig.FileInfoDict.TryGetValue(fileVersionInfo.File, out localVersionInfo))
|
|
|
+ if (this.TotalSize == 0)
|
|
|
{
|
|
|
- if (fileVersionInfo.MD5 == localVersionInfo.MD5)
|
|
|
- {
|
|
|
- continue;
|
|
|
- }
|
|
|
+ return 0;
|
|
|
}
|
|
|
|
|
|
- if (fileVersionInfo.File == "Version.txt")
|
|
|
+ long alreadyDownloadBytes = 0;
|
|
|
+ foreach (string downloadedBundle in this.downloadedBundles)
|
|
|
{
|
|
|
- continue;
|
|
|
+ long size = this.remoteVersionConfig.FileInfoDict[downloadedBundle].Size;
|
|
|
+ alreadyDownloadBytes += size;
|
|
|
}
|
|
|
-
|
|
|
- this.bundles.Enqueue(fileVersionInfo.File);
|
|
|
- this.TotalSize += fileVersionInfo.Size;
|
|
|
+ if (this.webRequest != null)
|
|
|
+ {
|
|
|
+ alreadyDownloadBytes += (long)this.webRequest.Request.downloadedBytes;
|
|
|
+ }
|
|
|
+ return (int)(alreadyDownloadBytes * 100f / this.TotalSize);
|
|
|
}
|
|
|
+ }
|
|
|
|
|
|
- if (this.bundles.Count == 0)
|
|
|
+ public async Task DownloadAsync()
|
|
|
+ {
|
|
|
+ if (this.bundles.Count == 0 && this.downloadingBundle == "")
|
|
|
{
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
- //Log.Debug($"need download bundles: {this.bundles.ToList().ListToString()}");
|
|
|
- await this.WaitAsync();
|
|
|
- }
|
|
|
-
|
|
|
- private async void UpdateAsync()
|
|
|
- {
|
|
|
try
|
|
|
{
|
|
|
while (true)
|
|
|
@@ -129,10 +148,6 @@ namespace ETModel
|
|
|
byte[] data = this.webRequest.Request.downloadHandler.data;
|
|
|
|
|
|
string path = Path.Combine(PathHelper.AppHotfixResPath, this.downloadingBundle);
|
|
|
- if (!Directory.Exists(Path.GetDirectoryName(path)))
|
|
|
- {
|
|
|
- Directory.CreateDirectory(Path.GetDirectoryName(path));
|
|
|
- }
|
|
|
using (FileStream fs = new FileStream(path, FileMode.Create))
|
|
|
{
|
|
|
fs.Write(data, 0, data.Length);
|
|
|
@@ -151,61 +166,11 @@ namespace ETModel
|
|
|
this.downloadingBundle = "";
|
|
|
this.webRequest = null;
|
|
|
}
|
|
|
-
|
|
|
- using (FileStream fs = new FileStream(Path.Combine(PathHelper.AppHotfixResPath, "Version.txt"), FileMode.Create))
|
|
|
- using (StreamWriter sw = new StreamWriter(fs))
|
|
|
- {
|
|
|
- sw.Write(JsonHelper.ToJson(this.VersionConfig));
|
|
|
- }
|
|
|
-
|
|
|
- this.Tcs?.SetResult(true);
|
|
|
}
|
|
|
catch (Exception e)
|
|
|
{
|
|
|
Log.Error(e);
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
- public int Progress
|
|
|
- {
|
|
|
- get
|
|
|
- {
|
|
|
- if (this.VersionConfig == null)
|
|
|
- {
|
|
|
- return 0;
|
|
|
- }
|
|
|
-
|
|
|
- if (this.TotalSize == 0)
|
|
|
- {
|
|
|
- return 0;
|
|
|
- }
|
|
|
-
|
|
|
- long alreadyDownloadBytes = 0;
|
|
|
- foreach (string downloadedBundle in this.downloadedBundles)
|
|
|
- {
|
|
|
- long size = this.VersionConfig.FileInfoDict[downloadedBundle].Size;
|
|
|
- alreadyDownloadBytes += size;
|
|
|
- }
|
|
|
- if (this.webRequest != null)
|
|
|
- {
|
|
|
- alreadyDownloadBytes += (long)this.webRequest.Request.downloadedBytes;
|
|
|
- }
|
|
|
- return (int)(alreadyDownloadBytes * 100f / this.TotalSize);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- private Task<bool> WaitAsync()
|
|
|
- {
|
|
|
- if (this.bundles.Count == 0 && this.downloadingBundle == "")
|
|
|
- {
|
|
|
- return Task.FromResult(true);
|
|
|
- }
|
|
|
-
|
|
|
- this.Tcs = new TaskCompletionSource<bool>();
|
|
|
-
|
|
|
- UpdateAsync();
|
|
|
-
|
|
|
- return this.Tcs.Task;
|
|
|
- }
|
|
|
}
|
|
|
}
|