2018-03-25 15:59:43 +00:00
/ *
* Minecraft Forge
2019-02-10 22:57:03 +00:00
* Copyright ( c ) 2016 - 2019 .
2018-03-25 15:59:43 +00:00
*
* This library 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 version 2 . 1
* of the License .
*
* This library 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 this library ; if not , write to the Free Software
* Foundation , Inc . , 51 Franklin Street , Fifth Floor , Boston , MA 02110 - 1301 USA
* /
package net.minecraftforge.fml.loading.moddiscovery ;
2018-06-06 15:37:56 +00:00
import com.electronwill.nightconfig.core.UnmodifiableConfig ;
2019-01-27 19:51:35 +00:00
import net.minecraftforge.fml.loading.StringUtils ;
2019-01-14 03:51:36 +00:00
import net.minecraftforge.forgespi.language.IModInfo ;
2018-06-11 01:12:46 +00:00
import net.minecraftforge.fml.loading.StringSubstitutor ;
2018-04-07 15:48:43 +00:00
2019-01-27 19:51:35 +00:00
import java.net.URL ;
2018-06-06 15:37:56 +00:00
import java.util.Arrays ;
import java.util.Collections ;
2018-04-07 15:48:43 +00:00
import java.util.List ;
2018-06-06 15:37:56 +00:00
import java.util.Map ;
2018-06-11 13:56:44 +00:00
import java.util.Optional ;
2019-01-21 02:26:31 +00:00
import java.util.regex.Pattern ;
2018-06-06 15:37:56 +00:00
import java.util.stream.Collectors ;
2018-03-25 15:59:43 +00:00
2019-01-21 02:26:31 +00:00
import org.apache.logging.log4j.LogManager ;
import org.apache.logging.log4j.Logger ;
2018-10-04 02:47:07 +00:00
import org.apache.maven.artifact.versioning.ArtifactVersion ;
import org.apache.maven.artifact.versioning.DefaultArtifactVersion ;
2018-09-22 10:40:22 +00:00
2018-06-11 01:36:50 +00:00
public class ModInfo implements IModInfo
{
2019-01-21 02:26:31 +00:00
private static final Logger LOGGER = LogManager . getLogger ( ) ;
2018-06-06 15:37:56 +00:00
private static final DefaultArtifactVersion DEFAULT_VERSION = new DefaultArtifactVersion ( " 1 " ) ;
2019-01-21 02:26:31 +00:00
private static final Pattern VALID_LABEL = Pattern . compile ( " ^[a-z][a-z0-9_-]{1,63}$ " ) ;
2018-06-11 01:12:46 +00:00
private final ModFileInfo owningFile ;
2018-03-25 15:59:43 +00:00
private final String modId ;
2019-01-21 02:26:31 +00:00
private final String namespace ;
2018-03-25 15:59:43 +00:00
private final ArtifactVersion version ;
private final String displayName ;
private final String description ;
2019-01-27 19:51:35 +00:00
private final URL updateJSONURL ;
2018-06-11 01:12:46 +00:00
private final List < IModInfo . ModVersion > dependencies ;
2018-06-06 15:37:56 +00:00
private final Map < String , Object > properties ;
private final UnmodifiableConfig modConfig ;
2018-03-25 15:59:43 +00:00
2018-06-11 01:12:46 +00:00
public ModInfo ( final ModFileInfo owningFile , final UnmodifiableConfig modConfig )
2018-06-06 15:37:56 +00:00
{
2018-03-25 15:59:43 +00:00
this . owningFile = owningFile ;
2018-06-06 15:37:56 +00:00
this . modConfig = modConfig ;
this . modId = modConfig . < String > getOptional ( " modId " ) . orElseThrow ( ( ) - > new InvalidModFileException ( " Missing modId entry " , owningFile ) ) ;
2019-01-21 02:26:31 +00:00
if ( ! VALID_LABEL . matcher ( this . modId ) . matches ( ) ) {
LOGGER . fatal ( " Invalid modId found in file {} - {} does not match the standard: {} " , this . owningFile . getFile ( ) . getFilePath ( ) , this . modId , VALID_LABEL . pattern ( ) ) ;
throw new InvalidModFileException ( " Invalid modId found : " + this . modId , owningFile ) ;
}
this . namespace = modConfig . < String > getOptional ( " namespace " ) . orElse ( this . modId ) ;
if ( ! VALID_LABEL . matcher ( this . namespace ) . matches ( ) ) {
LOGGER . fatal ( " Invalid override namespace found in file {} - {} does not match the standard: {} " , this . owningFile . getFile ( ) . getFilePath ( ) , this . namespace , VALID_LABEL . pattern ( ) ) ;
throw new InvalidModFileException ( " Invalid override namespace found : " + this . namespace , owningFile ) ;
}
2018-06-06 15:37:56 +00:00
this . version = modConfig . < String > getOptional ( " version " ) .
map ( s - > StringSubstitutor . replace ( s , owningFile ! = null ? owningFile . getFile ( ) : null ) ) .
map ( DefaultArtifactVersion : : new ) . orElse ( DEFAULT_VERSION ) ;
this . displayName = modConfig . < String > getOptional ( " displayName " ) . orElse ( null ) ;
this . description = modConfig . get ( " description " ) ;
2019-01-27 19:51:35 +00:00
this . updateJSONURL = modConfig . < String > getOptional ( " updateJSONURL " ) . map ( StringUtils : : toURL ) . orElse ( null ) ;
2018-06-06 15:37:56 +00:00
if ( owningFile ! = null ) {
this . dependencies = owningFile . getConfig ( ) . < List < UnmodifiableConfig > > getOptional ( Arrays . asList ( " dependencies " , this . modId ) ) .
orElse ( Collections . emptyList ( ) ) . stream ( ) . map ( dep - > new ModVersion ( this , dep ) ) . collect ( Collectors . toList ( ) ) ;
this . properties = owningFile . getConfig ( ) . < UnmodifiableConfig > getOptional ( Arrays . asList ( " modproperties " , this . modId ) ) .
map ( UnmodifiableConfig : : valueMap ) . orElse ( Collections . emptyMap ( ) ) ;
} else {
this . dependencies = Collections . emptyList ( ) ;
this . properties = Collections . emptyMap ( ) ;
}
2018-03-25 15:59:43 +00:00
}
2018-06-11 01:12:46 +00:00
@Override
public ModFileInfo getOwningFile ( ) {
return owningFile ;
2018-03-25 15:59:43 +00:00
}
2018-06-11 01:12:46 +00:00
@Override
2018-03-25 15:59:43 +00:00
public String getModId ( ) {
return modId ;
}
2018-06-11 13:56:44 +00:00
@Override
public String getDisplayName ( )
{
return this . displayName ;
}
@Override
public String getDescription ( )
{
return this . description ;
}
2019-01-27 19:51:35 +00:00
2018-06-11 01:12:46 +00:00
@Override
2018-03-25 15:59:43 +00:00
public ArtifactVersion getVersion ( ) {
return version ;
}
2018-06-11 01:12:46 +00:00
@Override
public List < IModInfo . ModVersion > getDependencies ( ) {
2018-06-06 15:37:56 +00:00
return this . dependencies ;
2018-04-07 15:48:43 +00:00
}
2018-06-11 01:12:46 +00:00
@Override
public UnmodifiableConfig getModConfig ( ) {
return this . modConfig ;
2018-03-25 15:59:43 +00:00
}
2019-01-21 02:26:31 +00:00
@Override
public String getNamespace ( ) {
return this . namespace ;
}
@Override
public Map < String , Object > getModProperties ( ) {
return this . properties ;
}
2019-01-27 19:51:35 +00:00
@Override
public URL getUpdateURL ( ) {
return this . updateJSONURL ;
}
2018-06-11 13:56:44 +00:00
public Optional < String > getLogoFile ( )
{
2018-09-29 05:29:35 +00:00
return this . owningFile ! = null ? this . owningFile . getConfig ( ) . getOptional ( " logoFile " ) : this . modConfig . getOptional ( " logoFile " ) ;
2018-09-05 00:23:45 +00:00
}
public boolean hasConfigUI ( )
{
return false ;
2018-06-11 13:56:44 +00:00
}
2018-03-25 15:59:43 +00:00
}