Skip to content
This repository has been archived by the owner on Feb 9, 2018. It is now read-only.

Commit

Permalink
Changed Version Checking
Browse files Browse the repository at this point in the history
* Version checking now connects to wordpress website, to get latest download info and comparing the dates of the Reference.VERSION_DATE to the download post's date.
* Versions.txt is now obsolete. Left it for older mod versions.
* Added date of update in Reference that needs to be changed for version checking.
* Fixed romulan ale name.
* Moved the Version Checking Thread in separate class.
  • Loading branch information
simeonradivoev committed Jul 13, 2015
1 parent c01dd47 commit 07e0e88
Show file tree
Hide file tree
Showing 5 changed files with 151 additions and 87 deletions.
10 changes: 5 additions & 5 deletions Versions.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{"versions":[
{"version": "0.4.0-RC2", "changes": "Server Crashes, Computercraft compatibility and more", "type": "beta", "date": "13.07,2015", "url": "http://simeon.co.vu/Mods/MatterOverdrive/download_category/downloads/"},
{"version": "0.4.0-RC1", "changes": "More Star Map work, fix crash when opening the Matter Scanner GUI", "type": "beta", "date": "3.07,2015", "url": "http://simeon.co.vu/Mods/MatterOverdrive/download_category/downloads/"},
{"version":"0.3.5", "changes":"New Star Map, bugfixes and more configurations", "type":"beta","date":"27.06,2015","url":"http://simeon.co.vu/Mods/MatterOverdrive/download_category/downloads/"},
{"version":"0.3.4.1", "changes":"Mad Scientist Server Crash Bugfix", "type":"beta","date":"16.06,2015","url":"http://simeon.co.vu/Mods/MatterOverdrive/download_category/downloads/"},
{"version":"0.3.4", "changes":"New Shield Ability and various fixes", "type":"beta","date":"12.06,2015","url":"http://simeon.co.vu/Mods/MatterOverdrive/download_category/downloads/"}
{"version": "0.4.0-RC2", "changes": "Server Crashes, Computercraft compatibility and more", "type": "beta", "date": "13.07.2015", "url": "http://simeon.co.vu/Mods/MatterOverdrive/download_category/downloads/"},
{"version": "0.4.0-RC1", "changes": "More Star Map work, fix crash when opening the Matter Scanner GUI", "type": "beta", "date": "3.07.2015", "url": "http://simeon.co.vu/Mods/MatterOverdrive/download_category/downloads/"},
{"version":"0.3.5", "changes":"New Star Map, bugfixes and more configurations", "type":"beta","date":"27.06.2015","url":"http://simeon.co.vu/Mods/MatterOverdrive/download_category/downloads/"},
{"version":"0.3.4.1", "changes":"Mad Scientist Server Crash Bugfix", "type":"beta","date":"16.06.2015","url":"http://simeon.co.vu/Mods/MatterOverdrive/download_category/downloads/"},
{"version":"0.3.4", "changes":"New Shield Ability and various fixes", "type":"beta","date":"12.06.2015","url":"http://simeon.co.vu/Mods/MatterOverdrive/download_category/downloads/"}
]}
5 changes: 3 additions & 2 deletions src/main/java/matteroverdrive/Reference.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ public class Reference
public static final String MOD_ID = "mo";
public static final String MOD_NAME = "Matter Overdrive";
public static final String VERSION = "0.4.0-RC2";
public static final String VERSION_DATE = "13.07.2015";
public static final String GUI_FACTORY_CLASS = "matteroverdrive.gui.GuiConfigFactory";
public static final String DEPEDNENCIES = "after:ThermalExpansion;after:exnihilo;after:EnderIO;";
public static final String CLIENT_PROXY_CLASS = "matteroverdrive.proxy.ClientProxy";
public static final String SERVER_PROXY_CLASS = "matteroverdrive.proxy.CommonProxy";
public static final String CHANNEL_NAME = MOD_ID + "_channel";
public static final String VERSIONS_FILE_URL = "https://raw.githubusercontent.com/simeonradivoev/MatterOverdrive/master/Versions.txt";
public static final String VERSIONS_FILE_URL_MIRROR = "http://simeon.co.vu/Mods/MatterOverdrive/Versions.txt";
public static final String VERSIONS_CHECK_URL = "http://simeon.co.vu/Mods/MatterOverdrive/wp-json/posts?type[]=mo_download&filter[posts_per_page]=1";
public static final String DOWNLOAD_URL = "http://simeon.co.vu/Mods/MatterOverdrive/download_category/downloads/";


