Skip to content

Commit

Permalink
maven version checking
Browse files Browse the repository at this point in the history
  • Loading branch information
dries007 committed Aug 17, 2014
1 parent 73e90a8 commit 29250bf
Show file tree
Hide file tree
Showing 10 changed files with 205 additions and 20 deletions.
7 changes: 1 addition & 6 deletions build.gradle
Expand Up @@ -48,12 +48,7 @@ new File("versions.json").write(builder.toPrettyString())

processResources {
from(sourceSets.main.resources.srcDirs) {
include '**/*.info'
expand 'version':project.version, 'mcversion':project.minecraft.version, 'modid':project.archivesBaseName, 'githuborg':githuborg, 'description':description
}

from(sourceSets.main.resources.srcDirs) {
exclude '**/*.info'
expand 'version':project.version, 'mcversion':project.minecraft.version, 'modid':project.archivesBaseName, 'githuborg':githuborg, 'description':description, 'group':project.group, 'artifactId':project.archivesBaseName
}
}

Expand Down
100 changes: 95 additions & 5 deletions src/main/java/net/doubledoordev/d3core/D3Core.java
Expand Up @@ -32,25 +32,41 @@

package net.doubledoordev.d3core;


import cpw.mods.fml.client.config.IConfigElement;
import cpw.mods.fml.client.event.ConfigChangedEvent;
import cpw.mods.fml.common.*;
import cpw.mods.fml.common.event.FMLInitializationEvent;
import cpw.mods.fml.common.event.FMLPostInitializationEvent;
import cpw.mods.fml.common.event.FMLPreInitializationEvent;
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
import cpw.mods.fml.common.registry.LanguageRegistry;
import cpw.mods.fml.common.versioning.ArtifactVersion;
import cpw.mods.fml.common.versioning.DefaultArtifactVersion;
import net.doubledoordev.d3core.util.CoreHelper;
import net.doubledoordev.d3core.util.DevPerks;
import net.doubledoordev.d3core.util.ID3Mod;
import net.minecraft.client.resources.I18n;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.common.config.ConfigElement;
import net.minecraftforge.common.config.Configuration;
import org.apache.logging.log4j.Logger;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.TreeSet;

import static net.doubledoordev.d3core.util.CoreConstants.*;

