Skip to content

Commit

Permalink
Add NamespaceProvider
Browse files Browse the repository at this point in the history
Add ParentSchema
Fix ModDependencies#mmoddingLibraryVersion not having enough access rights to be modified
Fix InjectedInterfaces parsing as FabricMC format even in QMJ
Bump Version
  • Loading branch information
FirstMegaGame4 committed Jun 3, 2024
1 parent 72342bd commit 2ad9971
Show file tree
Hide file tree
Showing 11 changed files with 201 additions and 37 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ plugins {
}

group = "com.mmodding"
version = "0.0.5-alpha"
version = "0.0.6-alpha"
val javaVersion = 17

repositories {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/mmodding/gradle/MModdingGradle.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public Dependency configureFMJForDependency(Dependency dependency, Action<Fabric

action.execute(modJson);

this.nestedJarsProcessor.addManifest(
this.nestedJarsProcessor.addModJson(
new NestedJarsProcessor.Metadata(dependency.getGroup(), dependency.getName(), dependency.getVersion()),
modJson
);
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/com/mmodding/gradle/api/EnvironmentTarget.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ public enum EnvironmentTarget {
CLIENT("client"),
DEDICATED_SERVER("server");

private final String manifestName;
private final String qualifier;

EnvironmentTarget(String manifestName) {
this.manifestName = manifestName;
EnvironmentTarget(String qualifier) {
this.qualifier = qualifier;
}

public String getManifestName() {
return this.manifestName;
public String getQualifier() {
return this.qualifier;
}
}
15 changes: 12 additions & 3 deletions src/main/java/com/mmodding/gradle/api/mod/json/FabricModJson.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,13 @@ public void writeJson(Path path) throws IOException {
writer.beginObject();

writer.name("schemaVersion").value(1)
.name("id").value(Objects.requireNonNull(this.namespace, "Missing namespace/mod ID in manifest declaration."));
.name("id").value(Objects.requireNonNull(this.namespace, "Missing namespace/mod ID in mod json declaration."));

if (this.name != null) {
writer.name("name").value(this.name);
}

writer.name("version").value(Objects.requireNonNull(this.version, "Missing version in manifest declaration."));
writer.name("version").value(Objects.requireNonNull(this.version, "Missing version in mod json declaration."));

if (this.description != null) {
writer.name("description").value(this.description);
Expand All @@ -90,7 +90,7 @@ public void writeJson(Path path) throws IOException {
}

if (this.environment != EnvironmentTarget.ANY) {
writer.name("environment").value(this.environment.getManifestName());
writer.name("environment").value(this.environment.getQualifier());
}

if (!this.entrypoints.isEmpty()) {
Expand Down Expand Up @@ -121,6 +121,11 @@ public void writeJson(Path path) throws IOException {
this.dependencies.writeJson(writer);
}

if (!this.provider.isEmpty()) {
writer.name("provides");
this.provider.writeJson(writer);
}

if (this.accessWidener != null) {
writer.name("accessWidener").value(this.accessWidener);
}
Expand All @@ -137,6 +142,10 @@ public void writeJson(Path path) throws IOException {
this.injectedInterfaces.fill(this.custom, false);
}

if (!this.parent.isEmpty()) {
this.parent.fill(this.custom);
}

if (!this.custom.isEmpty()) {
writer.name("custom");
this.custom.writeJson(writer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public void writeJson(JsonWriter writer) throws IOException {
if (this.environment != EnvironmentTarget.ANY) {
writer.beginObject();
writer.name("config").value(this.file);
writer.name("environment").value(this.environment.getManifestName());
writer.name("environment").value(this.environment.getQualifier());
writer.endObject();
} else {
writer.value(this.file);
Expand Down
24 changes: 24 additions & 0 deletions src/main/java/com/mmodding/gradle/api/mod/json/ModJson.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@ public abstract class ModJson<R extends ModDependencies.ModDependency, D extends
protected EnvironmentTarget environment = EnvironmentTarget.ANY;
protected final ContactInformation contact = new ContactInformation();
protected final ModEntrypoints entrypoints = new ModEntrypoints(this instanceof QuiltModJson);
protected final NamespaceProvider provider = new NamespaceProvider();
protected final List<MixinFile> mixins = new ArrayList<>();
protected String accessWidener;
protected final InjectedInterfaces injectedInterfaces = new InjectedInterfaces();
protected ParentSchema parent = new ParentSchema(null);
protected final CustomElement.CustomBlock custom = new CustomElement.CustomBlock();

public void fillDefaults(Project project) {
Expand Down Expand Up @@ -115,6 +117,14 @@ public void withEntrypoints(Action<ModEntrypoints> action) {

public abstract void withDependencies(Action<D> action);

public NamespaceProvider getProvider() {
return this.provider;
}

public void withProvider(Action<NamespaceProvider> action) {
action.execute(this.provider);
}

public List<MixinFile> getMixins() {
return this.mixins;
}
Expand Down Expand Up @@ -143,6 +153,20 @@ public void withInjectedInterfaces(Action<InjectedInterfaces> action) {
action.execute(this.injectedInterfaces);
}

public ParentSchema getParent() {
return this.parent;
}

public void withParent(String namespace) {
this.parent = new ParentSchema(namespace);
}

public void withParent(String namespace, Action<ParentSchema> action) {
ParentSchema parent = new ParentSchema(namespace);
action.execute(parent);
this.parent = parent;
}

public CustomElement.CustomBlock getCustom() {
return this.custom;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.mmodding.gradle.api.mod.json;

import org.quiltmc.parsers.json.JsonWriter;

import java.io.IOException;
import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

public class NamespaceProvider implements Serializable {

private final Set<String> namespaces = new HashSet<>();

public boolean isEmpty() {
return this.namespaces.isEmpty();
}

public void provide(String namespace) {
this.namespaces.add(namespace);
}

public void provide(Set<String> namespaces) {
this.namespaces.addAll(namespaces);
}

public void writeJson(JsonWriter writer) throws IOException {
writer.beginArray();
for (String namespace : this.namespaces) {
writer.value(namespace);
}
writer.endArray();
}
}
90 changes: 90 additions & 0 deletions src/main/java/com/mmodding/gradle/api/mod/json/ParentSchema.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package com.mmodding.gradle.api.mod.json;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

public class ParentSchema implements Serializable {

private final String namespace;
private final Set<String> badges = new HashSet<>();

private String name = null;
private String description = null;
private String icon = null;

public ParentSchema(String namespace) {
this.namespace = namespace;
}

public boolean isEmpty() {
return this.namespace == null;
}

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

public String getName() {
return this.name;
}

public void setName(String name) {
this.name = name;
}

public String getDescription() {
return this.description;
}

public void setDescription(String description) {
this.description = description;
}

public String getIcon() {
return this.icon;
}

public void setIcon(String icon) {
this.icon = icon;
}

public Set<String> getBadges() {
return this.badges;
}

public void addBadge(String badge) {
this.badges.add(badge);
}

public void addBadges(Set<String> badges) {
this.badges.addAll(badges);
}

public void fill(CustomElement.CustomBlock custom) {
if (this.name != null || this.description != null || this.icon != null || !this.badges.isEmpty()) {
custom.putBlock("modmenu", modmenu ->
modmenu.putBlock("parent", parent -> {
parent.put("id", this.namespace);
if (this.name != null) {
parent.put("name", this.name);
}
if (this.description != null) {
parent.put("description", this.description);
}
if (this.icon != null) {
parent.put("icon", this.icon);
}
if (!this.badges.isEmpty()) {
parent.putArray("badges", array -> this.badges.forEach(array::addUnique));
}
})
);
}
else {
custom.putBlock("modmenu", modmenu ->
modmenu.put("parent", this.namespace)
);
}
}
}
19 changes: 14 additions & 5 deletions src/main/java/com/mmodding/gradle/api/mod/json/QuiltModJson.java
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,9 @@ public void writeJson(Path path) throws IOException {
{
writer.name("quilt_loader")
.beginObject()
.name("group").value(Objects.requireNonNull(this.group, "Missing group in manifest declaration."))
.name("id").value(Objects.requireNonNull(this.namespace, "Missing namespace/mod ID in manifest declaration."))
.name("version").value(Objects.requireNonNull(this.version, "Missing version in manifest declaration."));
.name("group").value(Objects.requireNonNull(this.group, "Missing group in mod json declaration."))
.name("id").value(Objects.requireNonNull(this.namespace, "Missing namespace/mod ID in mod json declaration."))
.name("version").value(Objects.requireNonNull(this.version, "Missing version in mod json declaration."));

if(this.name != null && this.description != null && this.contact.notEmpty()) {
writer.name("metadata")
Expand Down Expand Up @@ -141,6 +141,11 @@ public void writeJson(Path path) throws IOException {
this.dependencies.writeJson(writer);
}

if (!this.provider.isEmpty()) {
writer.name("provides");
this.provider.writeJson(writer);
}

writer.endObject();
}

Expand All @@ -154,7 +159,7 @@ public void writeJson(Path path) throws IOException {

{
writer.name("minecraft").beginObject()
.name("environment").value(this.environment.getManifestName())
.name("environment").value(this.environment.getQualifier())
.endObject();
}

Expand Down Expand Up @@ -187,7 +192,11 @@ public void writeJson(Path path) throws IOException {
}

if (!this.injectedInterfaces.isEmpty()) {
this.injectedInterfaces.fill(this.custom, false);
this.injectedInterfaces.fill(this.custom, true);
}

if (!this.parent.isEmpty()) {
this.parent.fill(this.custom);
}

if (!this.custom.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ public void setMinecraftVersion(String minecraftVersion) {
this.minecraftVersion = minecraftVersion;
}

public String getMModdingLibraryVersion() {
public String getMmoddingLibraryVersion() {
return this.mmoddingLibraryVersion;
}

public void setMModdingLibraryVersion(String mmoddingLibraryVersion) {
public void setMmoddingLibraryVersion(String mmoddingLibraryVersion) {
this.mmoddingLibraryVersion = mmoddingLibraryVersion;
}

Expand Down
37 changes: 18 additions & 19 deletions src/main/java/com/mmodding/gradle/impl/NestedJarsProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,31 +38,30 @@ public NestedJarsProcessor(Project project) {
}

/**
* Adds a manifest to inject into the given dependency.
* Adds a mod json to inject into the given dependency.
*
* @param metadata the dependency identifier
* @param manifest the manifest to inject
* @param modJson the mod json to inject
*/
public void addManifest(@NotNull Metadata metadata, @NotNull ModJson<?, ?> manifest) {
public void addModJson(@NotNull Metadata metadata, @NotNull ModJson<?, ?> modJson) {
var set = this.toProcess.computeIfAbsent(metadata, md -> new HashSet<>());

for (var oManifest : set) {
if (oManifest.getClass().equals(manifest.getClass())) {
throw new IllegalArgumentException("Cannot add multiple manifests of the same type to the same dependency.");
for (var oModJson : set) {
if (oModJson.getClass().equals(modJson.getClass())) {
throw new IllegalArgumentException("Cannot add multiple mod json of the same type to the same dependency.");
}
}

// Add some manifest data that is always relevant for non-mod libraries.
manifest.withCustom(block -> {
// Add some mod json data that is always relevant for non-mod libraries.
modJson.withCustom(block ->
block.withBlock("modmenu", modMenu -> {
modMenu.withArray("badges", badges -> {
badges.addUnique("library");
});
modMenu.withArray("badges", badges ->
badges.addUnique("library"));
modMenu.put("update_checker", false);
});
});
})
);

set.add(manifest);
set.add(modJson);
}

/**
Expand Down Expand Up @@ -161,18 +160,18 @@ private void remapNestedJar(@NotNull File file) {
version
);

var manifests = this.toProcess.get(metadata);
var modJsonSet = this.toProcess.get(metadata);

if (manifests == null || manifests.isEmpty()) {
if (modJsonSet == null || modJsonSet.isEmpty()) {
return;
}

this.project.getLogger().lifecycle("Found dependency {} to process!", metadata);

try (var zipFs = FileSystems.newFileSystem(path)) {
for (var manifest : manifests) {
var manifestPath = zipFs.getPath(manifest.getFileName());
manifest.writeJson(manifestPath);
for (var modJson : modJsonSet) {
var modJsonPath = zipFs.getPath(modJson.getFileName());
modJson.writeJson(modJsonPath);
}
} catch (IOException e) {
throw new RuntimeException(e);
Expand Down

0 comments on commit 2ad9971

Please sign in to comment.