Skip to content

Commit

Permalink
Start work on production runs. Should be doable.
Browse files Browse the repository at this point in the history
  • Loading branch information
marchermans committed Aug 12, 2024
1 parent 308bf85 commit 240ee2b
Show file tree
Hide file tree
Showing 6 changed files with 141 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import org.gradle.api.tasks.Optional

import javax.inject.Inject
import java.lang.reflect.Type
import java.nio.file.Path
import java.util.function.BiFunction

import static net.neoforged.gradle.dsl.common.util.PropertyUtils.*
Expand All @@ -41,11 +42,37 @@ abstract class LauncherProfile implements ConfigurableDSLElement<LauncherProfile
result.assetIndex.set(right.assetIndex.orElse(left.assetIndex))
result.assets.set(right.assets.orElse(left.assets))
result.complianceLevel.set(right.complianceLevel.orElse(left.complianceLevel))
result.downloads.set(mergeDownloads(left.downloads, right.downloads))
result.javaVersion.set(right.javaVersion.orElse(left.javaVersion))
result.libraries.set(mergeLibraries(left.libraries, right.libraries))
result.loggingConfiguration.set(right.loggingConfiguration.orElse(left.loggingConfiguration))

return result
}

private static Provider<? extends Map<? extends String, ? extends LibraryDownload>> mergeDownloads(
final Provider<Map<String, LibraryDownload>> left,
final Provider<Map<String, LibraryDownload>> right
) {
return left.orElse(Map.of()).zip(right.orElse(Map.of()), new BiFunction<Map<String, LibraryDownload>, Map<String, LibraryDownload>, Map<String, LibraryDownload>>() {
@Override
Map<String, LibraryDownload> apply(Map<String, LibraryDownload> l, Map<String, LibraryDownload> r) {
final Map<String, LibraryDownload> result = new HashMap<>(l)
result.putAll(r)
return result
}
})
}


return result
static Provider<? extends Iterable<Library>> mergeLibraries(ListProperty<Library> left, ListProperty<Library> right) {
return left.orElse(List.of()).zip(right.orElse(List.of()), new BiFunction<List<Library>, List<Library>, List<Library>>() {
@Override
List<Library> apply(List<Library> libraries, List<Library> u) {
final List<Library> result = new ArrayList<>(libraries)
result.addAll(u)
return result
}
})
}

@Inject
Expand All @@ -56,6 +83,14 @@ abstract class LauncherProfile implements ConfigurableDSLElement<LauncherProfile
this.getLoggingConfiguration().set(factory.newInstance(LoggingConfiguration.class))
}

static LauncherProfile from(final ObjectFactory factory, Path path) {
return from(factory, path.toFile().text)
}

static LauncherProfile from(final ObjectFactory factory, String json) {
return createGson(factory).fromJson(json, LauncherProfile.class)
}