//region GUI
Expand Down
162 changes: 83 additions & 79 deletions src/main/java/matteroverdrive/handler/VersionCheckerHandler.java
Original file line number Diff line number Diff line change
@@ -1,24 +1,55 @@
/*
* This file is part of Matter Overdrive
* Copyright (c) 2015., Simeon Radivoev, All rights reserved.
*
* Matter Overdrive is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Matter Overdrive 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Matter Overdrive. If not, see <http://www.gnu.org/licenses>.
*/

package matteroverdrive.handler;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.stream.JsonReader;
import cpw.mods.fml.common.FMLLog;
import cpw.mods.fml.common.gameevent.TickEvent;
import jdk.nashorn.internal.parser.DateParser;
import matteroverdrive.Reference;
import matteroverdrive.handler.thread.VersionCheckThread;
import matteroverdrive.util.IConfigSubscriber;
import matteroverdrive.util.MOLog;
import matteroverdrive.util.MOStringHelper;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.event.ClickEvent;
import net.minecraft.event.HoverEvent;
import net.minecraft.server.MinecraftServer;
import net.minecraft.util.ChatComponentText;
import net.minecraft.util.ChatComponentTranslation;
import net.minecraft.util.ChatStyle;
import net.minecraft.util.EnumChatFormatting;
import net.minecraft.util.*;
import org.apache.logging.log4j.Level;
import scala.util.parsing.json.JSON;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.nio.charset.Charset;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.concurrent.*;

