From 9b12bf239a590645192bc2ed62b2945ed91db6cd Mon Sep 17 00:00:00 2001 From: Matt Date: Sat, 22 Dec 2018 15:28:19 +0100 Subject: [PATCH] UpdaterBehaviour now treats tracker data like media data, as discussed in slack --- .../Exhibition/ExhibitionApiDataPreloader.cs | 2 + .../Scripts/Updater/HttpHandler.cs | 9 +- .../Scripts/Updater/UpdaterBehaviour.cs | 87 ++++++++++++------- 3 files changed, 64 insertions(+), 34 deletions(-) diff --git a/Assets/RothenburgAR/Scripts/Exhibition/ExhibitionApiDataPreloader.cs b/Assets/RothenburgAR/Scripts/Exhibition/ExhibitionApiDataPreloader.cs index fdc5f58..917e396 100644 --- a/Assets/RothenburgAR/Scripts/Exhibition/ExhibitionApiDataPreloader.cs +++ b/Assets/RothenburgAR/Scripts/Exhibition/ExhibitionApiDataPreloader.cs @@ -16,6 +16,8 @@ namespace RothenburgAR.Exhibition { if (!Directory.Exists(exhibitionDirectory)) return false; + if (!File.Exists(Path.Combine(exhibitionDirectory, "version.txt"))) + return false; if (!File.Exists(Path.Combine(exhibitionDirectory, "tracker.xml"))) return false; if (!File.Exists(Path.Combine(exhibitionDirectory, "tracker.dat"))) diff --git a/Assets/RothenburgAR/Scripts/Updater/HttpHandler.cs b/Assets/RothenburgAR/Scripts/Updater/HttpHandler.cs index 6986f0b..4b2865d 100644 --- a/Assets/RothenburgAR/Scripts/Updater/HttpHandler.cs +++ b/Assets/RothenburgAR/Scripts/Updater/HttpHandler.cs @@ -65,11 +65,13 @@ namespace RothenburgAR.Updater public UnityWebRequest Request { get; set; } public UnityWebRequestAsyncOperation Operation { get; set; } + private bool isDone = false; + public bool IsDone { get { - return Operation.isDone && Request.isDone; + return Operation.isDone && Request.isDone && this.isDone; } } @@ -79,6 +81,11 @@ namespace RothenburgAR.Updater this.Download = download; this.Request = request; this.Operation = operation; + + this.Operation.completed += (_) => + { + this.isDone = true; + }; } } diff --git a/Assets/RothenburgAR/Scripts/Updater/UpdaterBehaviour.cs b/Assets/RothenburgAR/Scripts/Updater/UpdaterBehaviour.cs index 3d25c12..253fe03 100644 --- a/Assets/RothenburgAR/Scripts/Updater/UpdaterBehaviour.cs +++ b/Assets/RothenburgAR/Scripts/Updater/UpdaterBehaviour.cs @@ -241,18 +241,17 @@ namespace RothenburgAR.Updater downloadingMedia.Add(mediaId); var path = PathHelper.CombinePaths(PathHelper.MediaPath, mediaId); - var filepath = Path.Combine(path, mediaId + ".med"); + var filepath = Path.Combine(path, mediaId + ".obj"); // create dir if nonexistent if (!Directory.Exists(path)) Directory.CreateDirectory(path); - //System.Security.Cryptography.SHA1CryptoServiceProvider - //System.Security.Cryptography.SHA256 var eTag = GenerateETag(filepath); var url = ApiInfo.FileEndpoint.Replace("{id}", mediaId); var http = new HttpRequest(url, HttpVerb.GET) .WithHeader("If-None-Match", eTag) + .WithHeader("Accept-Encoding", "gzip,deflate") .Send(); httpHandlers.Add(http); @@ -277,7 +276,7 @@ namespace RothenburgAR.Updater var subfiles = subfilesHeader.Split(separator, StringSplitOptions.RemoveEmptyEntries); foreach (var subfile in subfiles) { - UpdateSubfile(path, mediaId, subfile); + UpdateSubfile(path, url, subfile); } } @@ -286,6 +285,55 @@ namespace RothenburgAR.Updater } } + private void UpdateTracker(ApiExhibitionVersion exhibition) + { + var path = PathHelper.CombinePaths(PathHelper.ExhibitionPath, exhibition.Id); + var filepath = Path.Combine(path, "tracker.dat"); //TODO test if it's the xml instead + + // create dir if nonexistent + if (!Directory.Exists(path)) Directory.CreateDirectory(path); + + var eTag = GenerateETag(filepath); + + var url = exhibition.Tracker.UpdateUrl; + var http = new HttpRequest(url, HttpVerb.GET) + .WithHeader("If-None-Match", eTag) + .Send(); + httpHandlers.Add(http); + + http.Operation.completed += ar => + { + if (CheckNetworkErrors(http)) + { + return; + } + + if (!Directory.Exists(path)) + { + Directory.CreateDirectory(path); + } + + // do nothing if unchanged + if (http.Request.responseCode == 304) return; + + // gzipped files are un-gzipped automagically + File.WriteAllBytes(filepath, http.Download.data); + + var subfilesHeader = http.Request.GetResponseHeader("subfiles"); + if (subfilesHeader != null) + { + string[] separator = new string[1] { ";;" }; + var subfiles = subfilesHeader.Split(separator, StringSplitOptions.RemoveEmptyEntries); + foreach (var subfile in subfiles) + { + UpdateSubfile(path, url, subfile); + } + } + + Debug.Log(string.Format("{1}-DONE with {0}", url, Time.realtimeSinceStartup)); + }; + } + private string GenerateETag(string filepath) { string eTag = ""; @@ -303,12 +351,12 @@ namespace RothenburgAR.Updater return eTag; } - private void UpdateSubfile(string path, string mediaId, string subfile) + private void UpdateSubfile(string path, string parentUrl, string subfile) { var filepath = Path.Combine(path, subfile); var eTag = GenerateETag(filepath); - var url = ApiInfo.FileEndpoint.Replace("{id}", mediaId) + "/" + subfile; + var url = parentUrl + "/" + subfile; var http = new HttpRequest(url, HttpVerb.GET) .WithHeader("If-None-Match", eTag) .Send(); @@ -355,33 +403,6 @@ namespace RothenburgAR.Updater return false; } - private void UpdateTracker(ApiExhibitionVersion exhibition) - { - //TODO test UpdateTracker - var path = PathHelper.CombinePaths(PathHelper.ExhibitionPath, exhibition.Id); - var url = exhibition.Tracker.UpdateUrl; - var http = new HttpRequest(url, HttpVerb.GET).Send(); - httpHandlers.Add(http); - - http.Operation.completed += ar => - { - if (CheckNetworkErrors(http)) - { - return; - } - - if (!Directory.Exists(path)) - { - Directory.CreateDirectory(path); - } - - //TODO unzip, should be tracker.xml and tracker.dat files - var type = http.Request.GetResponseHeader("Content-Type"); - File.WriteAllBytes(Path.Combine(path, "tracker.zip"), http.Download.data); - Debug.Log(string.Format("{1}-DONE with {0}", url, Time.realtimeSinceStartup)); - }; - } - public void LoadMainScene() { Debug.Log("Loading mainScene");