Skip to content

Commit aab49d4

Browse files
committed
up
1 parent 3992f55 commit aab49d4

6 files changed

Lines changed: 258 additions & 72 deletions

File tree

src/ColorMC.Gui/Resource/Language/zh-cn.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -726,6 +726,7 @@
726726
"AddGameWindow.Tab2.Text13": "没有选择压缩包",
727727
"AddGameWindow.Tab2.Text14": "没有选择压缩包类型",
728728
"AddGameWindow.Tab2.Text15": "该压缩包无法判断类型",
729+
"AddGameWindow.Tab2.Text16": "是否下载压缩包",
729730
"AddGameWindow.Tab3.Text1": "导入路径",
730731
"AddGameWindow.Tab3.Text2": "选择路径",
731732
"AddGameWindow.Tab3.Text3": "扫描文件",

src/ColorMC.Gui/UI/Controls/Add/AddGameTab2Control.axaml

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -50,23 +50,25 @@
5050
Text="{Binding ZipLocal}"
5151
Watermark="{setting:Localize AddGameWindow.Tab2.Text1}" />
5252
</DockPanel>
53-
<TextBlock Margin="0,0,0,5" Text="{setting:Localize AddGameWindow.Tab2.Text2}" />
54-
<StackPanel Margin="0,0,0,10" Orientation="Horizontal">
55-
<TextBlock Margin="0,0,5,0" Text="{setting:Localize Text.Type}" />
56-
<ComboBox
57-
Width="230"
58-
ItemsSource="{Binding PackTypeList}"
59-
SelectedIndex="{Binding Type}" />
60-
</StackPanel>
61-
<TextBlock Margin="0,0,0,5" Text="{setting:Localize AddGameWindow.Tab1.Text5}" />
62-
<StackPanel Orientation="Horizontal">
63-
<TextBlock Margin="0,0,5,0" Text="{setting:Localize Text.Group}" />
64-
<ComboBox
65-
Width="230"
66-
IsEditable="True"
67-
ItemsSource="{Binding GroupList}"
68-
Text="{Binding Group}" />
69-
</StackPanel>
53+
<Grid ColumnDefinitions="*,*">
54+
<DockPanel Margin="0,0,10,0">
55+
<TextBlock Margin="0,0,5,0" Text="{setting:Localize AddGameWindow.Tab2.Text2}" />
56+
<ComboBox
57+
HorizontalAlignment="Stretch"
58+
ItemsSource="{Binding PackTypeList}"
59+
SelectedIndex="{Binding Type}" />
60+
</DockPanel>
61+
<DockPanel Grid.Column="1" Margin="0,0,10,0">
62+
<TextBlock Margin="0,0,5,0" Text="{setting:Localize AddGameWindow.Tab1.Text5}" />
63+
<ComboBox
64+
HorizontalAlignment="Stretch"
65+
IsEditable="True"
66+
ItemsSource="{Binding GroupList}"
67+
Text="{Binding Group}" />
68+
</DockPanel>
69+
</Grid>
70+
<TreeDataGrid Margin="0,0,10,0" Source="{Binding ZipFiles}" />
71+
7072
</StackPanel>
7173
</DockPanel>
7274
</Border>

src/ColorMC.Gui/UI/Model/Add/AddGameTab2Model.cs

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
using System.Threading.Tasks;
2+
using Avalonia.Controls;
23
using ColorMC.Core.Helpers;
34
using ColorMC.Core.Objs;
45
using ColorMC.Gui.Manager;
6+
using ColorMC.Gui.UI.Model.Items;
57
using ColorMC.Gui.UIBinding;
68
using ColorMC.Gui.Utils;
79
using CommunityToolkit.Mvvm.ComponentModel;
@@ -20,6 +22,17 @@ public partial class AddGameModel
2022
/// </summary>
2123
public string[] PackTypeList { get; init; } = LangUtils.GetPackType();
2224

