From 34a42086d61a138ea6fd51e4854f94bd63beb596 Mon Sep 17 00:00:00 2001 From: Adubbz G Date: Fri, 14 Jun 2013 23:17:48 +1000 Subject: [PATCH] Versioning system! --- .../biomesoplenty/BiomesOPlenty.java | 10 +- .../configuration/BOPPotions.java | 4 +- .../biomesoplenty/helpers/Localizations.java | 5 +- .../helpers/TickHandlerClient.java | 52 +++++ .../biomesoplenty/helpers/Version.java | 206 ++++++++++++++++++ version.txt | 2 +- 6 files changed, 275 insertions(+), 4 deletions(-) create mode 100644 src/minecraft/biomesoplenty/helpers/TickHandlerClient.java create mode 100644 src/minecraft/biomesoplenty/helpers/Version.java diff --git a/src/minecraft/biomesoplenty/BiomesOPlenty.java b/src/minecraft/biomesoplenty/BiomesOPlenty.java index ed7574410..ee2fc4cb3 100644 --- a/src/minecraft/biomesoplenty/BiomesOPlenty.java +++ b/src/minecraft/biomesoplenty/BiomesOPlenty.java @@ -26,6 +26,8 @@ import biomesoplenty.helpers.BonemealUse; import biomesoplenty.helpers.CreativeTabsBOP; import biomesoplenty.helpers.EntitiesHelper; import biomesoplenty.helpers.Localizations; +import biomesoplenty.helpers.TickHandlerClient; +import biomesoplenty.helpers.Version; import biomesoplenty.integration.BOPCrossIntegration; import biomesoplenty.world.WorldProviderBOPhell; import biomesoplenty.world.WorldProviderPromised; @@ -44,8 +46,10 @@ import cpw.mods.fml.common.event.FMLPreInitializationEvent; import cpw.mods.fml.common.network.NetworkMod; import cpw.mods.fml.common.registry.GameRegistry; import cpw.mods.fml.common.registry.LanguageRegistry; +import cpw.mods.fml.common.registry.TickRegistry; +import cpw.mods.fml.relauncher.Side; -@Mod(modid="BiomesOPlenty", name="Biomes O' Plenty", version="0.5.6", dependencies="after:Natura") +@Mod(modid="BiomesOPlenty", name="Biomes O' Plenty", version=Version.VERSION, dependencies="after:Natura") @NetworkMod(clientSideRequired=true, serverSideRequired=false) public class BiomesOPlenty { @@ -99,6 +103,8 @@ public class BiomesOPlenty } BOPConfiguration.init(event.getSuggestedConfigurationFile()); + + Version.check(); tabBiomesOPlenty = new CreativeTabsBOP(CreativeTabs.getNextID(),"tabBiomesOPlenty"); @@ -159,5 +165,7 @@ public class BiomesOPlenty public void postInit(FMLPostInitializationEvent event) { BOPCrossIntegration.postInit(); + + TickRegistry.registerTickHandler(new TickHandlerClient(), Side.CLIENT); } } \ No newline at end of file diff --git a/src/minecraft/biomesoplenty/configuration/BOPPotions.java b/src/minecraft/biomesoplenty/configuration/BOPPotions.java index 0273cbc65..1bc91d67d 100644 --- a/src/minecraft/biomesoplenty/configuration/BOPPotions.java +++ b/src/minecraft/biomesoplenty/configuration/BOPPotions.java @@ -2,6 +2,7 @@ package biomesoplenty.configuration; import java.lang.reflect.Field; import java.lang.reflect.Modifier; +import java.util.logging.Level; import net.minecraft.potion.Potion; import net.minecraftforge.common.MinecraftForge; @@ -12,6 +13,7 @@ import biomesoplenty.potions.PotionParalysis; import com.google.common.base.Optional; +import cpw.mods.fml.common.FMLCommonHandler; import cpw.mods.fml.common.registry.LanguageRegistry; public class BOPPotions @@ -42,7 +44,7 @@ public class BOPPotions private static void extendPotionsArray() { - System.out.println("[BiomesOPlenty] Extending Potions Array."); + FMLCommonHandler.instance().getFMLLogger().log(Level.INFO, "[BiomesOPlenty] Extending Potions Array."); potionOffset = Potion.potionTypes.length; Potion[] potionTypes = new Potion[potionOffset + MAXNEWPOTIONS]; diff --git a/src/minecraft/biomesoplenty/helpers/Localizations.java b/src/minecraft/biomesoplenty/helpers/Localizations.java index b498769a5..e57762af0 100644 --- a/src/minecraft/biomesoplenty/helpers/Localizations.java +++ b/src/minecraft/biomesoplenty/helpers/Localizations.java @@ -1,5 +1,8 @@ package biomesoplenty.helpers; +import java.util.logging.Level; + +import cpw.mods.fml.common.FMLCommonHandler; import cpw.mods.fml.common.registry.LanguageRegistry; public class Localizations @@ -15,7 +18,7 @@ public class Localizations public static String getLocaleFromFileName(String fileName) { - System.out.println("[BiomesOPlenty] Localizations loaded for " + fileName.substring(fileName.lastIndexOf('/') + 1, fileName.lastIndexOf('.'))); + FMLCommonHandler.instance().getFMLLogger().log(Level.INFO, "[BiomesOPlenty] Localizations loaded for " + fileName.substring(fileName.lastIndexOf('/') + 1, fileName.lastIndexOf('.'))); return fileName.substring(fileName.lastIndexOf('/') + 1, fileName.lastIndexOf('.')); } diff --git a/src/minecraft/biomesoplenty/helpers/TickHandlerClient.java b/src/minecraft/biomesoplenty/helpers/TickHandlerClient.java new file mode 100644 index 000000000..784f2caca --- /dev/null +++ b/src/minecraft/biomesoplenty/helpers/TickHandlerClient.java @@ -0,0 +1,52 @@ +package biomesoplenty.helpers; + +import java.util.EnumSet; + +import net.minecraft.entity.player.EntityPlayer; +import cpw.mods.fml.common.ITickHandler; +import cpw.mods.fml.common.Loader; +import cpw.mods.fml.common.TickType; + +public class TickHandlerClient implements ITickHandler +{ + private boolean nagged; + + @Override + public void tickStart(EnumSet type, Object... tickData) + { + } + + @Override + public void tickEnd(EnumSet type, Object... tickData) + { + if (nagged) + return; + + EntityPlayer player = (EntityPlayer) tickData[0]; + + if (Version.needsUpdateNoticeAndMarkAsSeen()) + { + player.sendChatToPlayer(String.format("\u00A7cA new version of Biomes O Plenty is available: %s for Minecraft %s", Version.getRecommendedVersion(), Loader.instance().getMinecraftModContainer().getVersion())); + + for (String updateLine : Version.getChangelog()) + { + player.sendChatToPlayer("\u00A79" + updateLine); + } + } + + nagged = true; + } + + @Override + public EnumSet ticks() + { + return EnumSet.of(TickType.PLAYER); + } + + @Override + public String getLabel() + { + return "BiomesOPlenty - Player update tick"; + } + +} diff --git a/src/minecraft/biomesoplenty/helpers/Version.java b/src/minecraft/biomesoplenty/helpers/Version.java new file mode 100644 index 000000000..7d3166335 --- /dev/null +++ b/src/minecraft/biomesoplenty/helpers/Version.java @@ -0,0 +1,206 @@ +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.minecraftforge.common.Property; +import biomesoplenty.BiomesOPlenty; +import biomesoplenty.configuration.BOPConfiguration; +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 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 changelog = new ArrayList(); + 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(); + } +} diff --git a/version.txt b/version.txt index 1ade6ef88..3424060e1 100644 --- a/version.txt +++ b/version.txt @@ -1 +1 @@ -1.5.2:BiomesOPlenty:0.5.5 \ No newline at end of file +1.5.2:BiomesOPlenty:0.5.6 \ No newline at end of file