Skip to content

perf(CompSearch): 写一个结构体替代原有的字符串表示筛选器 #6388

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions Plain Craft Launcher 2/Modules/Minecraft/ModComp.vb
Original file line number Diff line number Diff line change
Expand Up @@ -804,8 +804,8 @@ NoSubtitle:
''' </summary>
Public ReadOnly Property CanContinue As Boolean
Get
If Tag.StartsWithF("/") OrElse Not Source.HasFlag(CompSourceType.CurseForge) Then Storage.CurseForgeTotal = 0
If Tag.EndsWithF("/") OrElse Not Source.HasFlag(CompSourceType.Modrinth) Then Storage.ModrinthTotal = 0
If Tag.NotSupportCf OrElse Not Source.HasFlag(CompSourceType.CurseForge) Then Storage.CurseForgeTotal = 0
If Tag.NotSupportMr OrElse Not Source.HasFlag(CompSourceType.Modrinth) Then Storage.ModrinthTotal = 0
If Storage.CurseForgeTotal = -1 OrElse Storage.ModrinthTotal = -1 Then Return True
Return Storage.CurseForgeOffset < Storage.CurseForgeTotal OrElse Storage.ModrinthOffset < Storage.ModrinthTotal
End Get
Expand All @@ -818,9 +818,9 @@ NoSubtitle:
''' </summary>
Public Type As CompType
''' <summary>
''' 筛选资源标签。空字符串代表不限制。格式例如 "406/worldgen",分别是 CurseForge 和 Modrinth 的 ID。
''' 筛选资源标签。
''' </summary>
Public Tag As String = ""
Public Tag As PageComp.FilterTag
''' <summary>
''' 筛选 Mod 加载器类别。
''' </summary>
Expand Down Expand Up @@ -853,7 +853,7 @@ NoSubtitle:
''' </summary>
Public Function GetCurseForgeAddress() As String
If Not Source.HasFlag(CompSourceType.CurseForge) Then Return Nothing
If Tag.StartsWithF("/") Then Storage.CurseForgeTotal = 0
If Tag.NotSupportCf Then Storage.CurseForgeTotal = 0
If Storage.CurseForgeTotal > -1 AndAlso Storage.CurseForgeTotal <= Storage.CurseForgeOffset Then Return Nothing
'应用筛选参数
Dim Address As String = $"https://api.curseforge.com/v1/mods/search?gameId=432&sortField=2&sortOrder=desc&pageSize={CompPageSize}"
Expand All @@ -869,7 +869,7 @@ NoSubtitle:
Case CompType.ResourcePack
Address += "&classId=12"
End Select
Address += "&categoryId=" & If(Tag = "", "0", Tag.BeforeFirst("/"))
Address += "&categoryId=" & If(Not Tag.HasFilter, "0", Tag.CfValue)
If ModLoader <> CompModLoaderType.Any Then Address += "&modLoaderType=" & CType(ModLoader, Integer)
If Not String.IsNullOrEmpty(GameVersion) Then Address += "&gameVersion=" & GameVersion
If Not String.IsNullOrEmpty(SearchText) Then Address += "&searchFilter=" & Net.WebUtility.UrlEncode(SearchText)
Expand All @@ -881,7 +881,7 @@ NoSubtitle:
''' </summary>
Public Function GetModrinthAddress() As String
If Not Source.HasFlag(CompSourceType.Modrinth) Then Return Nothing
If Tag.EndsWithF("/") Then Storage.ModrinthTotal = 0
If Tag.NotSupportMr Then Storage.ModrinthTotal = 0
If Storage.ModrinthTotal > -1 AndAlso Storage.ModrinthTotal <= Storage.ModrinthOffset Then Return Nothing
'应用筛选参数
Dim Address As String = $"https://api.modrinth.com/v2/search?limit={CompPageSize}&index=relevance"
Expand All @@ -890,7 +890,7 @@ NoSubtitle:
'facets=[["categories:'game-mechanics'"],["categories:'forge'"],["versions:1.19.3"],["project_type:mod"]]
Dim Facets As New List(Of String)
Facets.Add($"[""project_type:{GetStringFromEnum(Type).ToLower}""]")
If Not String.IsNullOrEmpty(Tag) Then Facets.Add($"[""categories:'{Tag.AfterLast("/")}'""]")
If Tag.HasFilter Then Facets.Add($"[""categories:'{Tag.MrValue}'""]")
If ModLoader <> CompModLoaderType.Any Then Facets.Add($"[""categories:'{GetStringFromEnum(ModLoader).ToLower}'""]")
If Not String.IsNullOrEmpty(GameVersion) Then Facets.Add($"[""versions:'{GameVersion}'""]")
Address += "&facets=[" & String.Join(",", Facets) & "]"
Expand All @@ -902,7 +902,7 @@ NoSubtitle:
Dim request = TryCast(obj, CompProjectRequest)
Return request IsNot Nothing AndAlso
Type = request.Type AndAlso TargetResultCount = request.TargetResultCount AndAlso
Tag = request.Tag AndAlso ModLoader = request.ModLoader AndAlso Source = request.Source AndAlso
Tag.Equals(request.Tag) AndAlso ModLoader = request.ModLoader AndAlso Source = request.Source AndAlso
GameVersion = request.GameVersion AndAlso SearchText = request.SearchText
End Function
Public Shared Operator =(left As CompProjectRequest, right As CompProjectRequest) As Boolean
Expand Down Expand Up @@ -1135,9 +1135,9 @@ Retry:
Else
If IsChineseSearch AndAlso Not (Task.Input.Type = CompType.Mod OrElse Task.Input.Type = CompType.DataPack) Then
Throw New Exception("没有搜索结果,请尝试使用英文搜索")
ElseIf Task.Input.Source = CompSourceType.CurseForge AndAlso Task.Input.Tag.StartsWithF("/") Then
ElseIf Task.Input.Source = CompSourceType.CurseForge AndAlso Task.Input.Tag.NotSupportCf Then
Throw New Exception("CurseForge 不兼容所选的类型")
ElseIf Task.Input.Source = CompSourceType.Modrinth AndAlso Task.Input.Tag.EndsWithF("/") Then
ElseIf Task.Input.Source = CompSourceType.Modrinth AndAlso Task.Input.Tag.NotSupportMr Then
Throw New Exception("Modrinth 不兼容所选的类型")
Else
Throw New Exception("没有搜索结果")
Expand Down
64 changes: 62 additions & 2 deletions Plain Craft Launcher 2/Pages/PageDownload/Comp/PageComp.xaml.vb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
Imports System.Windows.Markup
Imports System.ComponentModel
Imports System.Globalization
Imports System.Windows.Markup

<ContentProperty("SearchTags")>
Public Class PageComp
Expand Down Expand Up @@ -104,6 +106,64 @@ Public Class PageComp
CType(Parent, MyPageRight).PageLoaderInit(Load, PanLoad, PanContent, PanAlways, Loader, AddressOf Load_OnFinish, AddressOf LoaderInput)
If McVersionHighest = -1 Then McVersionHighest = Math.Max(McVersionHighest, Integer.Parse(CType(TextSearchVersion.Items(1), MyComboBoxItem).Content.ToString.Split(".")(1)))
End Sub


#Region "FilterTag"
Public Shared Function GetFilterTag(element As DependencyObject) As FilterTag
If element Is Nothing Then Throw New ArgumentNullException("element")
Return element.GetValue(FilterTagProperty)
End Function
Public Shared Sub SetFilterTag(element As DependencyObject, value As FilterTag)
If element Is Nothing Then Throw New ArgumentNullException("element")
element.SetValue(FilterTagProperty, value)
End Sub
Public Shared ReadOnly FilterTagProperty As DependencyProperty =
DependencyProperty.RegisterAttached("FilterTag", GetType(FilterTag), GetType(PageComp), New PropertyMetadata(New FilterTag))
''' <summary>
''' 搜索工程时的 Tag 筛选器,xaml 中的书写格式例如 "406/worldgen",分别是 CurseForge 和 Modrinth 的 ID。
''' </summary>
<TypeConverter(GetType(FilterTagTypeConverter))>
Public Structure FilterTag
Public ReadOnly HasFilter As Boolean '调无参构造的时候 HasFilter 就是 False,代表着不做任何筛选
Public ReadOnly CfValue As String '调有参构造的话这两个一定非 Nothing,为 String.Empty 时代表着筛选器不支持对应的搜索来源
Public ReadOnly MrValue As String
Public ReadOnly Property NotSupportCf As Boolean
Get
Return HasFilter AndAlso String.IsNullOrEmpty(CfValue)
End Get
End Property
Public ReadOnly Property NotSupportMr As Boolean
Get
Return HasFilter AndAlso String.IsNullOrEmpty(MrValue)
End Get
End Property
Public Sub New(CfValue As String, MrValue As String)
HasFilter = True
Me.CfValue = If(CfValue, "")
Me.MrValue = If(MrValue, "")
End Sub
Public Overrides Function ToString() As String
Return If(HasFilter, $"{CfValue}/{MrValue}", "")
End Function
End Structure
Public Class FilterTagTypeConverter
Inherits TypeConverter
Public Overrides Function CanConvertFrom(context As ITypeDescriptorContext, sourceType As Type) As Boolean
Return (sourceType Is GetType(String)) OrElse MyBase.CanConvertFrom(context, sourceType)
End Function
Public Overrides Function ConvertFrom(context As ITypeDescriptorContext, culture As CultureInfo, value As Object) As Object
If TypeOf value Is String Then
With CType(value, String)
If .Length = 0 Then Return New FilterTag
Dim Splited As String() = .Split("/"c)
If Splited.Length = 2 Then Return New FilterTag(Splited(0), Splited(1))
End With
End If
Return MyBase.ConvertFrom(context, culture, value)
End Function
End Class
#End Region

