Added version checker

This commit is contained in:
modmuss50 2015-08-06 18:39:13 +01:00
parent a6431776a8
commit 009bf28b95
4 changed files with 184 additions and 1 deletions

View file

@ -35,6 +35,7 @@ import techreborn.packets.PacketPipeline;
import techreborn.proxies.CommonProxy;
import techreborn.tiles.idsu.IDSUManager;
import techreborn.util.LogHelper;
import techreborn.util.VersionChecker;
import techreborn.world.TROreGen;
import java.io.File;
@ -51,6 +52,8 @@ public class Core {
public static final PacketPipeline packetPipeline = new PacketPipeline();
public VersionChecker versionChecker;
@Mod.EventHandler
public void preinit(FMLPreInitializationEvent event){
INSTANCE = this;
@ -58,12 +61,15 @@ public class Core {
.replace(ModInfo.MOD_ID, "TechReborn");
config = ConfigTechReborn.initialize(new File(path));
LogHelper.info("PreInitialization Complete");
for(ICompatModule compatModule : CompatManager.INSTANCE.compatModules){
compatModule.preInit(event);
}
RecipeConfigManager.load(event.getModConfigurationDirectory());
versionChecker = new VersionChecker("TechReborn");
versionChecker.checkVersionThreaded();
LogHelper.info("PreInitialization Complete");
}
@Mod.EventHandler

View file

@ -0,0 +1,44 @@
package techreborn.client;
import cpw.mods.fml.client.GuiModList;
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.client.event.GuiScreenEvent;
import org.lwjgl.opengl.GL11;
import techreborn.Core;
import java.awt.*;
public class VersionCheckerClient {
ResourceLocation texture;
public VersionCheckerClient() {
texture = new ResourceLocation("textures/gui/demo_background.png");
}
@SubscribeEvent
public void drawGui(GuiScreenEvent.DrawScreenEvent event){
if(event.gui instanceof GuiModList){
String s = "";
if(Core.INSTANCE.versionChecker.isChecking){
s = "Checking for update...";
} else if(Core.INSTANCE.versionChecker.isLatestVersion()){
s = "You have the latest version of TechReborn";
} else{
s = "There is an update for TechReborn with " + Core.INSTANCE.versionChecker.getChangeLogSinceCurrentVersion().size() + " changes.";
}
event.gui.drawString(event.gui.mc.fontRenderer, s, 10, 5, Color.white.getRGB());
}
if(!Core.INSTANCE.versionChecker.isLatestVersion()){
if(event.mouseY < 20){
GuiUtil.drawTooltipBox(5, 15, 330, Core.INSTANCE.versionChecker.getChangeLogSinceCurrentVersion().size() * 10 + 5);
int y = 20;
for(String change : Core.INSTANCE.versionChecker.getChangeLogSinceCurrentVersion()){
event.gui.drawString(event.gui.mc.fontRenderer, change, 10, y, Color.white.getRGB());
y+= 10;
}
}
}
}
}

View file

@ -3,6 +3,7 @@ package techreborn.proxies;
import cpw.mods.fml.client.registry.ClientRegistry;
import net.minecraftforge.common.MinecraftForge;
import techreborn.client.IconSupplier;
import techreborn.client.VersionCheckerClient;
import techreborn.client.hud.ChargeHud;
import techreborn.client.keybindings.KeyBindings;
@ -14,6 +15,7 @@ public class ClientProxy extends CommonProxy {
super.init();
MinecraftForge.EVENT_BUS.register(new IconSupplier());
MinecraftForge.EVENT_BUS.register(new ChargeHud());
MinecraftForge.EVENT_BUS.register(new VersionCheckerClient());
ClientRegistry.registerKeyBinding(KeyBindings.config);
}
}

View file

@ -0,0 +1,131 @@
package techreborn.util;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import org.apache.commons.io.IOUtils;
import techreborn.lib.ModInfo;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
public class VersionChecker {
public static final String apiAddress = "http://modmuss50.me/api/v1/version.php";
public String projectName;
ArrayList<ModifacationVersionInfo> versions;
public boolean isChecking;
public VersionChecker(String projectName) {
this.projectName = projectName;
}
public void checkVersion() throws IOException {
isChecking = true;
URL url = new URL(apiAddress + "?project=" + projectName);
URLConnection con = url.openConnection();
InputStream in = con.getInputStream();
String encoding = con.getContentEncoding();
encoding = encoding == null ? "UTF-8" : encoding;
String body = IOUtils.toString(in, encoding).replaceAll("<br />", "");
Gson gson = new Gson();
versions = gson.fromJson(body,new TypeToken<ArrayList<ModifacationVersionInfo>>(){}.getType());
isChecking = false;
}
public void checkVersionThreaded(){
class VersionCheckerThread extends Thread{
public void run(){
try {
checkVersion();
} catch (IOException e) {
e.printStackTrace();
}
}
}
VersionCheckerThread thread = new VersionCheckerThread();
thread.start();
}
public boolean isLatestVersion(){
if(versions == null || versions.isEmpty()){
return true;
}
return versions.get(0).version.equals(ModInfo.MOD_VERSION);
}
public ModifacationVersionInfo getLatestVersion(){
if(versions == null || versions.isEmpty()){
return null;
}
return versions.get(0);
}
public ArrayList<String> getChangeLogSinceCurrentVersion(){
ArrayList<String> log = new ArrayList<String>();
if(!isLatestVersion()){
for(ModifacationVersionInfo version : versions){
if(version.version.equals(ModInfo.MOD_VERSION)){
break;
}
log.addAll(version.changeLog);
}
}
return log;
}
static class ModifacationVersionInfo{
public String version;
public String minecraftVersion;
public ArrayList<String> changeLog;
public String releaseDate;
public boolean recommended;
public ModifacationVersionInfo(String version, String minecraftVersion, ArrayList<String> changeLog, String releaseDate, boolean recommended) {
this.version = version;
this.minecraftVersion = minecraftVersion;
this.changeLog = changeLog;
this.releaseDate = releaseDate;
this.recommended = recommended;
}
public ModifacationVersionInfo() {
}
}
//use this to make an example json file
public static void main(String[] args) throws IOException {
System.out.println("Generating example json file");
ArrayList<ModifacationVersionInfo> infos = new ArrayList<ModifacationVersionInfo>();
ArrayList<String> changelog = new ArrayList<String>();
changelog.add("A change");
changelog.add("Another change");
infos.add(new ModifacationVersionInfo("1.1.1", "1.7.10", changelog, "12th July", true));
infos.add(new ModifacationVersionInfo("1.2.0", "1.7.10", changelog, "28th July", true));
Gson gson = new GsonBuilder().setPrettyPrinting().create();
String json = gson.toJson(infos);
try {
FileWriter writer = new FileWriter(new File("master.json"));
writer.write(json);
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}