static Gson createGson(ObjectFactory factory) {
final GsonBuilder builder = new GsonBuilder().disableHtmlEscaping();

Expand Down Expand Up @@ -215,16 +250,21 @@ abstract class LauncherProfile implements ConfigurableDSLElement<LauncherProfile
static Arguments merge(ObjectFactory objectFactory, Arguments left, Arguments right) {
final Arguments result = objectFactory.newInstance(Arguments.class)

result.game.set(left.game.zip(right.game, this::merge))
result.JVM.set(left.JVM.zip(right.JVM, this::merge))
result.game.set(mergeList(left.game, right.game))
result.JVM.set(mergeList(left.JVM, right.JVM))

return result
}

static List<Argument> merge(List<Argument> left, List<Argument> right) {
final List<Argument> result = new ArrayList<>(left)
result.addAll(right)
return result
private static Provider<? extends Iterable<Argument>> mergeList(final ListProperty<Argument> left, final ListProperty<Argument> right) {
return left.orElse(List.of()).zip(right.orElse(List.of()), new BiFunction<List<Argument>, List<Argument>, List<Argument>>() {
@Override
List<Argument> apply(List<Argument> l, List<Argument> r) {
final List<Argument> result = new ArrayList<>(l)
result.addAll(r)
return result
}
})
}

@Inject
Expand All @@ -237,14 +277,14 @@ abstract class LauncherProfile implements ConfigurableDSLElement<LauncherProfile

Argument game(final String value) {
final Argument argument = getObjectFactory().newInstance(Argument.class);
argument.getValue().set(value);
argument.getValue().add(value);
getGame().add(argument);
return argument;
}

Argument game(final Provider<String> value) {
final Argument argument = getObjectFactory().newInstance(Argument.class);
argument.getValue().set(value);
argument.getValue().add(value);
getGame().add(argument);
return argument;
}
Expand All @@ -256,14 +296,14 @@ abstract class LauncherProfile implements ConfigurableDSLElement<LauncherProfile

Argument jvm(final String value) {
final Argument argument = getObjectFactory().newInstance(Argument.class);
argument.getValue().set(value);
argument.getValue().add(value);
getJVM().add(argument);
return argument;
}

Argument jvm(final Provider<String> value) {
final Argument argument = getObjectFactory().newInstance(Argument.class);
argument.getValue().set(value);
argument.getValue().add(value);
getJVM().add(argument);
return argument;
}
Expand Down Expand Up @@ -309,7 +349,7 @@ abstract class LauncherProfile implements ConfigurableDSLElement<LauncherProfile
@Input
@DSLProperty
@Optional
abstract Property<String> getValue();
abstract ListProperty<String> getValue();

@CompileStatic
static class Serializer extends WithRules.Serializer<Argument> {
Expand All @@ -322,26 +362,36 @@ abstract class LauncherProfile implements ConfigurableDSLElement<LauncherProfile
Argument deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {
if (jsonElement.isJsonPrimitive()) {
final Argument argument = factory.newInstance(Argument.class);
argument.getValue().set(jsonElement.getAsString());
argument.getValue().add(jsonElement.getAsString());
return argument;
}

def result = super.deserialize(jsonElement, type, jsonDeserializationContext) as Argument

deserializeString(result.getValue(), jsonElement.getAsJsonObject(), "value")
final JsonObject object = jsonElement.getAsJsonObject();
if (object.has("value")) {
final JsonElement valueElement = object.get("value");
if (valueElement.isJsonPrimitive()) {
result.getValue().add(valueElement.getAsString());
} else if (valueElement.isJsonArray()) {
deserializeList(result.getValue(), object, "value", String.class, jsonDeserializationContext)
}
}

return result;
}

@Override
JsonElement serialize(Argument argument, Type type, JsonSerializationContext jsonSerializationContext) {
if (argument.getRules().isPresent() && argument.getRules().get().isEmpty()) {
return new JsonPrimitive(argument.getValue().get())
if (argument.getValue().get().size() == 1) {
return new JsonPrimitive(argument.getValue().get().get(0))
}
}

def result = super.serialize(argument, type, jsonSerializationContext) as JsonObject

serializeString(argument.getValue(), result, "value")
serializeList(argument.getValue(), result, "value", jsonSerializationContext)

return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ import net.minecraftforge.gdi.annotations.DSLProperty
import net.neoforged.gradle.dsl.common.util.PropertyUtils
import org.gradle.api.model.ObjectFactory
import org.gradle.api.provider.Property
import org.gradle.api.provider.Provider
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.Optional

import java.lang.reflect.Type
import java.util.regex.Pattern

@CompileStatic
abstract class OsCondition implements ConfigurableDSLElement<OsCondition> {
Expand All @@ -36,6 +38,30 @@ abstract class OsCondition implements ConfigurableDSLElement<OsCondition> {
@Optional
abstract Property<String> getVersion();

Provider<Boolean> isActive() {
def nameMatches = name.map { n ->
if (n == "windows") {
return System.getProperty("os.name").toLowerCase().contains("win")
} else if (n == "linux") {
return System.getProperty("os.name").toLowerCase().contains("unix") || System.getProperty("os.name").toLowerCase().contains("linux")
} else if (n == "osx") {
return System.getProperty("os.name").toLowerCase().contains("mac")
} else {
return false
}
}.orElse(true)

def versionMatches = version.map { v -> return Pattern.compile(v).matcher(System.getProperty("os.version")).find()
}.orElse(true)

def archMatches = arch.map { a -> return Pattern.compile(a).matcher(System.getProperty("os.arch")).find()
}.orElse(true)

return nameMatches.zip(versionMatches.zip(archMatches,
{ v, a -> v && a }),
{ n, va -> n && va })
}

@CompileStatic
static class Serializer implements JsonSerializer<OsCondition>, JsonDeserializer<OsCondition> {

Expand All @@ -47,8 +73,7 @@ abstract class OsCondition implements ConfigurableDSLElement<OsCondition> {

@Override
OsCondition deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {
if (!jsonElement.isJsonObject())
throw new JsonParseException("OS condition must be a json object")
if (!jsonElement.isJsonObject()) throw new JsonParseException("OS condition must be a json object")

final JsonObject payload = jsonElement.getAsJsonObject();
final OsCondition instance = factory.newInstance(OsCondition.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import net.neoforged.gradle.dsl.common.util.PropertyUtils
import org.gradle.api.model.ObjectFactory
import org.gradle.api.provider.MapProperty
import org.gradle.api.provider.Property
import org.gradle.api.provider.Provider
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.Nested
import org.gradle.api.tasks.Optional
Expand All @@ -23,6 +24,16 @@ abstract class Rule implements ConfigurableDSLElement<Rule> {
@Optional
abstract Property<RuleAction> getAction();

Provider<Boolean> isActive(Provider<List<String>> enabledFeatures) {
def osMatches = getOs().flatMap {os -> os.isActive()}.orElse(true)
def featureMatches = features.zip(enabledFeatures, { Map<String, Boolean> f, List<String> ef -> ef.every {feature -> f.getOrDefault(feature, true)} }).orElse(true)

def combinedMatch = osMatches.zip(featureMatches, { Boolean os, Boolean features -> os && features }).orElse(true)
def matchingRuleAction = action.orElse(RuleAction.ALLOWED)

return combinedMatch.zip(matchingRuleAction, { Boolean match, RuleAction action -> action.isAllowed() == match }).orElse(true)
}

@Nested
@DSLProperty
@Optional
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,35 @@ import groovy.transform.CompileStatic
import net.minecraftforge.gdi.ConfigurableDSLElement
import net.minecraftforge.gdi.annotations.DSLProperty
import net.neoforged.gradle.dsl.common.util.PropertyUtils
import net.neoforged.gradle.util.TransformerUtils
import org.gradle.api.model.ObjectFactory
import org.gradle.api.provider.ListProperty
import org.gradle.api.provider.Provider
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.Optional

import javax.inject.Inject
import java.lang.reflect.Type

@CompileStatic
abstract class WithRules<TSelf extends WithRules<TSelf>> implements ConfigurableDSLElement<TSelf> {

@Inject
abstract ObjectFactory getObjectFactory();

@Input
@Optional
@DSLProperty
abstract ListProperty<Rule> getRules();

Provider<Boolean> isActive() {
return isActive(getObjectFactory().listProperty(String.class))
}

Provider<Boolean> isActive(Provider<List<String>> enabledFeatures) {
return getRules().map(TransformerUtils.<Rule>allTrue({Rule rule -> rule.isActive(enabledFeatures)}))
}

@Override
int hashCode() {
return Objects.hash(getRules());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import net.neoforged.gradle.common.util.ToolUtilities;
import net.neoforged.gradle.dsl.common.extensions.AccessTransformers;
import net.neoforged.gradle.dsl.common.extensions.Mappings;
import net.neoforged.gradle.dsl.common.extensions.MinecraftArtifactCache;
import net.neoforged.gradle.dsl.common.runs.run.Run;
import net.neoforged.gradle.dsl.common.runs.run.RunManager;
import net.neoforged.gradle.dsl.common.runs.type.RunType;
Expand Down Expand Up @@ -260,7 +261,9 @@ public void runtime(final String neoFormVersion, Directory patches, Directory re

final TaskProvider<SetupProjectFromRuntime> setupTask = configureSetupTasks(runtimeDefinition.getSourceJarTask().flatMap(WithOutput::getOutput), mainSource, runtimeDefinition.getMinecraftDependenciesConfiguration());
setupTask.configure(task -> task.getShouldLockDirectories().set(false));


final MinecraftArtifactCache artifactCache = project.getExtensions().getByType(MinecraftArtifactCache.class);

project.afterEvaluate(evaledProject -> {
final EnumMap<DistributionType, TaskProvider<? extends WithOutput>> cleanProviders = new EnumMap<>(DistributionType.class);
cleanProviders.put(DistributionType.CLIENT, createCleanProvider(runtimeDefinition.getGameArtifactProvidingTasks().get(GameArtifact.CLIENT_JAR), runtimeDefinition, workingDirectory));
Expand Down Expand Up @@ -397,6 +400,19 @@ public void runtime(final String neoFormVersion, Directory patches, Directory re

CommonRuntimeExtension.configureCommonRuntimeTaskParameters(task, runtimeDefinition, workingDirectory);
});

final TaskProvider<CreateLauncherJson> createBinaryCombinedLauncher = project.getTasks().register("createCombinedLauncher", CreateLauncherJson.class, task -> {
task.getProfile().set(artifactCache.cacheVersionManifest(runtimeDefinition.getSpecification().getMinecraftVersion()).map(
file -> LauncherProfile.from(project.getObjects(), file.toPath())
).map(vanillaLauncherProfile -> LauncherProfile.merge(project.getObjects(), vanillaLauncherProfile, launcherProfile)));
task.getLibraries().from(installerConfiguration);
task.getLibraries().from(pluginLayerLibraryConfiguration);
task.getLibraries().from(gameLayerLibraryConfiguration);
task.getLibraries().from(moduleOnlyConfiguration);
task.getRepositoryURLs().set(repoCollection);

CommonRuntimeExtension.configureCommonRuntimeTaskParameters(task, runtimeDefinition, workingDirectory);
});

final TaskProvider<? extends WithOutput> joinedCleanProvider = cleanProviders.get(DistributionType.JOINED);
final TaskProvider<StripBinPatchedClasses> strippedJar = project.getTasks().register("stripBinaryPatchedClasses", StripBinPatchedClasses.class, task -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.google.common.collect.Multimap;
import com.google.common.collect.Multimaps;
import groovy.lang.Closure;
import groovyjarjarantlr4.v4.runtime.misc.NotNull;
import org.gradle.api.Project;
import org.gradle.api.Transformer;
Expand Down Expand Up @@ -453,6 +454,10 @@ public static <T> Provider<T> lazyDefaulted(Provider<T> provider, Provider<T> va
return provider.orElse(value);
}

public static <E> Transformer<Boolean, ? super List<E>> allTrue(Function<E, Boolean> closure) {
return (Transformer<Boolean, List<E>>) es -> es.stream().map(closure).reduce(Boolean::logicalAnd).map(b -> b ? Boolean.TRUE : Boolean.FALSE).orElse(true);
}

/**
* A definition for a transformer which can throw an exception.
*
Expand Down

0 comments on commit 240ee2b

Please sign in to comment.