Skip to content

Commit 8620590

Browse files
committed
split dependency loader from FalsePatternLib main class
1 parent a15f849 commit 8620590

File tree

9 files changed

+151
-137
lines changed

9 files changed

+151
-137
lines changed
Lines changed: 10 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,30 @@
11
package com.falsepattern.lib;
22

3-
import com.falsepattern.lib.version.Version;
43
import com.google.common.eventbus.EventBus;
54
import cpw.mods.fml.common.DummyModContainer;
65
import cpw.mods.fml.common.LoadController;
76
import cpw.mods.fml.common.ModMetadata;
8-
import lombok.NonNull;
7+
import lombok.Getter;
98
import lombok.val;
10-
import lombok.var;
119
import net.minecraft.launchwrapper.Launch;
12-
import net.minecraft.launchwrapper.LaunchClassLoader;
1310
import org.apache.logging.log4j.LogManager;
1411
import org.apache.logging.log4j.Logger;
1512

16-
import javax.net.ssl.HttpsURLConnection;
17-
import java.io.*;
18-
import java.net.URL;
19-
import java.util.*;
20-
13+
import java.util.Arrays;
2114

15+
/**
16+
* Utility class used by FalsePatternLib's internal code. This can change between versions without notice, so do not use this in your code!
17+
*/
2218
public class FalsePatternLib extends DummyModContainer {
23-
public static Logger libLog = LogManager.getLogger(ModInfo.MODNAME);
24-
public static final boolean developerEnvironment = (boolean) Launch.blackboard.get("fml.deobfuscatedEnvironment");
19+
@Getter
20+
private static final Logger log = LogManager.getLogger(ModInfo.MODNAME);
2521

26-
private static final Map<String, Version> loadedLibraries = new HashMap<>();
27-
private static final Map<String, String> loadedLibraryMods = new HashMap<>();
28-
private static final Set<String> mavenRepositories = new HashSet<>();
22+
@Getter
23+
private static final boolean developerEnvironment = (boolean) Launch.blackboard.get("fml.deobfuscatedEnvironment");
2924

3025
public FalsePatternLib() {
3126
super(new ModMetadata());
32-
libLog.info("All your libraries are belong to us!");
27+
log.info("All your libraries are belong to us!");
3328
val meta = getMetadata();
3429
meta.modId = ModInfo.MODID;
3530
meta.name = ModInfo.MODNAME;
@@ -46,111 +41,4 @@ public FalsePatternLib() {
4641
public boolean registerBus(EventBus bus, LoadController controller) {
4742
return true;
4843
}
49-
50-
public static void addMavenRepo(String url) {
51-
mavenRepositories.add(url);
52-
}
53-
54-
@SuppressWarnings("ResultOfMethodCallIgnored")
55-
public static void loadLibrary(String loadingModId, String groupId, String artifactId, @NonNull Version minVersion, Version maxVersion, @NonNull Version preferredVersion, String regularSuffix, String devSuffix) {
56-
val suffix = developerEnvironment ? devSuffix : regularSuffix;
57-
libLog.info("Adding library {}:{}:{}{}, requested by mod {}", groupId, artifactId, preferredVersion, suffix != null ? "-" + suffix : "", loadingModId);
58-
var artifact = groupId + ":" + artifactId + ":" + suffix;
59-
if (loadedLibraries.containsKey(artifact)) {
60-
val currentVer = loadedLibraries.get(artifact);
61-
if (currentVer.equals(preferredVersion)) return;
62-
val rangeString = "(minimum: " + minVersion + (maxVersion == null ? "" : ", maximum: " + maxVersion) + ")";
63-
if (minVersion.compareTo(currentVer) > 0 || (maxVersion != null && maxVersion.compareTo(currentVer) < 0)) {
64-
for (int i = 0; i < 16; i++) {
65-
libLog.fatal("ALERT VVVVVVVVVVVV ALERT");
66-
}
67-
libLog.fatal("Library {}:{}{} already loaded with version {}, " +
68-
"but a version in the range {} was requested! Thing may go horribly wrong! " +
69-
"Requested by mod: {}, previously loaded by mod: {}",
70-
groupId, artifactId, suffix != null ? ":" + suffix : "", currentVer,
71-
rangeString,
72-
loadingModId, loadedLibraryMods.get(artifact));
73-
for (int i = 0; i < 16; i++) {
74-
libLog.fatal("ALERT ^^^^^^^^^^^^ ALERT");
75-
}
76-
} else {
77-
libLog.info("Attempted loading of library {}:{}{} with preferred version {}, " +
78-
"but version {} was already loaded, which matched the range {}. This is not an error. " +
79-
"Requested by mod: {}, previously loaded by mod: {}",
80-
groupId, artifactId, suffix != null ? ":" + suffix : "", preferredVersion,
81-
currentVer, rangeString,
82-
loadingModId, loadedLibraryMods.get(artifact));
83-
}
84-
return;
85-
}
86-
val modsDir = new File(CoreLoadingPlugin.mcDir, "mods");
87-
val mavenJarName = String.format("%s-%s%s.jar", artifactId, preferredVersion, (suffix != null) ? ("-" + suffix) : "");
88-
val jarName = groupId + "-" + mavenJarName;
89-
val libDir = new File(modsDir, "falsepattern");
90-
if (!libDir.exists()) {
91-
libDir.mkdirs();
92-
}
93-
val file = new File(libDir, jarName);
94-
if (file.exists()) {
95-
try {
96-
addToClasspath(file);
97-
loadedLibraries.put(artifact, preferredVersion);
98-
libLog.info("Library {}:{}:{}{} successfully loaded from disk!", groupId, artifactId, preferredVersion, (suffix != null) ? ":" + suffix : "");
99-
return;
100-
} catch (RuntimeException e) {
101-
libLog.warn("Failed to load library {}:{}:{}{} from file! Redownloading...", groupId, artifactId, preferredVersion, (suffix != null) ? ":" + suffix : "");
102-
file.delete();
103-
}
104-
}
105-
for (var repo: mavenRepositories) {
106-
try {
107-
if (!repo.endsWith("/")) repo = repo + "/";
108-
val url = new URL(String.format("%s%s/%s/%s/%s", repo, groupId.replace('.', '/'), artifactId, preferredVersion, mavenJarName));
109-
110-
val connection = (HttpsURLConnection) url.openConnection();
111-
connection.setConnectTimeout(1500);
112-
connection.setReadTimeout(1500);
113-
connection.setRequestProperty("User-Agent", "FalsePatternLib Downloader");
114-
if (connection.getResponseCode() != 200) {
115-
libLog.info("Artifact {}:{}:{}{} was not found on repo {}", groupId, artifactId, preferredVersion, (suffix != null) ? ":" + suffix : "", repo);
116-
connection.disconnect();
117-
continue;
118-
}
119-
libLog.info("Downloading {}:{}:{}{} from {}", groupId, artifactId, preferredVersion, (suffix != null) ? ":" + suffix : "", repo);
120-
download(connection.getInputStream(), file);
121-
libLog.info("Downloaded {}:{}:{}{}", groupId, artifactId, preferredVersion, (suffix != null) ? ":" + suffix : "");
122-
loadedLibraries.put(artifact, preferredVersion);
123-
loadedLibraryMods.put(artifact, loadingModId);
124-
addToClasspath(file);
125-
return;
126-
} catch (IOException ignored) {}
127-
}
128-
val errorMessage = "Failed to download library " + groupId + ":" + artifactId + ":" + preferredVersion + ((suffix != null) ? ":" + suffix : "") + " from any repository! Requested by mod: " + loadingModId;
129-
libLog.fatal(errorMessage);
130-
throw new IllegalStateException(errorMessage);
131-
}
132-
133-
private static void addToClasspath(File file) {
134-
try {
135-
val cl = (LaunchClassLoader) FalsePatternLib.class.getClassLoader();
136-
cl.addURL(file.toURI().toURL());
137-
libLog.info("Injected file {} into classpath!", file.getPath());
138-
} catch (Exception e) {
139-
throw new RuntimeException("Failed to add library to classpath: " + file.getAbsolutePath(), e);
140-
}
141-
}
142-
143-
private static void download(InputStream is, File target) throws IOException {
144-
if (target.exists()) return;
145-
146-
var bytesRead = 0;
147-
148-
val fileOutput = new BufferedOutputStream(new FileOutputStream(target));
149-
byte[] smallBuffer = new byte[4096];
150-
while ((bytesRead = is.read(smallBuffer)) >= 0) {
151-
fileOutput.write(smallBuffer, 0, bytesRead);
152-
}
153-
fileOutput.close();
154-
is.close();
155-
}
15644
}

src/main/java/com/falsepattern/lib/api/ComplexVersion.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
package com.falsepattern.lib.api;
22

3-
import com.falsepattern.lib.version.Version;
3+
import com.falsepattern.lib.dependencies.Version;
44
import lombok.NonNull;
55

66
/**
77
* Deprecated in 0.6.*, will be removed in 0.7.0.
8-
* Moved to {@link com.falsepattern.lib.version.ComplexVersion}.
8+
* Moved to {@link com.falsepattern.lib.dependencies.ComplexVersion}.
99
*/
1010
@Deprecated
11-
public class ComplexVersion extends com.falsepattern.lib.version.ComplexVersion {
11+
public class ComplexVersion extends com.falsepattern.lib.dependencies.ComplexVersion {
1212
public ComplexVersion(@NonNull Version mainVersion, Version... subVersions) {
1313
super(mainVersion, subVersions);
1414
Deprecation.warn();
Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,23 @@
11
package com.falsepattern.lib.api;
22

3-
import com.falsepattern.lib.FalsePatternLib;
4-
import com.falsepattern.lib.version.Version;
3+
import com.falsepattern.lib.dependencies.Version;
54
import lombok.Builder;
65
import lombok.NonNull;
76

87
/**
98
* Deprecated in 0.6.*, will be removed in 0.7.0.
10-
* Outdated wrapper on top of the {@link FalsePatternLib} class. Use that one instead.
9+
* Outdated wrapper on top of the {@link com.falsepattern.lib.dependencies.DependencyLoader} class. Use that one instead.
1110
*/
1211
@Deprecated
1312
public class DependencyLoader {
1413
@Builder
1514
public static void loadLibrary(String loadingModId, String groupId, String artifactId, @NonNull Version minVersion, Version maxVersion, @NonNull Version preferredVersion, String regularSuffix, String devSuffix) {
16-
FalsePatternLib.loadLibrary(loadingModId, groupId, artifactId, minVersion, maxVersion, preferredVersion, regularSuffix, devSuffix);
15+
com.falsepattern.lib.dependencies.DependencyLoader.loadLibrary(loadingModId, groupId, artifactId, minVersion, maxVersion, preferredVersion, regularSuffix, devSuffix);
1716
Deprecation.warn();
1817
}
1918

2019
public static void addMavenRepo(String url) {
21-
FalsePatternLib.addMavenRepo(url);
20+
com.falsepattern.lib.dependencies.DependencyLoader.addMavenRepo(url);
2221
Deprecation.warn();
2322
}
2423
}

src/main/java/com/falsepattern/lib/api/SemanticVersion.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
/**
44
* Deprecated in 0.6.*, will be removed in 0.7.0.
5-
* Moved to {@link com.falsepattern.lib.version.SemanticVersion}.
5+
* Moved to {@link com.falsepattern.lib.dependencies.SemanticVersion}.
66
*/
77
@Deprecated
8-
public class SemanticVersion extends com.falsepattern.lib.version.SemanticVersion {
8+
public class SemanticVersion extends com.falsepattern.lib.dependencies.SemanticVersion {
99
public SemanticVersion(int majorVersion, int minorVersion, int patchVersion, String preRelease, String build) {
1010
super(majorVersion, minorVersion, patchVersion, preRelease, build);
1111
Deprecation.warn();

src/main/java/com/falsepattern/lib/api/Version.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
/**
44
* Deprecated in 0.6.*, will be removed in 0.7.0.
5-
* Moved to {@link com.falsepattern.lib.version.SemanticVersion}.
5+
* Moved to {@link com.falsepattern.lib.dependencies.SemanticVersion}.
66
*/
77
@Deprecated
8-
public abstract class Version extends com.falsepattern.lib.version.Version {
8+
public abstract class Version extends com.falsepattern.lib.dependencies.Version {
99
protected Version() {
1010
super();
1111
Deprecation.warn();

src/main/java/com/falsepattern/lib/version/ComplexVersion.java renamed to src/main/java/com/falsepattern/lib/dependencies/ComplexVersion.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.falsepattern.lib.version;
1+
package com.falsepattern.lib.dependencies;
22

33
import lombok.NonNull;
44
import lombok.val;
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
package com.falsepattern.lib.dependencies;
2+
3+
import com.falsepattern.lib.CoreLoadingPlugin;
4+
import com.falsepattern.lib.FalsePatternLib;
5+
import lombok.NonNull;
6+
import lombok.val;
7+
import lombok.var;
8+
import net.minecraft.launchwrapper.LaunchClassLoader;
9+
10+
import javax.net.ssl.HttpsURLConnection;
11+
import java.io.*;
12+
import java.net.URL;
13+
import java.util.*;
14+
15+
16+
public class DependencyLoader {
17+
18+
private static final Map<String, Version> loadedLibraries = new HashMap<>();
19+
private static final Map<String, String> loadedLibraryMods = new HashMap<>();
20+
private static final Set<String> mavenRepositories = new HashSet<>();
21+
22+
public static void addMavenRepo(String url) {
23+
mavenRepositories.add(url);
24+
}
25+
26+
public static void loadLibrary(String loadingModId, String groupId, String artifactId, @NonNull Version minVersion, Version maxVersion, @NonNull Version preferredVersion, String regularSuffix, String devSuffix) {
27+
val suffix = FalsePatternLib.isDeveloperEnvironment() ? devSuffix : regularSuffix;
28+
FalsePatternLib.getLog().info("Adding library {}:{}:{}{}, requested by mod {}", groupId, artifactId, preferredVersion, suffix != null ? "-" + suffix : "", loadingModId);
29+
var artifact = groupId + ":" + artifactId + ":" + suffix;
30+
if (loadedLibraries.containsKey(artifact)) {
31+
val currentVer = loadedLibraries.get(artifact);
32+
if (currentVer.equals(preferredVersion)) return;
33+
val rangeString = "(minimum: " + minVersion + (maxVersion == null ? "" : ", maximum: " + maxVersion) + ")";
34+
if (minVersion.compareTo(currentVer) > 0 || (maxVersion != null && maxVersion.compareTo(currentVer) < 0)) {
35+
for (int i = 0; i < 16; i++) {
36+
FalsePatternLib.getLog().fatal("ALERT VVVVVVVVVVVV ALERT");
37+
}
38+
FalsePatternLib.getLog().fatal("Library {}:{}{} already loaded with version {}, " +
39+
"but a version in the range {} was requested! Thing may go horribly wrong! " +
40+
"Requested by mod: {}, previously loaded by mod: {}",
41+
groupId, artifactId, suffix != null ? ":" + suffix : "", currentVer,
42+
rangeString,
43+
loadingModId, loadedLibraryMods.get(artifact));
44+
for (int i = 0; i < 16; i++) {
45+
FalsePatternLib.getLog().fatal("ALERT ^^^^^^^^^^^^ ALERT");
46+
}
47+
} else {
48+
FalsePatternLib.getLog().info("Attempted loading of library {}:{}{} with preferred version {}, " +
49+
"but version {} was already loaded, which matched the range {}. This is not an error. " +
50+
"Requested by mod: {}, previously loaded by mod: {}",
51+
groupId, artifactId, suffix != null ? ":" + suffix : "", preferredVersion,
52+
currentVer, rangeString,
53+
loadingModId, loadedLibraryMods.get(artifact));
54+
}
55+
return;
56+
}
57+
val modsDir = new File(CoreLoadingPlugin.mcDir, "mods");
58+
val mavenJarName = String.format("%s-%s%s.jar", artifactId, preferredVersion, (suffix != null) ? ("-" + suffix) : "");
59+
val jarName = groupId + "-" + mavenJarName;
60+
val libDir = new File(modsDir, "falsepattern");
61+
if (!libDir.exists()) {
62+
libDir.mkdirs();
63+
}
64+
val file = new File(libDir, jarName);
65+
if (file.exists()) {
66+
try {
67+
addToClasspath(file);
68+
loadedLibraries.put(artifact, preferredVersion);
69+
FalsePatternLib.getLog().info("Library {}:{}:{}{} successfully loaded from disk!", groupId, artifactId, preferredVersion, (suffix != null) ? ":" + suffix : "");
70+
return;
71+
} catch (RuntimeException e) {
72+
FalsePatternLib.getLog().warn("Failed to load library {}:{}:{}{} from file! Redownloading...", groupId, artifactId, preferredVersion, (suffix != null) ? ":" + suffix : "");
73+
file.delete();
74+
}
75+
}
76+
for (var repo: mavenRepositories) {
77+
try {
78+
if (!repo.endsWith("/")) repo = repo + "/";
79+
val url = new URL(String.format("%s%s/%s/%s/%s", repo, groupId.replace('.', '/'), artifactId, preferredVersion, mavenJarName));
80+
81+
val connection = (HttpsURLConnection) url.openConnection();
82+
connection.setConnectTimeout(1500);
83+
connection.setReadTimeout(1500);
84+
connection.setRequestProperty("User-Agent", "FalsePatternLib Downloader");
85+
if (connection.getResponseCode() != 200) {
86+
FalsePatternLib.getLog().info("Artifact {}:{}:{}{} was not found on repo {}", groupId, artifactId, preferredVersion, (suffix != null) ? ":" + suffix : "", repo);
87+
connection.disconnect();
88+
continue;
89+
}
90+
FalsePatternLib.getLog().info("Downloading {}:{}:{}{} from {}", groupId, artifactId, preferredVersion, (suffix != null) ? ":" + suffix : "", repo);
91+
download(connection.getInputStream(), file);
92+
FalsePatternLib.getLog().info("Downloaded {}:{}:{}{}", groupId, artifactId, preferredVersion, (suffix != null) ? ":" + suffix : "");
93+
loadedLibraries.put(artifact, preferredVersion);
94+
loadedLibraryMods.put(artifact, loadingModId);
95+
addToClasspath(file);
96+
return;
97+
} catch (IOException ignored) {}
98+
}
99+
val errorMessage = "Failed to download library " + groupId + ":" + artifactId + ":" + preferredVersion + ((suffix != null) ? ":" + suffix : "") + " from any repository! Requested by mod: " + loadingModId;
100+
FalsePatternLib.getLog().fatal(errorMessage);
101+
throw new IllegalStateException(errorMessage);
102+
}
103+
104+
private static void addToClasspath(File file) {
105+
try {
106+
val cl = (LaunchClassLoader) DependencyLoader.class.getClassLoader();
107+
cl.addURL(file.toURI().toURL());
108+
FalsePatternLib.getLog().info("Injected file {} into classpath!", file.getPath());
109+
} catch (Exception e) {
110+
throw new RuntimeException("Failed to add library to classpath: " + file.getAbsolutePath(), e);
111+
}
112+
}
113+
114+
private static void download(InputStream is, File target) throws IOException {
115+
if (target.exists()) return;
116+
117+
var bytesRead = 0;
118+
119+
val fileOutput = new BufferedOutputStream(new FileOutputStream(target));
120+
byte[] smallBuffer = new byte[4096];
121+
while ((bytesRead = is.read(smallBuffer)) >= 0) {
122+
fileOutput.write(smallBuffer, 0, bytesRead);
123+
}
124+
fileOutput.close();
125+
is.close();
126+
}
127+
}

src/main/java/com/falsepattern/lib/version/SemanticVersion.java renamed to src/main/java/com/falsepattern/lib/dependencies/SemanticVersion.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.falsepattern.lib.version;
1+
package com.falsepattern.lib.dependencies;
22

33
import lombok.Builder;
44
import lombok.Getter;

src/main/java/com/falsepattern/lib/version/Version.java renamed to src/main/java/com/falsepattern/lib/dependencies/Version.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.falsepattern.lib.version;
1+
package com.falsepattern.lib.dependencies;
22

33
public abstract class Version implements Comparable<Version> {
44
protected Version(){}

0 commit comments

Comments
 (0)