Skip to content
This repository has been archived by the owner on Jun 28, 2023. It is now read-only.

Commit

Permalink
fix: filter curseforge mod versions
Browse files Browse the repository at this point in the history
錯誤修復:安裝 CurseForge 模組時會出現不符合安裝檔的版本
  • Loading branch information
SiongSng authored Jul 16, 2022
2 parents 5c99d91 + d530895 commit 75a4121
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 25 deletions.
42 changes: 41 additions & 1 deletion lib/mod/curseforge/curseforge_handler.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import 'package:rpmlauncher/util/data.dart';
import 'package:rpmlauncher/util/util.dart';
import 'package:rpmlauncher/widget/rwl_loading.dart';
import 'package:rpmtw_api_client/rpmtw_api_client.dart' hide ModLoader;
import 'package:rpmtw_dart_common_library/rpmtw_dart_common_library.dart';

class CurseForgeHandler {
static Text parseReleaseType(CurseForgeFileReleaseType releaseType) {
Expand Down Expand Up @@ -48,13 +49,15 @@ class CurseForgeHandler {

static Future<CurseForgeModFile?> needUpdates(
int curseID, String versionID, ModLoader loader, int hash) async {
final List<CurseForgeModFile> files =
List<CurseForgeModFile> files =
(await RPMTWApiClient.instance.curseforgeResource.getModFiles(curseID,
gameVersion: versionID,
modLoaderType: loader.toCurseForgeType()))
.where((e) => e.gameVersions.contains(versionID))
.toList();

files = filterModFiles(files, versionID, loader);

if (files.isEmpty) return null;
final file = files.first;
if (file.fileFingerprint != hash) {
Expand Down Expand Up @@ -118,4 +121,41 @@ class CurseForgeHandler {
return const RWLLoading();
}
}

static List<CurseForgeModFile> filterModFiles(
List<CurseForgeModFile> files, String gameVersion, ModLoader loader,
{bool strict = true}) {
List<CurseForgeModFile> result = [];

for (final file in files) {
bool isValid = CurseForgeHandler.filterVersion(
file.gameVersions, gameVersion, loader, strict);

if (isValid) result.add(file);
}

// if strict filtering mode cannot find any files, then use non-strict filtering mode
if (result.isEmpty && strict) {
return filterModFiles(files, gameVersion, loader, strict: false);
}

return result;
}

static bool filterVersion(List<String> versions, String gameVersion,
ModLoader loader, bool strict) {
// mod loader name the first char is capitalized
// eg. forge -> Forge, fabric -> Fabric etc.
String loaderName = loader.name.toCapitalized();

if (strict &&
versions.contains(gameVersion) &&
versions.contains(loaderName)) {
return true;
} else if (!strict && versions.any((v) => v.contains(gameVersion))) {
return true;
} else {
return false;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import 'package:rpmlauncher/util/util.dart';
import 'package:rpmtw_api_client/rpmtw_api_client.dart' hide ModLoader;
import 'package:rpmtw_dart_common_library/rpmtw_dart_common_library.dart';

import 'rwl_loading.dart';
import '../widget/rwl_loading.dart';

class CurseForgeModVersion extends StatefulWidget {
final int curseID;
Expand Down Expand Up @@ -44,23 +44,17 @@ class _CurseForgeModVersionState extends State<CurseForgeModVersion> {
@override
Widget build(BuildContext context) {
return FutureBuilder<List<CurseForgeModFile>>(
future: RPMTWApiClient.instance.curseforgeResource
.getModFiles(widget.curseID),
future: RPMTWApiClient.instance.curseforgeResource.getModFiles(
widget.curseID,
gameVersion: widget.instanceConfig.version,
modLoaderType: widget.instanceConfig.loaderEnum.toCurseForgeType()),
builder: (context, snapshot) {
if (snapshot.hasData) {
List<CurseForgeModFile> files = [];
final String gameVersion = widget.instanceConfig.version;
final String loader =
widget.instanceConfig.loaderEnum.name.toCapitalized();
List<CurseForgeModFile> files = CurseForgeHandler.filterModFiles(
snapshot.data!,
widget.instanceConfig.version,
widget.instanceConfig.loaderEnum);

snapshot.data!.forEach((file) {
//過濾版本
List<String> gameVersions = file.gameVersions;
if (gameVersions.any((v) => v == gameVersion) &&
gameVersions.any((v) => v == loader)) {
files.add(file);
}
});
files.sort((a, b) => DateTime.parse(b.fileDate)
.compareTo(DateTime.parse(a.fileDate)));

Expand All @@ -72,16 +66,15 @@ class _CurseForgeModVersionState extends State<CurseForgeModVersion> {
width: MediaQuery.of(context).size.width / 3,
child: ListView.builder(
itemCount: files.length,
itemBuilder:
(BuildContext fileBuildContext, int fileIndex) {
itemBuilder: (BuildContext context, int fileIndex) {
CurseForgeModFile file = files[fileIndex];

return ListTile(
leading: FutureBuilder(
leading: FutureBuilder<Widget>(
future: installedWidget(file),
builder: (context, AsyncSnapshot snapshot) {
builder: (context, snapshot) {
if (snapshot.hasData) {
return snapshot.data;
return snapshot.data!;
} else {
return const CircularProgressIndicator();
}
Expand Down
2 changes: 1 addition & 1 deletion lib/pages/curseforge_mods_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import 'package:rpmlauncher/model/Game/instance.dart';
import 'package:rpmlauncher/model/Game/mod_info.dart';
import 'package:rpmlauncher/pages/curseforge_addon_page.dart';
import 'package:rpmlauncher/util/I18n.dart';
import 'package:rpmlauncher/widget/CurseForgeModVersion.dart';
import 'package:rpmlauncher/pages/curseforge_mod_version.dart';
import 'package:rpmtw_api_client/rpmtw_api_client.dart';

class CurseForgeModsPage extends StatefulWidget {
Expand Down
2 changes: 1 addition & 1 deletion lib/screen/RecommendedModpackScreen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import 'package:rpmlauncher/widget/AddInstance.dart';
import 'package:rpmlauncher/widget/RPMNetworkImage.dart';
import 'package:rpmlauncher/widget/rwl_loading.dart';
import 'package:rpmlauncher/widget/WIPWidget.dart';
import 'package:rpmlauncher/widget/CurseForgeModVersion.dart'
import 'package:rpmlauncher/pages/curseforge_mod_version.dart'
as curseforge_version;
import 'package:rpmlauncher/util/data.dart';
import 'package:rpmtw_api_client/rpmtw_api_client.dart';
Expand Down
2 changes: 1 addition & 1 deletion lib/util/theme.dart
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ class ThemeUtility {
style: ElevatedButton.styleFrom(
primary: const Color.fromARGB(190, 86, 110, 244),
onPrimary: Colors.white)),
useMaterial3: true),
useMaterial3: false),
ThemeUtility.toInt(Themes.dark): ThemeData(
brightness: Brightness.dark,
fontFamily: 'font',
Expand Down
4 changes: 3 additions & 1 deletion lib/util/util.dart
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,9 @@ class Util {
snapshotPattern.allMatches(sourceVersion).toList().first;

String praseRelease(int year, int week) {
if (year == 22 && week >= 11 && week <= 19) {
if (year == 22 && week == 24) {
return '1.19.1';
} else if (year == 22 && week >= 11 && week <= 19) {
return '1.19.0';
} else if (year == 22 && week >= 3 && week <= 7) {
return '1.18.2';
Expand Down

0 comments on commit 75a4121

Please sign in to comment.