From 4f4e37661c8fa152b20c399254183e9dff78f3e4 Mon Sep 17 00:00:00 2001 From: Coloryr <402067010@qq.com> Date: Wed, 26 Jun 2024 16:22:17 +0800 Subject: [PATCH] =?UTF-8?q?up=20=E6=B7=BB=E5=8A=A0=E8=87=AA=E5=8A=A8mod?= =?UTF-8?q?=E6=A0=87=E8=AE=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/ColorMC.Core/Game/GameLaunch.cs | 2 +- src/ColorMC.Core/Game/Mods.cs | 25 ++------ src/ColorMC.Core/Helpers/ModrinthHelper.cs | 60 +++++++++++++++++++ src/ColorMC.Core/Net/Apis/ModrinthAPI.cs | 19 ++++++ .../Objs/Modrinth/ModrinthVersionFileObj.cs | 41 +++++++++++++ src/ColorMC.Core/Objs/Results.cs | 6 ++ .../Resource/Language/gui_zh-cn.json | 7 ++- .../UI/Controls/GameEdit/Tab4Control.axaml | 9 +++ .../UI/Model/GameEdit/GameEditTab4Model.cs | 23 +++++++ src/ColorMC.Gui/UIBinding/GameBinding.cs | 5 ++ 10 files changed, 174 insertions(+), 23 deletions(-) create mode 100644 src/ColorMC.Core/Objs/Modrinth/ModrinthVersionFileObj.cs diff --git a/src/ColorMC.Core/Game/GameLaunch.cs b/src/ColorMC.Core/Game/GameLaunch.cs index f2b1ccc09..23e6985bb 100644 --- a/src/ColorMC.Core/Game/GameLaunch.cs +++ b/src/ColorMC.Core/Game/GameLaunch.cs @@ -1045,7 +1045,7 @@ private static async Task> MakeArgAsync(this GameSettingObj obj, Ga res.Add(res3); } - if (obj.GetModeFast() && obj.Loader == Loaders.Normal && larg.Request != null) + if (obj.GetModFast() && obj.Loader == Loaders.Normal && larg.Request != null) { var res2 = await larg.Request(string.Format(LanguageHelper.Get("Core.Launch.Info13"), obj.Name)); if (!res2) diff --git a/src/ColorMC.Core/Game/Mods.cs b/src/ColorMC.Core/Game/Mods.cs index f18bd0a29..f648f0216 100644 --- a/src/ColorMC.Core/Game/Mods.cs +++ b/src/ColorMC.Core/Game/Mods.cs @@ -335,7 +335,7 @@ public static async Task> GetModsAsync(this GameSettingObj obj, boo //}, async (item, cancel) => await Parallel.ForEachAsync(files, async (item, cancel) => { - if (item.Extension is not (".zip" or ".jar" or ".disable")) + if (item.Extension is not (".zip" or ".jar" or ".disable" or ".disabled")) { return; } @@ -348,29 +348,12 @@ await Parallel.ForEachAsync(files, async (item, cancel) => sha1 = HashHelper.GenSha1(filestream); filestream.Seek(0, SeekOrigin.Begin); - //Mod 资源包 - if (item.Extension is ".zip") - { - var obj3 = new ModObj - { - Local = Path.GetFullPath(item.FullName), - Disable = item.Extension is ".disable", - Loader = Loaders.Fabric, - V2 = true, - name = item.Name, - Sha1 = sha1 - }; - list.Add(obj3); - add = true; - return; - } - using var zFile = new ZipFile(filestream); var mod = await ReadModAsync(zFile); if (mod != null) { mod.Local = Path.GetFullPath(item.FullName); - mod.Disable = item.Extension is ".disable"; + mod.Disable = item.Extension is ".disable" or ".disabled"; mod.Sha1 = sha1; mod.Game = obj; list.Add(mod); @@ -394,7 +377,7 @@ await Parallel.ForEachAsync(files, async (item, cancel) => { name = "", Local = Path.GetFullPath(item.FullName), - Disable = item.Extension is ".disable", + Disable = item.Extension is ".disable" or ".disabled", ReadFail = true, Sha1 = sha1, Game = obj @@ -515,7 +498,7 @@ public static void AddModInfo(this GameSettingObj obj, ModInfoObj info) /// /// /// - public static bool GetModeFast(this GameSettingObj obj) + public static bool GetModFast(this GameSettingObj obj) { string dir = obj.GetModsPath(); diff --git a/src/ColorMC.Core/Helpers/ModrinthHelper.cs b/src/ColorMC.Core/Helpers/ModrinthHelper.cs index 60ddf41c3..c8e574d33 100644 --- a/src/ColorMC.Core/Helpers/ModrinthHelper.cs +++ b/src/ColorMC.Core/Helpers/ModrinthHelper.cs @@ -1,3 +1,5 @@ +using System.Collections.Concurrent; +using ColorMC.Core.Game; using ColorMC.Core.LaunchPath; using ColorMC.Core.Net.Apis; using ColorMC.Core.Objs; @@ -178,4 +180,62 @@ public static List GetModrinthModInfo(GetModrinthModInfoArg arg return list; } + + /// + /// 自动标记mod + /// + /// + /// + /// + public static async Task AutoMark(GameSettingObj obj, bool cov) + { + var list = await obj.GetModsAsync(); + var mods1 = obj.Mods.Values.ToArray(); + + var res = new ConcurrentBag(); + + int error = 0; + int count = 0; + + await Parallel.ForEachAsync(list, async (item, cancel) => + { + if (mods1.Any(item1 => item.Sha1 == item1.SHA1) && !cov) + { + return; + } + + var data = await ModrinthAPI.GetVersionFromSha1(item.Sha1); + if (data == null) + { + error++; + return; + } + res.Add(new() + { + Path = "mods", + Name = data.files[0].filename, + File = Path.GetFileName(item.Local), + SHA1 = item.Sha1, + Url = data.files[0].url, + ModId = data.project_id, + FileId = data.id + }); + count++; + }); + + foreach (var item in res) + { + if (!obj.Mods.TryAdd(item.ModId, item)) + { + obj.Mods[item.ModId] = item; + } + } + + obj.SaveModInfo(); + if (error != 0) + { + return new() { Data = error, State = false }; + } + return new() { Data = count, State = true }; + } } diff --git a/src/ColorMC.Core/Net/Apis/ModrinthAPI.cs b/src/ColorMC.Core/Net/Apis/ModrinthAPI.cs index ae88f5f12..3066772dc 100644 --- a/src/ColorMC.Core/Net/Apis/ModrinthAPI.cs +++ b/src/ColorMC.Core/Net/Apis/ModrinthAPI.cs @@ -250,6 +250,25 @@ public static class ModrinthAPI } } + /// + /// 从文件Sha1获取项目 + /// + /// + /// + public static async Task GetVersionFromSha1(string sha1) + { + try + { + var res = await BaseClient.DownloadClient.GetStringAsync($"{UrlHelper.Modrinth}version_file/{sha1}"); + return JsonConvert.DeserializeObject(res); + } + catch (Exception e) + { + Logs.Error(LanguageHelper.Get("Core.Http.Modrinth.Error5"), e); + return null; + } + } + /// /// 获取Mod依赖 /// diff --git a/src/ColorMC.Core/Objs/Modrinth/ModrinthVersionFileObj.cs b/src/ColorMC.Core/Objs/Modrinth/ModrinthVersionFileObj.cs new file mode 100644 index 000000000..d4f561986 --- /dev/null +++ b/src/ColorMC.Core/Objs/Modrinth/ModrinthVersionFileObj.cs @@ -0,0 +1,41 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ColorMC.Core.Objs.Modrinth; + +public record ModrinthVersionFileObj +{ + public record FileObj + { + public record HashesObj + { + public string sha1 { get; set; } + public string sha512 { get; set; } + } + public HashesObj hashes { get; set; } + public string url { get; set; } + public string filename { get; set; } + public object file_type { get; set; } + public bool primary { get; set; } + public long size { get; set; } + } + public List game_versions { get; set; } + public List loaders { get; set; } + public string id { get; set; } + public string project_id { get; set; } + public string author_id { get; set; } + public bool featured { get; set; } + public string name { get; set; } + public string version_number { get; set; } + public string changelog { get; set; } + public string changelog_url { get; set; } + public string date_published { get; set; } + public int downloads { get; set; } + public string version_type { get; set; } + public string status { get; set; } + public object requested_status { get; set; } + public List files { get; set; } +} diff --git a/src/ColorMC.Core/Objs/Results.cs b/src/ColorMC.Core/Objs/Results.cs index 4cefdcc71..7c2f38877 100644 --- a/src/ColorMC.Core/Objs/Results.cs +++ b/src/ColorMC.Core/Objs/Results.cs @@ -191,4 +191,10 @@ public record MessageRes { public bool State; public string? Message; +} + +public record IntRes +{ + public bool State; + public int? Data; } \ No newline at end of file diff --git a/src/ColorMC.Gui/Resource/Language/gui_zh-cn.json b/src/ColorMC.Gui/Resource/Language/gui_zh-cn.json index 032eac298..b49e425fd 100644 --- a/src/ColorMC.Gui/Resource/Language/gui_zh-cn.json +++ b/src/ColorMC.Gui/Resource/Language/gui_zh-cn.json @@ -486,9 +486,10 @@ "GameEditWindow.Tab4.Text1": "过滤内容", "GameEditWindow.Tab4.Text2": "过滤选项", "GameEditWindow.Tab4.Text3": "检查更新", - "GameEditWindow.Tab4.Text4": "添加标记", + "GameEditWindow.Tab4.Text4": "手动标记", "GameEditWindow.Tab4.Text5": "检测依赖", "GameEditWindow.Tab4.Text6": "导入模组", + "GameEditWindow.Tab4.Text7": "自动标记", "GameEditWindow.Tab4.DataGrid.Text1": "启用", "GameEditWindow.Tab4.DataGrid.Text6": "链接", "GameEditWindow.Tab4.DataGrid.Text7": "加载器", @@ -510,9 +511,13 @@ "GameEditWindow.Tab4.Info15": "正在检测模组依赖", "GameEditWindow.Tab4.Info16": "没有缺失的依赖项", "GameEditWindow.Tab4.Info17": "是否同时禁用依赖此的 {0} 个模组", + "GameEditWindow.Tab4.Info18": "是否覆盖已有标记", + "GameEditWindow.Tab4.Info19": "正在自动标记模组", + "GameEditWindow.Tab4.Info20": "已标记 {0} 个模组", "GameEditWindow.Tab4.Error1": "模组列表加载失败", "GameEditWindow.Tab4.Error2": "模组导入失败", "GameEditWindow.Tab4.Error3": "模组启用/禁用失败 {0}", + "GameEditWindow.Tab4.Error4": "有 {0} 个模组标记失败", "GameEditWindow.Tab5.Text1": "备份文件夹", "GameEditWindow.Tab5.Text2": "还原世界备份", "GameEditWindow.Tab5.Text3": "地图编辑器", diff --git a/src/ColorMC.Gui/UI/Controls/GameEdit/Tab4Control.axaml b/src/ColorMC.Gui/UI/Controls/GameEdit/Tab4Control.axaml index 5e00cb2ca..8ebeb1162 100644 --- a/src/ColorMC.Gui/UI/Controls/GameEdit/Tab4Control.axaml +++ b/src/ColorMC.Gui/UI/Controls/GameEdit/Tab4Control.axaml @@ -70,6 +70,15 @@ Content="{setting:Localize GameEditWindow.Tab4.Text3}" ToolTip.Tip="{setting:Localize ToolTip.Text54}" ToolTip.VerticalOffset="-30" /> +