Skip to content
This repository was archived by the owner on Sep 9, 2023. It is now read-only.

Commit 3718dd9

Browse files
proper old mod deletion
1 parent 9fd2828 commit 3718dd9

6 files changed

Lines changed: 100 additions & 90 deletions

File tree

src/ml/northwestwind/Config.java

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -97,38 +97,6 @@ public static List<String> loadProfiles() {
9797
return list;
9898
}
9999

100-
public static Map<Integer, Map.Entry<Integer, String>> loadMods(String profile) {
101-
File[] files = new File(profileDir.getAbsolutePath() + File.separator + profile + File.separator + "mods").listFiles();
102-
Map<Integer, Map.Entry<Integer, String>> map = new HashMap<>();
103-
for (File file : files) {
104-
if (file.isDirectory() || !file.getName().endsWith(".jar")) continue;
105-
String[] splitted = file.getName().replace(".jar", "").split("_");
106-
String fileId = Utils.getLast(Arrays.asList(splitted));
107-
if (!Utils.isInteger(fileId)) continue;
108-
String[] splitted1 = Arrays.stream(Arrays.copyOf(splitted, splitted.length - 1)).toArray(String[]::new);
109-
String id = Utils.getLast(Arrays.asList(splitted1));
110-
if (!Utils.isInteger(id)) continue;
111-
String name = String.join("_", Arrays.copyOf(splitted1, splitted1.length - 1));
112-
map.put(Integer.parseInt(id), new Map.Entry<Integer, String>() {
113-
@Override
114-
public Integer getKey() {
115-
return Integer.parseInt(fileId);
116-
}
117-
118-
@Override
119-
public String getValue() {
120-
return name;
121-
}
122-
123-
@Override
124-
public String setValue(String value) {
125-
return null;
126-
}
127-
});
128-
}
129-
return map;
130-
}
131-
132100
public static void save() {
133101
try {
134102
JSONObject jo = new JSONObject();

src/ml/northwestwind/Constants.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
public class Constants {
88
public static final String CURSEFORGE_API = "https://northwestwind.ml/api/curseforge/mods/";
9-
public static final String VERSION = "1.3.4";
9+
public static final String VERSION = "1.3.5";
1010
private static final String OS = System.getProperty("os.name").toLowerCase();
1111
public static final boolean IS_WINDOWS = (OS.contains("win"));
1212
public static final boolean IS_MAC = (OS.contains("mac"));

src/ml/northwestwind/Modpack.java

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -118,12 +118,14 @@ public static void downloadMods(String folder, boolean force) {
118118
try {
119119
File modsFolder = new File(folder + File.separator + "mods");
120120
if (!modsFolder.exists() || !modsFolder.isDirectory()) modsFolder.mkdir();
121-
Map<String, String> mods = getAllMods(modsFolder.getAbsolutePath());
121+
Map<String, String> mods = Utils.getAllMods(modsFolder.getAbsolutePath());
122+
Map<String, String> modNames = Utils.getAllModNames(modsFolder.getAbsolutePath());
122123
File manifest = new File(folder + File.separator + "manifest.json");
123124
JSONObject json = (JSONObject) parser.parse(new FileReader(manifest));
124125
JSONArray array = (JSONArray) json.get("files");
125126
int i = 0, suc = 0, fai = 0, ski = 0, lastSuc = 0, lastFai = 0, lastSki = 0;
126127
Map<String, String> failed = new HashMap<>();
128+
Set<String> exists = new HashSet<>();
127129
for (Object o : array) {
128130
String name = "";
129131
JSONObject obj = (JSONObject) o;
@@ -149,14 +151,22 @@ public static void downloadMods(String folder, boolean force) {
149151
failed.put(project, file);
150152
if (!Config.silentExceptions) e.printStackTrace();
151153
} finally {
152-
if (suc > lastSuc) System.out.println(Ansi.ansi().fg(Ansi.Color.GREEN).a(String.format("[%d/%d] [S/F/S: %d/%d/%d] Downloaded %s", ++i, array.size(), suc, fai, ski, name)));
153-
else if (fai > lastFai) System.out.println(Ansi.ansi().fg(Ansi.Color.RED).a(String.format("[%d/%d] [S/F/S: %d/%d/%d] Failed to download %s", ++i, array.size(), suc, fai, ski, name)));
154+
exists.add(project);
155+
if (suc > lastSuc) {
156+
System.out.println(Ansi.ansi().fg(Ansi.Color.GREEN).a(String.format("[%d/%d] [S/F/S: %d/%d/%d] Downloaded %s", ++i, array.size(), suc, fai, ski, name)));
157+
File oldFile = new File(modsFolder.getAbsolutePath() + File.separator + modNames.get(project));
158+
if (oldFile.exists() && oldFile.isFile()) oldFile.delete();
159+
} else if (fai > lastFai) System.out.println(Ansi.ansi().fg(Ansi.Color.RED).a(String.format("[%d/%d] [S/F/S: %d/%d/%d] Failed to download %s", ++i, array.size(), suc, fai, ski, name)));
154160
else if (ski > lastSki) System.out.println(Ansi.ansi().fg(Ansi.Color.YELLOW).a(String.format("[%d/%d] [S/F/S: %d/%d/%d] Skipped %s", ++i, array.size(), suc, fai, ski, name)));
155161
lastSuc = suc;
156162
lastFai = fai;
157163
lastSki = ski;
158164
}
159165
}
166+
mods.keySet().stream().filter(key -> !exists.contains(key)).forEach(key -> {
167+
File oldFile = new File(modsFolder.getAbsolutePath() + File.separator + modNames.get(key));
168+
if (oldFile.exists() && oldFile.isFile()) oldFile.delete();
169+
});
160170
System.out.println(Ansi.ansi().fg(Ansi.Color.YELLOW).a(String.format("Iterated through %d projects. %d success, %d failed, %d skipped", i, suc, fai, ski)));
161171
if (fai > 0) {
162172
System.out.println(Ansi.ansi().fg(Ansi.Color.RED).a("Mods that failed to update:").reset());
@@ -168,23 +178,6 @@ public static void downloadMods(String folder, boolean force) {
168178
}
169179
}
170180

171-
private static Map<String, String> getAllMods(String modFolder) {
172-
Map<String, String> map = new HashMap<>();
173-
File modsFolder = new File(modFolder);
174-
if (!modsFolder.exists() || !modsFolder.isDirectory()) return map;
175-
for (String filename : modsFolder.list()) {
176-
if (!filename.endsWith(".jar")) continue;
177-
String[] splitted = filename.replace(".jar", "").split("_");
178-
String fileId = Utils.getLast(Arrays.asList(splitted));
179-
if (!Utils.isInteger(fileId)) continue;
180-
String[] splitted1 = Arrays.stream(Arrays.copyOf(splitted, splitted.length - 1)).toArray(String[]::new);
181-
String id = Utils.getLast(Arrays.asList(splitted1));
182-
if (!Utils.isInteger(id)) continue;
183-
map.put(id, fileId);
184-
}
185-
return map;
186-
}
187-
188181
// Pass in latestFiles' last element
189182
private static String getManifestFile(JSONObject latest) {
190183
if (latest.containsKey("modules")) {

0 commit comments

Comments
 (0)