Skip to content

Commit ed7ca48

Browse files
committed
Begin working on the update checker
Also added a config for toggling internet capabilities
1 parent 779d815 commit ed7ca48

File tree

5 files changed

+135
-0
lines changed

5 files changed

+135
-0
lines changed

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

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

3+
import com.falsepattern.lib.config.ConfigException;
34
import com.falsepattern.lib.config.ConfigurationManager;
45
import com.falsepattern.lib.util.ResourceUtil;
56
import com.google.common.eventbus.EventBus;
67
import com.google.common.eventbus.Subscribe;
78
import cpw.mods.fml.common.DummyModContainer;
89
import cpw.mods.fml.common.LoadController;
10+
import cpw.mods.fml.common.Loader;
911
import cpw.mods.fml.common.MetadataCollection;
1012
import cpw.mods.fml.common.event.FMLConstructionEvent;
13+
import cpw.mods.fml.common.event.FMLInterModComms;
14+
import cpw.mods.fml.common.event.FMLPostInitializationEvent;
15+
import cpw.mods.fml.common.event.FMLPreInitializationEvent;
1116
import lombok.Getter;
17+
import lombok.SneakyThrows;
1218
import net.minecraft.launchwrapper.Launch;
1319
import org.apache.logging.log4j.LogManager;
1420
import org.apache.logging.log4j.Logger;
@@ -17,6 +23,7 @@
1723
* Utility class used by FalsePatternLib's internal code. This can change between versions without notice, so do not use
1824
* this in your code!
1925
*/
26+
@SuppressWarnings("UnstableApiUsage")
2027
public class FalsePatternLib extends DummyModContainer {
2128
@Getter private static final Logger log = LogManager.getLogger(Tags.MODNAME);
2229

@@ -33,6 +40,16 @@ public FalsePatternLib() {
3340
@Subscribe
3441
public void construct(FMLConstructionEvent e) {
3542
ConfigurationManager.init();
43+
try {
44+
ConfigurationManager.registerConfig(LibraryConfig.class);
45+
} catch (ConfigException ex) {
46+
getLog().error("Failed to register " + Tags.MODNAME + " config!", ex);
47+
}
48+
}
49+
50+
@Subscribe
51+
public void postInit(FMLPostInitializationEvent e) {
52+
3653
}
3754

3855
@SuppressWarnings("UnstableApiUsage")
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.falsepattern.lib.internal;
2+
3+
import com.falsepattern.lib.config.Config;
4+
5+
@Config(modid = Tags.MODID)
6+
public class LibraryConfig {
7+
@Config.Comment({"Used to control whether FalsePatternLib should check for outdated mods.",
8+
"If you're building a public modpack, you should turn this off so that your users don't " +
9+
"get nagged about outdated mods."})
10+
@Config.LangKey("config.falsepatternlib.updatecheck")
11+
public static boolean ENABLE_UPDATE_CHECKER = true;
12+
13+
@Config.Comment({"Used to control whether FalsePatternLib should be allowed to use the internet.",
14+
"If this is enabled, library downloads will be blocked.",
15+
"Note that if a mod tries to download a library that is not downloaded yet, the game will crash."})
16+
@Config.LangKey("config.falsepatternlib.disableinternet")
17+
public static boolean ENABLE_LIBRARY_DOWNLOADS = true;
18+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.falsepattern.lib.updates;
2+
3+
import lombok.Data;
4+
5+
@Data
6+
public class ModUpdateInfo {
7+
public final String modID;
8+
public final String currentVersion;
9+
public final String latestVersion;
10+
public final String updateURL;
11+
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package com.falsepattern.lib.updates;
2+
3+
import com.falsepattern.lib.dependencies.DependencyLoader;
4+
import com.falsepattern.lib.dependencies.SemanticVersion;
5+
import com.falsepattern.lib.internal.FalsePatternLib;
6+
import com.falsepattern.lib.internal.LibraryConfig;
7+
import com.falsepattern.lib.internal.Tags;
8+
import lombok.val;
9+
import lombok.var;
10+
11+
import java.io.BufferedOutputStream;
12+
import java.io.File;
13+
import java.io.IOException;
14+
import java.io.InputStream;
15+
import java.net.URL;
16+
import java.nio.file.Files;
17+
import java.util.List;
18+
import java.util.concurrent.ExecutorService;
19+
import java.util.concurrent.Executors;
20+
import java.util.concurrent.Future;
21+
import java.util.concurrent.atomic.AtomicBoolean;
22+
23+
public class UpdateChecker {
24+
private static AtomicBoolean jsonLibraryLoaded = new AtomicBoolean(false);
25+
private static final ExecutorService asyncExecutor = Executors.newSingleThreadExecutor((runnable) -> {
26+
Thread thread = new Thread(runnable);
27+
thread.setDaemon(true);
28+
thread.setName(Tags.MODNAME + " Asynchronous Update Check Thread");
29+
return thread;
30+
});
31+
/**
32+
* Same this as {@link #fetchUpdates(String)}, but defers the check to a different thread. Useful for asynchronous
33+
* update checks, if you don't want to block loading.
34+
* @param url The URL to check
35+
* @return A future that will contain the update info about mods that were both available on the URL and installed
36+
*/
37+
public static Future<List<ModUpdateInfo>> fetchUpdatesAsync(String url) {
38+
return asyncExecutor.submit(() -> fetchUpdates(url));
39+
}
40+
41+
/**
42+
* Checks for updates. The URL should be a JSON file that contains a list of mods, each with a mod ID, one or more
43+
* versions, and a URL for the user to check for updates in case the current and latest versions are different.
44+
* The JSON file must have the following format:
45+
* <pre>{@code
46+
* [
47+
* {
48+
* "modID": "modid",
49+
* "latestVersion": ["1.0.0", "1.0.0-foo"],
50+
* "updateURL": "https://example.com/mods/mymod"
51+
* },
52+
* {
53+
* "modID": "modid2",
54+
* "latestVersion": ["0.2.0", "0.3.0-alpha"],
55+
* "updateURL": "https://example.com/mods/mymod2"
56+
* },
57+
* ...etc, one json object per mod.
58+
* ]
59+
* }</pre>
60+
* @param url The URL to check
61+
* @return A list of mods that were both available on the URL and installed
62+
*/
63+
public static List<ModUpdateInfo> fetchUpdates(String url) {
64+
if (!LibraryConfig.ENABLE_UPDATE_CHECKER) {
65+
return null;
66+
}
67+
if (!jsonLibraryLoaded.get()) {
68+
DependencyLoader.addMavenRepo("https://maven.falsepattern.com/");
69+
try {
70+
DependencyLoader.builder()
71+
.groupId("com.falsepattern")
72+
.artifactId("json")
73+
.minVersion(new SemanticVersion(0, 4, 0))
74+
.maxVersion(new SemanticVersion(0, Integer.MAX_VALUE, Integer.MAX_VALUE))
75+
.build();
76+
} catch (Exception e) {
77+
FalsePatternLib.getLog().error("Failed to load json library for update checker!", e);
78+
return null;
79+
}
80+
jsonLibraryLoaded.set(true);
81+
}
82+
//TODO
83+
return null;
84+
}
85+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
config.falsepatternlib.updatecheck=Enable update checks
2+
config.falsepatternlib.updatecheck.tooltip=Used to control whether FalsePatternLib should check for outdated mods.
3+
config.falsepatternlib.disableinternet=FalsePatternLib Offline Mode
4+
config.falsepatternlib.disableinternet.tooltip=Used to control whether FalsePatternLib should be allowed to use the internet. If this is enabled, update checks and library downloads will be completely blocked.

0 commit comments

Comments
 (0)