2014-11-14 12:02:52 +01:00
|
|
|
/*
|
|
|
|
* This file is part of Applied Energistics 2.
|
2015-05-16 20:48:32 +02:00
|
|
|
* Copyright (c) 2013 - 2015, AlgorithmX2, All rights reserved.
|
2014-11-14 12:02:52 +01:00
|
|
|
*
|
|
|
|
* 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-11-28 04:36:46 +01:00
|
|
|
|
2014-09-24 02:26:27 +02:00
|
|
|
import java.util.Date;
|
|
|
|
|
2014-12-29 21:59:05 +01:00
|
|
|
import net.minecraft.nbt.NBTTagCompound;
|
|
|
|
|
2015-03-09 17:35:19 +01:00
|
|
|
import cpw.mods.fml.common.Loader;
|
2014-12-29 21:59:05 +01:00
|
|
|
import cpw.mods.fml.common.event.FMLInterModComms;
|
|
|
|
|
2014-11-28 04:36:46 +01:00
|
|
|
import appeng.core.AEConfig;
|
|
|
|
import appeng.core.AELog;
|
|
|
|
import appeng.core.AppEng;
|
2015-03-09 17:35:19 +01:00
|
|
|
import appeng.services.version.ModVersionFetcher;
|
|
|
|
import appeng.services.version.Version;
|
|
|
|
import appeng.services.version.VersionCheckerConfig;
|
|
|
|
import appeng.services.version.VersionFetcher;
|
|
|
|
import appeng.services.version.VersionParser;
|
2015-04-03 08:54:31 +02:00
|
|
|
import appeng.services.version.github.FormattedRelease;
|
|
|
|
import appeng.services.version.github.ReleaseFetcher;
|
2015-03-09 17:35:19 +01:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tries to connect to GitHub to retrieve the most current build.
|
|
|
|
* After comparison with the local version, several path can be chosen.
|
|
|
|
*
|
|
|
|
* If the local version is invalid, somebody might have build that version themselves
|
|
|
|
* or it is run in a developer environment, then nothing needs to be done.
|
|
|
|
*
|
|
|
|
* If GitHub can not be reached, then either is GitHub down
|
|
|
|
* or the connection to GitHub disturbed, then nothing needs to be done,
|
|
|
|
* since no comparison can be reached
|
|
|
|
*
|
|
|
|
* If the version was just recently checked, then no need to poll again.
|
|
|
|
* Nobody wants to bother to update several times a day.
|
|
|
|
*
|
|
|
|
* Config enables to fine-tune when a version is considered newer
|
|
|
|
*
|
|
|
|
* If the local version is newer or equal to the GitHub version,
|
|
|
|
* then no update needs to be posted
|
|
|
|
*
|
|
|
|
* Only after all that cases, if the external version is higher than the local,
|
|
|
|
* use Version Checker Mod and post several information needed for it to update the mod.
|
|
|
|
*/
|
|
|
|
public final class VersionChecker implements Runnable
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
2015-03-09 17:35:19 +01:00
|
|
|
private static final int SEC_TO_HOUR = 3600;
|
|
|
|
private static final int MS_TO_SEC = 1000;
|
|
|
|
private final VersionCheckerConfig config;
|
2014-11-04 00:47:44 +01:00
|
|
|
|
2015-09-25 23:10:56 +02:00
|
|
|
public VersionChecker( final VersionCheckerConfig config )
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
2015-03-09 17:35:19 +01:00
|
|
|
this.config = config;
|
2014-09-24 02:26:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void run()
|
|
|
|
{
|
2015-03-09 17:35:19 +01:00
|
|
|
Thread.yield();
|
2014-09-24 02:26:27 +02:00
|
|
|
|
2015-03-09 17:35:19 +01:00
|
|
|
final String rawLastCheck = this.config.lastCheck();
|
|
|
|
final long lastCheck = Long.parseLong( rawLastCheck );
|
|
|
|
final Date now = new Date();
|
|
|
|
final long nowInMs = now.getTime();
|
|
|
|
final long intervalInMs = this.config.interval() * SEC_TO_HOUR * MS_TO_SEC;
|
|
|
|
final long lastAfterInterval = lastCheck + intervalInMs;
|
2014-09-24 02:26:27 +02:00
|
|
|
|
2015-03-09 17:35:19 +01:00
|
|
|
this.processInterval( nowInMs, lastAfterInterval );
|
2014-09-24 02:26:27 +02:00
|
|
|
|
2015-03-09 17:35:19 +01:00
|
|
|
AELog.info( "Stopping AE2 VersionChecker" );
|
|
|
|
}
|
2014-09-24 02:26:27 +02:00
|
|
|
|
2015-03-09 17:35:19 +01:00
|
|
|
/**
|
|
|
|
* checks if enough time since last check has expired
|
|
|
|
*
|
2015-08-06 18:49:57 +02:00
|
|
|
* @param nowInMs now in milli seconds
|
2015-03-09 17:35:19 +01:00
|
|
|
* @param lastAfterInterval last version check including the interval defined in the config
|
|
|
|
*/
|
2015-09-25 23:10:56 +02:00
|
|
|
private void processInterval( final long nowInMs, final long lastAfterInterval )
|
2015-03-09 17:35:19 +01:00
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
if( nowInMs > lastAfterInterval )
|
2015-03-09 17:35:19 +01:00
|
|
|
{
|
|
|
|
final String rawModVersion = AEConfig.VERSION;
|
|
|
|
final VersionParser parser = new VersionParser();
|
|
|
|
final VersionFetcher modFetcher = new ModVersionFetcher( rawModVersion, parser );
|
|
|
|
final ReleaseFetcher githubFetcher = new ReleaseFetcher( this.config, parser );
|
2014-09-24 02:26:27 +02:00
|
|
|
|
2015-03-09 17:35:19 +01:00
|
|
|
final Version modVersion = modFetcher.get();
|
|
|
|
final FormattedRelease githubRelease = githubFetcher.get();
|
|
|
|
|
|
|
|
this.processVersions( modVersion, githubRelease );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
AELog.info( "Last check was just recently." );
|
|
|
|
}
|
|
|
|
}
|
2014-09-24 02:26:27 +02:00
|
|
|
|
2015-03-09 17:35:19 +01:00
|
|
|
/**
|
|
|
|
* Checks if the retrieved version is newer as the current mod version.
|
|
|
|
* Will notify player if config is enabled.
|
|
|
|
*
|
2015-08-06 18:49:57 +02:00
|
|
|
* @param modVersion version of mod
|
2015-03-09 17:35:19 +01:00
|
|
|
* @param githubRelease release retrieved through github
|
|
|
|
*/
|
2015-09-25 23:10:56 +02:00
|
|
|
private void processVersions( final Version modVersion, final FormattedRelease githubRelease )
|
2015-03-09 17:35:19 +01:00
|
|
|
{
|
|
|
|
final Version githubVersion = githubRelease.version();
|
|
|
|
final String modFormatted = modVersion.formatted();
|
|
|
|
final String ghFormatted = githubVersion.formatted();
|
2014-09-24 02:26:27 +02:00
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
if( githubVersion.isNewerAs( modVersion ) )
|
2015-03-09 17:35:19 +01:00
|
|
|
{
|
|
|
|
final String changelog = githubRelease.changelog();
|
2014-09-24 02:26:27 +02:00
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
if( this.config.shouldNotifyPlayer() )
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
2015-03-09 17:35:19 +01:00
|
|
|
AELog.info( "Newer version is available: " + ghFormatted + " (found) > " + modFormatted + " (current)" );
|
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
if( this.config.shouldPostChangelog() )
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
2015-03-09 17:35:19 +01:00
|
|
|
AELog.info( "Changelog: " + changelog );
|
2014-09-24 02:26:27 +02:00
|
|
|
}
|
|
|
|
}
|
2015-03-09 17:35:19 +01:00
|
|
|
|
|
|
|
this.interactWithVersionCheckerMod( modFormatted, ghFormatted, changelog );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
AELog.info( "No newer version is available: " + ghFormatted + "(found) < " + modFormatted + " (current)" );
|
2014-09-24 02:26:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-09 17:35:19 +01:00
|
|
|
/**
|
|
|
|
* Checks if the version checker mod is installed and handles it depending on that information
|
|
|
|
*
|
|
|
|
* @param modFormatted mod version formatted as rv2-beta-8
|
2015-08-06 18:49:57 +02:00
|
|
|
* @param ghFormatted retrieved github version formatted as rv2-beta-8
|
|
|
|
* @param changelog retrieved github changelog
|
2015-03-09 17:35:19 +01:00
|
|
|
*/
|
2015-09-25 23:10:56 +02:00
|
|
|
private void interactWithVersionCheckerMod( final String modFormatted, final String ghFormatted, final String changelog )
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
if( Loader.isModLoaded( "VersionChecker" ) )
|
2015-03-09 17:35:19 +01:00
|
|
|
{
|
|
|
|
final NBTTagCompound versionInf = new NBTTagCompound();
|
|
|
|
versionInf.setString( "modDisplayName", AppEng.MOD_NAME );
|
|
|
|
versionInf.setString( "oldVersion", modFormatted );
|
|
|
|
versionInf.setString( "newVersion", ghFormatted );
|
|
|
|
versionInf.setString( "updateUrl", "http://ae-mod.info/builds/appliedenergistics2-" + ghFormatted + ".jar" );
|
|
|
|
versionInf.setBoolean( "isDirectLink", true );
|
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
if( !changelog.isEmpty() )
|
2015-03-09 17:35:19 +01:00
|
|
|
{
|
|
|
|
versionInf.setString( "changeLog", changelog );
|
|
|
|
}
|
|
|
|
|
|
|
|
versionInf.setString( "newFileName", "appliedenergistics2-" + ghFormatted + ".jar" );
|
2015-05-16 20:48:32 +02:00
|
|
|
FMLInterModComms.sendRuntimeMessage( AppEng.instance(), "VersionChecker", "addUpdate", versionInf );
|
2015-03-09 17:35:19 +01:00
|
|
|
|
|
|
|
AELog.info( "Reported new version to VersionChecker mod." );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
AELog.info( "VersionChecker mod is not installed; Proceeding." );
|
|
|
|
}
|
2014-09-24 02:26:27 +02:00
|
|
|
}
|
|
|
|
}
|