-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileUtils.java
More file actions
192 lines (163 loc) · 7.37 KB
/
FileUtils.java
File metadata and controls
192 lines (163 loc) · 7.37 KB
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package dev.kinau.resourcepackvalidator.utils;
import dev.kinau.resourcepackvalidator.cache.AssetDictionary;
import dev.kinau.resourcepackvalidator.validator.context.FileContext;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.experimental.Accessors;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Stream;
public class FileUtils {
@RequiredArgsConstructor
@Getter
public enum Directory {
ATLASES("atlases", true),
BLOCKSTATES("blockstates", true),
FONT("font", true),
LANG("lang", true),
MODELS("models", true),
PARTICLES("particles", true),
SHADERS("shaders", true),
TEXTS("texts", true),
TEXTURES("textures", false),
SOUNDS("sounds.json", true);
private final String path;
@Accessors(fluent = true)
private final boolean containsJson;
public File getFile(File namespace) {
return new File(namespace, path);
}
public File getFile(OverlayNamespace namespace) {
return new File(namespace.getAssetsDir(), path);
}
public File getFile(File rootDir, String namespace) {
return getFile(getNamespace(rootDir, namespace));
}
}
public static File getNamespace(File rootDir, String namespace) {
return new File(getAssetsDir(rootDir), namespace);
}
public static File getAssetsDir(File rootOrOverlayDir) {
return new File(rootOrOverlayDir, "assets");
}
public static List<File> getFiles(File file) {
List<File> files = new ArrayList<>();
if (file.isDirectory()) {
File[] children = file.listFiles();
if (children != null) {
for (File child : children) {
files.addAll(getFiles(child));
}
}
} else if (file.isFile())
files.add(file);
return files;
}
public static File getFileInOverlay(OverlayNamespace namespace, File origin) {
String[] parts = origin.getAbsolutePath().split("assets" + File.separator + namespace.getNamespaceName() + File.separator);
if (parts.length <= 1) return null;
String end = String.join(File.separator, Arrays.copyOfRange(parts, 1, parts.length));
return new File(namespace.getAssetsDir(), end);
}
private static File getFile(Directory directory, OverlayNamespace defaultNamespace, String relPath, String suffix) {
File filesDir = directory.getFile(defaultNamespace);
File file = new File(filesDir, relPath.replace("/", File.separator) + suffix);
if (relPath.contains(":")) {
String[] parts = relPath.split(":");
if (parts.length > 1) {
String namespace = parts[0];
String path = parts[1];
file = new File(directory.getFile(defaultNamespace.getOverlayOrRootDir(), namespace), path.replace("/", File.separator) + suffix);
}
}
return file;
}
public static File getFile(Directory directory, OverlayNamespace defaultNamespace, String relPath, String suffix, Predicate<String> isVanilla) {
if (relPath.endsWith(suffix))
relPath = relPath.substring(0, relPath.length() - suffix.length());
File file = getFile(directory, defaultNamespace, relPath, suffix);
if (!file.exists()) {
for (OverlayNamespace underlyingOverlay : defaultNamespace.getUnderlyingOverlays()) {
file = getFile(directory, underlyingOverlay, relPath, suffix);
if (file.exists()) break;
}
if (!file.exists()) {
if (isVanilla.test(relPath))
return file;
return null;
}
}
return file;
}
public static boolean fileExists(Directory directory, OverlayNamespace defaultNamespace, String relPath, String suffix, Predicate<String> isVanilla) {
return getFile(directory, defaultNamespace, relPath, suffix, isVanilla) != null;
}
public static File getTextureFile(OverlayNamespace defaultNamespace, String relPath) {
return getFile(Directory.TEXTURES, defaultNamespace, relPath, ".png", s -> false);
}
public static boolean textureExists(OverlayNamespace defaultNamespace, String relPath, AssetDictionary assetDictionary) {
return fileExists(Directory.TEXTURES, defaultNamespace, relPath, ".png", s -> FileUtils.isVanillaTexture(s, assetDictionary));
}
public static boolean modelExists(OverlayNamespace defaultNamespace, String relPath, AssetDictionary assetDictionary) {
return relPath.startsWith("builtin/") || fileExists(Directory.MODELS, defaultNamespace, relPath, ".json", s -> FileUtils.isVanillaModel(s, assetDictionary));
}
public static boolean isVanillaTexture(String texturePath, AssetDictionary assetDictionary) {
if (texturePath.startsWith("minecraft:"))
texturePath = texturePath.substring(10);
if (!texturePath.endsWith(".png"))
texturePath += ".png";
texturePath = "minecraft/" + Directory.TEXTURES.getPath() + "/" + texturePath;
return assetDictionary.contains(texturePath);
}
public static boolean isVanillaModel(String modelPath, AssetDictionary assetDictionary) {
if (modelPath.startsWith("minecraft:"))
modelPath = modelPath.substring(10);
if (!modelPath.endsWith(".json"))
modelPath += ".json";
modelPath = "minecraft/" + Directory.MODELS.getPath() + "/" + modelPath;
return assetDictionary.contains(modelPath);
}
public static void deleteDirectory(File directory) throws IOException {
if (!directory.exists()) return;
if (!directory.isDirectory()) {
Files.delete(directory.toPath());
return;
}
try (Stream<Path> fileTree = Files.walk(directory.toPath())){
fileTree.sorted(Comparator.reverseOrder())
.map(Path::toFile)
.forEach(File::delete);
}
}
public static boolean isArmorModel(File file, OverlayNamespace namespace) {
if (file == null || !file.exists()) return false;
File equipmentModels = new File(namespace.getAssetsDir(), "models/equipment");
if (!equipmentModels.exists()) return false;
return file.toPath().startsWith(equipmentModels.toPath());
}
public static String stripNamespace(String path) {
if (path.contains(":"))
path = path.substring(path.indexOf(":") + 1);
return path;
}
public static String getRelPath(FileContext context) {
return getRelPath(context.value(), context.namespace().getNamespaceName());
}
public static String getRelPath(File file, String namespaceName) {
String relPath = "";
String[] parts = file.getPath().split("assets" + File.separator + namespaceName + File.separator + FileUtils.Directory.MODELS.getPath() + File.separator);
if (parts.length > 1)
relPath = parts[1];
if (relPath.endsWith(".json"))
relPath = relPath.substring(0, relPath.length() - 5);
relPath = stripNamespace(relPath);
return relPath;
}
}