Skip to content

Commit dd170e7

Browse files
committed
added update checks and separate internet library
1 parent c02b787 commit dd170e7

File tree

7 files changed

+204
-98
lines changed

7 files changed

+204
-98
lines changed

dependencies.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@ dependencies {
55
compileOnly("org.spongepowered:mixin:0.7.11-SNAPSHOT") {
66
transitive = false
77
}
8+
compileOnly("com.falsepattern:json:0.4.1")
89
annotationProcessor("org.projectlombok:lombok:1.18.22")
910
}

repositories.gradle

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,8 @@ repositories {
33
name = "sponge"
44
url = "https://sponge.falsepattern.com/"
55
}
6+
maven {
7+
name = "mavenpattern"
8+
url = "https://maven.falsepattern.com/"
9+
}
610
}

src/main/java/com/falsepattern/lib/dependencies/DependencyLoader.java

Lines changed: 33 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
package com.falsepattern.lib.dependencies;
22

33
import com.falsepattern.lib.StableAPI;
4-
import com.falsepattern.lib.internal.CoreLoadingPlugin;
5-
import com.falsepattern.lib.internal.FalsePatternLib;
4+
import com.falsepattern.lib.internal.*;
5+
66
import java.io.BufferedOutputStream;
77
import java.io.File;
8-
import java.io.FileOutputStream;
98
import java.io.IOException;
109
import java.io.InputStream;
1110
import java.net.URL;
@@ -14,13 +13,8 @@
1413
import java.util.HashSet;
1514
import java.util.Map;
1615
import java.util.Set;
17-
import javax.net.ssl.HttpsURLConnection;
1816

19-
import com.falsepattern.lib.internal.LibraryConfig;
20-
import lombok.Builder;
21-
import lombok.NonNull;
22-
import lombok.val;
23-
import lombok.var;
17+
import lombok.*;
2418
import net.minecraft.launchwrapper.LaunchClassLoader;
2519

2620

@@ -36,21 +30,18 @@ public static void addMavenRepo(String url) {
3630
}
3731

