BiomesOPlenty/src/minecraft/biomesoplenty/helpers/Version.java

234 lines
6.4 KiB
Java
Raw Normal View History

2013-06-14 13:17:48 +00:00
package biomesoplenty.helpers;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.logging.Level;
import net.minecraft.world.World;
import net.minecraft.world.WorldType;
2013-06-14 13:17:48 +00:00
import net.minecraftforge.common.Property;
import biomesoplenty.configuration.BOPConfiguration;
import biomesoplenty.world.WorldTypeBOP;
2013-06-14 13:17:48 +00:00
import cpw.mods.fml.common.FMLCommonHandler;
import cpw.mods.fml.common.Loader;
public class Version implements Runnable {
private static Version instance = new Version();
public enum EnumUpdateState
{
CURRENT, OUTDATED, CONNECTION_ERROR
}
public static final String VERSION = "0.5.6";
private static final String REMOTE_VERSION_FILE = "https://raw.github.com/BiomesOPlenty/BiomesOPlenty/master/version.txt";
private static final String REMOTE_CHANGELOG_ROOT = "https://raw.github.com/BiomesOPlenty/BiomesOPlenty/master/changelog/";
public static EnumUpdateState currentVersion = EnumUpdateState.CURRENT;
private static String recommendedVersion;
private static String[] cachedChangelog;
public static String getVersion()
{
return VERSION;
}
public static boolean isOutdated()
{
return currentVersion == EnumUpdateState.OUTDATED;
}
public static boolean needsUpdateNoticeAndMarkAsSeen()
{
if (!isOutdated())
return false;
Property property = BOPConfiguration.config.get("Vars", "Seen Version", VERSION);
String seenVersion = property.getString();
if (recommendedVersion == null || recommendedVersion.equals(seenVersion))
return false;
property.set(recommendedVersion);
BOPConfiguration.config.save();
return true;
}
public static boolean needsBOPWorldtypeAndMarkAsSeen(World world)
{
WorldType terrainType = world.getWorldInfo().getTerrainType();
WorldTypeBOP bopType = new WorldTypeBOP();
Property property = BOPConfiguration.config.get("Vars", "Seen WorldType Msg", false);
String worldTypeProp = property.getString();
if (terrainType == null || property.getBoolean(false) == true)
return false;
if (terrainType.getWorldTypeName().equals(bopType.getWorldTypeName()))
{
property.set(true);
BOPConfiguration.config.save();
return false;
}
property.set(true);
BOPConfiguration.config.save();
return true;
}
2013-06-14 13:17:48 +00:00
public static String getRecommendedVersion()
{
return recommendedVersion;
}
public static void versionCheck()
{
try {
if ("0.0.0".equals(VERSION))
return;
String location = REMOTE_VERSION_FILE;
HttpURLConnection conn = null;
while (location != null && !location.isEmpty()) {
URL url = new URL(location);
conn = (HttpURLConnection) url.openConnection();
conn.setRequestProperty(
"User-Agent",
"Mozilla/5.0 (Windows; U; Windows NT 6.0; ru; rv:1.9.0.11) Gecko/2009060215 Firefox/3.0.11 (.NET CLR 3.5.30729)");
conn.connect();
location = conn.getHeaderField("Location");
}
BufferedReader reader = new BufferedReader(new InputStreamReader(
conn.getInputStream()));
String line = null;
String mcVersion = Loader.instance().getMinecraftModContainer().getVersion();
while ((line = reader.readLine()) != null)
{
if (line.startsWith(mcVersion)) {
if (line.contains("BiomesOPlenty")) {
String[] tokens = line.split(":");
recommendedVersion = tokens[2];
if (line.endsWith(VERSION))
{
FMLCommonHandler.instance().getFMLLogger().log(Level.FINER, "[BiomesOPlenty] Using the latest version ["+ getVersion() + "] for Minecraft " + mcVersion);
currentVersion = EnumUpdateState.CURRENT;
return;
}
}
}
}
FMLCommonHandler.instance().getFMLLogger().log(Level.WARNING,"[BiomesOPlenty] Using outdated version [" + VERSION + "] for Minecraft " + mcVersion + ". Consider updating.");
currentVersion = EnumUpdateState.OUTDATED;
} catch (Exception e)
{
FMLCommonHandler.instance().getFMLLogger().log(Level.WARNING, "[BiomesOPlenty] Unable to read from remote version authority.");
FMLCommonHandler.instance().getFMLLogger().log(Level.WARNING, e.toString());
currentVersion = EnumUpdateState.CONNECTION_ERROR;
}
}
public static String[] getChangelog()
{
if (cachedChangelog == null)
{
cachedChangelog = grabChangelog(recommendedVersion);
}
return cachedChangelog;
}
public static String[] grabChangelog(String version)
{
try {
String location = REMOTE_CHANGELOG_ROOT + version + ".txt";
System.out.println(location);
HttpURLConnection conn = null;
while (location != null && !location.isEmpty()) {
URL url = new URL(location);
conn = (HttpURLConnection) url.openConnection();
conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 6.0; ru; rv:1.9.0.11) Gecko/2009060215 Firefox/3.0.11 (.NET CLR 3.5.30729)");
conn.connect();
location = conn.getHeaderField("Location");
}
BufferedReader reader = new BufferedReader(new InputStreamReader(
conn.getInputStream()));
String line = null;
ArrayList<String> changelog = new ArrayList<String>();
while ((line = reader.readLine()) != null) {
if (line.startsWith("#")) {
continue;
}
if (line.isEmpty()) {
continue;
}
changelog.add(line);
}
return changelog.toArray(new String[0]);
} catch (Exception ex) {
ex.printStackTrace();
FMLCommonHandler.instance().getFMLLogger().log(Level.WARNING, "[BiomesOPlenty] Unable to read changelog from remote site.");
}
return new String[] { String.format("Unable to retrieve changelog for %s %s", "BiomesOPlenty", version) };
}
@Override
public void run()
{
int count = 0;
currentVersion = null;
FMLCommonHandler.instance().getFMLLogger().log(Level.INFO, "[BiomesOPlenty] Beginning version check");
try {
while ((count < 3)
&& ((currentVersion == null) || (currentVersion == EnumUpdateState.CONNECTION_ERROR))) {
versionCheck();
count++;
if (currentVersion == EnumUpdateState.CONNECTION_ERROR) {
FMLCommonHandler.instance().getFMLLogger().log(Level.INFO, "[BiomesOPlenty] Version check attempt " + count + " failed, trying again in 10 seconds");
Thread.sleep(10000);
}
}
}
catch (InterruptedException e) {
e.printStackTrace();
}
if (currentVersion == EnumUpdateState.CONNECTION_ERROR) {
FMLCommonHandler.instance().getFMLLogger().log(Level.INFO, "[BiomesOPlenty] Version check failed");
}
}
public static void check()
{
new Thread(instance).start();
}
}