-
-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathRadianceClient.java
More file actions
200 lines (173 loc) · 7.64 KB
/
Copy pathRadianceClient.java
File metadata and controls
200 lines (173 loc) · 7.64 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
193
194
195
196
197
198
199
200
package com.radiance.client;
import com.mojang.logging.LogUtils;
import com.radiance.client.option.Options;
import com.radiance.client.pipeline.Pipeline;
import com.radiance.client.proxy.vulkan.RendererProxy;
import java.io.IOException;
import java.io.InputStream;
import java.net.JarURLConnection;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.AccessDeniedException;
import java.nio.file.FileSystem;
import java.nio.file.FileSystemNotFoundException;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.Collections;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
import net.fabricmc.api.ClientModInitializer;
import net.minecraft.client.MinecraftClient;
import org.slf4j.Logger;
public class RadianceClient implements ClientModInitializer {
public static final Logger LOGGER = LogUtils.getLogger();
public static Path radianceDir;
@Override
public void onInitializeClient() {
MinecraftClient mc = MinecraftClient.getInstance();
Path mcBaseDir = mc.runDirectory.toPath();
radianceDir = mcBaseDir.resolve("radiance");
try {
Files.createDirectories(radianceDir);
} catch (IOException e) {
throw new RuntimeException(e);
}
// core lib
String osName = System.getProperty("os.name");
if (osName.toLowerCase().contains("windows")) {
Path libTargetPath = radianceDir.resolve("core.lib");
Path libResourcePath = Path.of("core.lib");
copyFileFromResource(libTargetPath, libResourcePath);
Path dllTargetPath = radianceDir.resolve("core.dll");
Path dllResourcePath = Path.of("core.dll");
copyFileFromResource(dllTargetPath, dllResourcePath);
Path xessPath = radianceDir.resolve("libxess.dll");
Path xessDx11Path = radianceDir.resolve("libxess_dx11.dll");
Path xessFgPath = radianceDir.resolve("libxess_fg.dll");
copyOptionalFileFromResource(xessPath, Path.of("libxess.dll"));
// currently not used, can be used later for fg
copyOptionalFileFromResource(xessDx11Path, Path.of("libxess_dx11.dll"));
copyOptionalFileFromResource(xessFgPath, Path.of("libxess_fg.dll"));
loadOptionalLibrary(xessPath);
System.load(dllTargetPath.toAbsolutePath().toString());
} else if (osName.toLowerCase().contains("linux")) {
Path soTargetPath = radianceDir.resolve("libcore.so");
Path soResourcePath = Path.of("libcore.so");
copyFileFromResource(soTargetPath, soResourcePath);
System.load(soTargetPath.toAbsolutePath().toString());
} else {
throw new RuntimeException("The OS " + osName + " is not supported");
}
// shaders
Path shaderTargetPath = radianceDir.resolve("shaders");
Path shaderResourcePath = Path.of("shaders");
copyFolderFromResource(shaderTargetPath, shaderResourcePath);
// modules
Path moduleTargetPath = radianceDir.resolve("modules");
Path moduleResourcePath = Path.of("modules");
copyFolderFromResource(moduleTargetPath, moduleResourcePath);
RendererProxy.initFolderPath(radianceDir.toAbsolutePath().toString());
Pipeline.initFolderPath(radianceDir);
Options.readOptions();
Pipeline.reloadAllModuleEntries();
}
public void copyFileFromResource(Path targetPath, Path resourcePath) {
try (InputStream is = getClass().getResourceAsStream(toResourcePath(resourcePath))) {
if (is == null) {
throw new IOException("Cannot find target path: " + resourcePath);
}
Files.createDirectories(targetPath.getParent());
Files.copy(is, targetPath, StandardCopyOption.REPLACE_EXISTING);
} catch (AccessDeniedException e) {
if (Files.exists(targetPath)) {
LOGGER.warn("Using existing locked native/resource file {}", targetPath);
return;
}
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public void copyOptionalFileFromResource(Path targetPath, Path resourcePath) {
try (InputStream is = getClass().getResourceAsStream(toResourcePath(resourcePath))) {
if (is == null) {
return;
}
Files.createDirectories(targetPath.getParent());
Files.copy(is, targetPath, StandardCopyOption.REPLACE_EXISTING);
} catch (AccessDeniedException e) {
if (Files.exists(targetPath)) {
LOGGER.warn("Using existing locked optional file {}", targetPath);
return;
}
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public void loadOptionalLibrary(Path path) {
if (Files.exists(path)) {
System.load(path.toAbsolutePath().toString());
}
}
public String toResourcePath(Path path) {
String joined = StreamSupport.stream(path.spliterator(), false).map(Object::toString)
.collect(Collectors.joining("/"));
return "/" + joined;
}
public void copyFolderFromResource(Path targetPath, Path resourcePath) {
String resourcePathStr = toResourcePath(resourcePath);
URL url = getClass().getResource(resourcePathStr);
if (url == null) {
throw new RuntimeException("Resource folder not found: " + resourcePathStr);
}
try {
URI uri = url.toURI();
if ("jar".equals(uri.getScheme())) {
JarURLConnection conn = (JarURLConnection) url.openConnection();
URI jarFileUri = conn.getJarFileURL().toURI();
URI jarFsUri = URI.create("jar:" + jarFileUri);
FileSystem fs = null;
boolean created = false;
try {
try {
fs = FileSystems.getFileSystem(jarFsUri);
} catch (FileSystemNotFoundException e) {
fs = FileSystems.newFileSystem(jarFsUri, Collections.emptyMap());
created = true;
}
Path root = fs.getPath(resourcePathStr);
walkAndCopy(root, targetPath, resourcePath);
} finally {
if (created) {
try {
fs.close();
} catch (IOException ignored) {
}
}
}
} else {
Path root = Paths.get(uri);
walkAndCopy(root, targetPath, resourcePath);
}
} catch (URISyntaxException | IOException e) {
throw new RuntimeException("Failed to copy resource folder", e);
}
}
private void walkAndCopy(Path walkRoot, Path targetRoot, Path baseResourcePath)
throws IOException {
try (Stream<Path> stream = Files.walk(walkRoot)) {
stream.filter(Files::isRegularFile).forEach(source -> {
String relativePathStr = walkRoot.relativize(source).toString();
Path targetFile = targetRoot.resolve(relativePathStr);
Path childResourcePath = baseResourcePath.resolve(relativePathStr);
copyFileFromResource(targetFile, childResourcePath);
});
}
}
}