Skip to content

Commit

Permalink
Add 3 more test mods, and minor code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
modmuss50 committed Sep 18, 2024
1 parent bf35bca commit 523a4d1
Show file tree
Hide file tree
Showing 9 changed files with 128 additions and 20 deletions.
44 changes: 44 additions & 0 deletions fabric-resource-loader-v0/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,47 @@ testDependencies(project, [
':fabric-api-base',
':fabric-resource-loader-v0'
])

// Setup 3 test mods used for testing resource sorting
sourceSets {
testmodA
testmodB
testmodC
}

[sourceSets.testmodA, sourceSets.testmodB, sourceSets.testmodC].each { sourceSet ->
dependencies {
testmodImplementation sourceSet.output
}
rootProject.dependencies {
testmodImplementation sourceSet.output
}

tasks.register("${sourceSet.name}Jar", Jar) {
from sourceSet.output
archiveBaseName.set(sourceSet.name)
}
}

rootProject.allprojects.each { p ->
if (p.extensions.findByName("loom") == null) {
return // Skip over the meta projects
}

p.loom.mods.register("fabric-resource-loader-v0-testmod-a") {
sourceSet sourceSets.testmodA
}
p.loom.mods.register("fabric-resource-loader-v0-testmod-b") {
sourceSet sourceSets.testmodB
}
p.loom.mods.register("fabric-resource-loader-v0-testmod-c") {
sourceSet sourceSets.testmodC
}
}

