From 13e69ed84bd6eeb429f73d2aa2f2cac54231c843 Mon Sep 17 00:00:00 2001 From: Yifan Liu Date: Fri, 3 Mar 2023 22:19:37 -0600 Subject: [PATCH 1/5] feat: support ota update of acitivity info (and any other resources) --- src/MaaWpfGui/Helper/StageManager.cs | 66 ++++++++++++++++++---------- src/MaaWpfGui/Helper/WebService.cs | 7 +-- src/MaaWpfGui/Main/AsstProxy.cs | 10 +++++ 3 files changed, 57 insertions(+), 26 deletions(-) diff --git a/src/MaaWpfGui/Helper/StageManager.cs b/src/MaaWpfGui/Helper/StageManager.cs index ea742d71e6..da5c1452fd 100644 --- a/src/MaaWpfGui/Helper/StageManager.cs +++ b/src/MaaWpfGui/Helper/StageManager.cs @@ -19,7 +19,6 @@ using System.Reflection; using System.Runtime.InteropServices; using System.Text; using System.Threading.Tasks; -using System.Windows.Documents; using MaaWpfGui.Helper; using Newtonsoft.Json.Linq; using Semver; @@ -75,8 +74,38 @@ namespace MaaWpfGui { string.Empty, new StageInfo { Display = Localization.GetString("DefaultStage"), Value = string.Empty } }, }; - var stageApi = "StageActivity.json"; - var activity = fromWeb ? WebService.RequestMaaApiWithCache(stageApi) : WebService.LoadApiCache(stageApi); + var clientType = ViewStatusStorage.Get("Start.ClientType", string.Empty); + + // 官服和B服使用同样的资源 + if (clientType == "Bilibili" || clientType == string.Empty) + { + clientType = "Official"; + } + + // Download the activities + var stageApi = "gui/StageActivity.json"; + JObject activity = fromWeb ? WebService.RequestMaaApiWithCache(stageApi) : WebService.LoadApiCache(stageApi); + + // Download the tasks resources into cache so MaaCore can load them later + var tasksPath = "resource/tasks.json"; + JObject tasksJson = fromWeb ? WebService.RequestMaaApiWithCache(tasksPath) : WebService.LoadApiCache(tasksPath); + + // Download the client specific resources only when the Official ones are successfully downloaded so that the client specific resource version is the actual version + // TODO: There may be an issue when the CN resource is loaded from cache (e.g. network down) while global resource is downloaded (e.g. network up again) + JObject tasksJsonClient = null; + if (clientType != "Official" && tasksJson != null) + { + tasksPath = clientType + '/' + tasksPath; + tasksJsonClient = fromWeb ? WebService.RequestMaaApiWithCache(tasksPath) : WebService.RequestMaaApiWithCache(tasksPath); + } + + bool isDebugVersion = Marshal.PtrToStringAnsi(AsstGetVersion()) == "DEBUG VERSION"; + bool curVerParsed = !isDebugVersion ? + SemVersion.TryParse(Marshal.PtrToStringAnsi(AsstGetVersion()), SemVersionStyles.AllowLowerV, out var curVersionObj) : + SemVersion.TryParse("4.11.7", SemVersionStyles.AllowLowerV, out curVersionObj); + bool curResourceVerParsed = SemVersion.TryParse( + tasksJsonClient?["ResourceVersion"]?.ToString() ?? tasksJson?["ResourceVersion"]?.ToString() ?? string.Empty, + SemVersionStyles.AllowLowerV, out var curResourceVersionObj); var resourceCollection = new StageActivityInfo() { @@ -88,40 +117,31 @@ namespace MaaWpfGui "yyyy/MM/dd HH:mm:ss", CultureInfo.InvariantCulture).AddHours(-Convert.ToInt32(keyValuePairs?["TimeZone"].ToString() ?? "0")); - var clientType = ViewStatusStorage.Get("Start.ClientType", string.Empty); - - // 官服和B服使用同样的资源 - if (clientType == "Bilibili" || clientType == string.Empty) - { - clientType = "Official"; - } - if (activity?[clientType] != null) { try { // 资源全开放活动 - var resource = activity[clientType]["resourceCollection"]; - if (resource != null) + var resourceCollectionData = activity[clientType]["resourceCollection"]; + if (resourceCollectionData != null) { - resourceCollection.Tip = resource["Tip"]?.ToString(); - resourceCollection.UtcStartTime = GetDateTime(resource, "UtcStartTime"); - resourceCollection.UtcExpireTime = GetDateTime(resource, "UtcExpireTime"); + resourceCollection.Tip = resourceCollectionData["Tip"]?.ToString(); + resourceCollection.UtcStartTime = GetDateTime(resourceCollectionData, "UtcStartTime"); + resourceCollection.UtcExpireTime = GetDateTime(resourceCollectionData, "UtcExpireTime"); } // 活动关卡 foreach (var stageObj in activity[clientType]["sideStoryStage"] ?? Enumerable.Empty()) { - bool isDebugVersion = Marshal.PtrToStringAnsi(AsstGetVersion()) == "DEBUG VERSION"; - bool curParsed = !isDebugVersion ? - SemVersion.TryParse(Marshal.PtrToStringAnsi(AsstGetVersion()), SemVersionStyles.AllowLowerV, out var curVersionObj) : - SemVersion.TryParse("4.11.7", SemVersionStyles.AllowLowerV, out curVersionObj); - bool minimumRequiredPared = SemVersion.TryParse(stageObj?["MinimumRequired"]?.ToString() ?? string.Empty, SemVersionStyles.AllowLowerV, out var minimumRequiredObj); + // MinimumResourceRequired is not necessarily provided in json, in which case it is ok even if there are no cached resources + bool minResourceRequiredParsed = SemVersion.TryParse(stageObj?["MinimumResourceRequired"]?.ToString() ?? string.Empty, SemVersionStyles.AllowLowerV, out var minResourceRequiredObj); + bool minRequiredParsed = SemVersion.TryParse(stageObj?["MinimumRequired"]?.ToString() ?? string.Empty, SemVersionStyles.AllowLowerV, out var minRequiredObj); var stageInfo = new StageInfo(); - if (curParsed && minimumRequiredPared) + if (curVerParsed && minRequiredParsed && (!minResourceRequiredParsed || curResourceVerParsed)) { - if (curVersionObj.CompareSortOrderTo(minimumRequiredObj) < 0) + if (curVersionObj.CompareSortOrderTo(minRequiredObj) < 0 && + (!minResourceRequiredParsed || curResourceVersionObj.CompareSortOrderTo(minResourceRequiredObj) < 0)) { if (!tempStage.ContainsKey(Localization.GetString("UnsupportedStages"))) { diff --git a/src/MaaWpfGui/Helper/WebService.cs b/src/MaaWpfGui/Helper/WebService.cs index 32afaf5244..0793948a17 100644 --- a/src/MaaWpfGui/Helper/WebService.cs +++ b/src/MaaWpfGui/Helper/WebService.cs @@ -63,7 +63,7 @@ namespace MaaWpfGui.Helper } } - private const string CacheDir = "cache/gui/"; + private const string CacheDir = "cache/"; private const string MaaApi = "https://ota.maa.plus/MaaAssistantArknights/api/"; // 如果请求失败,则读取缓存。否则写入缓存 @@ -85,7 +85,8 @@ namespace MaaWpfGui.Helper try { var json = (JObject)JsonConvert.DeserializeObject(response); - var cache = CacheDir + api.Replace('/', '_'); + var cache = CacheDir + api; + Directory.CreateDirectory(Path.GetDirectoryName(cache)); File.WriteAllText(cache, response); return json; @@ -98,7 +99,7 @@ namespace MaaWpfGui.Helper public static JObject LoadApiCache(string api) { - var cache = CacheDir + api.Replace('/', '_'); + var cache = CacheDir + api; if (!File.Exists(cache)) { return null; diff --git a/src/MaaWpfGui/Main/AsstProxy.cs b/src/MaaWpfGui/Main/AsstProxy.cs index fdaff86592..f761a73dd2 100644 --- a/src/MaaWpfGui/Main/AsstProxy.cs +++ b/src/MaaWpfGui/Main/AsstProxy.cs @@ -213,12 +213,18 @@ namespace MaaWpfGui } loaded = AsstLoadResource(Directory.GetCurrentDirectory()); + + // Load the cached incremental resources + loaded = loaded && AsstLoadResource(Directory.GetCurrentDirectory() + "\\cache"); } else if (_curResource == "Official" || _curResource == "Bilibili") { // Load basic resources for CN client first // Then load global incremental resources loaded = AsstLoadResource(Directory.GetCurrentDirectory() + "\\resource\\global\\" + _settingsViewModel.ClientType); + + // Load the cached incremental resources + loaded = loaded && AsstLoadResource(Directory.GetCurrentDirectory() + "\\cache\\resource\\global\\" + _settingsViewModel.ClientType); } else { @@ -226,6 +232,10 @@ namespace MaaWpfGui // Then load global incremental resources loaded = AsstLoadResource(Directory.GetCurrentDirectory()) && AsstLoadResource(Directory.GetCurrentDirectory() + "\\resource\\global\\" + _settingsViewModel.ClientType); + + // Load the cached incremental resources + loaded = loaded && AsstLoadResource(Directory.GetCurrentDirectory() + "\\cache") + && AsstLoadResource(Directory.GetCurrentDirectory() + "\\cache\\resource\\global\\" + _settingsViewModel.ClientType); } if (!loaded) From 5f12e1cef06e2c351542a9675ca7153bae02ab63 Mon Sep 17 00:00:00 2001 From: Yifan Liu Date: Sat, 4 Mar 2023 00:44:56 -0600 Subject: [PATCH 2/5] fix: ota update wrong path & version --- src/MaaWpfGui/Helper/StageManager.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/MaaWpfGui/Helper/StageManager.cs b/src/MaaWpfGui/Helper/StageManager.cs index da5c1452fd..ca1276b272 100644 --- a/src/MaaWpfGui/Helper/StageManager.cs +++ b/src/MaaWpfGui/Helper/StageManager.cs @@ -95,14 +95,14 @@ namespace MaaWpfGui JObject tasksJsonClient = null; if (clientType != "Official" && tasksJson != null) { - tasksPath = clientType + '/' + tasksPath; + tasksPath = "resource/global/" + clientType + '/' + tasksPath; tasksJsonClient = fromWeb ? WebService.RequestMaaApiWithCache(tasksPath) : WebService.RequestMaaApiWithCache(tasksPath); } bool isDebugVersion = Marshal.PtrToStringAnsi(AsstGetVersion()) == "DEBUG VERSION"; bool curVerParsed = !isDebugVersion ? SemVersion.TryParse(Marshal.PtrToStringAnsi(AsstGetVersion()), SemVersionStyles.AllowLowerV, out var curVersionObj) : - SemVersion.TryParse("4.11.7", SemVersionStyles.AllowLowerV, out curVersionObj); + SemVersion.TryParse("5.1.7", SemVersionStyles.AllowLowerV, out curVersionObj); bool curResourceVerParsed = SemVersion.TryParse( tasksJsonClient?["ResourceVersion"]?.ToString() ?? tasksJson?["ResourceVersion"]?.ToString() ?? string.Empty, SemVersionStyles.AllowLowerV, out var curResourceVersionObj); From 218f6ccc5dd40a9c0f0c229931586ff9a49a7167 Mon Sep 17 00:00:00 2001 From: Yifan Liu Date: Sat, 4 Mar 2023 00:45:41 -0600 Subject: [PATCH 3/5] chore: remove DH resources (which will be ota) --- resource/global/YoStarJP/resource/tasks.json | 15 ----- resource/tasks.json | 60 +------------------- 2 files changed, 1 insertion(+), 74 deletions(-) diff --git a/resource/global/YoStarJP/resource/tasks.json b/resource/global/YoStarJP/resource/tasks.json index 14fc5bd6df..2c21eaa60b 100644 --- a/resource/global/YoStarJP/resource/tasks.json +++ b/resource/global/YoStarJP/resource/tasks.json @@ -9,21 +9,6 @@ "幻滅" ] }, - "DH-Open": { - "text": [ - "ドッソレス", - "ホリデー", - "復刻", - "イベント", - "催中" - ] - }, - "DHChapterToDH": { - "text": [ - "大会", - "エントリー" - ] - }, "CharsNameOcrReplace": { "Doc": "该任务的 ocrReplace 被所有涉及干员名的日文识别任务复用 このタスクの「ocrReplace」は、オペレーター名に関連するすべての認識タスクで再利用されます", "replaceFull": true, diff --git a/resource/tasks.json b/resource/tasks.json index 582318289b..ea93b73183 100644 --- a/resource/tasks.json +++ b/resource/tasks.json @@ -785,63 +785,6 @@ "isAscii": true, "text": [] }, - "DH-9": { - "algorithm": "JustReturn", - "action": "DoNothing", - "sub": [ - "DH-Open" - ], - "next": [ - "DH-9@SideStoryStage", - "DH-9@SwipeToStage" - ] - }, - "DH-Open": { - "action": "ClickSelf", - "algorithm": "OcrDetect", - "text": [ - "假日", - "复刻", - "活动", - "开放" - ], - "roi": [ - 0, - 500, - 500, - 150 - ], - "postDelay": 2000, - "next": [ - "DHChapterToDH" - ] - }, - "DHChapterToDH": { - "algorithm": "OcrDetect", - "action": "ClickSelf", - "text": [ - "极限", - "铁人", - "大奖" - ], - "roi": [ - 934, - 280, - 234, - 124 - ], - "postDelay": 2000, - "next": [ - "DHChapterToDH", - "ChapterSwipeToTheRight" - ] - }, - "DH-9@SideStoryStage": { - "baseTask": "SideStoryStage", - "text": [ - "DH-9" - ] - }, "GA-6": { "algorithm": "JustReturn", "action": "DoNothing", @@ -10782,7 +10725,6 @@ "baseTask": "Reclamation@SkipAnnounce", "next": [] }, - "NavigateHome@Reclamation@Begin": { "algorithm": "JustReturn", "next": [ @@ -10934,4 +10876,4 @@ "Stop" ] } -} +} \ No newline at end of file From 47d375144f5db3a1022ba8bf5ce967033b163a99 Mon Sep 17 00:00:00 2001 From: Yifan Liu Date: Sat, 4 Mar 2023 11:57:56 -0600 Subject: [PATCH 4/5] chore: debug version considered satisfying min ver. requirment --- src/MaaWpfGui/Helper/StageManager.cs | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/src/MaaWpfGui/Helper/StageManager.cs b/src/MaaWpfGui/Helper/StageManager.cs index 137df19e32..a795ffb927 100644 --- a/src/MaaWpfGui/Helper/StageManager.cs +++ b/src/MaaWpfGui/Helper/StageManager.cs @@ -100,9 +100,7 @@ namespace MaaWpfGui } bool isDebugVersion = Marshal.PtrToStringAnsi(AsstGetVersion()) == "DEBUG VERSION"; - bool curVerParsed = !isDebugVersion ? - SemVersion.TryParse(Marshal.PtrToStringAnsi(AsstGetVersion()), SemVersionStyles.AllowLowerV, out var curVersionObj) : - SemVersion.TryParse("5.1.7", SemVersionStyles.AllowLowerV, out curVersionObj); + bool curVerParsed = SemVersion.TryParse(Marshal.PtrToStringAnsi(AsstGetVersion()), SemVersionStyles.AllowLowerV, out var curVersionObj); bool curResourceVerParsed = SemVersion.TryParse( tasksJsonClient?["ResourceVersion"]?.ToString() ?? tasksJson?["ResourceVersion"]?.ToString() ?? string.Empty, SemVersionStyles.AllowLowerV, out var curResourceVersionObj); @@ -137,12 +135,11 @@ namespace MaaWpfGui bool minResourceRequiredParsed = SemVersion.TryParse(stageObj?["MinimumResourceRequired"]?.ToString() ?? string.Empty, SemVersionStyles.AllowLowerV, out var minResourceRequiredObj); bool minRequiredParsed = SemVersion.TryParse(stageObj?["MinimumRequired"]?.ToString() ?? string.Empty, SemVersionStyles.AllowLowerV, out var minRequiredObj); - // DEBUG VISION - // curParsed = SemVersion.TryParse("4.11.7", SemVersionStyles.AllowLowerV, out curVersionObj); var stageInfo = new StageInfo(); - if (curVerParsed && minRequiredParsed && (!minResourceRequiredParsed || curResourceVerParsed)) + if ((isDebugVersion || (curVerParsed && minRequiredParsed)) && (!minResourceRequiredParsed || curResourceVerParsed)) { - if (curVersionObj.CompareSortOrderTo(minRequiredObj) < 0 && + // Debug Version will be considered satisfying min version requirement, but the resource version needs a comparison + if ((isDebugVersion || (curVersionObj.CompareSortOrderTo(minRequiredObj) < 0)) && (!minResourceRequiredParsed || curResourceVersionObj.CompareSortOrderTo(minResourceRequiredObj) < 0)) { if (!tempStage.ContainsKey(Localization.GetString("UnsupportedStages"))) From 87586d25e9aa0ee3788e362fd01096c33348c16f Mon Sep 17 00:00:00 2001 From: Yifan Liu Date: Sun, 5 Mar 2023 23:46:39 -0600 Subject: [PATCH 5/5] style: missing empty line --- src/MaaWpfGui/Helper/WebService.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/MaaWpfGui/Helper/WebService.cs b/src/MaaWpfGui/Helper/WebService.cs index c73c3deda9..51df2d1771 100644 --- a/src/MaaWpfGui/Helper/WebService.cs +++ b/src/MaaWpfGui/Helper/WebService.cs @@ -62,6 +62,7 @@ namespace MaaWpfGui.Helper return null; } } + public static string RequestPost(string url, string body) { try