Skip to content
This repository has been archived by the owner on Apr 29, 2024. It is now read-only.

Commit

Permalink
More refactoring
Browse files Browse the repository at this point in the history
Moving any and all downloading to DownloadHelper, downloading will only go to cache and be moved from the cache to the download location folder NOCI
  • Loading branch information
Nincodedo committed May 31, 2016
1 parent 894179a commit 34c8e4e
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 66 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.nincraft.modpackdownloader.container;

import com.google.common.base.Strings;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
import lombok.Data;
Expand All @@ -20,4 +21,11 @@ public abstract class DownloadableFile {
private String folder;
private String fileName;
private String downloadUrl;

public String getFileName(){
if(!Strings.isNullOrEmpty(getRename())){
return getRename();
}
return this.fileName;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,8 @@ public class ModLoader extends DownloadableFile {
public String getRename(boolean downloadInstaller) {
return downloadInstaller ? getRenameInstaller() : getRenameUniversal();
}

public String getForgeId(){
return getId().substring(getId().indexOf("-") + 1);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,22 +44,18 @@ private static void downloadCurseMod(final CurseFile mod) {
return;
}

val modName = mod.getName();

try {
val fileName = !Strings.isNullOrEmpty(mod.getRename()) ? mod.getRename()
: getCurseForgeDownloadLocation(mod.getDownloadUrl(), modName, modName);
mod.setFileName(fileName);

mod.setFileName(getCurseForgeDownloadLocation(mod));
DownloadHelper.downloadFile(mod);
} catch (final IOException e) {
log.error(e.getMessage());
}
}

private static String getCurseForgeDownloadLocation(final String url, final String projectName,
final String downloadLocation) throws IOException {
String encodedDownloadLocation = URLHelper.encodeSpaces(downloadLocation);
private static String getCurseForgeDownloadLocation(final CurseFile mod) throws IOException {
String url = mod.getDownloadUrl();
String projectName = mod.getName();
String encodedDownloadLocation = URLHelper.encodeSpaces(projectName);

if (!encodedDownloadLocation.contains(Reference.JAR_FILE_EXT)) {
val newUrl = url + Reference.COOKIE_TEST_1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@ protected static void downloadFile(final DownloadableFile downloadableFile) {
return;
}

FileSystemHelper.copyToLocalRepo(downloadableFile.getName(), downloadedFile);
} else {
FileSystemHelper.copyFromLocalRepo(downloadableFile.getName(), decodedFileName, downloadableFile.getFolder());
}
FileSystemHelper.copyFromLocalRepo(downloadableFile, decodedFileName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,28 +28,25 @@ public static void downloadForge(String minecraftVersion, List<ModLoader> modLoa
}

for (ModLoader modLoader : modLoaders) {
String forgeVersion = modLoader.getId();
String folder = modLoader.getFolder();
String forgeId = forgeVersion.substring(forgeVersion.indexOf("-") + 1);

log.info(String.format("Downloading Forge version %s", forgeVersion));
log.info(String.format("Downloading Forge version %s", modLoader.getId()));

if (BooleanUtils.isTrue(modLoader.getDownloadInstaller())) {
downloadForgeFile(minecraftVersion, modLoader, folder, forgeId, true);
downloadForgeFile(minecraftVersion, modLoader, true);
}
if (BooleanUtils.isTrue(modLoader.getDownloadUniversal())) {
downloadForgeFile(minecraftVersion, modLoader, folder, forgeId, false);
downloadForgeFile(minecraftVersion, modLoader, false);
}
}
}

private static void downloadForgeFile(String minecraftVersion, ModLoader modLoader, String folder, String forgeId, boolean downloadInstaller) {
downloadForgeFile(minecraftVersion, modLoader, folder, forgeId, downloadInstaller, false);
private static void downloadForgeFile(String minecraftVersion, ModLoader modLoader, boolean downloadInstaller) {
downloadForgeFile(minecraftVersion, modLoader, downloadInstaller, true);
}

private static void downloadForgeFile(String minecraftVersion, ModLoader modLoader, String folder, String forgeId, boolean downloadInstaller, boolean alternateDownloadUrl) {
String forgeFileName = "forge-" + minecraftVersion + "-" + forgeId;
String forgeURL = Reference.forgeURL + minecraftVersion + "-" + forgeId;
private static void downloadForgeFile(String minecraftVersion, ModLoader modLoader, boolean downloadInstaller, boolean alternateDownloadUrl) {
String forgeFileName = "forge-" + minecraftVersion + "-" + modLoader.getForgeId();
String forgeURL = Reference.forgeURL + minecraftVersion + "-" + modLoader.getForgeId();
if (alternateDownloadUrl) {
forgeFileName += "-" + minecraftVersion;
forgeURL += "-" + minecraftVersion;
Expand All @@ -61,24 +58,24 @@ private static void downloadForgeFile(String minecraftVersion, ModLoader modLoad
if (!FileSystemHelper.isInLocalRepo("forge", forgeFileName) || Reference.forceDownload) {
File downloadedFile;
if (modLoader.getRename(downloadInstaller) != null) {
downloadedFile = FileSystemHelper.getDownloadedFile(modLoader.getRename(downloadInstaller), folder);
downloadedFile = FileSystemHelper.getDownloadedFile(modLoader.getRename(downloadInstaller), modLoader.getFolder());
} else {
downloadedFile = FileSystemHelper.getDownloadedFile(forgeFileName, folder);
downloadedFile = FileSystemHelper.getDownloadedFile(forgeFileName, modLoader.getFolder());
}

try {
FileUtils.copyURLToFile(new URL(forgeURL), downloadedFile);
} catch (final IOException e) {
log.error(String.format("Could not download %s.", forgeFileName), e.getMessage());
if (!alternateDownloadUrl) {
if (alternateDownloadUrl) {
log.warn("Attempting alternate Forge download URL");
downloadForgeFile(minecraftVersion, modLoader, folder, forgeId, downloadInstaller, true);
downloadForgeFile(minecraftVersion, modLoader, downloadInstaller, false);
}
return;
}
FileSystemHelper.copyToLocalRepo("forge", downloadedFile, forgeFileName);
//FileSystemHelper.copyToLocalRepo("forge", downloadedFile, forgeFileName);
} else {
FileSystemHelper.copyFromLocalRepo("forge", forgeFileName, folder, modLoader.getRename(downloadInstaller));
FileSystemHelper.copyFromLocalRepo("forge", forgeFileName, modLoader.getFolder(), modLoader.getRename(downloadInstaller));
}
log.info(String.format("Completed downloading Forge version %s", forgeFileName));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package com.nincraft.modpackdownloader.util;

import java.io.File;
import java.io.IOException;

import org.apache.commons.io.FileUtils;

import lombok.val;
import com.google.common.base.Strings;
import com.nincraft.modpackdownloader.container.DownloadableFile;
import lombok.experimental.UtilityClass;
import lombok.extern.log4j.Log4j2;
import lombok.val;
import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;

@Log4j2
@UtilityClass
Expand All @@ -22,42 +23,17 @@ public static void createFolder(final String folder) {
}
}

public static void copyToLocalRepo(final String projectName, final File downloadedFile) {
val newProjectName = getProjectNameOrDefault(projectName);

try {
FileUtils.copyFileToDirectory(downloadedFile, new File(Reference.userhome + newProjectName));
} catch (final IOException e) {
log.error(String.format("Could not copy %s to local repo.", newProjectName), e);
}
}

public static void copyToLocalRepo(final String projectName, File downloadedFile, String originalName) {
val newProjectName = getProjectNameOrDefault(projectName);

try {
File copyFile = getLocalFile(downloadedFile.getName(), newProjectName);
FileUtils.copyFileToDirectory(downloadedFile, new File(Reference.userhome + newProjectName));
copyFile.renameTo(new File(copyFile.getParent() + File.separator + originalName));
} catch (final IOException e) {
log.error(String.format("Could not copy %s to local repo.", newProjectName), e);
}
}

public static void copyFromLocalRepo(final String projectName, final String fileName, String folder) {
copyFromLocalRepo(projectName, fileName, folder, null);
}

public static void copyFromLocalRepo(final String projectName, final String fileName, String folder, String rename) {
val newProjectName = getProjectNameOrDefault(projectName);
if (folder == null) {
public static void copyFromLocalRepo(final DownloadableFile downloadableFile, final String fileName) {
val newProjectName = getProjectNameOrDefault(downloadableFile.getName());
String folder = "";
if (Strings.isNullOrEmpty(downloadableFile.getFolder())) {
folder = Reference.modFolder;
}
try {
FileUtils.copyFileToDirectory(getLocalFile(fileName, newProjectName), new File(folder));
File downloadedFile = getDownloadedFile(fileName, folder);
if (rename != null) {
downloadedFile.renameTo(new File(downloadedFile.getParent() + File.separator + rename));
if (!Strings.isNullOrEmpty(downloadableFile.getRename())) {
downloadedFile.renameTo(new File(downloadedFile.getParent() + File.separator + downloadableFile.getRename()));
}
} catch (final IOException e) {
log.error(String.format("Could not copy %s from local repo.", newProjectName), e);
Expand Down

0 comments on commit 34c8e4e

Please sign in to comment.