Deny access to CurseForge without api key (#1858)

This commit is contained in:
Glavo 2022-11-23 16:32:07 +08:00 committed by GitHub
parent 024c90d688
commit 7ee81f7425
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 28 additions and 8 deletions

View File

@ -39,6 +39,7 @@ import org.jackhuang.hmcl.ui.animation.ContainerAnimations;
import org.jackhuang.hmcl.ui.animation.TransitionPane;
import org.jackhuang.hmcl.ui.construct.AdvancedListBox;
import org.jackhuang.hmcl.ui.construct.MessageDialogPane;
import org.jackhuang.hmcl.ui.construct.TabControl;
import org.jackhuang.hmcl.ui.construct.TabHeader;
import org.jackhuang.hmcl.ui.decorator.DecoratorAnimatedPage;
import org.jackhuang.hmcl.ui.decorator.DecoratorPage;
@ -128,19 +129,19 @@ public class DownloadPage extends DecoratorAnimatedPage implements DecoratorPage
item.setTitle(i18n("resourcepack"));
item.setLeftGraphic(wrap(SVG::textureBox));
item.activeProperty().bind(tab.getSelectionModel().selectedItemProperty().isEqualTo(resourcePackTab));
item.setOnAction(e -> tab.select(resourcePackTab));
item.setOnAction(e -> selectTabIfCurseForgeAvailable(resourcePackTab));
})
// .addNavigationDrawerItem(item -> {
// item.setTitle(i18n("download.curseforge.customization"));
// item.setLeftGraphic(wrap(SVG::script));
// item.activeProperty().bind(tab.getSelectionModel().selectedItemProperty().isEqualTo(customizationTab));
// item.setOnAction(e -> tab.select(customizationTab));
// item.setOnAction(e -> selectTabIfCurseForgeAvailable(customizationTab));
// })
.addNavigationDrawerItem(item -> {
item.setTitle(i18n("world"));
item.setLeftGraphic(wrap(SVG::earth));
item.activeProperty().bind(tab.getSelectionModel().selectedItemProperty().isEqualTo(worldTab));
item.setOnAction(e -> tab.select(worldTab));
item.setOnAction(e -> selectTabIfCurseForgeAvailable(worldTab));
});
FXUtils.setLimitWidth(sideBar, 200);
setLeft(sideBar);
@ -149,7 +150,14 @@ public class DownloadPage extends DecoratorAnimatedPage implements DecoratorPage
setCenter(transitionPane);
}
private <T extends Node> Supplier<T> loadVersionFor(Supplier<T> nodeSupplier) {
private void selectTabIfCurseForgeAvailable(TabControl.Tab<?> newTab) {
if (CurseForgeRemoteModRepository.isAvailable())
tab.select(newTab);
else
Controllers.dialog(i18n("download.curseforge.unavailable"));
}
private static <T extends Node> Supplier<T> loadVersionFor(Supplier<T> nodeSupplier) {
return () -> {
T node = nodeSupplier.get();
if (node instanceof VersionPage.VersionLoadable) {
@ -159,7 +167,7 @@ public class DownloadPage extends DecoratorAnimatedPage implements DecoratorPage
};
}
private void download(Profile profile, @Nullable String version, RemoteMod.Version file, String subdirectoryName) {
private static void download(Profile profile, @Nullable String version, RemoteMod.Version file, String subdirectoryName) {
if (version == null) version = profile.getSelectedVersion();
Path runDirectory = profile.getRepository().hasVersion(version) ? profile.getRepository().getRunDirectory(version).toPath() : profile.getRepository().getBaseDirectory().toPath();

View File

@ -32,7 +32,10 @@ public class ModDownloadListPage extends DownloadListPage {
supportChinese.set(true);
downloadSources.get().setAll("mods.curseforge", "mods.modrinth");
downloadSource.set("mods.curseforge");
if (CurseForgeRemoteModRepository.isAvailable())
downloadSource.set("mods.curseforge");
else
downloadSource.set("mods.modrinth");
}
private class Repository extends LocalizedRemoteModRepository {

View File

@ -32,7 +32,10 @@ public class ModpackDownloadListPage extends DownloadListPage {
supportChinese.set(true);
downloadSources.get().setAll("mods.curseforge", "mods.modrinth");
downloadSource.set("mods.curseforge");
if (CurseForgeRemoteModRepository.isAvailable())
downloadSource.set("mods.curseforge");
else
downloadSource.set("mods.modrinth");
}
private class Repository extends LocalizedRemoteModRepository {

View File

@ -308,6 +308,7 @@ download=Download
download.code.404=File not found on the remote server\: %s
download.content=Addons
download.curseforge.customization=Light and shadow, and game customization
download.curseforge.unavailable=HMCL nightly build does not support access to CurseForge, please use stable version or beta version to download.
download.existing=The file cannot be saved because it already exists. You can use 'Save As' to save the file elsewhere.
download.external_link=Open Website
download.failed=Failed to download %1$s, response code\: %2$d

View File

@ -295,6 +295,7 @@ download=下載
download.code.404=遠端伺服器沒有需要下載的檔案: %s
download.content=遊戲內容
download.curseforge.customization=光影與遊戲定制
download.curseforge.unavailable=HMCL 預覽版暫不支持訪問 CurseForge請使用穩定版或測試版進行下載。
download.existing=檔案已存在,無法保存。你可以選擇另存為將檔案保存至其他地方。
download.external_link=打開下載網站
download.failed=下載失敗: %1$s錯誤碼%2$d

View File

@ -295,6 +295,7 @@ download=下载
download.code.404=远程服务器不包含需要下载的文件: %s
download.content=游戏内容
download.curseforge.customization=光影与游戏定制
download.curseforge.unavailable=HMCL 预览版暂不支持访问 CurseForge请使用稳定版或测试版进行下载。
download.existing=文件已存在,无法保存。你可以在模组选择栏中的右侧按钮另存为将文件保存至其他地方。
download.external_link=打开下载网站
download.failed=下载失败: %1$s错误码%2$d

View File

@ -40,9 +40,12 @@ import static org.jackhuang.hmcl.util.Pair.pair;
public final class CurseForgeRemoteModRepository implements RemoteModRepository {
private static final String PREFIX = "https://api.curseforge.com";
private static final String apiKey = System.getProperty("hmcl.curseforge.apikey", JarUtils.getManifestAttribute("CurseForge-Api-Key", ""));
public static boolean isAvailable() {
return !apiKey.isEmpty();
}
private final Type type;
private final int section;