Refactored the remote version check to check against the GitHub repo instead of my DropBox folder, added some colourized text to the result message on the client, and reformatted some of the localization.

This commit is contained in:
pahimar 2012-11-20 14:42:39 -05:00
parent e0783ef4b5
commit 01304fb4c4
7 changed files with 188 additions and 154 deletions

View file

@ -4,7 +4,9 @@ import java.util.EnumSet;
import cpw.mods.fml.client.FMLClientHandler;
import cpw.mods.fml.common.ITickHandler;
import cpw.mods.fml.common.Loader;
import cpw.mods.fml.common.TickType;
import cpw.mods.fml.common.registry.LanguageRegistry;
import ee3.common.core.helper.VersionHelper;
import ee3.common.lib.Colours;
import ee3.common.lib.ConfigurationSettings;
@ -13,7 +15,8 @@ import ee3.common.lib.Reference;
/**
* VersionCheckTickHandler
*
* Class for notifying the player on their client when they get in game the outcome of the remote version check
* Class for notifying the player on their client when they get in game the
* outcome of the remote version check
*
* @author pahimar
* @license Lesser GNU Public License v3 (http://www.gnu.org/licenses/lgpl.html)
@ -21,37 +24,42 @@ import ee3.common.lib.Reference;
*/
public class VersionCheckTickHandler implements ITickHandler {
private static boolean initialized = false;
private static boolean initialized = false;
@Override
public void tickStart(EnumSet<TickType> type, Object... tickData) { }
@Override
public void tickStart(EnumSet<TickType> type, Object... tickData) {
@Override
public void tickEnd(EnumSet<TickType> type, Object... tickData) {
if (ConfigurationSettings.ENABLE_VERSION_CHECK) {
if (!initialized) {
for (TickType tickType : type) {
if (tickType == TickType.CLIENT) {
if (FMLClientHandler.instance().getClient().currentScreen == null) {
initialized = true;
if (VersionHelper.result != VersionHelper.CURRENT){
FMLClientHandler.instance().getClient().ingameGUI.getChatGUI().printChatMessage(Colours.VERSION_CHECK_PREFIX + "[" + Reference.MOD_NAME + "] " + VersionHelper.getResultMessage());
}
}
}
}
}
}
}
}
@Override
public EnumSet<TickType> ticks() {
return EnumSet.of(TickType.CLIENT);
}
@Override
public void tickEnd(EnumSet<TickType> type, Object... tickData) {
@Override
public String getLabel() {
return Reference.MOD_NAME + ": " + this.getClass().getSimpleName();
}
if (ConfigurationSettings.ENABLE_VERSION_CHECK) {
if (!initialized) {
for (TickType tickType : type) {
if (tickType == TickType.CLIENT) {
if (FMLClientHandler.instance().getClient().currentScreen == null) {
initialized = true;
if (VersionHelper.result == VersionHelper.OUTDATED) {
FMLClientHandler.instance().getClient().ingameGUI.getChatGUI().printChatMessage(VersionHelper.getResultMessageForClient());
}
}
}
}
}
}
}
@Override
public EnumSet<TickType> ticks() {
return EnumSet.of(TickType.CLIENT);
}
@Override
public String getLabel() {
return Reference.MOD_NAME + ": " + this.getClass().getSimpleName();
}
}

View file