25+
/// <summary>
26+
/// 压缩包文件列表
27+
/// </summary>
28+
[ObservableProperty]
29+
private HierarchicalTreeDataGridSource<FileTreeNodeModel>? _zipFiles;
30+
31+
/// <summary>
32+
/// 文件列表
33+
/// </summary>
34+
private ZipPage? _zipFileModel;
35+
2336
/// <summary>
2437
/// 压缩包位置
2538
/// </summary>
@@ -38,10 +51,29 @@ public partial class AddGameModel
3851
/// <param name="value"></param>
3952
async partial void OnZipLocalChanged(string? value)
4053
{
41-
if (value != null && Type == null)
54+
if (value != null)
4255
{
56+
if (value.StartsWith("http"))
57+
{
58+
var res1 = await Window.ShowWait(LangUtils.Get("AddGameWindow.Tab2.Text16"));
59+
if (res1 is true)
60+
{
61+
var file = await WebBinding.DownloadTempZip(value);
62+
if (file != null)
63+
{
64+
ZipLocal = file;
65+
}
66+
}
67+
return;
68+
}
69+
4370
//测试获取压缩包类型
4471
var dialog = Window.ShowProgress(LangUtils.Get("AddGameWindow.Tab2.Text11"));
72+
_zipFileModel = await Task.Run(() =>
73+
{
74+
return new ZipPage(value);
75+
});
76+
4577
var res = await GameBinding.CheckTypeAsync(value);
4678
Window.CloseDialog(dialog);
4779
if (res == null)
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
using System.Collections.Generic;
2+
using Avalonia.Controls;
3+
using Avalonia.Controls.Models.TreeDataGrid;
4+
using ColorMC.Core.Objs;
5+
using ColorMC.Gui.UI.Model.Items;
6+
using ColorMC.Gui.Utils;
7+
8+
namespace ColorMC.Gui.UI.Model;
9+
10+
/// <summary>
11+
/// 文件列表树
12+
/// </summary>
13+
public class ZipPage
14+
{
15+
/// <summary>
16+
/// 根路径
17+
/// </summary>
18+
private readonly FileTreeNodeModel _root;
19+
20+
public PackType Type;
21+
22+
/// <summary>
23+
/// 显示内容
24+
/// </summary>
25+
public HierarchicalTreeDataGridSource<FileTreeNodeModel> Source { get; init; }
26+
27+
public ZipPage(string obj, bool check, List<string>? unselect = null)
28+
{
29+
_root = new FileTreeNodeModel(null, obj, true, check, true);
30+
Source = new HierarchicalTreeDataGridSource<FileTreeNodeModel>(_root)
31+
{
32+
Columns =
33+
{
34+
new TemplateColumn<FileTreeNodeModel>(
35+
null,
36+
cellTemplateResourceKey: "FileNameCell1",
37+
options: new TemplateColumnOptions<FileTreeNodeModel>
38+
{
39+
CanUserResizeColumn = false
40+
}),
41+
new HierarchicalExpanderColumn<FileTreeNodeModel>(
42+
new TemplateColumn<FileTreeNodeModel>(
43+
LangUtils.Get("Text.FileName"),
44+
"FileNameCell",
45+
null,
46+
new GridLength(1, GridUnitType.Star),
47+
new TemplateColumnOptions<FileTreeNodeModel>
48+
{
49+
CompareAscending = FileTreeNodeModel.SortAscending(x => x.Name),
50+
CompareDescending = FileTreeNodeModel.SortDescending(x => x.Name),
51+
IsTextSearchEnabled = true,
52+
TextSearchValueSelector = x => x.Name
53+
}),
54+
x => x.Children,
55+
x => x.HasChildren,
56+
x => x.IsExpanded),
57+
new TextColumn<FileTreeNodeModel, long?>(
58+
LangUtils.Get("Text.Size"),
59+
x => x.Size,
60+
options: new TextColumnOptions<FileTreeNodeModel>
61+
{
62+
CompareAscending = FileTreeNodeModel.SortAscending(x => x.Size),
63+
CompareDescending = FileTreeNodeModel.SortDescending(x => x.Size),
64+
}),
65+
new TextColumn<FileTreeNodeModel, string?>(
66+
LangUtils.Get("GameExportWindow.Text3"),
67+
x => x.Modified,
68+
options: new TextColumnOptions<FileTreeNodeModel>
69+
{
70+
CompareAscending = FileTreeNodeModel.SortAscending(x => x.Modified),
71+
CompareDescending = FileTreeNodeModel.SortDescending(x => x.Modified),
72+
}),
73+
}
74+
};
75+
76+
Source.RowSelection!.SingleSelect = false;
77+
78+
if (unselect != null)
79+
{
80+
foreach (var item in unselect)
81+
{
82+
foreach (var item1 in _root.Children!)
83+
{
84+
if (item1.Name == item || item1.Path == item)
85+
{
86+
item1.IsChecked = false;
87+
}
88+
}
89+
}
90+
}
91+
}
92+
93+
/// <summary>
94+
/// 设置未选择的文件
95+
/// </summary>
96+
/// <param name="config"></param>
97+
public void SetUnSelectItems(List<string> config)
98+
{
99+
foreach (var item in config)
100+
{
101+
_root.UnSelect(item);
102+
}
103+
}
104+
/// <summary>
105+
/// 获取所有未选择的文件
106+
/// </summary>
107+
/// <returns></returns>
108+
public List<string> GetUnSelectItems()
109+
{
110+
return _root.GetUnSelectItems();
111+
}
112+
/// <summary>
113+
/// 获取所有选中的文件
114+
/// </summary>
115+
/// <param name="getdir">是否获取目录</param>
116+
/// <returns></returns>
117+
public List<string> GetSelectItems(bool getdir = false)
118+
{
119+
return _root.GetSelectItems(getdir);
120+
}
121+
/// <summary>
122+
/// 设置选中的文件
123+
/// </summary>
124+
/// <param name="config"></param>
125+
public void SetSelectItems(List<string> config)
126+
{
127+
foreach (var item in config)
128+
{
129+
_root.Select(item);
130+
}
131+
}
132+
/// <summary>
133+
/// 设置所有文件选中
134+
/// </summary>
135+
public void SetSelectItems()
136+
{
137+
_root.Select();
138+
}
139+
/// <summary>
140+
/// 设置所有文件不选中
141+
/// </summary>
142+
public void SetUnSelectItems()
143+
{
144+
_root.UnSelect();
145+
}
146+
}

src/ColorMC.Gui/UIBinding/GameBinding.cs

Lines changed: 26 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1335,68 +1335,40 @@ public static async Task<bool> AddFileAsync(GameSettingObj obj, IDataTransfer da
13351335
/// </summary>
13361336
/// <param name="local">文件位置</param>
13371337
/// <returns>压缩包类型</returns>
1338-
public static async Task<PackType?> CheckTypeAsync(string local)
1338+
public static async Task<PackType?> CheckTypeAsync(string local, ZipArchive zip)
13391339
{
1340-
Stream? stream = null;
1341-
try
1340+
if (local.EndsWith(Names.NameMrpackExt))
13421341
{
1343-
if (local.StartsWith("http"))
1344-
{
1345-
using var res = await CoreHttpClient.GetAsync(local);
1346-
using var stream1 = await res.Content.ReadAsStreamAsync();
1347-
var memoryStream = new MemoryStream();
1348-
await stream1.CopyToAsync(memoryStream);
1349-
stream = memoryStream;
1350-
}
1351-
else
1352-
{
1353-
stream = PathHelper.OpenRead(local);
1354-
}
1355-
1356-
if (stream == null)
1357-
{
1358-
return null;
1359-
}
1360-
1361-
if (local.EndsWith(Names.NameMrpackExt))
1362-
{
1363-
return PackType.Modrinth;
1364-
}
1365-
if (local.EndsWith(Names.NameZipExt))
1342+
return PackType.Modrinth;
1343+
}
1344+
if (local.EndsWith(Names.NameZipExt))
1345+
{
1346+
foreach (var item in zip.Entries)
13661347
{
1367-
using var zFile = new ZipArchive(stream);
1368-
foreach (var item in zFile.Entries)
1348+
if (item.Name == Names.NameGameFile)
13691349
{
1370-
if (item.Name == Names.NameGameFile)
1371-
{
1372-
return PackType.ColorMC;
1373-
}
1374-
else if (item.Name == Names.NameHMCLFile)
1375-
{
1376-
return PackType.HMCL;
1377-
}
1378-
else if (item.Name == Names.NameMMCCfgFile)
1379-
{
1380-
return PackType.MMC;
1381-
}
1382-
else if (item.Name == Names.NameManifestFile)
1383-
{
1384-
return PackType.CurseForge;
1385-
}
1350+
return PackType.ColorMC;
13861351
}
1387-
foreach (var item in zFile.Entries)
1352+
else if (item.Name == Names.NameHMCLFile)
13881353
{
1389-
if (item.FullName.StartsWith(".minecraft/"))
1390-
{
1391-
return PackType.ZipPack;
1392-
}
1354+
return PackType.HMCL;
1355+
}
1356+
else if (item.Name == Names.NameMMCCfgFile)
1357+
{
1358+
return PackType.MMC;
1359+
}
1360+
else if (item.Name == Names.NameManifestFile)
1361+
{
1362+
return PackType.CurseForge;
1363+
}
1364+
}
1365+
foreach (var item in zip.Entries)
1366+
{
1367+
if (item.FullName.StartsWith(".minecraft/"))
1368+
{
1369+
return PackType.ZipPack;
13931370
}
13941371
}
1395-
}
1396-
finally
1397-
{
1398-
stream?.Close();
1399-
stream?.Dispose();
14001372
}
14011373

14021374
return null;

src/ColorMC.Gui/UIBinding/WebBinding.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
using ColorMC.Core.Objs.Minecraft;
2222
using ColorMC.Core.Objs.MinecraftAPI;
2323
using ColorMC.Core.Objs.Modrinth;
24+
using ColorMC.Core.Utils;
2425
using ColorMC.Gui.Manager;
2526
using ColorMC.Gui.Net.Apis;
2627
using ColorMC.Gui.Objs;
@@ -1720,4 +1721,36 @@ public static void OpenRegister(AuthType type, string? name)
17201721

17211722
return null;
17221723
}
1724+
1725+
/// <summary>
1726+
/// 下载临时压缩包
1727+
/// </summary>
1728+
/// <param name="url"></param>
1729+
/// <returns></returns>
1730+
public static async Task<string?> DownloadTempZip(string url)
1731+
{
1732+
var name = Path.GetFileName(url);
1733+
if (!name.EndsWith(Names.NameZipExt) && !name.EndsWith(Names.NameMrpackExt))
1734+
{
1735+
name = FunctionUtils.NewUUID();
1736+
}
1737+
1738+
var file = Path.Combine(DownloadManager.DownloadDir, name);
1739+
1740+
var res = await DownloadManager.StartAsync([new FileItemObj()
1741+
{
1742+
Name = name,
1743+
Local = file,
1744+
Url = url
1745+
}]);
1746+
1747+
if (res)
1748+
{
1749+
return file;
1750+
}
1751+
else
1752+
{
1753+
return null;
1754+
}
1755+
}
17231756
}

0 commit comments

Comments
 (0)