Skip to content

Commit

Permalink
up 添加自动mod标记
Browse files Browse the repository at this point in the history
  • Loading branch information
Coloryr committed Jun 26, 2024
1 parent a67743c commit 4f4e376
Show file tree
Hide file tree
Showing 10 changed files with 174 additions and 23 deletions.
2 changes: 1 addition & 1 deletion src/ColorMC.Core/Game/GameLaunch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1045,7 +1045,7 @@ private static async Task<List<string>> 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)
Expand Down
25 changes: 4 additions & 21 deletions src/ColorMC.Core/Game/Mods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ public static async Task<List<ModObj>> 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;
}
Expand All @@ -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);
Expand All @@ -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
Expand Down Expand Up @@ -515,7 +498,7 @@ public static void AddModInfo(this GameSettingObj obj, ModInfoObj info)
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public static bool GetModeFast(this GameSettingObj obj)
public static bool GetModFast(this GameSettingObj obj)
{
string dir = obj.GetModsPath();

Expand Down
60 changes: 60 additions & 0 deletions src/ColorMC.Core/Helpers/ModrinthHelper.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -178,4 +180,62 @@ public static List<DownloadItemObj> GetModrinthModInfo(GetModrinthModInfoArg arg

return list;
}

/// <summary>
/// 自动标记mod
/// </summary>
/// <param name="obj"></param>
/// <param name="cov"></param>
/// <returns></returns>
public static async Task<IntRes> AutoMark(GameSettingObj obj, bool cov)
{
var list = await obj.GetModsAsync();
var mods1 = obj.Mods.Values.ToArray();

var res = new ConcurrentBag<ModInfoObj>();

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 };
}
}
19 changes: 19 additions & 0 deletions src/ColorMC.Core/Net/Apis/ModrinthAPI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,25 @@ public static class ModrinthAPI
}
}

/// <summary>
/// 从文件Sha1获取项目
/// </summary>
/// <param name="sha1"></param>
/// <returns></returns>
public static async Task<ModrinthVersionFileObj?> GetVersionFromSha1(string sha1)
{
try
{
var res = await BaseClient.DownloadClient.GetStringAsync($"{UrlHelper.Modrinth}version_file/{sha1}");
return JsonConvert.DeserializeObject<ModrinthVersionFileObj>(res);
}
catch (Exception e)
{
Logs.Error(LanguageHelper.Get("Core.Http.Modrinth.Error5"), e);
return null;
}
}

/// <summary>
/// 获取Mod依赖
/// </summary>
Expand Down
41 changes: 41 additions & 0 deletions src/ColorMC.Core/Objs/Modrinth/ModrinthVersionFileObj.cs
Original file line number Diff line number Diff line change
@@ -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<string> game_versions { get; set; }
public List<string> 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<FileObj> files { get; set; }
}
6 changes: 6 additions & 0 deletions src/ColorMC.Core/Objs/Results.cs
Original file line number Diff line number Diff line change
Expand Up @@ -191,4 +191,10 @@ public record MessageRes
{
public bool State;
public string? Message;
}

public record IntRes
{
public bool State;
public int? Data;
}
7 changes: 6 additions & 1 deletion src/ColorMC.Gui/Resource/Language/gui_zh-cn.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": "加载器",
Expand All @@ -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": "地图编辑器",
Expand Down
9 changes: 9 additions & 0 deletions src/ColorMC.Gui/UI/Controls/GameEdit/Tab4Control.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,15 @@
Content="{setting:Localize GameEditWindow.Tab4.Text3}"
ToolTip.Tip="{setting:Localize ToolTip.Text54}"
ToolTip.VerticalOffset="-30" />
<Button
Width="100"
Height="30"
Margin="-2,-3,3,2"
Classes="top"
Command="{Binding StartAutoSetMod}"
Content="{setting:Localize GameEditWindow.Tab4.Text7}"
ToolTip.Tip="{setting:Localize ToolTip.Text53}"
ToolTip.VerticalOffset="-30" />
<Button
Width="100"
Height="30"
Expand Down
23 changes: 23 additions & 0 deletions src/ColorMC.Gui/UI/Model/GameEdit/GameEditTab4Model.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,29 @@ public async Task StartSetMod()
_isModSet = false;
}

[RelayCommand]
public async Task StartAutoSetMod()
{
if (_isModSet)
return;

var res = await Model.ShowWait(App.Lang("GameEditWindow.Tab4.Info18"));

_isModSet = true;
Model.Progress(App.Lang("GameEditWindow.Tab4.Info19"));
var res1 = await GameBinding.AutoMarkMods(_obj, res);
Model.ProgressClose();
if (!res1.State)
{
Model.Show(string.Format(App.Lang("GameEditWindow.Tab4.Error4"), res1.Data));
}
else
{
Model.Show(string.Format(App.Lang("GameEditWindow.Tab4.Info20"), res1.Data));
}
_isModSet = false;
}

[RelayCommand]
public async Task ImportMod()
{
Expand Down
5 changes: 5 additions & 0 deletions src/ColorMC.Gui/UIBinding/GameBinding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2055,4 +2055,9 @@ public static async Task<string> GetGameLoader(GameSettingObj obj)
{
return ColorMCAPI.GetMcModGroup();
}

public static Task<IntRes> AutoMarkMods(GameSettingObj obj, bool cov)
{
return ModrinthHelper.AutoMark(obj, cov);
}
}

0 comments on commit 4f4e376

Please sign in to comment.