@ -1,21 +1,25 @@
package ee3.common.core.helper;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Properties;
import java.util.logging.Level;
import cpw.mods.fml.common.FMLCommonHandler;
import cpw.mods.fml.common.Loader;
import cpw.mods.fml.common.registry.LanguageRegistry;
import ee3.common.lib.Colours;
import ee3.common.lib.ConfigurationSettings;
import ee3.common.lib.Reference;
import ee3.common.lib.Strings;
/**
* VersionHelper
*
* Contains methods for checking the version of the currently running instance of the mod against a remote version number authority.
* Meant to help users by notifying them if they are behind the latest published version of the mod
* Contains methods for checking the version of the currently running instance
* of the mod against a remote version number authority. Meant to help users by
* notifying them if they are behind the latest published version of the mod
*
* @author pahimar
* @license Lesser GNU Public License v3 (http://www.gnu.org/licenses/lgpl.html)
@ -23,109 +27,125 @@ import ee3.common.lib.Reference;
*/
public class VersionHelper {
// The (publicly available) remote version number authority file
private static final String REMOTE_VERSION_FILE = "https://dl.dropbox.com/u/25591134/EE3/version.txt";
// The (publicly available) remote version number authority file
private static final String REMOTE_VERSION_XML_FILE = "https://raw.github.com/pahimar/Equivalent-Exchange-3/master/version.xml";
// All possible results of the remote version number check
public static final byte UNINITIALIZED = 0;
public static final byte CURRENT = 1;
public static final byte OUTDATED = 2;
public static final byte CONNECTION_ERROR = 3;
public static Properties remoteVersionProperties = new Properties();
// Localization keys
private static final String VERSION_CHECK_DISABLED = "version.check_disabled";
private static final String VERSION_CHECK_INIT_LOG_MESSAGE = "version.init_log_message";
private static final String UNINITIALIZED_MESSAGE = "version.uninitialized";
private static final String CURRENT_MESSAGE = "version.current";
private static final String OUTDATED_MESSAGE = "version.outdated";
private static final String CONNECTION_ERROR_MESSAGE = "version.connection_error";
// All possible results of the remote version number check
public static final byte UNINITIALIZED = 0;
public static final byte CURRENT = 1;
public static final byte OUTDATED = 2;
public static final byte GENERAL_ERROR = 3;
// Var to hold the result of the remote version check, initially set to uninitialized
public static byte result = UNINITIALIZED;
// Var to hold the result of the remote version check, initially set to uninitialized
public static byte result = UNINITIALIZED;
public static String remoteVersion = null;
public static String remoteUpdateLocation = null;
/***
* Checks the version of the currently running instance of the mod against the remote version authority, and sets the result of the check appropriately
*/
public static void checkVersion() {
try {
URL url = new URL(REMOTE_VERSION_FILE);
/***
* Checks the version of the currently running instance of the mod against
* the remote version authority, and sets the result of the check
* appropriately
*/
public static void checkVersion() {
InputStreamReader isr = new InputStreamReader(url.openStream());
BufferedReader reader = new BufferedReader(isr);
InputStream remoteVersionRepoStream = null;
String line = null;
try {
URL remoteVersionURL = new URL(REMOTE_VERSION_XML_FILE);
remoteVersionRepoStream = remoteVersionURL.openStream();
remoteVersionProperties.loadFromXML(remoteVersionRepoStream);
// While we are not at the end of the file, fetch the next line in the file
while ((line = reader.readLine()) != null) {
// If the current line is for this version of Minecraft, read further
if (line.startsWith(Loader.instance().getMCVersionString())) {
// If the current line is also for this mod, read further
if (line.contains(Reference.CHANNEL_NAME)) {
// If the current line is also the same as the running version of the mod, set the result appropriately
if (line.endsWith(Reference.VERSION)) {
// Set the version check result to CURRENT
result = CURRENT;
String remoteVersionProperty = remoteVersionProperties.getProperty(Loader.instance().getMCVersionString());
// Close the associated network resources
reader.close();
isr.close();
if (remoteVersionProperty != null) {
remoteVersion = remoteVersionProperty.substring(0, remoteVersionProperty.indexOf("|"));
remoteUpdateLocation = remoteVersionProperty.substring(remoteVersionProperty.indexOf("|") + 1);
}
return;
}
}
}
}
if ((remoteVersion != null) && (remoteVersion.equals(Reference.VERSION))) {
result = CURRENT;
return;
}
// Since this mod version is not the current version, set the version check appropriately
result = OUTDATED;
result = OUTDATED;
}
catch (IOException e) {
e.printStackTrace();
}
finally {
if (result == UNINITIALIZED) {
result = GENERAL_ERROR;
}
// Close the associated network resources
reader.close();
isr.close();
} catch (Exception e) {
// If we cannot connect to the remote version number authority, set the version check appropriately
e.printStackTrace(System.err);
result = CONNECTION_ERROR;
}
try {
if (remoteVersionRepoStream != null) {
remoteVersionRepoStream.close();
}
}
catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
public static void logResult() {
public static void logResult() {
if (ConfigurationSettings.ENABLE_VERSION_CHECK) {
LogHelper.log(Level.INFO, LanguageRegistry.instance().getStringLocalization(VERSION_CHECK_INIT_LOG_MESSAGE) + " " + REMOTE_VERSION_FILE);
if ((result == CURRENT) || (result == OUTDATED)) {
LogHelper.log(Level.INFO, getResultMessage());
}
else {
LogHelper.log(Level.WARNING, getResultMessage());
}
}
else {
LogHelper.log(Level.INFO, getResultMessage());
}
}
if (ConfigurationSettings.ENABLE_VERSION_CHECK) {
LogHelper.log(Level.INFO, LanguageRegistry.instance().getStringLocalization(Strings.VERSION_CHECK_INIT_LOG_MESSAGE) + " " + REMOTE_VERSION_XML_FILE);
if ((result == CURRENT) || (result == OUTDATED)) {
LogHelper.log(Level.INFO, getResultMessage());
}
else {
LogHelper.log(Level.WARNING, getResultMessage());
}
}
else {
LogHelper.log(Level.INFO, getResultMessage());
}
}
public static String getResultMessage() {
if (ConfigurationSettings.ENABLE_VERSION_CHECK) {
if (result == UNINITIALIZED) {
return LanguageRegistry.instance().getStringLocalization(UNINITIALIZED_MESSAGE);
}
else if (result == CURRENT) {
return LanguageRegistry.instance().getStringLocalization(CURRENT_MESSAGE);
}
else if (result == OUTDATED) {
return LanguageRegistry.instance().getStringLocalization(OUTDATED_MESSAGE);
}
else if (result == CONNECTION_ERROR) {
return LanguageRegistry.instance().getStringLocalization(CONNECTION_ERROR_MESSAGE);
}
else {
return null;
}
}
else {
return LanguageRegistry.instance().getStringLocalization(VERSION_CHECK_DISABLED);
}
}
public static String getResultMessage() {
if (ConfigurationSettings.ENABLE_VERSION_CHECK) {
if (result == UNINITIALIZED) {
return LanguageRegistry.instance().getStringLocalization(Strings.UNINITIALIZED_MESSAGE);
}
else if (result == CURRENT) {
String returnString = LanguageRegistry.instance().getStringLocalization(Strings.CURRENT_MESSAGE);
returnString = returnString.replace("@REMOTE_MOD_VERSION@", remoteVersion);
returnString = returnString.replace("@MINECRAFT_VERSION@", Loader.instance().getMCVersionString());
return returnString;
}
else if (result == OUTDATED) {
String returnString = LanguageRegistry.instance().getStringLocalization(Strings.OUTDATED_MESSAGE);
returnString = returnString.replace("@MOD_NAME@", Reference.MOD_NAME);
returnString = returnString.replace("@REMOTE_MOD_VERSION@", remoteVersion);
returnString = returnString.replace("@MINECRAFT_VERSION@", Loader.instance().getMCVersionString());
returnString = returnString.replace("@MOD_UPDATE_LOCATION@", remoteUpdateLocation);
return returnString;
}
else if (result == GENERAL_ERROR) {
return LanguageRegistry.instance().getStringLocalization(Strings.GENERAL_ERROR_MESSAGE);
}
else {
return null;
}
}
else {
return LanguageRegistry.instance().getStringLocalization(Strings.VERSION_CHECK_DISABLED);
}
}
public static String getResultMessageForClient() {
String returnString = LanguageRegistry.instance().getStringLocalization(Strings.OUTDATED_MESSAGE);
returnString = returnString.replace("@MOD_NAME@", Colours.TEXT_COLOUR_PREFIX_YELLOW + Reference.MOD_NAME + Colours.TEXT_COLOUR_PREFIX_WHITE);
returnString = returnString.replace("@REMOTE_MOD_VERSION@", Colours.TEXT_COLOUR_PREFIX_YELLOW + VersionHelper.remoteVersion + Colours.TEXT_COLOUR_PREFIX_WHITE);
returnString = returnString.replace("@MINECRAFT_VERSION@", Colours.TEXT_COLOUR_PREFIX_YELLOW + Loader.instance().getMCVersionString() + Colours.TEXT_COLOUR_PREFIX_WHITE);
returnString = returnString.replace("@MOD_UPDATE_LOCATION@", Colours.TEXT_COLOUR_PREFIX_YELLOW + VersionHelper.remoteUpdateLocation + Colours.TEXT_COLOUR_PREFIX_WHITE);
return returnString;
}
}

View file

@ -14,6 +14,8 @@ public class Colours {
public static final String PURE_RED = "ff0000";
/* Text colour related constants */
public static final String VERSION_CHECK_PREFIX = "\u00a7e";
public static final String TEXT_COLOUR_PREFIX_YELLOW = "\u00a7e";
public static final String TEXT_COLOUR_PREFIX_WHITE = "\u00a7f";
}

View file

@ -3,8 +3,14 @@ package ee3.common.lib;
public class Strings {
/* General text keys */
public static final String TEXT_IMPURE = "text.impure";
public static final String TEXT_PURE = "text.pure";
/* Version check related constants */
public static final String VERSION_CHECK_DISABLED = "version.check_disabled";
public static final String VERSION_CHECK_INIT_LOG_MESSAGE = "version.init_log_message";
public static final String UNINITIALIZED_MESSAGE = "version.uninitialized";
public static final String CURRENT_MESSAGE = "version.current";
public static final String OUTDATED_MESSAGE = "version.outdated";
public static final String GENERAL_ERROR_MESSAGE = "version.general_error";
/* Gui related constants */
public static final String GUI_CALCINATOR_NAME = "gui.calcinator.name";

View file

@ -21,12 +21,10 @@
<entry key="tile.calcinator.name">Calcinator</entry>
<entry key="gui.calcinator.name">Calcinator</entry>
<entry key="itemGroup.EE3">Equivalent Exchange 3</entry>
<entry key="text.impure">Impure</entry>
<entry key="text.pure">Pure</entry>
<entry key="version.init_log_message">Initializing version check against the remote version authority file, located at</entry>
<entry key="version.uninitialized">The version check did not complete successfully (version check did not initialize properly)</entry>
<entry key="version.current">You are currently using the most up to date version for your version of Minecraft</entry>
<entry key="version.outdated">You are currently using an out of date version; consider updating here - http://goo.gl/Ria2V</entry>
<entry key="version.connection_error">Error connecting to the remote version authority file (check your Internet connection?)</entry>
<entry key="version.init_log_message">Initializing remote version check against remote version authority, located at</entry>
<entry key="version.uninitialized">Remote version check failed to initialize properly</entry>
<entry key="version.current">Currently using the most up to date version (@REMOTE_MOD_VERSION@) of Equivalent Exchange 3 for @MINECRAFT_VERSION@</entry>
<entry key="version.outdated">A new @MOD_NAME@ version exists (@REMOTE_MOD_VERSION@) for @MINECRAFT_VERSION@. Get it here: @MOD_UPDATE_LOCATION@</entry>
<entry key="version.general_error">Error checking the remote version authority file</entry>
<entry key="version.check_disabled">Remote version check disabled, skipping</entry>
</properties>