/**
* @author Dries007
*/
@Mod(modid = MODID, name = NAME, canBeDeactivated = false, guiFactory = MOD_GUI_FACTORY)
public class D3Core
public class D3Core implements ID3Mod
{
@Mod.Instance(MODID)
public static D3Core instance;
Expand All @@ -65,6 +81,8 @@ public class D3Core
private boolean debug = false;
private boolean sillyness = true;

private List<CoreHelper.ModUpdateDate> updateDateList = new ArrayList<>();

@Mod.EventHandler
public void preInit(FMLPreInitializationEvent event)
{
Expand All @@ -75,10 +93,75 @@ public void preInit(FMLPreInitializationEvent event)
syncConfig();
}

@Mod.EventHandler
public void init(FMLInitializationEvent event)
{
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
for (ModContainer modContainer : Loader.instance().getActiveModList())
{
try
{
if (modContainer instanceof FMLModContainer && modContainer.getMod() instanceof ID3Mod)
{
if (debug()) logger.info(String.format("[%s] Found a D3 Mod!", modContainer.getModId()));
Properties properties = ((FMLModContainer) modContainer).searchForVersionProperties();
if (properties != null && properties.containsKey(modContainer.getModId() + ".group") && properties.containsKey(modContainer.getModId() + ".artifactId"))
{
TreeSet<ArtifactVersion> availableVersions = new TreeSet<>();

String group = (String) properties.get(modContainer.getModId() + ".group");
String artifactId = (String) properties.get(modContainer.getModId() + ".artifactId");
if (debug()) logger.info(String.format("[%s] Group: %s ArtifactId: %s", modContainer.getModId(), group, artifactId));

URL url = new URL(MAVENURL + group.replace('.', '/') + '/' + artifactId + "/maven-metadata.xml");
if (debug()) logger.info(String.format("[%s] Maven URL: %s", modContainer.getModId(), url));

DocumentBuilder builder = dbf.newDocumentBuilder();
Document document = builder.parse(url.toURI().toString());
NodeList list = document.getDocumentElement().getElementsByTagName("version");
for (int i = 0; i < list.getLength(); i++)
{
String version = list.item(i).getFirstChild().getNodeValue();
if (version.startsWith(Loader.MC_VERSION + "-"))
{
availableVersions.add(new DefaultArtifactVersion(version.replace(Loader.MC_VERSION + "-", "")));
}
}
DefaultArtifactVersion current = new DefaultArtifactVersion(modContainer.getVersion().replace(Loader.MC_VERSION + "-", ""));

if (debug()) logger.info(String.format("[%s] Current: %s Latest: %s All versions for MC %s: %s", modContainer.getModId(), current, availableVersions.last(), Loader.MC_VERSION, availableVersions));

if (current.compareTo(availableVersions.last()) > 0)
{
updateDateList.add(new CoreHelper.ModUpdateDate(modContainer.getName(), modContainer.getModId(), current.toString(), availableVersions.last().toString()));
}
}
else
{
logger.info("D3 Mod " + modContainer.getModId() + " doesn't have the appropriate properties for version checks.");
}
}
}
catch (Exception e)
{
logger.info("D3 Mod " + modContainer.getModId() + " Version check FAILED. Please report this error!");
e.printStackTrace();
}
}
}

@Mod.EventHandler
public void init(FMLPostInitializationEvent event)
{
for (CoreHelper.ModUpdateDate updateDate : updateDateList)
{
logger.warn(String.format("Update available for %s (%s)! Current version: %s New version: %s. Please update ASAP!", updateDate.getName(), updateDate.getModId(), updateDate.getCurrentVersion(), updateDate.getLatestVersion()));
}
}

@SubscribeEvent
public void onConfigChanged(ConfigChangedEvent.OnConfigChangedEvent eventArgs)
{
if (eventArgs.modID.equals(MODID)) syncConfig();
for (ModContainer modContainer : Loader.instance().getActiveModList())
{
if (modContainer.getMod() instanceof ID3Mod)
Expand All @@ -88,9 +171,10 @@ public void onConfigChanged(ConfigChangedEvent.OnConfigChangedEvent eventArgs)
}
}

@Override
public void syncConfig()
{
configuration.setCategoryLanguageKey(MODID, "d3.core.config.core").setCategoryComment(MODID, I18n.format("d3.core.config.core.tooltip"));
configuration.setCategoryLanguageKey(MODID, "d3.core.config.core").setCategoryComment(MODID, LanguageRegistry.instance().getStringLocalization("d3.core.config.core"));

debug = configuration.getBoolean("debug", MODID, debug, "Enable debug mode", "d3.core.config.debug");
sillyness = configuration.getBoolean("sillyness", MODID, sillyness, "Enable sillyness", "d3.core.config.sillyness");
Expand All @@ -101,6 +185,12 @@ public void syncConfig()
if (configuration.hasChanged()) configuration.save();
}

@Override
public void addConfigElements(List<IConfigElement> list)
{
list.add(new ConfigElement(configuration.getCategory(MODID.toLowerCase())));
}

public static Logger getLogger()
{
return instance.logger;
Expand Down
Expand Up @@ -37,20 +37,16 @@
import cpw.mods.fml.client.config.IConfigElement;
import cpw.mods.fml.common.Loader;
import cpw.mods.fml.common.ModContainer;
import net.doubledoordev.d3core.D3Core;
import net.doubledoordev.d3core.util.CoreConstants;
import net.doubledoordev.d3core.util.ID3Mod;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiScreen;
import net.minecraft.client.resources.I18n;
import net.minecraftforge.common.config.ConfigElement;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import static net.doubledoordev.d3core.util.CoreConstants.MODID;

/**
* @author Dries007
*/
Expand All @@ -66,17 +62,13 @@ public D3ConfigGuiScreen(GuiScreen parentScreen)
private static List<IConfigElement> getConfigElements()
{
List<IConfigElement> list = new ArrayList<>();

list.add(new ConfigElement(D3Core.getConfiguration().getCategory(MODID.toLowerCase())));

for (ModContainer modContainer : Loader.instance().getActiveModList())
{
if (modContainer.getMod() instanceof ID3Mod)
{
((ID3Mod) modContainer.getMod()).addConfigElements(list);
}
}

return list;
}
}
Expand Down
Expand Up @@ -41,6 +41,7 @@ public class CoreConstants
public static final String NAME = "D³ Core";
public static final String BASEURL = "http://doubledoordev.net/";
public static final String PERKSURL = BASEURL + "perks.json";
public static final String MAVENURL = BASEURL + "maven/";
/**
* @see net.doubledoordev.d3core.client.ModConfigGuiFactory
*/
Expand Down
79 changes: 79 additions & 0 deletions src/main/java/net/doubledoordev/d3core/util/CoreHelper.java
@@ -0,0 +1,79 @@
/*
* Copyright (c) 2014,
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* Neither the name of the {organization} nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*
*/

package net.doubledoordev.d3core.util;

/**
* @author Dries007
*/
public class CoreHelper
{
private CoreHelper()
{
}

public static class ModUpdateDate
{
private final String name;
private final String modId;
private final String currentVersion;
private final String latestVersion;

public ModUpdateDate(String name, String modId, String currentVersion, String latestVersion)
{
this.name = name;
this.modId = modId;
this.currentVersion = currentVersion;
this.latestVersion = latestVersion;
}

public String getName()
{
return name;
}

public String getModId()
{
return modId;
}

public String getCurrentVersion()
{
return currentVersion;
}

public String getLatestVersion()
{
return latestVersion;
}
}
}
2 changes: 1 addition & 1 deletion src/main/resources/mcmod.info
Expand Up @@ -2,7 +2,7 @@
"modListVersion": 2,
"modList": [{
"modid": "D3Core",
"name": "${modid}",
"name": "D3Core",
"description": "${description}",
"version": "${version}",
"mcversion": "${mcversion}",
Expand Down
3 changes: 3 additions & 0 deletions src/main/resources/version.properties
@@ -0,0 +1,3 @@
D3Core.version=1.1.0
D3Core.group=net.doubledoordev.d3core
D3Core.artifactId=D3Core
1 change: 1 addition & 0 deletions template/README.md
@@ -0,0 +1 @@
This folder contains files for a correct dependent mod setup.
21 changes: 21 additions & 0 deletions template/resources/mcmod.info
@@ -0,0 +1,21 @@
{
"modListVersion": 2,
"modList": [{
"modid": "${modid}",
"name": "${modid}",
"description": "${description}",
"version": "${version}",
"mcversion": "${mcversion}",
"url": "https://github.com/${githuborg}/${modid}",
"updateUrl": "",
"authorList": [ "Dries007", "Doubledoor team" ],
"credits": "",
"logoFile": "",
"screenshots": [],
"parent": "",
"requiredMods": [ "Forge" ],
"dependencies": [ ],
"dependants": [ ],
"useDependencyInformation": true
}]
}
3 changes: 3 additions & 0 deletions template/resources/version.properties
@@ -0,0 +1,3 @@
${modid}.version=${version}
${modid}.group=${group}
${modid}.artifactId=${artifactId}

0 comments on commit 29250bf

Please sign in to comment.