From 2582dea870afd18e99199d10eabae70157092670 Mon Sep 17 00:00:00 2001 From: goat <16760685+goaaats@users.noreply.github.com> Date: Thu, 29 Oct 2020 13:01:46 +0100 Subject: [PATCH] feat: reimplement asset version check --- Dalamud/Interface/AssetManager.cs | 54 ++++++++++++++++++++++++++++--- 1 file changed, 50 insertions(+), 4 deletions(-) diff --git a/Dalamud/Interface/AssetManager.cs b/Dalamud/Interface/AssetManager.cs index a5e31fc79..6b8362c98 100644 --- a/Dalamud/Interface/AssetManager.cs +++ b/Dalamud/Interface/AssetManager.cs @@ -33,25 +33,71 @@ namespace Dalamud.Interface public static async Task EnsureAssets(string baseDir) { using var client = new WebClient(); - Log.Verbose("Starting asset download"); + Log.Verbose("[ASSET] Starting asset download"); + + var versionRes = CheckAssetRefreshNeeded(baseDir); foreach (var entry in AssetDictionary) { var filePath = Path.Combine(baseDir, entry.Value); Directory.CreateDirectory(Path.GetDirectoryName(filePath)); - if (!File.Exists(filePath)) { - Log.Verbose("Downloading {0} to {1}...", entry.Key, entry.Value); + if (!File.Exists(filePath) || versionRes.isRefreshNeeded) { + Log.Verbose("[ASSET] Downloading {0} to {1}...", entry.Key, entry.Value); try { File.WriteAllBytes(filePath, client.DownloadData(entry.Key)); } catch (Exception ex) { - Log.Error(ex, "Could not download asset."); + Log.Error(ex, "[ASSET] Could not download asset."); return false; } } } + if (versionRes.isRefreshNeeded) + SetLocalAssetVer(baseDir, versionRes.version); + + Log.Verbose("[ASSET] Assets OK"); + return true; } + + private static string GetAssetVerPath(string baseDir) => Path.Combine(baseDir, "asset.ver"); + + + /// + /// Check if an asset update is needed. When this fails, just return false - the route to github + /// might be bad, don't wanna just bail out in that case + /// + /// Base directory for assets + /// Update state + private static (bool isRefreshNeeded, int version) CheckAssetRefreshNeeded(string baseDir) { + using var client = new WebClient(); + + try { + var localVerFile = GetAssetVerPath(baseDir); + var localVer = 0; + + if (File.Exists(localVerFile)) + localVer = int.Parse(File.ReadAllText(localVerFile)); + + var remoteVer = int.Parse(client.DownloadString(AssetStoreUrl + "asset.ver")); + + Log.Verbose("[ASSET] Ver check - local:{0} remote:{1}", localVer, remoteVer); + + return remoteVer > localVer ? (true, remoteVer) : (false, localVer); + } catch (Exception e) { + Log.Error(e, "[ASSET] Could not check asset version"); + return (false, 0); + } + } + + private static void SetLocalAssetVer(string baseDir, int version) { + try { + var localVerFile = GetAssetVerPath(baseDir); + File.WriteAllText(localVerFile, version.ToString()); + } catch (Exception e) { + Log.Error(e, "[ASSET] Could not write local asset version"); + } + } } }