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

Commit 7219447

Browse files
Use Absolute Path
1 parent 4c24f5b commit 7219447

5 files changed

Lines changed: 71 additions & 59 deletions

File tree

src/ml/northwestwind/Config.java

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -39,21 +39,21 @@ public static void printHelp(String prefix) {
3939
}
4040

4141
private static void createDefaultConfig() {
42-
directory = new File("./curseforge-cli");
42+
directory = new File(Constants.ABSOLUTE_PATH + File.separator + "curseforge-cli");
4343
save();
4444
}
4545

4646
public static void load() {
47-
File config = new File("./cf.json");
47+
File config = new File(Constants.ABSOLUTE_PATH + File.separator + "cf.json");
4848
if (!config.exists()) createDefaultConfig();
4949
JSONParser parser = new JSONParser();
5050
try {
5151
JSONObject json = (JSONObject) parser.parse(new FileReader(config));
52-
directory = new File((String) json.getOrDefault("directory", "./curseforge-cli"));
53-
modpackDir = new File(directory.getPath() + File.separator + "modpack");
54-
profileDir = new File(directory.getPath() + File.separator + "profile");
55-
exportDir = new File(directory.getPath() + File.separator + "exported");
56-
tempDir = new File(directory.getPath() + File.separator + "tmp");
52+
directory = new File((String) json.getOrDefault("directory", Constants.ABSOLUTE_PATH + File.separator + "curseforge-cli"));
53+
modpackDir = new File(directory.getAbsolutePath() + File.separator + "modpack");
54+
profileDir = new File(directory.getAbsolutePath() + File.separator + "profile");
55+
exportDir = new File(directory.getAbsolutePath() + File.separator + "exported");
56+
tempDir = new File(directory.getAbsolutePath() + File.separator + "tmp");
5757
acceptParentVersionMod = (boolean) json.getOrDefault("acceptParent", true);
5858
suppressUpdates = (boolean) json.getOrDefault("suppressUpdates", false);
5959
retries = (long) json.getOrDefault("retries", 3L);
@@ -79,10 +79,7 @@ public static Map<Integer, String> loadModpacks() {
7979
if (!Utils.isInteger(fileId)) continue;
8080
String[] splitted1 = Arrays.stream(Arrays.copyOf(splitted, splitted.length - 1)).toArray(String[]::new);
8181
String id = Utils.getLast(Arrays.asList(splitted1));
82-
if (!Utils.isInteger(id)) {
83-
id = fileId;
84-
fileId = null;
85-
}
82+
if (!Utils.isInteger(id)) id = fileId;
8683
String slug = Arrays.stream(Arrays.copyOf(splitted, splitted.length - 1)).collect(Collectors.joining("_"));
8784
map.put(Integer.parseInt(id), slug);
8885
}
@@ -100,7 +97,7 @@ public static List<String> loadProfiles() {
10097
}
10198

10299
public static Map<Integer, Map.Entry<Integer, String>> loadMods(String profile) {
103-
File[] files = new File(profileDir.getPath() + File.separator + profile + File.separator + "mods").listFiles();
100+
File[] files = new File(profileDir.getAbsolutePath() + File.separator + profile + File.separator + "mods").listFiles();
104101
Map<Integer, Map.Entry<Integer, String>> map = new HashMap<>();
105102
for (File file : files) {
106103
if (file.isDirectory() || !file.getName().endsWith(".jar")) continue;
@@ -134,7 +131,7 @@ public String setValue(String value) {
134131
public static void save() {
135132
try {
136133
JSONObject jo = new JSONObject();
137-
jo.put("directory", directory.getPath());
134+
jo.put("directory", directory.getAbsolutePath());
138135
jo.put("acceptParent", acceptParentVersionMod);
139136
jo.put("suppressUpdates", suppressUpdates);
140137
jo.put("retries", retries);
Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,21 @@
11
package ml.northwestwind;
22

3+
import java.io.File;
4+
import java.net.URISyntaxException;
5+
import java.util.function.Supplier;
6+
37
public class Constants {
48
public static final String CURSEFORGE_API = "https://addons-ecs.forgesvc.net/api/v2/addon/";
5-
public static final String VERSION = "1.2.9";
9+
public static final String VERSION = "1.2.10";
610
private static final String OS = System.getProperty("os.name").toLowerCase();
711
public static final boolean IS_WINDOWS = (OS.contains("win"));
812
public static final boolean IS_MAC = (OS.contains("mac"));
913
public static final boolean IS_UNIX = (OS.contains("nix") || OS.contains("nux") || OS.contains("aix"));
14+
public static final String ABSOLUTE_PATH = ((Supplier<String>) () -> {
15+
try {
16+
return new File(Constants.class.getProtectionDomain().getCodeSource().getLocation().toURI()).getParentFile().getPath();
17+
} catch (URISyntaxException exception) {
18+
return ".";
19+
}
20+
}).get();
1021
}

src/ml/northwestwind/Modpack.java

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ private static void install(String[] ids) {
6767
String slug = (String) json.get("slug");
6868
String name = (String) json.get("name");
6969
System.out.println(Ansi.ansi().fg(Ansi.Color.YELLOW).a("Installing ").a(name).a("...").reset());
70-
File packFolder = new File(Config.modpackDir.getPath() + File.separator + slug + "_" + id + (fileId != null ? "_" + fileId : ""));
70+
File packFolder = new File(Config.modpackDir.getAbsolutePath() + File.separator + slug + "_" + id + (fileId != null ? "_" + fileId : ""));
7171
if (packFolder.exists()) {
7272
Scanner scanner = new Scanner(System.in);
7373
System.out.println(Ansi.ansi().fg(Ansi.Color.YELLOW).a("Found old folder of ").a(name).a(". If you want to re-install while keeping your files, use \"repair\" instead.").reset());
@@ -82,15 +82,15 @@ private static void install(String[] ids) {
8282
}
8383
packFolder.mkdir();
8484
String downloadUrl = ((String) files.get("downloadUrl")).replaceFirst("edge", "media");
85-
String loc = Utils.downloadFile(downloadUrl, packFolder.getPath());
85+
String loc = Utils.downloadFile(downloadUrl, packFolder.getAbsolutePath());
8686
if (loc == null) throw new SyncFailedException("Failed to download modpack " + name);
8787
boolean success = Utils.unzip(loc);
8888
if (!success) throw new FileSystemException("Failed to extract modpack content of " + name);
8989
File manifest = new File(packFolder + File.separator + getManifestFile(files));
9090
if (!manifest.exists()) throw new FileNotFoundException("Cannot find modpack manifest of " + name);
9191
JSONObject manifestJson = (JSONObject) parser.parse(new FileReader(manifest));
92-
copyFromOverride(packFolder.getPath(), (String) manifestJson.get("overrides"));
93-
downloadMods(packFolder.getPath());
92+
copyFromOverride(packFolder.getAbsolutePath(), (String) manifestJson.get("overrides"));
93+
downloadMods(packFolder.getAbsolutePath());
9494
String thumb = downloadThumb(json);
9595
genProfile(name, packFolder.getAbsolutePath(), thumb, getModVersion(manifestJson));
9696
System.out.println(Ansi.ansi().fg(Ansi.Color.GREEN).a("Finished download of " + name).reset());
@@ -303,7 +303,7 @@ private static void delete(String[] ids) {
303303
AtomicInteger failed = new AtomicInteger();
304304
folders.forEach((key, value) -> {
305305
try {
306-
FileUtils.deleteDirectory(new File(Config.modpackDir.getPath() + File.separator + key));
306+
FileUtils.deleteDirectory(new File(Config.modpackDir.getAbsolutePath() + File.separator + key));
307307
} catch (Exception e) {
308308
e.printStackTrace();
309309
failed.getAndIncrement();
@@ -321,7 +321,7 @@ private static void list() {
321321
List<Ansi> modpack = new ArrayList<>();
322322
for (Map.Entry<Integer, String> entry : modpacks.entrySet()) {
323323
String folderName = entry.getValue() + "_" + entry.getKey();
324-
File manifest = new File(Config.modpackDir.getPath() + File.separator + folderName + File.separator + "manifest.json");
324+
File manifest = new File(Config.modpackDir.getAbsolutePath() + File.separator + folderName + File.separator + "manifest.json");
325325
if (!manifest.exists() || !manifest.isFile()) continue;
326326
try {
327327
JSONObject json = (JSONObject) parser.parse(new FileReader(manifest));
@@ -362,18 +362,18 @@ private static void update(String[] ids, boolean force) {
362362
throw new NoSuchObjectException("The ID " + id + " does not represent a modpack.");
363363
String name = (String) json.get("name");
364364
System.out.println(Ansi.ansi().fg(Ansi.Color.YELLOW).a("Updating ").a(name).a("...").reset());
365-
File packFolder = new File(Config.modpackDir.getPath() + File.separator + slug + "_" + key);
365+
File packFolder = new File(Config.modpackDir.getAbsolutePath() + File.separator + slug + "_" + key);
366366
JSONObject latest = (JSONObject) Utils.getLast((JSONArray) json.get("latestFiles"));
367367
String downloadUrl = ((String) latest.get("downloadUrl")).replaceFirst("edge", "media");
368-
String loc = Utils.downloadFile(downloadUrl, packFolder.getPath());
368+
String loc = Utils.downloadFile(downloadUrl, packFolder.getAbsolutePath());
369369
if (loc == null) throw new SyncFailedException("Failed to download modpack " + name);
370370
boolean success = Utils.unzip(loc);
371371
if (!success) throw new FileSystemException("Failed to extract modpack content of " + name);
372372
File manifest = new File(packFolder + File.separator + "manifest.json");
373373
if (!manifest.exists()) throw new FileNotFoundException("Cannot find modpack manifest of " + name);
374-
if (force) FileUtils.cleanDirectory(new File(packFolder.getPath() + File.separator + "mods"));
375-
copyFromOverride(packFolder.getPath(), (String) ((JSONObject) parser.parse(new FileReader(manifest))).get("overrides"));
376-
downloadMods(packFolder.getPath(), force);
374+
if (force) FileUtils.cleanDirectory(new File(packFolder.getAbsolutePath() + File.separator + "mods"));
375+
copyFromOverride(packFolder.getAbsolutePath(), (String) ((JSONObject) parser.parse(new FileReader(manifest))).get("overrides"));
376+
downloadMods(packFolder.getAbsolutePath(), force);
377377
System.out.println(Ansi.ansi().fg(Ansi.Color.GREEN).a("Finished download of " + name).reset());
378378
} catch (Exception e) {
379379
e.printStackTrace();
@@ -400,13 +400,13 @@ private static void convert(String[] ids) {
400400
}
401401
if (slug == null) throw new NoSuchObjectException("Cannot find modpack with name " + id);
402402
}
403-
File packFolder = new File(Config.modpackDir.getPath() + File.separator + slug + "_" + key);
404-
File manifest = new File(packFolder.getPath() + File.separator + "manifest.json");
403+
File packFolder = new File(Config.modpackDir.getAbsolutePath() + File.separator + slug + "_" + key);
404+
File manifest = new File(packFolder.getAbsolutePath() + File.separator + "manifest.json");
405405
if (!manifest.exists() || !manifest.isFile())
406406
throw new FileNotFoundException("Modpack is missing manifest. Cannot convert to profile.");
407407
FileUtils.moveDirectoryToDirectory(packFolder, Config.profileDir, true);
408-
packFolder = new File(Config.profileDir.getPath() + File.separator + slug + "_" + key);
409-
manifest = new File(packFolder.getPath() + File.separator + "manifest.json");
408+
packFolder = new File(Config.profileDir.getAbsolutePath() + File.separator + slug + "_" + key);
409+
manifest = new File(packFolder.getAbsolutePath() + File.separator + "manifest.json");
410410
JSONObject json = (JSONObject) parser.parse(new FileReader(manifest));
411411
JSONObject profile = new JSONObject();
412412
JSONObject minecraft = (JSONObject) json.get("minecraft");
@@ -415,7 +415,7 @@ private static void convert(String[] ids) {
415415
profile.put("mcVer", minecraft.get("version"));
416416
profile.put("launcher", launcher[0]);
417417
profile.put("modVer", launcher[1]);
418-
PrintWriter pw = new PrintWriter(packFolder.getPath() + File.separator + "profile.json");
418+
PrintWriter pw = new PrintWriter(packFolder.getAbsolutePath() + File.separator + "profile.json");
419419
pw.write(profile.toJSONString());
420420

421421
pw.flush();

0 commit comments

Comments
 (0)