Introduce a new centralized version checking system.

Using the @Mod annotation mods can opt-in to a centrally controlled update system.
This is PURELY a notification system and will NOT automatically download any updates.
The End User can control which mods check for updates and disabel the system entirely using the Forge Config and GUI.
Format for the json the URL must point to is described here: https://gist.github.com/LexManos/7aacb9aa991330523884
This commit is contained in:
Lex Manos committed 2015-11-04 14:37:15 -08:00
1 parent 479c7f8b54
commit d790008353
11 files changed
+291 -41

No files matched your search

@@ -12,13 +12,14 @@ import static net.minecraftforge.common.ForgeVersion.revisionVersion;
import static net.minecraftforge.common.config.Configuration.CATEGORY_GENERAL;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.security.cert.Certificate;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import net.minecraft.init.Blocks;
import net.minecraft.nbt.NBTBase;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.world.storage.SaveHandler;
@@ -58,6 +59,7 @@ import net.minecraftforge.fml.common.network.NetworkRegistry;
public class ForgeModContainer extends DummyModContainer implements WorldAccessContainer
{
public static final String VERSION_CHECK_CAT = "version_checking";
public static int clumpingThreshold = 64;
public static boolean removeErroringEntities = false;
public static boolean removeErroringTileEntities = false;
@@ -73,6 +75,13 @@ public class ForgeModContainer extends DummyModContainer implements WorldAccessC
public static boolean forgeLightPipelineEnabled = true;
private static Configuration config;
private static ForgeModContainer INSTANCE;
public static ForgeModContainer getInstance()
{
return INSTANCE;
}
private URL updateJSONUrl = null;
public ForgeModContainer()
{
@@ -82,7 +91,7 @@ public class ForgeModContainer extends DummyModContainer implements WorldAccessC
meta.name = "Minecraft Forge";
meta.version = String.format("%d.%d.%d.%d", majorVersion, minorVersion, revisionVersion, buildVersion);
meta.credits = "Made possible with help from many people";
meta.authorList = Arrays.asList("LexManos", "Eloraam", "Spacetoad");
meta.authorList = Arrays.asList("LexManos", "Cpw");
meta.description = "Minecraft Forge is a common open source API allowing a broad range of mods " +
"to work cooperatively together. It allows many mods to be created without " +
"them editing the main Minecraft code.";
@@ -90,12 +99,17 @@ public class ForgeModContainer extends DummyModContainer implements WorldAccessC
meta.updateUrl = "http://MinecraftForge.net/forum/index.php/topic,5.0.html";
meta.screenshots = new String[0];
meta.logoFile = "/forge_logo.png";
try {
updateJSONUrl = new URL("http://files.minecraftforge.net/maven/net/minecraftforge/forge/promotions_slim.json");
} catch (MalformedURLException e) {}
config = null;
File cfgFile = new File(Loader.instance().getConfigDir(), "forge.cfg");
config = new Configuration(cfgFile);
syncConfig(true);
INSTANCE = this;
}
@Override
@@ -230,6 +244,12 @@ public class ForgeModContainer extends DummyModContainer implements WorldAccessC
config.setCategoryPropertyOrder(CATEGORY_GENERAL, propOrder);
propOrder = new ArrayList<String>();
prop = config.get(VERSION_CHECK_CAT, "Global", true, "Enable the entire mod update check system. This only applies to mods using the Forge system.");
propOrder.add("Global");
config.setCategoryPropertyOrder(VERSION_CHECK_CAT, propOrder);
if (config.hasChanged())
{
config.save();
@@ -254,6 +274,10 @@ public class ForgeModContainer extends DummyModContainer implements WorldAccessC
ForgeChunkManager.syncConfigDefaults();
ForgeChunkManager.loadConfiguration();
}
else if (VERSION_CHECK_CAT.equals(event.configID))
{
syncConfig(false);
}
}
}
@@ -396,4 +420,10 @@ public class ForgeModContainer extends DummyModContainer implements WorldAccessC
Certificate[] certificates = getClass().getProtectionDomain().getCodeSource().getCertificates();
return certificates != null ? certificates[0] : null;
}
@Override
public URL getUpdateUrl()
{
return updateJSONUrl;
}
}
@@ -8,13 +8,25 @@ import static net.minecraftforge.common.ForgeVersion.Status.*;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;
import org.apache.logging.log4j.Level;
import com.google.common.io.ByteStreams;
import com.google.gson.Gson;
import net.minecraftforge.fml.common.versioning.ArtifactVersion;
import net.minecraftforge.fml.common.versioning.DefaultArtifactVersion;
import net.minecraftforge.fml.common.FMLLog;
import net.minecraftforge.fml.common.InjectedModContainer;
import net.minecraftforge.fml.common.Loader;
import net.minecraftforge.fml.common.ModContainer;
import net.minecraftforge.fml.common.versioning.ComparableVersion;
public class ForgeVersion
{
@@ -55,12 +67,13 @@ public class ForgeVersion
public static Status getStatus()
{
return status;
return getResult(ForgeModContainer.getInstance()).status;
}
public static String getTarget()
{
return target;
CheckResult res = getResult(ForgeModContainer.getInstance());
return res.target != null ? res.target.toString() : null;
}
public static String getVersion()
@@ -79,32 +92,75 @@ public class ForgeVersion
BETA_OUTDATED
}
public static class CheckResult
{
public final Status status;
public final ComparableVersion target;
public final Map<ComparableVersion, String> changes;
public final String url;
private CheckResult(Status status, ComparableVersion target, Map<ComparableVersion, String> changes, String url)
{
this.status = status;
this.target = target;
this.changes = changes == null ? null : Collections.unmodifiableMap(changes);
this.url = url;
}
}
public static void startVersionCheck()
{
new Thread("Forge Version Check")
{
@SuppressWarnings("unchecked")
@Override
public void run()
{
if (!ForgeModContainer.getConfig().get(ForgeModContainer.VERSION_CHECK_CAT, "Global", true).getBoolean())
{
FMLLog.log("ForgeVersionCheck", Level.INFO, "Global Forge version check system disabeld, no futher processing.");
return;
}
for (Entry<ModContainer, URL> entry : gatherMods().entrySet())
{
ModContainer mod = entry.getKey();
if (ForgeModContainer.getConfig().get(ForgeModContainer.VERSION_CHECK_CAT, mod.getModId(), true).getBoolean())
{
process(mod, entry.getValue());
}
else
{
FMLLog.log("ForgeVersionCheck", Level.INFO, "[%s] Skipped version check", mod.getModId());
}
}
}
private void process(ModContainer mod, URL url)
{
try
{
URL url = new URL("http://files.minecraftforge.net/maven/net/minecraftforge/forge/promotions_slim.json");
FMLLog.log("ForgeVersionCheck", Level.INFO, "[%s] Starting version check at %s", mod.getModId(), url.toString());
Status status = PENDING;
ComparableVersion target = null;
InputStream con = url.openStream();
String data = new String(ByteStreams.toByteArray(con));
con.close();
FMLLog.log("ForgeVersionCheck", Level.DEBUG, "[%s] Received version check data:\n%s", mod.getModId(), data);
Map<String, Object> json = new Gson().fromJson(data, Map.class);
//String homepage = (String)json.get("homepage");
Map<String, String> promos = (Map<String, String>)json.get("promos");
String display_url = (String)json.get("homepage");
String rec = promos.get(MinecraftForge.MC_VERSION + "-recommended");
String lat = promos.get(MinecraftForge.MC_VERSION + "-latest");
ArtifactVersion current = new DefaultArtifactVersion(getVersion());
ComparableVersion current = new ComparableVersion(mod.getVersion());
if (rec != null)
{
ArtifactVersion recommended = new DefaultArtifactVersion(rec);
ComparableVersion recommended = new ComparableVersion(rec);
int diff = recommended.compareTo(current);
if (diff == 0)
@@ -114,39 +170,95 @@ public class ForgeVersion
status = AHEAD;
if (lat != null)
{
if (current.compareTo(new DefaultArtifactVersion(lat)) < 0)
ComparableVersion latest = new ComparableVersion(lat);
if (current.compareTo(latest) < 0)
{
status = OUTDATED;
target = lat;
target = latest;
}
}
}
else
{
status = OUTDATED;
target = rec;
target = recommended;
}
}
else if (lat != null)
{
if (current.compareTo(new DefaultArtifactVersion(lat)) < 0)
ComparableVersion latest = new ComparableVersion(lat);
if (current.compareTo(latest) < 0)
{
status = BETA_OUTDATED;
target = lat;
target = latest;
}
else
status = BETA;
}
else
status = BETA;
FMLLog.log("ForgeVersionCheck", Level.INFO, "[%s] Found status: %s Target: %s", mod.getModId(), status, target);
Map<ComparableVersion, String> changes = new LinkedHashMap<ComparableVersion, String>();
Map<String, String> tmp = (Map<String, String>)json.get(MinecraftForge.MC_VERSION);
if (tmp != null)
{
List<ComparableVersion> ordered = new ArrayList<ComparableVersion>();
for (String key : tmp.keySet())
{
ComparableVersion ver = new ComparableVersion(key);
if (ver.compareTo(current) > 0 && (target == null || ver.compareTo(target) < 1))
{
ordered.add(ver);
}
}
Collections.sort(ordered);
for (ComparableVersion ver : ordered)
{
changes.put(ver, tmp.get(ver.toString()));
}
}
if (mod instanceof InjectedModContainer)
mod = ((InjectedModContainer)mod).wrappedContainer;
results.put(mod, new CheckResult(status, target, changes, display_url));
}
catch (Exception e)
{
e.printStackTrace();
FMLLog.log("ForgeVersionCheck", Level.DEBUG, e, "Failed to process update information");
status = FAILED;
}
}
}.start();
}
// Gather a list of mods that have opted in to this update system by providing a URL.
// Small hack needed to support a interface change until we force a recompile.
public static Map<ModContainer, URL> gatherMods()
{
Map<ModContainer, URL> ret = new HashMap<ModContainer, URL>();
for (ModContainer mod : Loader.instance().getActiveModList())
{
URL url = null;
try {
url = mod.getUpdateUrl();
} catch (AbstractMethodError abs) { } //TODO: Remove this in 1.8.8+?
if (url != null)
ret.put(mod, url);
}
return ret;
}
private static Map<ModContainer, CheckResult> results = new ConcurrentHashMap<ModContainer, CheckResult>();
private static final CheckResult PENDING_CHECK = new CheckResult(PENDING, null, null, null);
public static CheckResult getResult(ModContainer mod)
{
if (mod instanceof InjectedModContainer)
mod = ((InjectedModContainer)mod).wrappedContainer;
CheckResult ret = results.get(mod);
return ret == null ? PENDING_CHECK : ret;
}
}