Private Function LoaderInput() As CompProjectRequest
Dim Request As New CompProjectRequest(PageType, Storage, (Page + 1) * PageSize)
Dim GameVersion As String = If(TextSearchVersion.Text = "全部 (也可自行输入)", Nothing,
Expand All @@ -119,7 +179,7 @@ Public Class PageComp
With Request
.SearchText = TextSearchName.Text
.GameVersion = GameVersion
.Tag = ComboSearchTag.SelectedItem.Tag
.Tag = GetFilterTag(ComboSearchTag.SelectedItem)
.ModLoader = ModLoader
.Source = CType(Val(ComboSearchSource.SelectedItem.Tag), CompSourceType)
End With
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,22 @@
mc:Ignorable="d" x:Class="PageDownloadDataPack"
PanScroll="{Binding ElementName=Content}">
<local:PageComp x:Name="Content" PageType="DataPack" TypeName="数据包" TypeNameSpaced="数据包">
<local:MyComboBoxItem Tag="" Content="全部" IsSelected="True" />
<local:MyComboBoxItem Content="世界元素" Tag="/worldgen" />
<local:MyComboBoxItem Content="科技" Tag="6951/technology" />
<local:MyComboBoxItem Content="游戏机制" Tag="/game-mechanics" />
<local:MyComboBoxItem Content="运输" Tag="/transportation" />
<local:MyComboBoxItem Content="仓储" Tag="/storage" />
<local:MyComboBoxItem Content="魔法" Tag="6952/magic" />
<local:MyComboBoxItem Content="冒险" Tag="6948/adventure" />
<local:MyComboBoxItem Content="幻想" Tag="6949/" />
<local:MyComboBoxItem Content="装饰" Tag="/decoration" />
<local:MyComboBoxItem Content="生物" Tag="/mobs" />
<local:MyComboBoxItem Content="实用" Tag="6953/utility" />
<local:MyComboBoxItem Content="装备与工具" Tag="/equipment" />
<local:MyComboBoxItem Content="性能优化" Tag="/optimization" />
<local:MyComboBoxItem Content="服务器" Tag="/social" />
<local:MyComboBoxItem Content="支持库" Tag="6950/library" />
<local:MyComboBoxItem Content="Mod 相关" Tag="6946/" />
<local:MyComboBoxItem Content="全部" IsSelected="True" />
<local:MyComboBoxItem Content="世界元素" local:PageComp.FilterTag="/worldgen" />
<local:MyComboBoxItem Content="科技" local:PageComp.FilterTag="6951/technology" />
<local:MyComboBoxItem Content="游戏机制" local:PageComp.FilterTag="/game-mechanics" />
<local:MyComboBoxItem Content="运输" local:PageComp.FilterTag="/transportation" />
<local:MyComboBoxItem Content="仓储" local:PageComp.FilterTag="/storage" />
<local:MyComboBoxItem Content="魔法" local:PageComp.FilterTag="6952/magic" />
<local:MyComboBoxItem Content="冒险" local:PageComp.FilterTag="6948/adventure" />
<local:MyComboBoxItem Content="幻想" local:PageComp.FilterTag="6949/" />
<local:MyComboBoxItem Content="装饰" local:PageComp.FilterTag="/decoration" />
<local:MyComboBoxItem Content="生物" local:PageComp.FilterTag="/mobs" />
<local:MyComboBoxItem Content="实用" local:PageComp.FilterTag="6953/utility" />
<local:MyComboBoxItem Content="装备与工具" local:PageComp.FilterTag="/equipment" />
<local:MyComboBoxItem Content="性能优化" local:PageComp.FilterTag="/optimization" />
<local:MyComboBoxItem Content="服务器" local:PageComp.FilterTag="/social" />
<local:MyComboBoxItem Content="支持库" local:PageComp.FilterTag="6950/library" />
<local:MyComboBoxItem Content="Mod 相关" local:PageComp.FilterTag="6946/" />
</local:PageComp>
</local:MyPageRight>
52 changes: 26 additions & 26 deletions Plain Craft Launcher 2/Pages/PageDownload/Comp/PageDownloadMod.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,31 @@
mc:Ignorable="d" x:Class="PageDownloadMod"
PanScroll="{Binding ElementName=Content}">
<local:PageComp x:Name="Content" PageType="Mod" TypeName="Mod" TypeNameSpaced=" Mod ">
<local:MyComboBoxItem Content="全部" Tag="" IsSelected="True" />
<local:MyComboBoxItem Content="世界元素" Tag="406/worldgen" />
<local:MyComboBoxItem Content="生物群系" Tag="407/" Margin="15,0,0,0" />
<local:MyComboBoxItem Content="维度" Tag="410/" Margin="15,0,0,0" />
<local:MyComboBoxItem Content="矿物与资源" Tag="408/" Margin="15,0,0,0" />
<local:MyComboBoxItem Content="天然结构" Tag="409/" Margin="15,0,0,0" />
<local:MyComboBoxItem Content="科技" Tag="412/technology" />
<local:MyComboBoxItem Content="管道与物流" Tag="415/" Margin="15,0,0,0" />
<local:MyComboBoxItem Content="自动化" Tag="4843/" Margin="15,0,0,0" />
<local:MyComboBoxItem Content="能源" Tag="417/" Margin="15,0,0,0" />
<local:MyComboBoxItem Content="红石" Tag="4558/" Margin="15,0,0,0" />
<local:MyComboBoxItem Content="食物与烹饪" Tag="436/food" />
<local:MyComboBoxItem Content="农业" Tag="416/" Margin="15,0,0,0" />
<local:MyComboBoxItem Content="游戏机制" Tag="/game-mechanics" />
<local:MyComboBoxItem Content="运输" Tag="414/transportation" />
<local:MyComboBoxItem Content="仓储" Tag="420/storage" />
<local:MyComboBoxItem Content="魔法" Tag="419/magic" />
<local:MyComboBoxItem Content="冒险" Tag="422/adventure" />
<local:MyComboBoxItem Content="装饰" Tag="424/decoration" />
<local:MyComboBoxItem Content="生物" Tag="411/mobs" />
<local:MyComboBoxItem Content="实用" Tag="5191/utility" />
<local:MyComboBoxItem Content="装备与工具" Tag="434/equipment" />
<local:MyComboBoxItem Content="性能优化" Tag="/optimization" />
<local:MyComboBoxItem Content="信息显示" Tag="423/" />
<local:MyComboBoxItem Content="服务器" Tag="435/social" />
<local:MyComboBoxItem Content="支持库" Tag="421/library" />
<local:MyComboBoxItem Content="全部" IsSelected="True" />
<local:MyComboBoxItem Content="世界元素" local:PageComp.FilterTag="406/worldgen" />
<local:MyComboBoxItem Content="生物群系" local:PageComp.FilterTag="407/" Margin="15,0,0,0" />
<local:MyComboBoxItem Content="维度" local:PageComp.FilterTag="410/" Margin="15,0,0,0" />
<local:MyComboBoxItem Content="矿物与资源" local:PageComp.FilterTag="408/" Margin="15,0,0,0" />
<local:MyComboBoxItem Content="天然结构" local:PageComp.FilterTag="409/" Margin="15,0,0,0" />
<local:MyComboBoxItem Content="科技" local:PageComp.FilterTag="412/technology" />
<local:MyComboBoxItem Content="管道与物流" local:PageComp.FilterTag="415/" Margin="15,0,0,0" />
<local:MyComboBoxItem Content="自动化" local:PageComp.FilterTag="4843/" Margin="15,0,0,0" />
<local:MyComboBoxItem Content="能源" local:PageComp.FilterTag="417/" Margin="15,0,0,0" />
<local:MyComboBoxItem Content="红石" local:PageComp.FilterTag="4558/" Margin="15,0,0,0" />
<local:MyComboBoxItem Content="食物与烹饪" local:PageComp.FilterTag="436/food" />
<local:MyComboBoxItem Content="农业" local:PageComp.FilterTag="416/" Margin="15,0,0,0" />
<local:MyComboBoxItem Content="游戏机制" local:PageComp.FilterTag="/game-mechanics" />
<local:MyComboBoxItem Content="运输" local:PageComp.FilterTag="414/transportation" />
<local:MyComboBoxItem Content="仓储" local:PageComp.FilterTag="420/storage" />
<local:MyComboBoxItem Content="魔法" local:PageComp.FilterTag="419/magic" />
<local:MyComboBoxItem Content="冒险" local:PageComp.FilterTag="422/adventure" />
<local:MyComboBoxItem Content="装饰" local:PageComp.FilterTag="424/decoration" />
<local:MyComboBoxItem Content="生物" local:PageComp.FilterTag="411/mobs" />
<local:MyComboBoxItem Content="实用" local:PageComp.FilterTag="5191/utility" />
<local:MyComboBoxItem Content="装备与工具" local:PageComp.FilterTag="434/equipment" />
<local:MyComboBoxItem Content="性能优化" local:PageComp.FilterTag="/optimization" />
<local:MyComboBoxItem Content="信息显示" local:PageComp.FilterTag="423/" />
<local:MyComboBoxItem Content="服务器" local:PageComp.FilterTag="435/social" />
<local:MyComboBoxItem Content="支持库" local:PageComp.FilterTag="421/library" />
</local:PageComp>
</local:MyPageRight>
Loading