Applied-Energistics-2-tiler.../src/main/java/appeng/services/VersionChecker.java

152 lines
4.5 KiB
Java
Raw Normal View History

2014-11-14 12:02:52 +01:00
/*
* This file is part of Applied Energistics 2.
* Copyright (c) 2013 - 2014, AlgorithmX2, All rights reserved.
*
* Applied Energistics 2 is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Applied Energistics 2 is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Applied Energistics 2. If not, see <http://www.gnu.org/licenses/lgpl>.
*/
2014-09-24 02:26:27 +02:00
package appeng.services;
2014-09-24 02:26:27 +02:00
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.Date;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
2014-12-29 21:59:05 +01:00
import net.minecraft.nbt.NBTTagCompound;
import cpw.mods.fml.common.event.FMLInterModComms;
import appeng.core.AEConfig;
import appeng.core.AELog;
import appeng.core.AppEng;
2014-09-24 02:26:27 +02:00
public class VersionChecker implements Runnable
{
private static final int FOUR_HOURS = 1000 * 3600 * 4;
2014-11-04 00:44:50 +01:00
private final long delay;
2014-09-24 02:26:27 +02:00
public VersionChecker()
{
2014-11-04 00:44:50 +01:00
final long now = new Date().getTime();
final long timeDiff = now - AEConfig.instance.latestTimeStamp;
this.delay = Math.max( 1, FOUR_HOURS - timeDiff);
2014-09-24 02:26:27 +02:00
}
@Override
public void run()
{
try
{
2014-12-29 15:13:47 +01:00
this.sleep( this.delay );
2014-09-24 02:26:27 +02:00
}
catch (InterruptedException e)
{
// :(
}
while (true)
{
Thread.yield();
try
{
String MCVersion = cpw.mods.fml.common.Loader.instance().getMCVersionString().replace( "Minecraft ", "" );
URL url = new URL( "http://feeds.ae-mod.info/latest.json?VersionMC=" + MCVersion + "&Channel=" + AEConfig.CHANNEL + "&CurrentVersion="
+ AEConfig.VERSION );
URLConnection yc = url.openConnection();
yc.setRequestProperty( "User-Agent", "AE2/" + AEConfig.VERSION + " (Channel:" + AEConfig.CHANNEL + ',' + MCVersion.replace( " ", ":" ) + ')' );
2014-09-24 02:26:27 +02:00
BufferedReader in = new BufferedReader( new InputStreamReader( yc.getInputStream() ) );
StringBuilder Version = new StringBuilder();
2014-09-24 02:26:27 +02:00
String inputLine;
while ((inputLine = in.readLine()) != null)
Version.append( inputLine );
2014-09-24 02:26:27 +02:00
in.close();
if ( Version.length() > 2 )
{
JsonElement element = (new JsonParser()).parse( Version.toString() );
2014-09-24 02:26:27 +02:00
int version = element.getAsJsonObject().get( "FormatVersion" ).getAsInt();
if ( version == 1 )
{
JsonObject Meta = element.getAsJsonObject().get( "Meta" ).getAsJsonObject();
JsonArray Versions = element.getAsJsonObject().get( "Versions" ).getAsJsonArray();
if ( Versions.size() > 0 )
{
JsonObject Latest = Versions.get( 0 ).getAsJsonObject();
AEConfig.instance.latestVersion = Latest.get( "Version" ).getAsString();
AEConfig.instance.latestTimeStamp = (new Date()).getTime();
AEConfig.instance.save();
if ( !AEConfig.VERSION.equals( AEConfig.instance.latestVersion ) )
2014-09-24 02:26:27 +02:00
{
NBTTagCompound versionInf = new NBTTagCompound();
versionInf.setString( "modDisplayName", "Applied Energistics 2" );
versionInf.setString( "oldVersion", AEConfig.VERSION );
versionInf.setString( "newVersion", AEConfig.instance.latestVersion );
versionInf.setString( "updateUrl", Latest.get( "UserBuild" ).getAsString() );
versionInf.setBoolean( "isDirectLink", true );
JsonElement changeLog = Latest.get( "ChangeLog" );
if ( changeLog == null )
versionInf.setString( "changeLog", "For full change log please see: " + Meta.get( "DownloadLink" ).getAsString() );
else
versionInf.setString( "changeLog", changeLog.getAsString() );
versionInf.setString( "newFileName", "appliedenergistics2-" + AEConfig.instance.latestVersion + ".jar" );
FMLInterModComms.sendRuntimeMessage( AppEng.instance, "VersionChecker", "addUpdate", versionInf );
AELog.info( "Stopping VersionChecker" );
return;
2014-09-24 02:26:27 +02:00
}
}
}
}
2014-12-29 15:13:47 +01:00
this.sleep( FOUR_HOURS );
2014-09-24 02:26:27 +02:00
}
catch (Exception e)
{
try
{
2014-12-29 15:13:47 +01:00
this.sleep( FOUR_HOURS );
2014-09-24 02:26:27 +02:00
}
catch (InterruptedException e1)
{
AELog.error( e );
}
}
}
}
private void sleep(long i) throws InterruptedException
{
Thread.sleep( i );
}
}