progressbar now also shows subfile download progress

tested dropped http connections
tested retriesUntilFailure feature
replaced workarounds for backend errors with sanity checks
This commit is contained in:
2018-12-27 18:03:26 +01:00
parent bc4ceaef23
commit 284e64e75f

View File

@@ -20,7 +20,7 @@ namespace RothenburgAR.Updater
public GameObject UpdateFailedDialog; public GameObject UpdateFailedDialog;
private float afterDownloadWaitTime = 10f; private float afterDownloadWaitTime = 10f;
private readonly int retriesUntilFailure = 3; private readonly int retriesUntilFailure = 4;
private readonly string trackerMainFile = "tracker.dat"; private readonly string trackerMainFile = "tracker.dat";
public ApiVersioncheckAnswer VersionAnswer { get; set; } public ApiVersioncheckAnswer VersionAnswer { get; set; }
@@ -72,7 +72,7 @@ namespace RothenburgAR.Updater
var answer = JsonConvert.DeserializeObject<ApiVersioncheckAnswer>(http.Download.text); var answer = JsonConvert.DeserializeObject<ApiVersioncheckAnswer>(http.Download.text);
if (http.IsError) if (http.IsError)
{ {
if (retries <= retriesUntilFailure) if (retries < retriesUntilFailure)
{ {
CheckForUpdates(versionMap, retries + 1); CheckForUpdates(versionMap, retries + 1);
return; return;
@@ -248,7 +248,7 @@ namespace RothenburgAR.Updater
{ {
if (http.IsError) if (http.IsError)
{ {
if (retries <= retriesUntilFailure) if (retries < retriesUntilFailure)
{ {
exhibitionDownloads.Remove(http); exhibitionDownloads.Remove(http);
UpdateMetaFile(exhibition, path, lang, exhibitionDownloads, retries + 1); UpdateMetaFile(exhibition, path, lang, exhibitionDownloads, retries + 1);
@@ -284,20 +284,17 @@ namespace RothenburgAR.Updater
var usedFileList = new Dictionary<string, FileDownloadInfo>(); var usedFileList = new Dictionary<string, FileDownloadInfo>();
GenerateUsedFileList(usedFileList); GenerateUsedFileList(usedFileList);
int progress = 0; ProgressBar.maxValue = usedFileList.Count;
foreach (var file in usedFileList) foreach (var file in usedFileList)
{ {
var fileInfo = file.Value;
Dictionary<string, byte[]> data = new Dictionary<string, byte[]>(); Dictionary<string, byte[]> data = new Dictionary<string, byte[]>();
yield return UpdateFile(file.Value, data); yield return DownloadFile(fileInfo, data);
foreach (var item in data) foreach (var item in data)
{ {
File.WriteAllBytes(item.Key, item.Value); File.WriteAllBytes(item.Key, item.Value);
} }
float downloadProgress = progress / usedFileList.Count;
ProgressBar.value = ProgressBar.value < downloadProgress ? downloadProgress : ProgressBar.value;
progress++;
} }
var deletedData = VersionAnswer.Data.Where(d => var deletedData = VersionAnswer.Data.Where(d =>
@@ -376,16 +373,16 @@ namespace RothenburgAR.Updater
} }
} }
private IEnumerator UpdateFile(FileDownloadInfo info, Dictionary<string, byte[]> data) private IEnumerator DownloadFile(FileDownloadInfo info, Dictionary<string, byte[]> data)
{ {
// create dir if nonexistent // create dir if nonexistent
if (!Directory.Exists(info.directory)) Directory.CreateDirectory(info.directory); if (!Directory.Exists(info.directory)) Directory.CreateDirectory(info.directory);
var eTag = GenerateETag(info.filepath); var eTag = GenerateETag(info.filepath);
yield return UpdateFile(info, eTag, data); yield return DownloadFile(info, eTag, data);
} }
private IEnumerator UpdateFile(FileDownloadInfo info, string eTag, Dictionary<string, byte[]> data, int retries = 0) private IEnumerator DownloadFile(FileDownloadInfo info, string eTag, Dictionary<string, byte[]> data, int retries = 0)
{ {
var url = info.url; var url = info.url;
var http = new HttpRequest(url, HttpVerb.GET) var http = new HttpRequest(url, HttpVerb.GET)
@@ -397,9 +394,9 @@ namespace RothenburgAR.Updater
if (http.IsError) if (http.IsError)
{ {
if (retries <= retriesUntilFailure) if (retries < retriesUntilFailure)
{ {
yield return UpdateFile(info, eTag, data, retries + 1); yield return DownloadFile(info, eTag, data, retries + 1);
} }
else else
{ {
@@ -410,10 +407,9 @@ namespace RothenburgAR.Updater
if (http.Request.responseCode != 304) if (http.Request.responseCode != 304)
{ {
// gzipped files are un-gzipped automagically
if (!data.ContainsKey(info.filepath)) if (!data.ContainsKey(info.filepath))
{ {
// gzipped files are un-gzipped automagically
data.Add(info.filepath, http.Download.data); data.Add(info.filepath, http.Download.data);
} }
} }
@@ -422,8 +418,11 @@ namespace RothenburgAR.Updater
var test = http.Request.GetResponseHeaders(); var test = http.Request.GetResponseHeaders();
if (subfilesHeader != null) if (subfilesHeader != null)
{ {
string[] separator = new string[1] { ";;" }; var subfiles = subfilesHeader.Split(new string[1] { ";;" }, StringSplitOptions.RemoveEmptyEntries);
var subfiles = subfilesHeader.Split(separator, StringSplitOptions.RemoveEmptyEntries);
//TODO: will add already downloaded files to maxValue, fix by filtering?
ProgressBar.maxValue += subfiles.Length;
foreach (var subfileName in subfiles) foreach (var subfileName in subfiles)
{ {
var subfileInfo = new FileDownloadInfo( var subfileInfo = new FileDownloadInfo(
@@ -432,15 +431,15 @@ namespace RothenburgAR.Updater
info.directory); info.directory);
var subfileETag = GenerateETag(info.filepath); var subfileETag = GenerateETag(info.filepath);
// HACK to kill errorneous recursive file definition on server if (!data.ContainsKey(subfileInfo.filepath))
if (!data.ContainsKey(subfileInfo.filepath) && subfileName != "brick.png")
{ {
yield return UpdateFile(subfileInfo, subfileETag, data); yield return DownloadFile(subfileInfo, subfileETag, data);
} }
} }
} }
Debug.Log(string.Format("{1}-DONE with {0}", url, Time.realtimeSinceStartup)); Debug.Log(string.Format("{1}-DONE with {0}", url, Time.realtimeSinceStartup));
ProgressBar.value += 1;
} }
private string GenerateETag(string filepath) private string GenerateETag(string filepath)