Skip to content

Commit

Permalink
1.8.1
Browse files Browse the repository at this point in the history
Performance: Drastically improve mod pack extraction performance

Add built in mods system for some weird modrinth modpacks
  • Loading branch information
FlowArg committed Aug 30, 2023
1 parent 8f791e8 commit 1fbc52d
Show file tree
Hide file tree
Showing 12 changed files with 90 additions and 25 deletions.
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ apply plugin: 'maven-publish'
apply plugin: 'signing'

group 'fr.flowarg'
version '1.8.0'
version '1.8.1'
archivesBaseName = "flowupdater"

java {
Expand Down Expand Up @@ -35,7 +35,7 @@ dependencies {

// Only for internal tests
testImplementation 'fr.flowarg:openlauncherlib:3.2.6'
testImplementation 'org.junit.jupiter:junit-jupiter:5.9.3'
testImplementation 'org.junit.jupiter:junit-jupiter:5.10.0'
}

artifacts {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/fr/flowarg/flowupdater/FlowUpdater.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
public class FlowUpdater
{
/** FlowUpdater's version string constant */
public static final String FU_VERSION = "1.8.0";
public static final String FU_VERSION = "1.8.1";

/** Vanilla version's object to update/install */
private final VanillaVersion vanillaVersion;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ public void loadModrinthIntegration(Path dir, IModrinthFeaturesUser modrinthFeat
this.progressCallback.step(Step.MOD_PACK);
final ModrinthModPack modPack = modrinthIntegration.getCurseModPack(modPackInfo);
this.logger.info(String.format("Loading mod pack: %s (%s).", modPack.getName(), modPack.getVersion()));
modrinthFeaturesUser.setModrinthModPack(modPack);

for (Mod mod : modPack.getMods())
this.checkMod(mod, allModrinthMods, dir);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.util.*;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.Executors;
Expand Down Expand Up @@ -309,13 +310,7 @@ private void transferAndClose(@NotNull Path flPath, ZipFile zipFile, ZipEntry en
if(Files.notExists(flPath.getParent()))
Files.createDirectories(flPath.getParent());

try(OutputStream pathStream = Files.newOutputStream(flPath);
BufferedOutputStream fo = new BufferedOutputStream(pathStream);
InputStream is = zipFile.getInputStream(entry)) {

while (is.available() > 0)
fo.write(is.read());
}
Files.copy(zipFile.getInputStream(entry), flPath, StandardCopyOption.REPLACE_EXISTING);
}

private static class ProjectMod extends CurseFileInfo
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,18 @@ public interface IModrinthFeaturesUser
*/
ModrinthModPackInfo getModrinthModPackInfo();

/**
* Get the modrinth mod pack.
* @return the modrinth mod pack.
*/
ModrinthModPack getModrinthModPack();

/**
* Define the modrinth mod pack.
* @param modrinthModPack the modrinth mod pack.
*/
void setModrinthModPack(ModrinthModPack modrinthModPack);

/**
* Define all modrinth mods to update.
* @param modrinthMods modrinth mods to define.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
Expand All @@ -35,6 +36,8 @@ public class ModrinthIntegration extends Integration
private static final String MODRINTH_VERSION_ENDPOINT = "version/{versionId}";
private static final String MODRINTH_PROJECT_VERSION_ENDPOINT = "project/{projectId}/version";

private final List<Mod> builtInMods = new ArrayList<>();

/**
* Default constructor of a basic Integration.
*
Expand Down Expand Up @@ -130,8 +133,8 @@ private void extractModPack(@NotNull Path out, boolean installExtFiles) throws E
while (entries.hasMoreElements())
{
final ZipEntry entry = entries.nextElement();
final Path flPath = dirPath.resolve(StringUtils.empty(StringUtils.empty(entry.getName(), "client-overrides/"), "overrides/"));
final String entryName = entry.getName();
final Path flPath = dirPath.resolve(StringUtils.empty(StringUtils.empty(entryName, "client-overrides/"), "overrides/"));

if(entryName.equalsIgnoreCase("modrinth.index.json"))
{
Expand All @@ -140,6 +143,15 @@ private void extractModPack(@NotNull Path out, boolean installExtFiles) throws E
continue;
}

final String withoutOverrides = StringUtils.empty(StringUtils.empty(entryName, "overrides/"), "client-overrides/");

if(withoutOverrides.startsWith("mods/") || withoutOverrides.startsWith("mods\\"))
{
final String modName = withoutOverrides.substring(withoutOverrides.lastIndexOf('/') + 1);
final Mod mod = new Mod(modName, "", "", entry.getSize());
this.builtInMods.add(mod);
}

if(!installExtFiles || Files.exists(flPath)) continue;

if (flPath.getFileName().toString().endsWith(flPath.getFileSystem().getSeparator()))
Expand All @@ -163,7 +175,7 @@ private void extractModPack(@NotNull Path out, boolean installExtFiles) throws E
final String modPackVersion = manifestObj.get("versionId").getAsString();
final List<Mod> mods = this.parseManifest(manifestObj);

return new ModrinthModPack(modPackName, modPackVersion, mods);
return new ModrinthModPack(modPackName, modPackVersion, mods, this.builtInMods);
}

private @NotNull List<Mod> parseManifest(@NotNull JsonObject manifestObject)
Expand All @@ -178,7 +190,7 @@ private void extractModPack(@NotNull Path out, boolean installExtFiles) throws E
if(file.getAsJsonObject("env").get("client").getAsString().equals("unsupported"))
return;

final String name = StringUtils.empty(file.get("path").getAsString(), "mods/");
final String name = StringUtils.empty(StringUtils.empty(file.get("path").getAsString(), "mods/"), "mods\\");
final String downloadURL = file.getAsJsonArray("downloads").get(0).getAsString();
final String sha1 = file.getAsJsonObject("hashes").get("sha1").getAsString();
final long size = file.get("fileSize").getAsLong();
Expand All @@ -194,12 +206,6 @@ private void transferAndClose(@NotNull Path flPath, ZipFile zipFile, ZipEntry en
if(Files.notExists(flPath.getParent()))
Files.createDirectories(flPath.getParent());

try(OutputStream pathStream = Files.newOutputStream(flPath);
BufferedOutputStream fo = new BufferedOutputStream(pathStream);
InputStream is = zipFile.getInputStream(entry)) {

while (is.available() > 0)
fo.write(is.read());
}
Files.copy(zipFile.getInputStream(entry), flPath, StandardCopyOption.REPLACE_EXISTING);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,27 @@

import fr.flowarg.flowupdater.download.json.Mod;

import java.util.ArrayList;
import java.util.List;

public class ModrinthModPack
{
private final String name;
private final String version;
private final List<Mod> mods;
private final List<Mod> builtInMods;

ModrinthModPack(String name, String version, List<Mod> mods)
{
this(name, version, mods, new ArrayList<>());
}

ModrinthModPack(String name, String version, List<Mod> mods, List<Mod> builtInMods)
{
this.name = name;
this.version = version;
this.mods = mods;
this.builtInMods = builtInMods;
}

/**
Expand Down Expand Up @@ -44,4 +52,17 @@ public List<Mod> getMods()
{
return this.mods;
}

/**
* Get the built-in mods in the mod pack.
* Built-in mods are mods directly put in the mods folder in the .mrpack file.
* They are not downloaded from a remote server.
* This is not a very good way to add mods because it disables some mod verification on these mods.
* We recommend mod pack creators to use the built-in mods feature only for mods that are not available remotely.
* @return the built-in mods in the mod pack.
*/
public List<Mod> getBuiltInMods()
{
return this.builtInMods;
}
}
20 changes: 18 additions & 2 deletions src/main/java/fr/flowarg/flowupdater/utils/ModFileDeleter.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import fr.flowarg.flowio.FileUtils;
import fr.flowarg.flowupdater.download.json.Mod;
import fr.flowarg.flowupdater.integrations.modrinthintegration.ModrinthModPack;
import fr.flowarg.flowupdater.integrations.optifineintegration.OptiFine;

import java.io.IOException;
Expand Down Expand Up @@ -33,17 +34,32 @@ public ModFileDeleter(String... modsToIgnore)
* Delete all bad files in the provided directory.
* @param modsDir the mod's folder.
* @param mods the mods list.
* @param optiFine the OptiFine object.
* @throws Exception thrown if an error occurred
*/
public void delete(Path modsDir, List<Mod> mods, OptiFine optiFine) throws Exception
public void delete(Path modsDir, List<Mod> mods) throws Exception
{
this.delete(modsDir, mods, null, null);
}

/**
* Delete all bad files in the provided directory.
* @param modsDir the mod's folder.
* @param mods the mods list.
* @param optiFine the OptiFine object. (SPECIFIC USE CASE)
* @param modrinthModPack the modrinth mod pack. (SPECIFIC USE CASE)
* @throws Exception thrown if an error occurred
*/
public void delete(Path modsDir, List<Mod> mods, OptiFine optiFine, ModrinthModPack modrinthModPack) throws Exception
{
if(!this.isUseFileDeleter()) return;

final Set<Path> badFiles = new HashSet<>();
final List<Path> verifiedFiles = new ArrayList<>();
Arrays.stream(this.modsToIgnore).forEach(fileName -> verifiedFiles.add(modsDir.resolve(fileName)));

if(modrinthModPack != null)
modrinthModPack.getBuiltInMods().forEach(mod -> verifiedFiles.add(modsDir.resolve(mod.getName())));

for(Path fileInDir : FileUtils.list(modsDir).stream().filter(path -> !Files.isDirectory(path)).collect(Collectors.toList()))
{
if(verifiedFiles.contains(fileInDir))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ public void installMods(Path modsDir) throws Exception
this.callback.update(this.downloadList.getDownloadInfo());
}

this.fileDeleter.delete(modsDir, this.mods, ofObj);
this.fileDeleter.delete(modsDir, this.mods, ofObj, this.modrinthModPack);
}

/** This method packs the modified installer to a JAR file.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import fr.flowarg.flowupdater.download.json.*;
import fr.flowarg.flowupdater.integrations.curseforgeintegration.ICurseFeaturesUser;
import fr.flowarg.flowupdater.integrations.modrinthintegration.IModrinthFeaturesUser;
import fr.flowarg.flowupdater.integrations.modrinthintegration.ModrinthModPack;
import fr.flowarg.flowupdater.utils.ModFileDeleter;
import org.jetbrains.annotations.NotNull;

Expand All @@ -27,6 +28,7 @@ public abstract class AbstractModLoaderVersion implements ICurseFeaturesUser, IM
protected DownloadList downloadList;
protected IProgressCallback callback;
protected String javaPath;
protected ModrinthModPack modrinthModPack;

public AbstractModLoaderVersion(List<Mod> mods, String modLoaderVersion, List<CurseFileInfo> curseMods,
List<ModrinthVersionInfo> modrinthMods, ModFileDeleter fileDeleter, CurseModPackInfo curseModPackInfo,
Expand Down Expand Up @@ -162,4 +164,16 @@ public ModFileDeleter getFileDeleter()
{
return this.fileDeleter;
}

@Override
public void setModrinthModPack(ModrinthModPack modrinthModPack)
{
this.modrinthModPack = modrinthModPack;
}

@Override
public ModrinthModPack getModrinthModPack()
{
return this.modrinthModPack;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ public void installMods(Path modsDir) throws Exception
this.callback.step(Step.MODS);

this.installAllMods(modsDir);
this.fileDeleter.delete(modsDir, this.mods, null);
this.fileDeleter.delete(modsDir, this.mods, null, this.modrinthModPack);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ public void installMods(Path modsDir) throws Exception
this.callback.step(Step.MODS);

this.installAllMods(modsDir);
this.fileDeleter.delete(modsDir, this.mods, null);
this.fileDeleter.delete(modsDir, this.mods, null, this.modrinthModPack);
}

/**
Expand Down

0 comments on commit 1fbc52d

Please sign in to comment.