3832
@Builder
39-
public static void loadLibrary(String loadingModId,
40-
String groupId,
41-
String artifactId,
33+
public static void loadLibrary(@NonNull String loadingModId,
34+
@NonNull String groupId,
35+
@NonNull String artifactId,
4236
@NonNull Version minVersion,
4337
Version maxVersion,
4438
@NonNull Version preferredVersion,
4539
String regularSuffix,
4640
String devSuffix) {
4741
val suffix = FalsePatternLib.isDeveloperEnvironment() ? devSuffix : regularSuffix;
42+
val artifactLogName = String.format("%s:%s:%s%s", groupId, artifactId, preferredVersion, suffix != null ? "-" + suffix : "");
4843
FalsePatternLib.getLog()
49-
.info("Adding library {}:{}:{}{}, requested by mod {}",
50-
groupId,
51-
artifactId,
52-
preferredVersion,
53-
suffix != null ? "-" + suffix : "",
44+
.info("Adding library {}, requested by mod {}", artifactLogName,
5445
loadingModId);
5546
var artifact = groupId + ":" + artifactId + ":" + suffix;
5647
if (loadedLibraries.containsKey(artifact)) {
@@ -110,19 +101,11 @@ public static void loadLibrary(String loadingModId,
110101
addToClasspath(file);
111102
loadedLibraries.put(artifact, preferredVersion);
112103
FalsePatternLib.getLog()
113-
.info("Library {}:{}:{}{} successfully loaded from disk!",
114-
groupId,
115-
artifactId,
116-
preferredVersion,
117-
(suffix != null) ? ":" + suffix : "");
104+
.info("Library {} successfully loaded from disk!", artifactLogName);
118105
return;
119106
} catch (RuntimeException e) {
120107
FalsePatternLib.getLog()
121-
.warn("Failed to load library {}:{}:{}{} from file! Redownloading...",
122-
groupId,
123-
artifactId,
124-
preferredVersion,
125-
(suffix != null) ? ":" + suffix : "");
108+
.warn("Failed to load library {} from file! Redownloading...", artifactLogName);
126109
if (!file.delete()) {
127110
FalsePatternLib.getLog().fatal("Failed to delete file {}", file);
128111
throw new RuntimeException("Failed to delete file " + file);
@@ -131,9 +114,9 @@ public static void loadLibrary(String loadingModId,
131114
}
132115
if (!LibraryConfig.ENABLE_LIBRARY_DOWNLOADS) {
133116
val errorMessage = "Failed to load library " + groupId + ":" + artifactId + ":" + preferredVersion +
134-
((suffix != null) ? ":" + suffix : "") + ": FalsePatternLib library downloading has " +
135-
"been disabled in the config, and the library is not present on disk! Requested by mod: " +
136-
loadingModId;
117+
((suffix != null) ? ":" + suffix : "") + ": " + Tags.MODNAME +
118+
" library downloading has been disabled in the config, and the library is not present " +
119+
"on disk! Requested by mod: " + loadingModId;
137120
FalsePatternLib.getLog().fatal(errorMessage);
138121
throw new IllegalStateException(errorMessage);
139122
}
@@ -148,39 +131,24 @@ public static void loadLibrary(String loadingModId,
148131
artifactId,
149132
preferredVersion,
150133
mavenJarName));
151-
152-
val connection = (HttpsURLConnection) url.openConnection();
153-
connection.setConnectTimeout(1500);
154-
connection.setReadTimeout(1500);
155-
connection.setRequestProperty("User-Agent", "FalsePatternLib Downloader");
156-
if (connection.getResponseCode() != 200) {
134+
String finalRepo = repo;
135+
Internet.connect(url, (ex) -> {
157136
FalsePatternLib.getLog()
158-
.info("Artifact {}:{}:{}{} was not found on repo {}",
159-
groupId,
160-
artifactId,
161-
preferredVersion,
162-
(suffix != null) ? ":" + suffix : "",
163-
repo);
164-
connection.disconnect();
165-
continue;
166-
}
167-
FalsePatternLib.getLog()
168-
.info("Downloading {}:{}:{}{} from {}",
169-
groupId,
170-
artifactId,
171-
preferredVersion,
172-
(suffix != null) ? ":" + suffix : "",
173-
repo);
174-
download(connection.getInputStream(), file);
175-
FalsePatternLib.getLog()
176-
.info("Downloaded {}:{}:{}{}",
177-
groupId,
178-
artifactId,
179-
preferredVersion,
180-
(suffix != null) ? ":" + suffix : "");
181-
loadedLibraries.put(artifact, preferredVersion);
182-
loadedLibraryMods.put(artifact, loadingModId);
183-
addToClasspath(file);
137+
.info("Artifact {} could not be downloaded from repo {}: {}",
138+
artifactLogName,
139+
finalRepo,
140+
ex.getMessage());
141+
}, (input) -> {
142+
FalsePatternLib.getLog()
143+
.info("Downloading {} from {}",
144+
artifactLogName,
145+
finalRepo);
146+
download(input, file);
147+
FalsePatternLib.getLog().info("Downloaded {}", artifactLogName);
148+
loadedLibraries.put(artifact, preferredVersion);
149+
loadedLibraryMods.put(artifact, loadingModId);
150+
addToClasspath(file);
151+
});
184152
return;
185153
} catch (IOException ignored) {
186154
}
@@ -202,19 +170,11 @@ private static void addToClasspath(File file) {
202170
}
203171
}
204172

205-
private static void download(InputStream is, File target) throws IOException {
173+
@SneakyThrows
174+
private static void download(InputStream is, File target) {
206175
if (target.exists()) {
207176
return;
208177
}
209-
210-
var bytesRead = 0;
211-
212-
val fileOutput = new BufferedOutputStream(Files.newOutputStream(target.toPath()));
213-
byte[] smallBuffer = new byte[4096];
214-
while ((bytesRead = is.read(smallBuffer)) >= 0) {
215-
fileOutput.write(smallBuffer, 0, bytesRead);
216-
}
217-
fileOutput.close();
218-
is.close();
178+
Internet.transferAndClose(is, new BufferedOutputStream(Files.newOutputStream(target.toPath())));
219179
}
220180
}

src/main/java/com/falsepattern/lib/internal/FalsePatternLib.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import com.falsepattern.lib.config.ConfigException;
44
import com.falsepattern.lib.config.ConfigurationManager;
5+
import com.falsepattern.lib.updates.ModUpdateInfo;
6+
import com.falsepattern.lib.updates.UpdateChecker;
57
import com.falsepattern.lib.util.ResourceUtil;
68
import com.google.common.eventbus.EventBus;
79
import com.google.common.eventbus.Subscribe;
@@ -15,10 +17,15 @@
1517
import cpw.mods.fml.common.event.FMLPreInitializationEvent;
1618
import lombok.Getter;
1719
import lombok.SneakyThrows;
20+
import lombok.val;
1821
import net.minecraft.launchwrapper.Launch;
1922
import org.apache.logging.log4j.LogManager;
2023
import org.apache.logging.log4j.Logger;
2124

25+
import java.util.List;
26+
import java.util.concurrent.ExecutionException;
27+
import java.util.concurrent.Future;
28+
2229
/**
2330
* Utility class used by FalsePatternLib's internal code. This can change between versions without notice, so do not use
2431
* this in your code!
@@ -30,6 +37,8 @@ public class FalsePatternLib extends DummyModContainer {
3037
@Getter private static final boolean developerEnvironment =
3138
(boolean) Launch.blackboard.get("fml.deobfuscatedEnvironment");
3239

40+
private Future<List<ModUpdateInfo>> updateCheckFuture;
41+
3342
public FalsePatternLib() {
3443
super(MetadataCollection.from(ResourceUtil.getResourceFromJar("/mcmod.info", FalsePatternLib.class), Tags.MODID)
3544
.getMetadataForId(Tags.MODID, null));
@@ -47,9 +56,27 @@ public void construct(FMLConstructionEvent e) {
4756
}
4857
}
4958

59+
@Subscribe
60+
public void preInit(FMLPreInitializationEvent e) {
61+
if (LibraryConfig.ENABLE_UPDATE_CHECKER) {
62+
getLog().info("Launching asynchronous update check. I'll check back on it during postInit.");
63+
updateCheckFuture = UpdateChecker.fetchUpdatesAsync("https://falsepattern.com/mc/versions.json");
64+
}
65+
}
66+
5067
@Subscribe
5168
public void postInit(FMLPostInitializationEvent e) {
69+
if (updateCheckFuture != null && !updateCheckFuture.isCancelled()) {
70+
try {
71+
val updates = updateCheckFuture.get();
72+
if (updates != null)
73+
for (val update: updates)
74+
update.log(getLog());
75+
} catch (InterruptedException | ExecutionException ex) {
76+
getLog().warn("Error while checking updates", ex);
77+
}
5278

79+
}
5380
}
5481

5582
@SuppressWarnings("UnstableApiUsage")
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.falsepattern.lib.internal;
2+
3+
import lombok.val;
4+
import lombok.var;
5+
6+
import java.io.*;
7+
import java.net.HttpURLConnection;
8+
import java.net.URL;
9+
import java.util.function.Consumer;
10+
11+
public class Internet {
12+
public static void connect(URL URL, Consumer<Exception> onError, Consumer<InputStream> onSuccess) {
13+
try {
14+
val connection = (HttpURLConnection) URL.openConnection();
15+
connection.setConnectTimeout(3500);
16+
connection.setReadTimeout(5000);
17+
connection.setRequestProperty("User-Agent", "FalsePatternLib Internet Connector");
18+
if (connection.getResponseCode() != 200) {
19+
onError.accept(new Exception("HTTP response code " + connection.getResponseCode()));
20+
} else {
21+
onSuccess.accept(connection.getInputStream());
22+
}
23+
connection.disconnect();
24+
} catch (Exception e) {
25+
onError.accept(e);
26+
}
27+
}
28+
29+
30+
public static void transferAndClose(InputStream is, OutputStream target) throws IOException {
31+
var bytesRead = 0;
32+
33+
byte[] smallBuffer = new byte[4096];
34+
while ((bytesRead = is.read(smallBuffer)) >= 0) {
35+
target.write(smallBuffer, 0, bytesRead);
36+
}
37+
target.close();
38+
is.close();
39+
}
40+
}
Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,22 @@
11
package com.falsepattern.lib.updates;
22

33
import lombok.Data;
4+
import lombok.NonNull;
5+
import org.apache.logging.log4j.Logger;
46

57
@Data
68
public class ModUpdateInfo {
7-
public final String modID;
8-
public final String currentVersion;
9-
public final String latestVersion;
10-
public final String updateURL;
9+
@NonNull public final String modID;
10+
@NonNull public final String currentVersion;
11+
@NonNull public final String latestVersion;
12+
@NonNull public final String updateURL;
13+
14+
public void log(Logger logger) {
15+
logger.info("Updates are available for mod {}: Currently installed version is {}, " +
16+
"latest available version is {}. Update URL: {}",
17+
modID,
18+
currentVersion,
19+
latestVersion,
20+
updateURL.isEmpty() ? "unavailable": updateURL);
21+
}
1122
}

0 commit comments

Comments
 (0)