From ff0996372ebc13d59bbc04c16fc0f103c683a317 Mon Sep 17 00:00:00 2001 From: Timo Ley Date: Wed, 29 Jul 2020 21:34:49 +0200 Subject: [PATCH] Updates: -Removed LinkInstPair -Added installer field to FileOrLink -Changed to file system in FileOrLink -Added filter for multiple types in getRelations --- .../anvil/addonscript/wrapper/ASWrapper.java | 9 ++-- .../anvil/addonscript/wrapper/FileOrLink.java | 34 ++++++++++++ .../addonscript/wrapper/LinkInstPair.java | 52 ------------------- 3 files changed, 40 insertions(+), 55 deletions(-) delete mode 100644 src/main/java/ley/anvil/addonscript/wrapper/LinkInstPair.java diff --git a/src/main/java/ley/anvil/addonscript/wrapper/ASWrapper.java b/src/main/java/ley/anvil/addonscript/wrapper/ASWrapper.java index 4d0dd22..eff2cde 100644 --- a/src/main/java/ley/anvil/addonscript/wrapper/ASWrapper.java +++ b/src/main/java/ley/anvil/addonscript/wrapper/ASWrapper.java @@ -27,6 +27,8 @@ public class ASWrapper { public Map INSTALLERS; public Map ADDONS; public Map VERSIONS; + @Nullable + File asdir; public ASWrapper(AddonscriptJSON json) { this.json = json; @@ -57,6 +59,7 @@ public class ASWrapper { public ASWrapper(File file) throws FileNotFoundException { this(AddonscriptJSON.read(new FileReader(file))); + asdir = file.getParentFile(); } public ASWrapper(URL url) { @@ -122,7 +125,7 @@ public class ASWrapper { return version != null; } - public List getRelations(String[] conditions, @Nullable String type) { + public List getRelations(String[] conditions, @Nullable String[] types) { List list = new ArrayList<>(); if (exists() && version.relations != null) { for (AddonscriptJSON.Relation r : version.relations) { @@ -131,7 +134,7 @@ public class ASWrapper { opt = r.options; else opt = defaultOptions(); - if (opt.containsAll(Arrays.asList(conditions)) && (type== null || r.type.equals(type))) { + if (opt.containsAll(Arrays.asList(conditions)) && (types == null || Arrays.asList(types).contains(r.type))) { list.add(new RelationWrapper(r)); } } @@ -196,7 +199,7 @@ public class ASWrapper { } public FileOrLink get() { - return new FileOrLink(getLink()); + return new FileOrLink(getLink(), file.installer).setASDir(asdir); } public String getLink() { diff --git a/src/main/java/ley/anvil/addonscript/wrapper/FileOrLink.java b/src/main/java/ley/anvil/addonscript/wrapper/FileOrLink.java index 39f88d9..6fb18bb 100644 --- a/src/main/java/ley/anvil/addonscript/wrapper/FileOrLink.java +++ b/src/main/java/ley/anvil/addonscript/wrapper/FileOrLink.java @@ -10,11 +10,19 @@ import java.net.URL; public class FileOrLink { String link; + File asdir; + public String installer; + public FileOrLink(String link) { this.link = link; } + public FileOrLink(String link, String installer) { + this.link = link; + this.installer = installer; + } + public boolean isFile() { return Utils.notEmpty(link) && link.startsWith("file://"); } @@ -33,12 +41,23 @@ public class FileOrLink { * @param path The path to the directory, in which the Addonscript.json is placed * @return A File */ + @Deprecated public File getFile(@Nonnull String path) { if (isFile()) return new File(Utils.slashEnd(path) + link.replace("file://", "")); throw new RuntimeException("This is no file"); } + /** + * To call this, the ASDir should be set. If it isn't set, set it with setASDir before calling this. + * @return The File, which is represented by this object + */ + public File getFile() { + if (isFile() && isASDirSet()) + return new File(Utils.slashEnd(asdir.getPath()) + link.replace("file://", "")); + throw new RuntimeException("This is no file or the AS Dir was not set"); + } + public URL getURL() { if (isURL()) { try { @@ -50,4 +69,19 @@ public class FileOrLink { throw new RuntimeException("This is no URL"); } + /** + * In Addonscript file:// links are relative paths from the Addonscript.json, + * so you have to specify the path of the directory in which it is placed. + * @param dir The path to the directory, in which the Addonscript.json is placed + * @return This FileOrLink object + */ + public FileOrLink setASDir(File dir) { + asdir = dir; + return this; + } + + public boolean isASDirSet() { + return asdir != null; + } + } diff --git a/src/main/java/ley/anvil/addonscript/wrapper/LinkInstPair.java b/src/main/java/ley/anvil/addonscript/wrapper/LinkInstPair.java deleted file mode 100644 index a5c1e70..0000000 --- a/src/main/java/ley/anvil/addonscript/wrapper/LinkInstPair.java +++ /dev/null @@ -1,52 +0,0 @@ -package ley.anvil.addonscript.wrapper; - -import java.io.File; -import java.net.MalformedURLException; -import java.net.URL; - -public class LinkInstPair { - - public String link; - public String installer; - - public LinkInstPair(String link, String installer) { - this.link = link; - this.installer = installer; - } - - public String getLink() { - return link; - } - - public String getInstaller() { - return installer; - } - - public boolean isURL() { - return link.startsWith("http://") || link.startsWith("https://"); - } - - public boolean isFile() { - return link.startsWith("file://"); - } - - public File asFile(String jsonDir) { - if (isFile()) { - String f = link.replace("file://", jsonDir + (jsonDir.endsWith("/") ? "" : "/")); - return new File(f); - } - throw new RuntimeException("This is not a file"); - } - - public URL asURL() { - if (isURL()) { - try { - return new URL(link); - } catch (MalformedURLException e) { - throw new RuntimeException(e); - } - } - throw new RuntimeException("This is not a URL"); - } - -}