tasks.named("remapTestmodJar", net.fabricmc.loom.task.RemapJarTask) {
nestedJars.from(tasks.testmodAJar)
nestedJars.from(tasks.testmodBJar)
nestedJars.from(tasks.testmodCJar)
addNestedDependencies = true
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ public class ModNioResourcePack implements ResourcePack, ModResourcePack {
*/
private final boolean modBundled;

@Nullable
public static ModNioResourcePack create(String id, ModContainer mod, String subPath, ResourceType type, ResourcePackActivationType activationType, boolean modBundled) {
List<Path> rootPaths = mod.getRootPaths();
List<Path> paths;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package net.fabricmc.fabric.impl.resource.loader;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.Set;
Expand All @@ -35,6 +34,7 @@
import net.minecraft.text.Text;

import net.fabricmc.fabric.api.resource.ModResourcePack;
import net.fabricmc.loader.api.FabricLoader;

/**
* Represents a resource pack provider for mods and built-in mods resource packs.
Expand Down Expand Up @@ -134,8 +134,7 @@ public void register(Consumer<ResourcePackProfile> consumer) {
}

private void registerModPack(Consumer<ResourcePackProfile> consumer, @Nullable String subPath, Predicate<Set<String>> parents) {
List<ModResourcePack> packs = new ArrayList<>();
ModResourcePackUtil.appendModResourcePacks(packs, this.type, subPath);
List<ModResourcePack> packs = ModResourcePackUtil.getModResourcePacks(FabricLoader.getInstance(), this.type, subPath);

for (ModResourcePack pack : packs) {
ResourcePackProfile profile = ResourcePackProfile.create(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedHashMap;
import java.util.List;
Expand All @@ -44,8 +45,8 @@ public class ModResourcePackSorter {
this.packs = new ModResourcePack[0];
}

public void appendPacks(List<ModResourcePack> packs) {
packs.addAll(Arrays.asList(this.packs));
public List<ModResourcePack> getPacks() {
return Collections.unmodifiableList(Arrays.asList(this.packs));
}

public void addPack(ModResourcePack pack) {
Expand Down Expand Up @@ -95,7 +96,7 @@ private void rebuildPackList(int newLength) {
}
}

public void addLoadOrdering(String firstPhase, String secondPhase, String order) {
public void addLoadOrdering(String firstPhase, String secondPhase, ModResourcePackUtil.Order order) {
Objects.requireNonNull(firstPhase, "Tried to add an ordering for a null phase.");
Objects.requireNonNull(secondPhase, "Tried to add an ordering for a null phase.");
if (firstPhase.equals(secondPhase)) throw new IllegalArgumentException("Tried to add a phase that depends on itself.");
Expand All @@ -104,10 +105,9 @@ public void addLoadOrdering(String firstPhase, String secondPhase, String order)
LoadPhaseData first = getOrCreatePhase(firstPhase, false);
LoadPhaseData second = getOrCreatePhase(secondPhase, false);

if (order.equals("before")) {
LoadPhaseData.link(first, second);
} else {
LoadPhaseData.link(second, first);
switch (order) {
case BEFORE -> LoadPhaseData.link(first, second);
case AFTER -> LoadPhaseData.link(second, first);
}

NodeSorting.sort(this.sortedPhases, "event phases", Comparator.comparing(data -> data.modId));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,16 +72,15 @@ private ModResourcePackUtil() {
}

/**
* Appends mod resource packs to the given list.
* Returns a list of mod resource packs.
*
* @param packs the resource pack list to append
* @param type the type of resource
* @param subPath the resource pack sub path directory in mods, may be {@code null}
*/
public static void appendModResourcePacks(List<ModResourcePack> packs, ResourceType type, @Nullable String subPath) {
public static List<ModResourcePack> getModResourcePacks(FabricLoader fabricLoader, ResourceType type, @Nullable String subPath) {
ModResourcePackSorter sorter = new ModResourcePackSorter();

Collection<ModContainer> containers = FabricLoader.getInstance().getAllMods();
Collection<ModContainer> containers = fabricLoader.getAllMods();
List<String> allIds = containers.stream().map(ModContainer::getMetadata).map(ModMetadata::getId).toList();

for (ModContainer container : containers) {
Expand All @@ -105,17 +104,17 @@ public static void appendModResourcePacks(List<ModResourcePack> packs, ResourceT
if (loadOrder != null && loadOrder.getType() == CustomValue.CvType.OBJECT) {
CustomValue.CvObject object = loadOrder.getAsObject();

addLoadOrdering(object, allIds, sorter, "before", id);
addLoadOrdering(object, allIds, sorter, "after", id);
addLoadOrdering(object, allIds, sorter, Order.BEFORE, id);
addLoadOrdering(object, allIds, sorter, Order.AFTER, id);
}
}

sorter.appendPacks(packs);
return sorter.getPacks();
}

public static void addLoadOrdering(CustomValue.CvObject object, List<String> allIds, ModResourcePackSorter sorter, String order, String currentId) {
public static void addLoadOrdering(CustomValue.CvObject object, List<String> allIds, ModResourcePackSorter sorter, Order order, String currentId) {
List<String> modIds = new ArrayList<>();
CustomValue array = object.get(order);
CustomValue array = object.get(order.jsonKey);

if (array != null && array.getType() == CustomValue.CvType.ARRAY) {
for (CustomValue id : array.getAsArray()) {
Expand Down Expand Up @@ -283,4 +282,15 @@ public static DataPackSettings createTestServerSettings(List<String> enabled, Li
public static ResourcePackManager createClientManager() {
return new ResourcePackManager(new VanillaDataPackProvider(new SymlinkFinder((path) -> true)), new ModResourcePackCreator(ResourceType.SERVER_DATA, true));
}

public enum Order {
BEFORE("before"),
AFTER("after");

private final String jsonKey;

Order(String jsonKey) {
this.jsonKey = jsonKey;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
"environment": "*",
"license": "Apache-2.0",
"depends": {
"fabric-resource-loader-v0": "*"
"fabric-resource-loader-v0": "*",
"fabric-resource-loader-v0-testmod-a": "*",
"fabric-resource-loader-v0-testmod-b": "*",
"fabric-resource-loader-v0-testmod-c": "*"
},
"entrypoints": {
"main": [
Expand Down
22 changes: 22 additions & 0 deletions fabric-resource-loader-v0/src/testmodA/resources/fabric.mod.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"schemaVersion": 1,
"id": "fabric-resource-loader-v0-testmod-a",
"name": "Fabric Resource Loader (v0) Test Mod A",
"version": "1.0.0",
"environment": "*",
"license": "Apache-2.0",
"depends": {
"fabric-resource-loader-v0": "*"
},
"custom": {
"fabric:resource_load_order": {
"before": [
"fabric-resource-loader-v0-testmod-b",
"mod-that-does-not-exist"
],
"after": [
"mod-that-also-does-not-exist"
]
}
}
}
11 changes: 11 additions & 0 deletions fabric-resource-loader-v0/src/testmodB/resources/fabric.mod.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"schemaVersion": 1,
"id": "fabric-resource-loader-v0-testmod-b",
"name": "Fabric Resource Loader (v0) Test Mod B",
"version": "1.0.0",
"environment": "*",
"license": "Apache-2.0",
"depends": {
"fabric-resource-loader-v0": "*"
}
}
18 changes: 18 additions & 0 deletions fabric-resource-loader-v0/src/testmodC/resources/fabric.mod.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"schemaVersion": 1,
"id": "fabric-resource-loader-v0-testmod-c",
"name": "Fabric Resource Loader (v0) Test Mod C",
"version": "1.0.0",
"environment": "*",
"license": "Apache-2.0",
"depends": {
"fabric-resource-loader-v0": "*"
},
"custom": {
"fabric:resource_load_order": {
"after": [
"fabric-resource-loader-v0-testmod-b"
]
}
}
}

0 comments on commit 523a4d1

Please sign in to comment.