Skip to content
This repository has been archived by the owner on Dec 10, 2020. It is now read-only.

Commit

Permalink
new: Added Official Download Manager Support
Browse files Browse the repository at this point in the history
  • Loading branch information
FlashyReese committed Aug 13, 2020
1 parent 002caa8 commit d046032
Show file tree
Hide file tree
Showing 10 changed files with 75 additions and 32 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Fabric Mod Manager
version = 0.0.8-RC4
version = 0.0.8
group = me.flashyreese

# Dependencies
Expand Down
7 changes: 2 additions & 5 deletions src/main/java/me/flashyreese/common/util/FileUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,11 @@ public static File changeExtension(File file, String extension) {
return newFile;
}

public static String getFileNameWithExtension(File file) throws Exception {
if (file.isDirectory()){
throw new Exception("Not a valid file");
}
public static String getFileNameWithExtension(File file) {
return file.getName();
}

public static String getFileName(File file) throws Exception {
public static String getFileName(File file) {
String fileNameWithExtension = getFileNameWithExtension(file);
if(fileNameWithExtension.contains(".")){
return fileNameWithExtension.substring(0, fileNameWithExtension.lastIndexOf('.'));
Expand Down
29 changes: 14 additions & 15 deletions src/main/java/me/flashyreese/fabricmm/api/RepositoryManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public RepositoryManager(File repositoryCache, String repositoryUrl) throws Exce
this.users = new ArrayList<>();
}

private void loadLocalRepositories(boolean force) throws Exception {
private void loadLocalRepositories(boolean force) throws IOException {
users.clear();
Moshi moshi = new Moshi.Builder().build();
Type type = Types.newParameterizedType(List.class, User.class);
Expand All @@ -49,8 +49,8 @@ private void loadLocalRepositories(boolean force) throws Exception {
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
String repository = in.lines().collect(Collectors.joining());
List<User> usersRepository = adapter.fromJson(repository);
/*final ExecutorService executor = Executors.newFixedThreadPool(4);
final List<Future<?>> futures = new ArrayList<>();*/
final ExecutorService executor = Executors.newFixedThreadPool(4);
final List<Future<?>> futures = new ArrayList<>();
assert usersRepository != null;
for (User user: usersRepository){
for (Project project: user.getProjects()){
Expand All @@ -66,35 +66,34 @@ private void loadLocalRepositories(boolean force) throws Exception {
fileReader.close();
List<MinecraftVersion> minecraftVersions = jsonAdapter.fromJson(projectJson);
project.setMinecraftVersions(minecraftVersions);
loadProjectInfo(project);
loadProjectInfo(project);//Fixme: Saved Locally CurseForge doesn't like many request
}else{
/*Future<?> future = executor.submit(() -> {
Future<?> future = executor.submit(() -> {
try {
downloadProjectFile(project);
} catch (Exception e) {
e.printStackTrace();
}
});
futures.add(future);*/
downloadProjectFile(project);
futures.add(future);
}
}else{
List<MinecraftVersion> minecraftVersions = new ArrayList<>();
project.setMinecraftVersions(minecraftVersions);
}
}
}
/*try {
try {
for (Future<?> future : futures) {
future.get();
}
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}*/
}
users.addAll(usersRepository);
}

private File getProjectFile(int curseForgeId) throws Exception {
private File getProjectFile(int curseForgeId) {
for (File file: Objects.requireNonNull(this.repositoryCache.listFiles())){
if (file.isFile() && file.getName().endsWith(".json") && FileUtil.getFileName(file).equals(String.valueOf(curseForgeId))){
return file;
Expand All @@ -103,7 +102,7 @@ private File getProjectFile(int curseForgeId) throws Exception {
return null;
}

private void downloadProjectFile(Project project) throws Exception {
private void downloadProjectFile(Project project) throws IOException {
CurseAddon curseAddon = getCurseAddonFromProjectID(project.getCurseForgeProject(), true);
if (curseAddon.getDefaultCurseAttachment() != null && curseAddon.getDefaultCurseAttachment().getThumbnailUrl() != null){
project.setIconUrl(curseAddon.getDefaultCurseAttachment().getThumbnailUrl());
Expand All @@ -120,7 +119,7 @@ private void downloadProjectFile(Project project) throws Exception {
project.setMinecraftVersions(minecraftVersions);
}

private void loadProjectInfo(Project project) throws Exception {
private void loadProjectInfo(Project project) throws IOException {
CurseAddon curseAddon = getCurseAddonFromProjectID(project.getCurseForgeProject(), false);
project.setProjectUrl(curseAddon.getWebsiteUrl());
}
Expand All @@ -129,7 +128,7 @@ public List<MinecraftVersion> convertCurseAddonToMinecraftFiles(CurseAddon curse
return convertCurseFilesToMinecraftVersions(curseAddon.getFiles());
}

private CurseAddon getCurseAddon(String json, boolean includesFiles) throws Exception {
private CurseAddon getCurseAddon(String json, boolean includesFiles) throws IOException {
Moshi moshi = new Moshi.Builder().build();
CurseAddon curseAddon = moshi.adapter(CurseAddon.class).fromJson(json);
assert curseAddon != null;
Expand All @@ -139,7 +138,7 @@ private CurseAddon getCurseAddon(String json, boolean includesFiles) throws Exce
return curseAddon;
}

private void processCurseFiles(CurseAddon curseAddon) throws Exception {
private void processCurseFiles(CurseAddon curseAddon) throws IOException {
URL url = new URL(String.format("https://addons-ecs.forgesvc.net/api/v2/addon/%s/files", curseAddon.getId()));
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
String filesJson = in.lines().collect(Collectors.joining());
Expand All @@ -161,7 +160,7 @@ private void processCurseFiles(CurseAddon curseAddon) throws Exception {
}
}

private CurseAddon getCurseAddonFromProjectID(long id, boolean includeFiles) throws Exception {
private CurseAddon getCurseAddonFromProjectID(long id, boolean includeFiles) throws IOException {
URL url = new URL(String.format("https://addons-ecs.forgesvc.net/api/v2/addon/%s", id));
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
String addonJson = in.lines().collect(Collectors.joining());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public String getGameVersionDateReleased() {
}

public void removeFabricFromGameVersion(){
getGameVersion().remove("Fabric");
//getGameVersion().remove("Fabric");//Fixme: CurseForge takes a while
getGameVersion().remove("Forge");//Some people are just weird
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ private void initComponents() throws Exception {
contentPane.setPreferredSize(new Dimension(16* SCALE, 9* SCALE));

library = new LibraryManagerUI(contentPane);
modBrowser = new ModRepositoryBrowserUI(contentPane, repositoryManager, trayIcon);
downloadManager = new DownloadManagerUI(contentPane);
downloadManager = new DownloadManagerUI(contentPane, trayIcon);
modBrowser = new ModRepositoryBrowserUI(contentPane, repositoryManager, downloadManager, trayIcon);
settings = new SettingsUI(contentPane);

menuBar = new FabricModManagerMenuBar(this, repositoryManager, library, modBrowser, i18nManager);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;

import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.border.LineBorder;
import javax.swing.filechooser.FileSystemView;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,31 @@
package me.flashyreese.fabricmm.ui.tab;

import me.flashyreese.common.i18n.I18nText;
import me.flashyreese.common.i18n.ParsableI18nText;
import me.flashyreese.fabricmm.core.ConfigurationManager;
import me.flashyreese.fabricmm.schema.InstalledMod;
import me.flashyreese.fabricmm.schema.MinecraftInstance;
import me.flashyreese.fabricmm.ui.components.ModFileDropList;
import me.flashyreese.fabricmm.util.Dim2i;
import me.flashyreese.fabricmm.util.Util;

import javax.swing.*;
import java.awt.*;
import java.io.File;
import java.io.IOException;
import java.nio.file.*;

public class DownloadManagerUI extends JPanel {

private ModFileDropList downloadModFileDropList;
private JComboBox<MinecraftInstance> minecraftInstances;
private JButton install;

public DownloadManagerUI(JTabbedPane jTabbedPane) throws Exception {
public DownloadManagerUI(JTabbedPane jTabbedPane, TrayIcon trayIcon) throws Exception {
setLayout(null);
setSize(new Dimension((int)jTabbedPane.getPreferredSize().getWidth() - 5, (int)jTabbedPane.getPreferredSize().getHeight() - 28));//Fixme: Jank AF
initComponents();
setupComponents();
setupComponents(trayIcon);
loadComponents();
updateComponentsText();
}
Expand All @@ -31,11 +36,12 @@ private void initComponents() {
install = new JButton();
}

private void setupComponents() {
private void setupComponents(TrayIcon trayIcon) throws Exception {
Dim2i downloadModFileDropListDim = new Dim2i(10, 10, this.getWidth() - 20, this.getHeight() - 60);
downloadModFileDropList.setLocation(downloadModFileDropListDim.getOriginX(), downloadModFileDropListDim.getOriginY());
downloadModFileDropList.setSize(downloadModFileDropListDim.getWidth(), downloadModFileDropListDim.getHeight());
downloadModFileDropList.setDirectory(ConfigurationManager.getInstance().MOD_CACHE_DIR);
downloadModFileDropList.reloadMods();
downloadModFileDropList.getList().addListSelectionListener(arg0 -> onModFileDropListSelect());

Dim2i minecraftInstancesDim = new Dim2i(10, this.getHeight() - 40, this.getWidth() / 4 * 3 - 30, 30);
Expand All @@ -54,6 +60,13 @@ public Component getListCellRendererComponent(JList list, Object value, int inde
});
Dim2i installDim = new Dim2i(this.getWidth() / 4 * 3 - 10, this.getHeight() - 40, this.getWidth() / 4, 30);
install.setBounds(installDim.getOriginX(), installDim.getOriginY(), installDim.getWidth(), installDim.getHeight());
install.addActionListener(e -> {
try {
onInstallMod(trayIcon);
} catch (IOException ioException) {
ioException.printStackTrace();
}
});
}

private void loadComponents() {
Expand All @@ -70,4 +83,27 @@ public void updateComponentsText(){
private void onModFileDropListSelect(){
install.setEnabled(downloadModFileDropList.getSelectedValue() != null);
}

private void onInstallMod(TrayIcon trayIcon) throws IOException {
if (downloadModFileDropList.getSelectedValue() != null){
InstalledMod mod = downloadModFileDropList.getSelectedValue();
File currentLocation = new File(mod.getInstalledPath());
MinecraftInstance instance = (MinecraftInstance) minecraftInstances.getSelectedItem();
File newLocation = new File(instance.getDirectory(), currentLocation.getName());
Path FROM = Paths.get(currentLocation.getAbsolutePath());
Path TO = Paths.get(newLocation.getAbsolutePath());
CopyOption[] options = new CopyOption[]{
StandardCopyOption.REPLACE_EXISTING,
StandardCopyOption.COPY_ATTRIBUTES
};
Files.copy(FROM, TO, options);
trayIcon.displayMessage(new I18nText("fmm.download_manager.tray_icon.install_complete.caption").toString(),
new ParsableI18nText("fmm.download_manager.tray_icon.install_complete.text",
mod.getModMetadata().getName(), instance.getName()).toString(), TrayIcon.MessageType.INFO);
}
}

public void refreshMods() throws Exception {
downloadModFileDropList.reloadMods();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,11 @@ public class ModRepositoryBrowserUI extends JPanel{
private JLabel authorName;
private ArrayList<JButton> authorContacts;

public ModRepositoryBrowserUI(JTabbedPane jTabbedPane, RepositoryManager repositoryManager, TrayIcon trayIcon) {
public ModRepositoryBrowserUI(JTabbedPane jTabbedPane, RepositoryManager repositoryManager, DownloadManagerUI downloadManager, TrayIcon trayIcon) {
setLayout(null);
setSize(new Dimension((int)jTabbedPane.getPreferredSize().getWidth() - 5, (int)jTabbedPane.getPreferredSize().getHeight() - 28));//Fixme: Jank AF
initComponents();
setupComponents(repositoryManager, trayIcon);
setupComponents(repositoryManager, downloadManager, trayIcon);
loadComponents();
updateComponentsText();
}
Expand Down Expand Up @@ -90,7 +90,7 @@ private void initComponents() {
authorName = new JLabel();
}

private void setupComponents(RepositoryManager repositoryManager, TrayIcon trayIcon){
private void setupComponents(RepositoryManager repositoryManager, DownloadManagerUI downloadManager, TrayIcon trayIcon){
Dim2i searchBarDim = new Dim2i(10, 10, this.getWidth() / 8 * 3 - 20, 30);
searchBar.setBounds(searchBarDim.getOriginX(), searchBarDim.getOriginY(), searchBarDim.getWidth(), searchBarDim.getHeight());
updateModList(repositoryManager);
Expand Down Expand Up @@ -142,7 +142,7 @@ public Component getListCellRendererComponent(JList list, Object value, int inde
download.setBounds(downloadDim.getOriginX(), downloadDim.getOriginY(), downloadDim.getWidth(), downloadDim.getHeight());
download.addActionListener(e -> {
try {
downloadMod(trayIcon);
downloadMod(downloadManager, trayIcon);
} catch (MalformedURLException | FileNotFoundException malformedURLException) {
malformedURLException.printStackTrace();
}
Expand Down Expand Up @@ -363,7 +363,7 @@ private void updateModVersions(){
}
}

private void downloadMod(TrayIcon trayIcon) throws MalformedURLException, FileNotFoundException {//Fixme: literally
private void downloadMod(DownloadManagerUI downloadManager, TrayIcon trayIcon) throws MalformedURLException, FileNotFoundException {//Fixme: literally
if(projectList.getSelectedValue() != null && minecraftVersion.getSelectedItem() != null && modVersion.getSelectedItem() != null){
Project project = projectList.getSelectedValue();
MinecraftVersion mcVer = (MinecraftVersion) minecraftVersion.getSelectedItem();
Expand All @@ -380,6 +380,11 @@ public void onComplete() {
trayIcon.displayMessage(new ParsableI18nText("fmm.mod_browser.tray_icon.download_complete.caption",
project.getName(), modVer.getModVersion(), mcVer.getMinecraftVersion()).toString(),
new I18nText("fmm.mod_browser.tray_icon.download_complete.text").toString(), TrayIcon.MessageType.INFO);
try {
downloadManager.refreshMods();
} catch (Exception e) {
e.printStackTrace();
}
}

public void onCancel() {
Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/assets/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@
"fmm.mod_browser.tray_icon.download_complete.caption":"{1} {2} for Minecraft {3}",
"fmm.download_manager":"Download Manager",
"fmm.download_manager.install":"Install",
"fmm.download_manager.tray_icon.install_complete.caption":"Installation Complete!",
"fmm.download_manager.tray_icon.install_complete.text":"{1} has been installed to {2}",
"fmm.settings":"Settings",
"fmm.settings.mmc_path":"MultiMC Directory:",
"fmm.installed_mod.minecraft_version.not_available":"Not Available!",
Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/assets/lang/es_mx.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@
"fmm.mod_browser.tray_icon.download_complete.caption":"{1} {2} para Minecraft {3}",
"fmm.download_manager":"Gestor de descargas",
"fmm.download_manager.install":"Instalar",
"fmm.download_manager.tray_icon.install_complete.caption":"¡Instalación completa!",
"fmm.download_manager.tray_icon.install_complete.text":"{1} se ha instalado en {2}",
"fmm.settings":"Ajustes",
"fmm.settings.mmc_path":"Directorio MultiMC:",
"fmm.installed_mod.minecraft_version.not_available":"¡No disponible!",
Expand Down

0 comments on commit d046032

Please sign in to comment.