/
ModResourcePackUtil.java
106 lines (88 loc) · 3.21 KB
/
ModResourcePackUtil.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/*
* 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.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import com.google.common.base.Charsets;
import org.apache.commons.io.IOUtils;
import org.jetbrains.annotations.Nullable;
import net.minecraft.SharedConstants;
import net.minecraft.resource.ResourceType;
import net.fabricmc.fabric.api.resource.ModResourcePack;
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.ModMetadata;
/**
* Internal utilities for managing resource packs.
*/
public final class ModResourcePackUtil {
private ModResourcePackUtil() {
}
/**
* Appends mod resource packs to the given list.
*
* @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) {
for (ModContainer container : FabricLoader.getInstance().getAllMods()) {
if (container.getMetadata().getType().equals("builtin")) {
continue;
}
Path path = container.getRootPath();
if (subPath != null) {
Path childPath = path.resolve(subPath.replace("/", path.getFileSystem().getSeparator())).toAbsolutePath().normalize();
if (!childPath.startsWith(path) || !Files.exists(childPath)) {
continue;
}
path = childPath;
}
ModResourcePack pack = new ModNioResourcePack(container.getMetadata(), path, type, null, ResourcePackActivationType.ALWAYS_ENABLED);
if (!pack.getNamespaces(type).isEmpty()) {
packs.add(pack);
}
}
}
public static boolean containsDefault(ModMetadata info, String filename) {
return "pack.mcmeta".equals(filename);
}
public static InputStream openDefault(ModMetadata info, ResourceType type, String filename) {
switch (filename) {
case "pack.mcmeta":
String description = info.getName();
if (description == null) {
description = "";
} else {
description = description.replaceAll("\"", "\\\"");
}
String pack = String.format("{\"pack\":{\"pack_format\":" + type.getPackVersion(SharedConstants.getGameVersion()) + ",\"description\":\"%s\"}}", description);
return IOUtils.toInputStream(pack, Charsets.UTF_8);
default:
return null;
}
}
public static String getName(ModMetadata info) {
if (info.getName() != null) {
return info.getName();
} else {
return "Fabric Mod \"" + info.getId() + "\"";
}
}
}