Skip to content

Commit

Permalink
Move to 1.21.2, make relative to other mods
Browse files Browse the repository at this point in the history
  • Loading branch information
Apollounknowndev committed Sep 5, 2024
1 parent 2ade441 commit 0d02f0e
Show file tree
Hide file tree
Showing 3 changed files with 186 additions and 5 deletions.
2 changes: 2 additions & 0 deletions fabric-resource-loader-v0/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ loom {
accessWidenerPath = file("src/main/resources/fabric-resource-loader-v0.accesswidener")
}

moduleDependencies(project, ['fabric-api-base'])

testDependencies(project, [
':fabric-lifecycle-events-v1',
':fabric-api-base',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
/*
* Copyright (c) 2016, 2017, 2018, 2019 FabricMC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

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

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import net.minecraft.util.Identifier;

Check failure on line 27 in fabric-resource-loader-v0/src/main/java/net/fabricmc/fabric/impl/resource/loader/ModResourcePackSorter.java

View workflow job for this annotation

GitHub Actions / build (21-ubuntu)

Unused import - net.minecraft.util.Identifier.

import net.fabricmc.fabric.api.resource.ModResourcePack;
import net.fabricmc.fabric.impl.base.toposort.NodeSorting;
import net.fabricmc.fabric.impl.base.toposort.SortableNode;

public class ModResourcePackSorter {
private final Object lock = new Object();
private ModResourcePack[] packs;
/**
* Registered load phases.
*/
private final Map<String, LoadPhaseData> phases = new LinkedHashMap<>();
/**
* Phases sorted in the correct dependency order.
*/
private final List<LoadPhaseData> sortedPhases = new ArrayList<>();

ModResourcePackSorter() {
this.packs = new ModResourcePack[0];
}

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

public void addPack(ModResourcePack pack) {
Objects.requireNonNull(pack, "Can't register a null pack");

String modId = pack.getId();
Objects.requireNonNull(modId, "Can't register a pack without a mod id");

synchronized (lock) {
getOrCreatePhase(modId, true).addPack(pack);
rebuildPackList(packs.length + 1);
}
}

private LoadPhaseData getOrCreatePhase(String id, boolean sortIfCreate) {
LoadPhaseData phase = phases.get(id);

if (phase == null) {
phase = new LoadPhaseData(id);
phases.put(id, phase);
sortedPhases.add(phase);

if (sortIfCreate) {
NodeSorting.sort(sortedPhases, "mod resource packs", Comparator.comparing(data -> data.modId));
}
}

return phase;
}

private void rebuildPackList(int newLength) {
// Rebuild pack list.
if (sortedPhases.size() == 1) {
// Special case with a single phase: use the array of the phase directly.
packs = sortedPhases.getFirst().packs;
} else {
ModResourcePack[] newHandlers = new ModResourcePack[newLength];
int newHandlersIndex = 0;

for (LoadPhaseData existingPhase : sortedPhases) {
int length = existingPhase.packs.length;
System.arraycopy(existingPhase.packs, 0, newHandlers, newHandlersIndex, length);
newHandlersIndex += length;
}

packs = newHandlers;
}
}

public void addLoadOrdering(String firstPhase, String secondPhase, boolean before) {
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.");

synchronized (lock) {
LoadPhaseData first = getOrCreatePhase(firstPhase, false);
LoadPhaseData second = getOrCreatePhase(secondPhase, false);

if (before) {
LoadPhaseData.link(first, second);
} else {
LoadPhaseData.link(second, first);
}

NodeSorting.sort(this.sortedPhases, "event phases", Comparator.comparing(data -> data.modId));
rebuildPackList(packs.length);
}
}

public static class LoadPhaseData extends SortableNode<LoadPhaseData> {
final String modId;
ModResourcePack[] packs;

LoadPhaseData(String modId) {
this.modId = modId;
this.packs = new ModResourcePack[0];
}

void addPack(ModResourcePack pack) {
int oldLength = packs.length;
packs = Arrays.copyOf(packs, oldLength + 1);
packs[oldLength] = pack;
}

@Override
protected String getDescription() {
return modId;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashSet;
Expand Down Expand Up @@ -56,6 +57,7 @@
import net.fabricmc.fabric.api.resource.ResourcePackActivationType;
import net.fabricmc.loader.api.FabricLoader;
import net.fabricmc.loader.api.ModContainer;
import net.fabricmc.loader.api.metadata.CustomValue;
import net.fabricmc.loader.api.metadata.ModMetadata;

/**
Expand All @@ -64,6 +66,7 @@
public final class ModResourcePackUtil {
public static final Gson GSON = new Gson();
private static final Logger LOGGER = LoggerFactory.getLogger(ModResourcePackUtil.class);
private static final String LOAD_ORDER_KEY = "fabric:resource_load_order";

private ModResourcePackUtil() {
}
Expand All @@ -76,17 +79,53 @@ private ModResourcePackUtil() {
* @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) {
for (ModContainer container : FabricLoader.getInstance().getAllMods()) {
if (container.getMetadata().getType().equals("builtin")) {
ModResourcePackSorter sorter = new ModResourcePackSorter();

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

for (ModContainer container : containers) {
ModMetadata metadata = container.getMetadata();
String id = metadata.getId();

if (metadata.getType().equals("builtin")) {
continue;
}

ModResourcePack pack = ModNioResourcePack.create(container.getMetadata().getId(), container, subPath, type, ResourcePackActivationType.ALWAYS_ENABLED, true);
ModResourcePack pack = ModNioResourcePack.create(id, container, subPath, type, ResourcePackActivationType.ALWAYS_ENABLED, true);

if (pack == null) {
continue;
}

sorter.addPack(pack);

CustomValue loadOrder = metadata.getCustomValue(LOAD_ORDER_KEY);

if (loadOrder != null && loadOrder.getType() == CustomValue.CvType.OBJECT) {
CustomValue.CvObject object = loadOrder.getAsObject();

if (pack != null) {
packs.add(pack);
addLoadOrdering(object, allIds, sorter, true, id);
addLoadOrdering(object, allIds, sorter, false, id);
}
}

sorter.appendPacks(packs);
}

public static void addLoadOrdering(CustomValue.CvObject object, List<String> allIds, ModResourcePackSorter sorter, boolean before, String currentId) {
List<String> modIds = new ArrayList<>();
CustomValue array = object.get(before ? "before" : "after");

if (array != null && array.getType() == CustomValue.CvType.ARRAY) {
for (CustomValue id : array.getAsArray()) {
if (id.getType() == CustomValue.CvType.STRING) {
modIds.add(id.getAsString());
}
}
}

modIds.stream().filter(allIds::contains).forEach(modId -> sorter.addLoadOrdering(modId, currentId, before));
}

public static void refreshAutoEnabledPacks(List<ResourcePackProfile> enabledProfiles, Map<String, ResourcePackProfile> allProfiles) {
Expand Down

0 comments on commit 0d02f0e

Please sign in to comment.