Skip to content

Commit

Permalink
close #6
Browse files Browse the repository at this point in the history
- added features (a cluster of mixins)
- added configuration for disabling features/mixins
- added restricting annotations to depTools
  • Loading branch information
Taskeren committed Mar 25, 2024
1 parent dd9603c commit b0e26d1
Show file tree
Hide file tree
Showing 12 changed files with 182 additions and 111 deletions.
7 changes: 7 additions & 0 deletions .idea/codeStyles/Project.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions .idea/codeStyles/codeStyleConfig.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 0 additions & 25 deletions .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions depTools/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ repositories {
}

dependencies {
implementation("org.jetbrains:annotations:24.0.0")
}

tasks.test {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class DepTools {
Dependency.impl("Buildcraft", "buildcraft-([\\d.]+)", "com.github.GTNewHorizons:BuildCraft::dev", true),
Dependency.impl("EnderIO", "EnderIO-([\\d.]+)", "com.github.GTNewHorizons:EnderIO::dev", true),
Dependency.impl("ProjectRed", "ProjRed-([\\d.]+)-GTNH", "com.github.GTNewHorizons:ProjectRed:%version%-GTNH:dev", true),
Dependency.impl("TinkerConstruct", "TConstruct-([\\d.]+)-GTNH", "com.github.GTNewHorizons:TinkersConstruct:%version%-GTNH:dev", true)
Dependency.impl("TinkerConstruct", "TConstruct-([\\d.]+)-GTNH", "com.github.GTNewHorizons:TinkersConstruct:%version%-GTNH:dev", true),
};

public static String generateDependencyCode(Dependency[] dependencies, Path instModPath) throws IOException {
Expand Down
101 changes: 33 additions & 68 deletions depTools/src/main/java/cn/taskeren/gtnn/tools/Dependency.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package cn.taskeren.gtnn.tools;

import cn.taskeren.gtnn.tools.annotations.DepNotation;
import org.intellij.lang.annotations.Language;
import org.intellij.lang.annotations.MagicConstant;

import java.io.Serializable;
import java.nio.file.FileVisitResult;
import java.nio.file.FileVisitor;
Expand All @@ -18,6 +22,10 @@ public class Dependency implements Serializable {

public static final String VERSION_TEMPLATE = "%version%";

public static final String TYPE_IMPLEMENTATION = "implementation";
public static final String TYPE_COMPILE_ONLY = "compileOnly";
public static final String TYPE_RUNTIME_ONLY = "runtimeOnly";

private final String name;

private final Pattern pattern;
Expand Down Expand Up @@ -47,17 +55,7 @@ public class Dependency implements Serializable {
* @param dependencyType the import type of the dependency used to generate gradle code
* @param noTransitiveDependency if the dependency not transitive used to generate gradle code
*/
public Dependency(
String name,
Pattern pattern,
int versionGroupIndex,
String groupName,
String artifactName,
String versionTemplate,
String subversion,
String dependencyType,
boolean noTransitiveDependency
) {
public Dependency(String name, Pattern pattern, int versionGroupIndex, String groupName, String artifactName, String versionTemplate, String subversion, String dependencyType, boolean noTransitiveDependency) {
this.name = name;
this.pattern = pattern;
this.versionGroupIndex = versionGroupIndex;
Expand All @@ -75,17 +73,7 @@ public Dependency(
* @param source the source
*/
public Dependency(Dependency source) {
this(
source.name,
source.pattern,
source.versionGroupIndex,
source.groupName,
source.artifactName,
source.versionTemplate,
source.subversion,
source.dependencyType,
source.noTransitiveDependency
);
this(source.name, source.pattern, source.versionGroupIndex, source.groupName, source.artifactName, source.versionTemplate, source.subversion, source.dependencyType, source.noTransitiveDependency);
}

/**
Expand All @@ -99,11 +87,7 @@ public Dependency(Dependency source) {
* @param dependencyNotation the version-unassigned dependency notation
* @return the new instance
*/
public static Dependency impl(
String name,
String regex,
String dependencyNotation
) {
public static Dependency impl(String name, @Language("RegExp") String regex, @DepNotation String dependencyNotation) {
return impl(name, regex, dependencyNotation, false);
}

Expand All @@ -119,17 +103,8 @@ public static Dependency impl(
* @param noTransitive if the dependency not transitive
* @return the new instance
*/
public static Dependency impl(
String name,
String regex,
String dependencyNotation,
boolean noTransitive
) {
var builder = builder()
.name(name)
.pattern(regex)
.dependencyNotation(dependencyNotation)
.dependencyType("implementation");
public static Dependency impl(String name, @Language(value = "RegExp") String regex, @DepNotation String dependencyNotation, boolean noTransitive) {
var builder = builder().name(name).pattern(regex).dependencyNotation(dependencyNotation).implementationType();
if(noTransitive) {
builder.noTransitive();
}
Expand Down Expand Up @@ -170,9 +145,7 @@ public String getTemplatedVersion() {
}

private StringBuilder getDependencyNotationBase() {
return new StringBuilder()
.append(groupName).append(":")
.append(artifactName);
return new StringBuilder().append(groupName).append(":").append(artifactName);
}

public String getDependencyNotation() {
Expand All @@ -189,11 +162,7 @@ public String getDependencyNotationCode() {
// append comment
sb.append("// ").append(name).append("\n");
// append dependency code
sb.append(dependencyType).append("(")
.append("\"")
.append(getDependencyNotation())
.append("\"")
.append(")");
sb.append(dependencyType).append("(").append("\"").append(getDependencyNotation()).append("\"").append(")");
if(noTransitiveDependency) {
sb.append(" { ");
sb.append("transitive = false");
Expand Down Expand Up @@ -245,7 +214,7 @@ public Builder name(String name) {
return this;
}

public Builder pattern(String regex) {
public Builder pattern(@Language("RegExp") String regex) {
this.pattern = Pattern.compile(regex);
return this;
}
Expand Down Expand Up @@ -275,31 +244,37 @@ public Builder subversion(String subversion) {
return this;
}

public Builder dependencyNotation(String dependencyNotation) {
public Builder dependencyNotation(@DepNotation String dependencyNotation) {
var notation = dependencyNotation.split(":");
if(notation.length < 2 || notation.length > 4)
throw new IllegalStateException("invalid dependencyNotation: there should be only 2-4 parts in the notation");

groupName(notation[0]);
artifactName(notation[1]);
versionTemplate(
notation.length == 2 // if there is no version part
? VERSION_TEMPLATE
: notation[2].isEmpty() // if the version part is empty
? VERSION_TEMPLATE
: notation[2]
);
versionTemplate(notation.length == 2 // if there is no version part
? VERSION_TEMPLATE : notation[2].isEmpty() // if the version part is empty
? VERSION_TEMPLATE : notation[2]);
subversion(notation.length > 3 ? notation[3] : null);
return this;
}

public Builder dependencyType(String dependencyType) {
public Builder dependencyType(@MagicConstant(stringValues = {TYPE_IMPLEMENTATION, TYPE_COMPILE_ONLY, TYPE_RUNTIME_ONLY}) String dependencyType) {
this.dependencyType = dependencyType;
return this;
}

public Builder implementationType() {
dependencyType("implementation");
dependencyType(TYPE_IMPLEMENTATION);
return this;
}

public Builder compileOnlyType() {
dependencyType(TYPE_COMPILE_ONLY);
return this;
}

public Builder runtimeOnlyType() {
dependencyType(TYPE_RUNTIME_ONLY);
return this;
}

Expand All @@ -316,17 +291,7 @@ public Dependency build() {
Objects.requireNonNull(name);
Objects.requireNonNull(pattern);

return new Dependency(
name,
pattern,
versionGroupIndex,
groupName,
artifactName,
versionTemplate,
subversion,
dependencyType,
noTransitiveDependency
);
return new Dependency(name, pattern, versionGroupIndex, groupName, artifactName, versionTemplate, subversion, dependencyType, noTransitiveDependency);
}

}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package cn.taskeren.gtnn.tools.annotations;

import org.intellij.lang.annotations.Pattern;

/**
* Used to hint the dependency notation string error.
* <p>
* Examples:
* <p>
* <code>com.example:example-artifact</code>
* <code>com.example:example-artifact:</code>
* <code>com.example:example-artifact::dev</code>
* <code>com.example:example-artifact:%version%-suffix:dev</code>
*/
@Pattern("([^:]+):([^:]+)(?::([^:]*))?(?::([^:]+))?")
public @interface DepNotation {
}
53 changes: 53 additions & 0 deletions src/main/java/cn/taskeren/gtnn/mixinplugin/Feature.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package cn.taskeren.gtnn.mixinplugin;

import java.util.Arrays;
import java.util.Collection;

public enum Feature {

ReturnDisassembler(
"Add disassemblers back to the game.",
Mixin.GTLoaderMachinesMixin,
Mixin.GTShapedRecipe,
Mixin.GTShapelessRecipe
),

NoLightningRodDestruction(
"Remove the destruction when generating EU.",
Mixin.GTMetaTileEntityLightningRod
),

LargeEssentiaGeneratorLaserHatchCompat(
"Make Large Essentia Generators laser hatches compatible.",
Mixin.LargeEssentiaGeneratorMixin
),

LegacyEyeOfHarmony(
"Replace new Eye of Harmony with legacy one.",
Mixin.EyeOfHarmonyMixin
),

LegacySupercriticalTurbine(
"Replace new Supercritical Steam Turbine with legacy one.",
Mixin.XLPlasmaTurbineMixin,
Mixin.XLSCTurbineMixin
),
;

public final String desc;
public final Mixin[] mixins;

Feature(String desc, Mixin... mixins) {
this.desc = desc;
this.mixins = mixins;
}

public boolean isEnabled() {
return MixinConfig.isFeatureEnabled(this);
}

public boolean isTargetedModsLoad(Collection<TargetedMod> loadedMods) {
return Arrays.stream(mixins).allMatch(mixin -> mixin.shouldLoad(loadedMods));
}

}
3 changes: 2 additions & 1 deletion src/main/java/cn/taskeren/gtnn/mixinplugin/Mixin.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cn.taskeren.gtnn.mixinplugin;

import java.util.Arrays;
import java.util.Collection;
import java.util.List;

public enum Mixin {
Expand All @@ -27,7 +28,7 @@ public enum Mixin {
this.targetedMods = Arrays.asList(targetedMods);
}

public boolean shouldLoad(List<TargetedMod> loadedMods) {
public boolean shouldLoad(Collection<TargetedMod> loadedMods) {
return loadedMods.containsAll(this.targetedMods);
}

Expand Down
32 changes: 32 additions & 0 deletions src/main/java/cn/taskeren/gtnn/mixinplugin/MixinConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package cn.taskeren.gtnn.mixinplugin;

import com.google.common.collect.Maps;
import net.minecraftforge.common.config.Configuration;

import java.io.File;
import java.util.Map;

public class MixinConfig {

private static final Configuration CONF = new Configuration(new File("gtnn.cfg"));
private static final String CATEGORY_MIXIN = "mixin";

private static final Map<Feature, Boolean> VALUES = Maps.newHashMap();

public static void init() {
CONF.load();
VALUES.clear();

for(var feature : Feature.values()) {
var value = CONF.getBoolean("Enable" + feature.name(), CATEGORY_MIXIN, true, feature.desc);
VALUES.put(feature, value);
}

CONF.save();
}

public static boolean isFeatureEnabled(Feature feature) {
return VALUES.getOrDefault(feature, true);
}

}
Loading

0 comments on commit b0e26d1

Please sign in to comment.