testcraftupdate/src/main/java/ley/modding/tcu/UpdateTweaker.java

84 lines
2.9 KiB
Java
Raw Normal View History

2021-05-27 21:48:38 +02:00
package ley.modding.tcu;
2021-06-01 15:33:08 +02:00
import ley.anvil.addonscript.v1.AddonscriptJSON;
import ley.modding.tcu.model.Config;
import ley.modding.tcu.model.RelationFile;
import ley.modding.tcu.model.Version;
2021-05-27 21:48:38 +02:00
import net.minecraft.launchwrapper.ITweaker;
import net.minecraft.launchwrapper.LaunchClassLoader;
import java.io.File;
2021-06-01 15:33:08 +02:00
import java.io.FileWriter;
2021-06-01 16:01:19 +02:00
import java.io.IOException;
2021-05-27 21:48:38 +02:00
import java.util.List;
public class UpdateTweaker implements ITweaker {
private List<String> args;
private File gameDir;
private File assetsDir;
private String profile;
private LaunchClassLoader classLoader;
@Override
public void acceptOptions(List<String> args, File gameDir, File assetsDir, String profile) {
this.args = args;
this.gameDir = gameDir;
this.assetsDir = assetsDir;
this.profile = profile;
if(gameDir == null) {
this.gameDir = new File(".").getAbsoluteFile();
}
}
@Override
public void injectIntoClassLoader(LaunchClassLoader classLoader) {
this.classLoader = classLoader;
try {
2021-06-01 15:33:08 +02:00
File configfile = new File(gameDir.getAbsolutePath() + "/pack.json");
if (configfile.exists()) {
2023-08-02 19:18:59 +02:00
boolean updated = false;
2021-06-01 15:33:08 +02:00
Config config = Util.getConfig(configfile);
Version version = Util.checkVersion(config);
2023-08-02 19:18:59 +02:00
if (!version.isLatest()) {
System.out.println(version.current + " is outdated, starting update...");
AddonscriptJSON oldAS = config.getAPI().getASFromTag(version.current);
AddonscriptJSON newAS = config.getAPI().getASFromTag(version.latest);
List<RelationFile> oldRel = Util.getRelations(oldAS);
List<RelationFile> newRel = Util.getRelations(newAS);
FileHandler handler = new FileHandler(gameDir);
handler.removeFiles(Util.getToRemove(oldRel, newRel));
handler.addFiles(Util.getToAdd(oldRel, newRel));
handler.processOverrides(version.overrides, newAS, config);
updated = true;
}
2021-06-01 15:33:08 +02:00
config.version = version.latest;
FileWriter writer = new FileWriter(configfile);
config.toJson(writer);
writer.close();
2023-08-02 19:18:59 +02:00
if (updated) {
throw new RuntimeException("Successfully updated modpack. Please restart the game.");
}
2021-05-27 21:48:38 +02:00
}
2021-06-01 16:01:19 +02:00
} catch (IOException e) {
2021-05-27 21:48:38 +02:00
throw new RuntimeException(e);
}
}
@Override
public String getLaunchTarget() {
return "net.minecraft.client.main.Main";
}
@Override
public String[] getLaunchArguments() {
return new String[0];
}
public void callInjectedTweaker(ITweaker tweaker) {
tweaker.acceptOptions(args, gameDir, assetsDir, profile);
tweaker.injectIntoClassLoader(classLoader);
}
}