[FEATURE] Implement ResourceLayer API

This commit is contained in:
Gravita 2025-07-18 11:46:49 +07:00
parent 9844fc1abd
commit 9d3b3031cc
5 changed files with 61 additions and 3 deletions

View file

@ -68,6 +68,7 @@ public class LauncherBackendImpl implements LauncherBackendAPI, TextureUploadExt
private volatile List<Java> availableJavas;
private volatile CompletableFuture<List<Java>> availableJavasFuture;
private volatile CompletableFuture<Void> processHardwareFuture;
private volatile Path vfsRootPath;
private final Map<UUID, CompletableFuture<ServerPingInfo>> pingFutures = new ConcurrentHashMap<>();
@Override
@ -315,6 +316,14 @@ public boolean isTestMode() {
}
}
@Override
public ResourceLayer makeResourceLayer(List<Path> overlayList) {
if(vfsRootPath == null) {
vfsRootPath = initVfsDirectory();
}
return new ResourceLayerImpl(vfsRootPath, overlayList);
}
@SuppressWarnings("unchecked")
@Override
public <T extends Extension> T getExtension(Class<T> clazz) {

View file

@ -0,0 +1,39 @@
package pro.gravit.launcher.runtime.backend;
import pro.gravit.launcher.base.vfs.Vfs;
import pro.gravit.launcher.base.vfs.VfsDirectory;
import pro.gravit.launcher.base.vfs.directory.OverlayVfsDirectory;
import pro.gravit.launcher.core.backend.LauncherBackendAPI;
import pro.gravit.utils.helper.SecurityHelper;
import java.io.IOException;
import java.net.URL;
import java.nio.file.Path;
import java.util.List;
import java.util.Objects;
import java.util.stream.Stream;
public class ResourceLayerImpl implements LauncherBackendAPI.ResourceLayer {
private final Path vfsPath;
public ResourceLayerImpl(Path basePath, List<Path> overlayList) {
if(overlayList == null || overlayList.isEmpty()) {
vfsPath = basePath;
return;
}
List<VfsDirectory> overlays = Stream.concat(overlayList.stream(), Stream.of(Path.of("")))
.map(basePath::resolve)
.map(x -> (VfsDirectory) Vfs.get().resolve(x))
.filter(Objects::nonNull)
.toList();
OverlayVfsDirectory directory = new OverlayVfsDirectory(overlays);
String randomName = SecurityHelper.randomStringToken();
vfsPath = Path.of(randomName);
Vfs.get().put(vfsPath, directory);
}
@Override
public URL getURL(Path path) throws IOException {
return Vfs.get().getURL(vfsPath.resolve(path));
}
}

View file

@ -33,10 +33,10 @@ public VfsEntry find(String name) {
@Override
public VfsEntry resolve(Path path) {
if(path == null) {
return null;
return this;
}
Path target = path.resolve(path);
Path target = this.path.resolve(path);
if(Files.exists(target)) {
if(Files.isDirectory(target)) {
return new FileVfsDirectory(target);

View file

@ -19,7 +19,7 @@ public VfsEntry find(String name) {
@Override
public VfsEntry resolve(Path path) {
if(path == null) {
return null;
return this;
}
VfsDirectory current = this;
@ -30,6 +30,9 @@ public VfsEntry resolve(Path path) {
if(entity instanceof SimpleVfsDirectory) {
current = newDir;
} else {
if (i+1 >= path.getNameCount()) {
return newDir;
}
Path newPath = path.subpath(i+1, path.getNameCount());
return newDir.resolve(newPath);
}

View file

@ -9,6 +9,8 @@
import pro.gravit.launcher.core.api.model.UserPermissions;
import pro.gravit.launcher.core.backend.extensions.Extension;
import java.io.IOException;
import java.net.URL;
import java.nio.file.Path;
import java.util.List;
import java.util.Set;
@ -38,6 +40,7 @@ public interface LauncherBackendAPI {
String getUsername();
SelfUser getSelfUser();
boolean isTestMode();
ResourceLayer makeResourceLayer(List<Path> overlayList);
// Extensions
<T extends Extension> T getExtension(Class<T> clazz);
void shutdown();
@ -173,4 +176,8 @@ interface ServerPingInfo {
int getOnline();
List<String> getPlayerNames();
}
interface ResourceLayer {
URL getURL(Path path) throws IOException;
}
}