This repository has been archived by the owner on Feb 18, 2021. It is now read-only.
forked from FabricMC/tiny-remapper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOutputConsumerPath.java
144 lines (120 loc) · 4.17 KB
/
OutputConsumerPath.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
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
/*
* Copyright (C) 2016, 2018 Player, asie
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package net.fabricmc.tinyremapper;
import java.io.Closeable;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.file.FileAlreadyExistsException;
import java.nio.file.FileSystems;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.StandardCopyOption;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.HashMap;
import java.util.Map;
import java.util.function.BiConsumer;
public class OutputConsumerPath implements BiConsumer<String, byte[]>, Closeable {
public OutputConsumerPath(Path dstFile) throws IOException {
if (!isJar(dstFile)) {
Files.createDirectories(dstFile);
dstDir = dstFile;
closeFs = false;
isJar = false;
} else {
createParentDirs(dstFile);
URI uri;
try {
uri = new URI("jar:"+dstFile.toUri().toString());
} catch (URISyntaxException e) {
throw new RuntimeException(e);
}
Map<String, String> env = new HashMap<>();
env.put("create", "true");
dstDir = FileSystems.newFileSystem(uri, env).getPath("/");
closeFs = true;
isJar = true;
}
}
public OutputConsumerPath(Path dstDir, boolean closeFs) {
this.dstDir = dstDir;
this.closeFs = closeFs;
isJar = dstDir.getFileSystem().provider().getScheme().equals("jar");
}
public void addNonClassFiles(Path srcFile) throws IOException {
if (Files.isDirectory(srcFile)) {
addNonClassFiles(srcFile, false);
} else if (Files.exists(srcFile)) {
addNonClassFiles(FileSystems.newFileSystem(srcFile, null).getPath("/"), true);
} else {
throw new FileNotFoundException("file "+srcFile+" doesn't exist");
}
}
public void addNonClassFiles(Path srcDir, boolean closeFs) throws IOException {
try {
Files.walkFileTree(srcDir, new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
if (!file.toString().endsWith(classSuffix)) {
Path dstFile = dstDir.resolve(srcDir.relativize(file).toString());
createParentDirs(dstFile);
Files.copy(file, dstFile, StandardCopyOption.REPLACE_EXISTING);
}
return FileVisitResult.CONTINUE;
}
});
} finally {
if (closeFs) srcDir.getFileSystem().close();
}
}
@Override
public void accept(String clsName, byte[] data) {
try {
Path dstFile = dstDir.resolve(clsName + classSuffix);
if (isJar && Files.exists(dstFile)) {
if (Files.isDirectory(dstFile)) throw new FileAlreadyExistsException("dst file "+dstFile+" is a directory");
Files.delete(dstFile); // workaround for sporadic FileAlreadyExistsException (Files.write should overwrite, jdk bug?)
}
createParentDirs(dstFile);
Files.write(dstFile, data);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
@Override
public void close() throws IOException {
if (closeFs) dstDir.getFileSystem().close();
}
private static boolean isJar(Path path) {
if (Files.exists(path)) {
return !Files.isDirectory(path);
}
String name = path.getFileName().toString();
return name.endsWith(".jar") || name.endsWith(".zip");
}
private static void createParentDirs(Path path) throws IOException {
Path parent = path.getParent();
if (parent != null) Files.createDirectories(parent);
}
private static final String classSuffix = ".class";
private final Path dstDir;
private final boolean closeFs;
private final boolean isJar;
}