Skip to content

Commit

Permalink
Implement deletion of archives after extraction
Browse files Browse the repository at this point in the history
  • Loading branch information
ACGaming committed Jul 12, 2023
1 parent ba07976 commit 8dd39ff
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public class InstallationPolicy {
private final String name;
private final String description;
private final boolean extract;
private final boolean deleteAfterExtract;
private final boolean downloadAlways;
private final String supersede;
private final String modpackVersion;
Expand All @@ -22,6 +23,7 @@ public InstallationPolicy(
@JsonProperty(value = "name") String name,
@JsonProperty(value = "description") String description,
@JsonProperty(value = "extract") boolean extract,
@JsonProperty(value = "deleteAfterExtract") boolean deleteAfterExtract,
@JsonProperty(value = "downloadAlways") boolean downloadAlways,
@JsonProperty(value = "supersede") String supersede,
@JsonProperty(value = "modpackVersion") String modpackVersion
Expand All @@ -32,6 +34,7 @@ public InstallationPolicy(
this.name = name;
this.description = description;
this.extract = extract;
this.deleteAfterExtract = deleteAfterExtract;
this.downloadAlways = downloadAlways;
this.supersede = supersede;
this.modpackVersion = modpackVersion;
Expand Down Expand Up @@ -61,6 +64,10 @@ public boolean shouldExtract() {
return extract;
}

public boolean shouldDeleteAfterExtract() {
return deleteAfterExtract;
}

public boolean shouldDownloadAlways() {
return downloadAlways;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public ModDirectorRemoteMod(
null,
false,
false,
false,
null,
null
) : installationPolicy;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,9 @@ public void performInstall(Path targetFile, ProgressCallback progressCallback, M
zipEntry = zipInputStream.getNextEntry();
}
}
if (this.getInstallationPolicy().shouldDeleteAfterExtract()) {
Files.delete(targetFile);
}
}
} catch(IOException e) {
throw new ModDirectorException("Failed to write file to disk", e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.util.function.BiFunction;

public class InstallController {
private static final String LOG_DOMAIN = "ModDirector/InstallController";
private final ModDirector director;

public InstallController(ModDirector director) {
Expand Down Expand Up @@ -69,7 +70,7 @@ public List<Callable<Void>> createPreInstallTasks(
try {
information = mod.queryInformation();
} catch(ModDirectorException e) {
director.getLogger().logThrowable(ModDirectorSeverityLevel.ERROR, "ModDirector/InstallController",
director.getLogger().logThrowable(ModDirectorSeverityLevel.ERROR, LOG_DOMAIN,
"CORE", e, "Failed to query information for %s from %s",
mod.offlineName(), mod.remoteType());
director.addError(new ModDirectorError(downloadSeverityLevelFor(mod),
Expand Down Expand Up @@ -105,7 +106,7 @@ public List<Callable<Void>> createPreInstallTasks(

switch(hashResult) {
case UNKNOWN:
director.getLogger().log(ModDirectorSeverityLevel.DEBUG, "ModDirector/InstallController",
director.getLogger().log(ModDirectorSeverityLevel.DEBUG, LOG_DOMAIN,
"CORE", "Skipping download of %s as hashes can't be determined but file exists",
targetFile.toString());
callback.done();
Expand All @@ -114,15 +115,15 @@ public List<Callable<Void>> createPreInstallTasks(
return null;

case MATCHED:
director.getLogger().log(ModDirectorSeverityLevel.INFO, "ModDirector/InstallController",
director.getLogger().log(ModDirectorSeverityLevel.INFO, LOG_DOMAIN,
"CORE", "Skipping download of %s as the hashes match", targetFile.toString());
callback.done();

excludedMods.add(mod);
return null;

case UNMATCHED:
director.getLogger().log(ModDirectorSeverityLevel.WARN, "ModDirector/InstallController",
director.getLogger().log(ModDirectorSeverityLevel.WARN, LOG_DOMAIN,
"CORE", "File %s exists, but hashes do not match, downloading again!",
targetFile.toString());
}
Expand All @@ -131,13 +132,13 @@ public List<Callable<Void>> createPreInstallTasks(
reinstallMods.add(installableMod);

} else if(mod.getInstallationPolicy().shouldDownloadAlways() && Files.isRegularFile(targetFile)) {
director.getLogger().log(ModDirectorSeverityLevel.INFO, "ModDirector/InstallController",
director.getLogger().log(ModDirectorSeverityLevel.INFO, LOG_DOMAIN,
"CORE", "Force downloading file %s as download always option is set.",
targetFile.toString());
reinstallMods.add(installableMod);

} else if(Files.isRegularFile(targetFile)) {
director.getLogger().log(ModDirectorSeverityLevel.DEBUG, "ModDirector/InstallController",
director.getLogger().log(ModDirectorSeverityLevel.DEBUG, LOG_DOMAIN,
"CORE", "File %s exists and no metadata given, skipping download.",
targetFile.toString());
excludedMods.add(mod);
Expand Down Expand Up @@ -174,7 +175,7 @@ private Path computeInstallationTargetPath(ModDirectorRemoteMod mod, RemoteModIn
.toAbsolutePath().normalize();

if(!targetFile.startsWith(installationRoot)) {
director.getLogger().log(ModDirectorSeverityLevel.ERROR, "ModDirector/InstallController",
director.getLogger().log(ModDirectorSeverityLevel.ERROR, LOG_DOMAIN,
"CORE", "Tried to install a file to %s, which is outside the installation root of %s!",
targetFile.toString(), director.getPlatform().installationRoot());
director.addError(new ModDirectorError(ModDirectorSeverityLevel.ERROR,
Expand Down Expand Up @@ -228,7 +229,7 @@ public void markDisabledMods(List<InstallableMod> mods) {
} catch (IOException e) {
director.getLogger().logThrowable(
ModDirectorSeverityLevel.WARN,
"ModDirector/InstallController",
LOG_DOMAIN,
"CORE",
e,
"Failed to create disabled file, the user might be asked again if he wants to install the mod"
Expand Down Expand Up @@ -262,15 +263,15 @@ public List<Callable<Void>> createInstallTasks(
private void handle(InstallableMod mod, ProgressCallback callback) {
ModDirectorRemoteMod remoteMod = mod.getRemoteMod();

director.getLogger().log(ModDirectorSeverityLevel.DEBUG, "ModDirector/InstallController", "CORE",
director.getLogger().log(ModDirectorSeverityLevel.DEBUG, LOG_DOMAIN, "CORE",
"Now handling %s from backend %s", remoteMod.offlineName(), remoteMod.remoteType());

Path targetFile = mod.getTargetFile();

try {
Files.createDirectories(targetFile.getParent());
} catch(IOException e) {
director.getLogger().logThrowable(ModDirectorSeverityLevel.ERROR, "ModDirector/InstallController",
director.getLogger().logThrowable(ModDirectorSeverityLevel.ERROR, LOG_DOMAIN,
"CORE", e, "Failed to create directory %s", targetFile.getParent().toString());
director.addError(new ModDirectorError(ModDirectorSeverityLevel.ERROR,
"Failed to create directory" + targetFile.getParent().toString(), e));
Expand All @@ -281,7 +282,7 @@ private void handle(InstallableMod mod, ProgressCallback callback) {
try {
mod.performInstall(director, callback);
} catch(ModDirectorException e) {
director.getLogger().logThrowable(ModDirectorSeverityLevel.ERROR, "ModDirector/InstallController",
director.getLogger().logThrowable(ModDirectorSeverityLevel.ERROR, LOG_DOMAIN,
"CORE", e, "Failed to install mod %s", remoteMod.offlineName());
director.addError(new ModDirectorError(downloadSeverityLevelFor(remoteMod),
"Failed to install mod " + remoteMod.offlineName(), e));
Expand All @@ -290,16 +291,16 @@ private void handle(InstallableMod mod, ProgressCallback callback) {
}

if(remoteMod.getMetadata() != null && remoteMod.getMetadata().checkHashes(targetFile, director) == HashResult.UNMATCHED) {
director.getLogger().log(ModDirectorSeverityLevel.ERROR, "ModDirector/InstallController",
director.getLogger().log(ModDirectorSeverityLevel.ERROR, LOG_DOMAIN,
"CORE", "Mod did not match hash after download, aborting!");
director.addError(new ModDirectorError(ModDirectorSeverityLevel.ERROR,
"Mod did not match hash after download"));
} else {
if(remoteMod.getInstallationPolicy().shouldExtract()) {
director.getLogger().log(ModDirectorSeverityLevel.INFO, "ModDirector/InstallController",
director.getLogger().log(ModDirectorSeverityLevel.INFO, LOG_DOMAIN,
"CORE", "Extracted mod file %s", targetFile.toString());
} else {
director.getLogger().log(ModDirectorSeverityLevel.INFO, "ModDirector/InstallController",
director.getLogger().log(ModDirectorSeverityLevel.INFO, LOG_DOMAIN,
"CORE", "Installed mod file %s", targetFile.toString());
}
director.installSuccess(new InstalledMod(targetFile, remoteMod.getOptions(), remoteMod.forceInject()));
Expand Down

0 comments on commit 8dd39ff

Please sign in to comment.