Expand All @@ -28,7 +59,7 @@
public class VersionCheckerHandler implements IConfigSubscriber {
private boolean updateInfoDisplayed = false;
public Future<String> download;
public static final String[] mirrors = new String[]{Reference.VERSIONS_FILE_URL, Reference.VERSIONS_FILE_URL_MIRROR};
public static final String[] mirrors = new String[]{Reference.VERSIONS_CHECK_URL};
private int currentMirror = 0;
int lastPoll = 400;
private boolean checkForUpdates;
Expand All @@ -40,6 +71,7 @@ public VersionCheckerHandler() {

//Called when a player ticks.
public void onPlayerTick(TickEvent.PlayerTickEvent event) {

if (event.phase != TickEvent.Phase.START || !checkForUpdates) {
return;
}
Expand Down Expand Up @@ -87,85 +119,57 @@ public void onPlayerTick(TickEvent.PlayerTickEvent event) {
}
}

private static class VersionCheckThread implements Callable<String>
private boolean constructVersionAndCheck(String jsonText,EntityPlayer player)
{
String url;

public VersionCheckThread(String url)
{
this.url = url;
}

@Override
public String call() throws Exception
JsonParser parser = new JsonParser();
JsonObject root = parser.parse(jsonText).getAsJsonArray().get(0).getAsJsonObject();
SimpleDateFormat websiteDatePraser = new SimpleDateFormat("y-M-d");
SimpleDateFormat modDateFormat = new SimpleDateFormat("d.M.y");
String websiteDateString = root.get("date").getAsString();
websiteDateString = websiteDateString.substring(0, websiteDateString.indexOf('T'));
Date websiteDate = null;
Date modDate = null;
try {
websiteDate = websiteDatePraser.parse(websiteDateString);
} catch (ParseException e)
{
return readFromUrl(url);
}

private String readFromUrl(String url) throws IOException {
URL orlObj = new URL(url);
InputStream inputStream = orlObj.openStream();

BufferedReader rd = new BufferedReader(new InputStreamReader(inputStream, Charset.forName("UTF-8")));
return readAll(rd);
MOLog.log(Level.WARN, e, "Website Date was incorrect");
}

private static String readAll(Reader rd) throws IOException {
StringBuilder sb = new StringBuilder();
int cp;
while ((cp = rd.read()) != -1) {
sb.append((char) cp);
}
return sb.toString();
try {
modDate = modDateFormat.parse(Reference.VERSION_DATE);
} catch (ParseException e) {
MOLog.log(Level.WARN, e, "Mod version Date was incorrect");
}
}

static class Version
{
String version;
String changes;
String type;
String date;
String url;
}

static class Versions
{
List<Version> versions;
}

public boolean constructVersionAndCheck(String jsonText,EntityPlayer player) {
Gson gson = new Gson();
Versions versions = gson.fromJson(jsonText, Versions.class);
if (versions.versions.size() > 0) {
Version version = versions.versions.get(0);

if (version != null) {
if (!version.version.equals(Reference.VERSION)) {
ChatComponentText chat = new ChatComponentText(EnumChatFormatting.GOLD + "[Matter Overdrive] " + EnumChatFormatting.WHITE + MOStringHelper.translateToLocal("alert.new_update"));
ChatStyle style = new ChatStyle();
player.addChatMessage(chat);

chat = new ChatComponentText("");
chat.appendText(EnumChatFormatting.AQUA + "Matter Overdrive " + version.version + " ");
chat.appendText(EnumChatFormatting.WHITE + "[");
style.setChatClickEvent(new ClickEvent(ClickEvent.Action.OPEN_URL, version.url));
style.setChatHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ChatComponentTranslation("info." + Reference.MOD_ID + ".updater.hover").setChatStyle(new ChatStyle().setColor(EnumChatFormatting.YELLOW))));
style.setColor(EnumChatFormatting.GREEN);
chat.appendSibling(new ChatComponentTranslation("info." + Reference.MOD_ID + ".updater.download")).setChatStyle(style);
chat.appendText(EnumChatFormatting.WHITE + "]");
player.addChatMessage(chat);

chat = new ChatComponentText(version.changes);
style = new ChatStyle();
style.setColor(EnumChatFormatting.GRAY);
chat.setChatStyle(style);
player.addChatMessage(chat);
return true;

} else {
MOLog.log(Level.INFO, "Matter Overdrive Version %1$s is up to date. From '%2$s'",version.version,mirrors[currentMirror-1]);
}
if (modDate != null && websiteDate != null ) {

if (modDate.before(websiteDate))
{
ChatComponentText chat = new ChatComponentText(EnumChatFormatting.GOLD + "[Matter Overdrive] " + EnumChatFormatting.WHITE + MOStringHelper.translateToLocal("alert.new_update"));
ChatStyle style = new ChatStyle();
player.addChatMessage(chat);

chat = new ChatComponentText("");
IChatComponent versionName = new ChatComponentText(root.get("title").getAsString() + " ").setChatStyle(new ChatStyle().setColor(EnumChatFormatting.AQUA));
chat.appendSibling(versionName);
chat.appendText(EnumChatFormatting.WHITE + "[");
style.setChatClickEvent(new ClickEvent(ClickEvent.Action.OPEN_URL, Reference.DOWNLOAD_URL));
style.setChatHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ChatComponentTranslation("info." + Reference.MOD_ID + ".updater.hover").setChatStyle(new ChatStyle().setColor(EnumChatFormatting.YELLOW))));
style.setColor(EnumChatFormatting.GREEN);
chat.appendSibling(new ChatComponentTranslation("info." + Reference.MOD_ID + ".updater.download")).setChatStyle(style);
chat.appendText(EnumChatFormatting.WHITE + "]");
player.addChatMessage(chat);

chat = new ChatComponentText(root.get("excerpt").getAsString().replaceAll("\\<.*?\\>", ""));
style = new ChatStyle();
style.setColor(EnumChatFormatting.GRAY);
chat.setChatStyle(style);
player.addChatMessage(chat);
return true;

} else
{
MOLog.log(Level.INFO, "Matter Overdrive Version %1$s is up to date. From '%2$s'", root.get("title").getAsString(), mirrors[currentMirror - 1]);
}
}
return false;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* This file is part of Matter Overdrive
* Copyright (c) 2015., Simeon Radivoev, All rights reserved.
*
* Matter Overdrive is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Matter Overdrive 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Matter Overdrive. If not, see <http://www.gnu.org/licenses>.
*/

package matteroverdrive.handler.thread;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.concurrent.Callable;

/**
* Created by Simeon on 7/13/2015.
*/
public class VersionCheckThread implements Callable<String>
{
String url;

public VersionCheckThread(String url)
{
this.url = url;
}

@Override
public String call() throws Exception
{
return readFromUrl(url);
}

private String readFromUrl(String urlPath) throws IOException {
URL url = new URL(urlPath);
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("GET");
BufferedReader in = new BufferedReader(
new InputStreamReader(connection.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();

while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
return response.toString();
}
}
2 changes: 1 addition & 1 deletion src/main/resources/assets/mo/lang/en_US.lang
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ item.pattern_drive.name=Pattern Drive
item.pattern_drive.details=Stores 2 Item Patterns
item.earl_gray_tea.name=Tea. Earl Grey. Hot.
item.romulan_ale.name=Romulan Ale
item.romulan_ale.name=Highly intoxicating alcoholic beverage of Romulan origin
item.romulan_ale.details=Highly intoxicating alcoholic beverage of Romulan origin
item.security_protocol.empty.name=Security Protocol
item.security_protocol.claim.name=Security Protocol [Claim]
item.security_protocol.claim.details=Claims Machines
Expand Down

0 comments on commit 07e0e88

Please sign in to comment.