Skip to content

Commit

Permalink
Expose a couple of mods.toml properties properly. Half-implemented
Browse files Browse the repository at this point in the history
override namespace - still needs to be reviewed and determined how this
might work in detail.

Signed-off-by: cpw <cpw+github@weeksfamily.ca>
  • Loading branch information
cpw committed Jan 21, 2019
1 parent eefc4d3 commit 040cc4b
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 13 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Expand Up @@ -261,7 +261,7 @@ project(':forge') {
installer 'cpw.mods:modlauncher:0.6.0'
installer 'net.minecraftforge:accesstransformers:0.14.+:shadowed'
installer 'net.minecraftforge:eventbus:0.6.+:service'
installer 'net.minecraftforge:forgespi:0.2.+'
installer 'net.minecraftforge:forgespi:0.4.+'
installer 'net.minecraftforge:coremods:0.2.+'
installer 'com.electronwill.night-config:core:3.4.2'
installer 'com.electronwill.night-config:toml:3.4.2'
Expand Down
9 changes: 6 additions & 3 deletions mods.toml
Expand Up @@ -25,13 +25,16 @@ key="value"
This is my mod, there may be
others, but this one is mine
'''
# Override the default resourcelocation namespace with this instead
# Currently not implemented
namespace="invsorter"
# A random extra property for a mod loader
randomExtraProperty="somevalue"
# Arbitrary key-value pairs
[inventorysorter.properties]
[modproperties.inventorysorter]
key="value"
# A list of dependencies
[[inventorysorter.dependencies]]
[[dependencies.inventorysorter]]
# the modid of the dependency
modid="forge"
# Does this dependency have to exist - if not, ordering below must be specified
Expand All @@ -43,7 +46,7 @@ key="value"
# Side this dependency is applied on - BOTH, CLIENT or SERVER
side="BOTH"
# Here's another dependency
[[inventorysorter.dependencies]]
[[dependencies.inventorysorter]]
modid="minecraft"
mandatory=true
versionRange="[1.12.2]"
Expand Down
Expand Up @@ -98,6 +98,11 @@ public VersionRange getModLoaderVersion()
return modLoaderVersion;
}

@Override
public Map<String, Object> getFileProperties() {
return this.properties;
}

public Optional<Manifest> getManifest() {
return modFile.getLocator().findManifest(modFile.getFilePath());
}
Expand Down
Expand Up @@ -28,16 +28,23 @@
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.maven.artifact.versioning.ArtifactVersion;
import org.apache.maven.artifact.versioning.DefaultArtifactVersion;

public class ModInfo implements IModInfo
{
private static final Logger LOGGER = LogManager.getLogger();
private static final DefaultArtifactVersion DEFAULT_VERSION = new DefaultArtifactVersion("1");
private static final Pattern VALID_LABEL = Pattern.compile("^[a-z][a-z0-9_-]{1,63}$");

private final ModFileInfo owningFile;
private final String modId;
private final String namespace;
private final ArtifactVersion version;
private final String displayName;
private final String description;
Expand All @@ -50,6 +57,15 @@ public ModInfo(final ModFileInfo owningFile, final UnmodifiableConfig modConfig)
this.owningFile = owningFile;
this.modConfig = modConfig;
this.modId = modConfig.<String>getOptional("modId").orElseThrow(() -> new InvalidModFileException("Missing modId entry", owningFile));
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);
}
this.version = modConfig.<String>getOptional("version").
map(s->StringSubstitutor.replace(s, owningFile != null ? owningFile.getFile() : null )).
map(DefaultArtifactVersion::new).orElse(DEFAULT_VERSION);
Expand Down Expand Up @@ -102,6 +118,16 @@ public UnmodifiableConfig getModConfig() {
return this.modConfig;
}

@Override
public String getNamespace() {
return this.namespace;
}

@Override
public Map<String, Object> getModProperties() {
return this.properties;
}

public Optional<String> getLogoFile()
{
return this.owningFile != null ? this.owningFile.getConfig().getOptional("logoFile") : this.modConfig.getOptional("logoFile");
Expand Down
11 changes: 5 additions & 6 deletions src/main/java/net/minecraftforge/fml/ModContainer.java
Expand Up @@ -29,7 +29,6 @@
import java.util.Optional;
import java.util.function.Consumer;
import java.util.function.Supplier;
import java.util.regex.Pattern;

/**
* The container that wraps around mods in the system.
Expand All @@ -46,18 +45,18 @@

public abstract class ModContainer
{
private static final Pattern VALID_MODIDS = Pattern.compile("^[a-z0-9_-]{3,64}$");
protected final String modId;
protected final String namespace;
protected final IModInfo modInfo;
protected ModLoadingStage modLoadingStage;
protected final Map<ModLoadingStage, Consumer<LifecycleEventProvider.LifecycleEvent>> triggerMap;
protected final Map<ExtensionPoint, Supplier<?>> extensionPoints = new IdentityHashMap<>();

public ModContainer(IModInfo info)
{
if (!VALID_MODIDS.matcher(info.getModId()).matches())
throw new IllegalArgumentException("Invalid modid " + info.getModId() + "! Mod ids need to be lowercase alphanumeric characters or '-'/'_' and need to be between 3 and 64 chars long.");
this.modId = info.getModId();
// TODO: Currently not reading namespace from configuration..
this.namespace = this.modId;
this.modInfo = info;
this.triggerMap = new HashMap<>();
this.modLoadingStage = ModLoadingStage.CONSTRUCT;
Expand All @@ -74,9 +73,9 @@ public final String getModId()
/**
* @return the resource prefix for the mod
*/
public final String getPrefix()
public final String getNamespace()
{
return modId;
return namespace;
}

/**
Expand Down
Expand Up @@ -276,7 +276,7 @@ ForgeRegistry<V> copy(RegistryManager stage)

int add(int id, V value)
{
final String owner = ModThreadContext.get().getActiveContainer().getPrefix();
final String owner = ModThreadContext.get().getActiveContainer().getNamespace();
return add(id, value, owner);
}

Expand Down
3 changes: 1 addition & 2 deletions src/main/java/net/minecraftforge/registries/GameData.java
Expand Up @@ -56,7 +56,6 @@
import javax.annotation.Nullable;
import java.lang.reflect.Field;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
Expand Down Expand Up @@ -821,7 +820,7 @@ public static ResourceLocation checkPrefix(String name)
int index = name.lastIndexOf(':');
String oldPrefix = index == -1 ? "" : name.substring(0, index).toLowerCase(Locale.ROOT);
name = index == -1 ? name : name.substring(index + 1);
String prefix = ModThreadContext.get().getActiveContainer().getPrefix();
String prefix = ModThreadContext.get().getActiveContainer().getNamespace();
if (!oldPrefix.equals(prefix) && oldPrefix.length() > 0)
{
LogManager.getLogger().info("Potentially Dangerous alternative prefix `{}` for name `{}`, expected `{}`. This could be a intended override, but in most cases indicates a broken mod.", oldPrefix, name, prefix);
Expand Down

0 comments on commit 040cc4b

Please sign in to comment.