Skip to content

Commit 2c06eae

Browse files
committed
feat: 过滤不支持的项目类别
1 parent 024cb24 commit 2c06eae

9 files changed

Lines changed: 97 additions & 32 deletions

File tree

ZalithLauncher/src/main/java/com/movtery/zalithlauncher/game/download/assets/platform/PlatformProject.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,11 @@ interface PlatformProject {
7777
*/
7878
fun platformModLoaders(): List<PlatformDisplayLabel>?
7979

80+
/**
81+
* 检查项目类别是否受支持
82+
*/
83+
fun checkClasses()
84+
8085
/**
8186
* 在平台上标注的类别信息
8287
*/
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* Zalith Launcher 2
3+
* Copyright (C) 2025 MovTery <movtery228@qq.com> and contributors
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13+
* See the GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program. If not, see <https://www.gnu.org/licenses/gpl-3.0.txt>.
17+
*/
18+
19+
package com.movtery.zalithlauncher.game.download.assets.platform
20+
21+
/**
22+
* 不受支持的项目类别
23+
*/
24+
class UnsupportedClassesException(classes: Any?): IllegalArgumentException("Unsupported classes: $classes")

ZalithLauncher/src/main/java/com/movtery/zalithlauncher/game/download/assets/platform/curseforge/models/CurseForgeProject.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import com.movtery.zalithlauncher.game.download.assets.platform.PlatformClasses
2323
import com.movtery.zalithlauncher.game.download.assets.platform.PlatformDisplayLabel
2424
import com.movtery.zalithlauncher.game.download.assets.platform.PlatformFilterCode
2525
import com.movtery.zalithlauncher.game.download.assets.platform.PlatformProject
26+
import com.movtery.zalithlauncher.game.download.assets.platform.UnsupportedClassesException
2627
import kotlinx.serialization.SerialName
2728
import kotlinx.serialization.Serializable
2829

@@ -57,6 +58,11 @@ class CurseForgeProject(
5758
return data.platformModLoaders()
5859
}
5960

61+
override fun checkClasses() {
62+
val classId = data.classId
63+
if (CurseForgeClassID.entries.none { id -> classId == id.classID }) throw UnsupportedClassesException(classId)
64+
}
65+
6066
override fun platformCategories(classes: PlatformClasses): List<PlatformFilterCode>? {
6167
return data.platformCategories(classes)
6268
}

ZalithLauncher/src/main/java/com/movtery/zalithlauncher/game/download/assets/platform/modrinth/models/ModrinthProjectType.kt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import kotlinx.serialization.SerialName
2323
import kotlinx.serialization.Serializable
2424

2525
@Serializable
26-
enum class ModrinthProjectType(val platform: PlatformClasses) {
26+
enum class ModrinthProjectType(val platform: PlatformClasses?) {
2727
@SerialName("mod")
2828
MOD(PlatformClasses.MOD),
2929

@@ -34,5 +34,8 @@ enum class ModrinthProjectType(val platform: PlatformClasses) {
3434
RESOURCEPACK(PlatformClasses.RESOURCE_PACK),
3535

3636
@SerialName("shader")
37-
SHADER(PlatformClasses.SHADERS)
37+
SHADER(PlatformClasses.SHADERS),
38+
39+
@SerialName("minecraft_java_server")
40+
SERVER(null),
3841
}

ZalithLauncher/src/main/java/com/movtery/zalithlauncher/game/download/assets/platform/modrinth/models/ModrinthSingleProject.kt

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import com.movtery.zalithlauncher.game.download.assets.platform.PlatformClasses
2323
import com.movtery.zalithlauncher.game.download.assets.platform.PlatformDisplayLabel
2424
import com.movtery.zalithlauncher.game.download.assets.platform.PlatformFilterCode
2525
import com.movtery.zalithlauncher.game.download.assets.platform.PlatformProject
26+
import com.movtery.zalithlauncher.game.download.assets.platform.UnsupportedClassesException
2627
import kotlinx.serialization.SerialName
2728
import kotlinx.serialization.Serializable
2829

@@ -243,7 +244,7 @@ class ModrinthSingleProject(
243244

244245
override fun platformId(): String = id
245246

246-
override fun platformClasses(defaultClasses: PlatformClasses): PlatformClasses = projectType.platform
247+
override fun platformClasses(defaultClasses: PlatformClasses): PlatformClasses = projectType.platform ?: defaultClasses
247248

248249
override fun platformSlug(): String = slug
249250

@@ -267,6 +268,10 @@ class ModrinthSingleProject(
267268
return modloaders?.sortedWith { o1, o2 -> o1.index() - o2.index() }
268269
}
269270

271+
override fun checkClasses() {
272+
if (projectType.platform == null) throw UnsupportedClassesException(projectType.name)
273+
}
274+
270275
override fun platformCategories(classes: PlatformClasses): List<PlatformFilterCode>? {
271276
return categories.take(4) //没有主要类别,则展示前4个
272277
.mapNotNull { string ->
@@ -278,8 +283,9 @@ class ModrinthSingleProject(
278283
}
279284

280285
override fun platformUrls(defaultClasses: PlatformClasses): PlatformProject.Urls {
286+
val classes = projectType.platform ?: defaultClasses
281287
return PlatformProject.Urls(
282-
projectUrl = "https://modrinth.com/${projectType.platform.modrinth!!.facetValue()}/${slug}",
288+
projectUrl = "https://modrinth.com/${classes.modrinth!!.facetValue()}/${slug}",
283289
sourceUrl = sourceUrl,
284290
issuesUrl = issuesUrl,
285291
wikiUrl = wikiUrl

ZalithLauncher/src/main/java/com/movtery/zalithlauncher/ui/screens/content/download/assets/search/SearchIdScreen.kt

Lines changed: 46 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ import androidx.compose.runtime.setValue
4848
import androidx.compose.ui.Alignment
4949
import androidx.compose.ui.Modifier
5050
import androidx.compose.ui.graphics.graphicsLayer
51+
import androidx.compose.ui.graphics.painter.Painter
5152
import androidx.compose.ui.platform.LocalContext
5253
import androidx.compose.ui.res.painterResource
5354
import androidx.compose.ui.res.stringResource
@@ -61,6 +62,7 @@ import com.movtery.zalithlauncher.game.download.assets.mapExceptionToMessage
6162
import com.movtery.zalithlauncher.game.download.assets.platform.Platform
6263
import com.movtery.zalithlauncher.game.download.assets.platform.PlatformClasses
6364
import com.movtery.zalithlauncher.game.download.assets.platform.PlatformProject
65+
import com.movtery.zalithlauncher.game.download.assets.platform.UnsupportedClassesException
6466
import com.movtery.zalithlauncher.game.download.assets.platform.getProjectByVersion
6567
import com.movtery.zalithlauncher.game.download.assets.platform.isAllNull
6668
import com.movtery.zalithlauncher.game.download.assets.utils.ModTranslations
@@ -101,8 +103,12 @@ private sealed interface SearchIdOperation {
101103
val mcMod: ModTranslations.McMod?,
102104
val mod: ModTranslations,
103105
) : SearchIdOperation
106+
104107
/** 未找到该项目 */
105108
data object NotFound : SearchIdOperation
109+
/** 项目类别不受支持 */
110+
data object Unsupported : SearchIdOperation
111+
106112
/** 获取过程中出现异常 */
107113
data class Error(val message: Int, val args: Array<Any>? = null) : SearchIdOperation {
108114
override fun equals(other: Any?): Boolean {
@@ -126,7 +132,7 @@ private sealed interface SearchIdOperation {
126132
}
127133

128134
private class SearchIdViewModel: ViewModel() {
129-
//fixme: 默认视为模组,通常情况下,获取到的项目会带有类型,需要过滤类型
135+
//fixme: 默认视为模组,通常情况下,获取到的项目会带有类别
130136
val defaultClasses = PlatformClasses.MOD
131137

132138
var projectId by mutableStateOf("")
@@ -150,7 +156,9 @@ private class SearchIdViewModel: ViewModel() {
150156
projectId = id,
151157
platform = platform0
152158
)
153-
val translations = defaultClasses.getTranslations()
159+
project.checkClasses()
160+
val classes = project.platformClasses(defaultClasses)
161+
val translations = classes.getTranslations()
154162
operation = SearchIdOperation.Result(
155163
project = project,
156164
mcMod = project.getMcMod(translations),
@@ -162,6 +170,7 @@ private class SearchIdViewModel: ViewModel() {
162170
Logger.debug(TAG, "The search task has been cancelled.")
163171
SearchIdOperation.None
164172
}
173+
is UnsupportedClassesException -> SearchIdOperation.Unsupported
165174
is NotFoundException -> SearchIdOperation.NotFound
166175
is ClientRequestException -> {
167176
if (e.response.status == HttpStatusCode.NotFound) {
@@ -292,17 +301,10 @@ private fun ContentResult(
292301
) {
293302
when (operation) {
294303
is SearchIdOperation.None -> {
295-
Column(
296-
horizontalAlignment = Alignment.CenterHorizontally,
297-
verticalArrangement = Arrangement.spacedBy(12.dp)
298-
) {
299-
Icon(
300-
modifier = Modifier.size(84.dp),
301-
painter = painterResource(R.drawable.ic_search),
302-
contentDescription = null
303-
)
304-
Text(text = stringResource(R.string.download_assets_id_tip))
305-
}
304+
IconTip(
305+
icon = painterResource(R.drawable.ic_search),
306+
text = stringResource(R.string.download_assets_id_tip)
307+
)
306308
}
307309
is SearchIdOperation.Loading -> {
308310
LinearWavyProgressIndicator(
@@ -318,20 +320,20 @@ private fun ContentResult(
318320
defaultClasses = defaultClasses,
319321
onView = onView,
320322
openLink = openLink,
323+
contentPadding = contentPadding,
321324
)
322325
}
323326
is SearchIdOperation.NotFound -> {
324-
Column(
325-
horizontalAlignment = Alignment.CenterHorizontally,
326-
verticalArrangement = Arrangement.spacedBy(12.dp)
327-
) {
328-
Icon(
329-
modifier = Modifier.size(84.dp),
330-
painter = painterResource(R.drawable.ic_box),
331-
contentDescription = null
332-
)
333-
Text(text = stringResource(R.string.download_assets_id_not_found))
334-
}
327+
IconTip(
328+
icon = painterResource(R.drawable.ic_box),
329+
text = stringResource(R.string.download_assets_id_not_found)
330+
)
331+
}
332+
is SearchIdOperation.Unsupported -> {
333+
IconTip(
334+
icon = painterResource(R.drawable.ic_box),
335+
text = stringResource(R.string.download_assets_id_unsupported)
336+
)
335337
}
336338
is SearchIdOperation.Error -> {
337339
Box(modifier.padding(all = 12.dp)) {
@@ -352,6 +354,24 @@ private fun ContentResult(
352354
}
353355
}
354356

357+
@Composable
358+
private fun IconTip(
359+
icon: Painter,
360+
text: String,
361+
) {
362+
Column(
363+
horizontalAlignment = Alignment.CenterHorizontally,
364+
verticalArrangement = Arrangement.spacedBy(12.dp)
365+
) {
366+
Icon(
367+
modifier = Modifier.size(68.dp),
368+
painter = icon,
369+
contentDescription = null
370+
)
371+
Text(text = text)
372+
}
373+
}
374+
355375
@Composable
356376
private fun ResultLayout(
357377
mcmod: ModTranslations.McMod?,
@@ -375,7 +395,7 @@ private fun ResultLayout(
375395
val classes = remember(project) { project.platformClasses(defaultClasses) }
376396
val categories = remember(project, classes) { project.platformCategories(classes) }
377397

378-
val urls = remember { project.platformUrls(defaultClasses) }
398+
val urls = remember { project.platformUrls(classes) }
379399
val screenshots = remember { project.platformScreenshots() }
380400

381401
LazyColumn(
@@ -385,9 +405,7 @@ private fun ResultLayout(
385405
) {
386406
item {
387407
ResultProjectLayout(
388-
modifier = Modifier
389-
.fillMaxWidth()
390-
.padding(vertical = 6.dp),
408+
modifier = Modifier.fillMaxWidth(),
391409
platform = platform,
392410
title = mcmod.getMcmodTitle(title, context),
393411
description = description,

ZalithLauncher/src/main/res/values-zh-rCN/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,7 @@
266266
<string name="download_assets_id_holder">项目 ID</string>
267267
<string name="download_assets_id_tip">根据项目 ID 快速获取对应项目</string>
268268
<string name="download_assets_id_not_found">未找到该项目!</string>
269+
<string name="download_assets_id_unsupported">不支持该项目!</string>
269270
<string name="download_modpack_warning1">在安装前,您需要理解,绝大多数整合包都是为电脑端制作的,当您尝试启动时会有极大概率出现崩溃或是其他问题,这往往不是在启动器层面上能够解决的问题。</string>
270271
<string name="download_modpack_warning2">您需要自己了解相关知识后尝试自己解决,或是寻求其他人帮忙。</string>
271272
<string name="download_modpack_warning3">注:启动器开发者通常不会受理整合包带来的任何问题。</string>

ZalithLauncher/src/main/res/values-zh-rTW/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1109,6 +1109,7 @@
11091109
<string name="download_assets_id_holder">專案 ID</string>
11101110
<string name="download_assets_id_tip">根據專案 ID 快速取得對應專案</string>
11111111
<string name="download_assets_id_not_found">未找到該專案!</string>
1112+
<string name="download_assets_id_unsupported">不支援該專案!</string>
11121113
<string name="control_keyboard_bind_title">綁定按鍵</string>
11131114
<string name="control_keyboard_bind_summary">請在您的鍵盤上按下想要綁定的按鍵</string>
11141115
<string name="control_manage_create_new">建立</string>

ZalithLauncher/src/main/res/values/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,7 @@
267267
<string name="download_assets_id_holder">Project ID</string>
268268
<string name="download_assets_id_tip">Quickly get the corresponding project by project ID</string>
269269
<string name="download_assets_id_not_found">Project not found!</string>
270+
<string name="download_assets_id_unsupported">Project not supported!</string>
270271
<string name="download_modpack_warning1">Before installation, you need to understand that most modpacks are made for PC, and there\'s a high chance of crashes or other issues when you try to launch them. These problems are often beyond what the launcher can resolve.</string>
271272
<string name="download_modpack_warning2">You will need to learn the relevant knowledge to try and resolve the issues yourself, or seek help from others.</string>
272273
<string name="download_modpack_warning3">Note: The launcher developers typically do not handle issues caused by modpacks.</string>

0 commit comments

Comments
 (0)