Bug 17: Create the Asset LibGDX implementation of Storage api II

This commit is contained in:
Robert Vokac 2024-09-14 18:01:20 +02:00
parent da0d8f74b2
commit 7df3021ded
No known key found for this signature in database
GPG Key ID: C459E1E4B4A986BB

View File

@ -35,7 +35,7 @@ public class AssetsLibGDXStorage implements Storage {
@Override @Override
public String changeDirectory(String path) { public String changeDirectory(String path) {
path = convertToAbsolutePathIfNeeded(path); path = convertToAbsolutePathIfNeededButWithoutLeadingSlash(path);
if (!assets.containsDirectory(path)) { if (!assets.containsDirectory(path)) {
String msg = "There is no such directory in assets: " + path; String msg = "There is no such directory in assets: " + path;
Pixel.app().log(msg); Pixel.app().log(msg);
@ -61,10 +61,17 @@ public class AssetsLibGDXStorage implements Storage {
@Override @Override
public List<String> list(String path) { public List<String> list(String path) {
path = convertToAbsolutePathIfNeeded(path); if (path.equals("/")) {
return assets
.listRoot()
.stream()
.map(s -> "/" + s)
.collect(Collectors.toList());
}
path = convertToAbsolutePathIfNeededButWithoutLeadingSlash(path);
final String finalPath = path; final String finalPath = path;
return assets return assets
.list(path) .list(finalPath)
.stream() .stream()
.map(s -> finalPath + "/" + s) .map(s -> finalPath + "/" + s)
.collect(Collectors.toList()); .collect(Collectors.toList());
@ -97,13 +104,13 @@ public class AssetsLibGDXStorage implements Storage {
@Override @Override
public String readString(String path) { public String readString(String path) {
path = convertToAbsolutePathIfNeeded(path); path = convertToAbsolutePathIfNeededButWithoutLeadingSlash(path);
return createEmbeddedLibGDXFileHandle(path).readString(); return createEmbeddedLibGDXFileHandle(path).readString();
} }
@Override @Override
public byte[] readBytes(String path) { public byte[] readBytes(String path) {
path = convertToAbsolutePathIfNeeded(path); path = convertToAbsolutePathIfNeededButWithoutLeadingSlash(path);
return createEmbeddedLibGDXFileHandle(path).readBytes(); return createEmbeddedLibGDXFileHandle(path).readBytes();
} }
@ -120,19 +127,23 @@ public class AssetsLibGDXStorage implements Storage {
@Override @Override
public boolean exists(String path) { public boolean exists(String path) {
path = convertToAbsolutePathIfNeeded(path); path = convertToAbsolutePathIfNeededButWithoutLeadingSlash(path);
return createEmbeddedLibGDXFileHandle(path).exists(); return createEmbeddedLibGDXFileHandle(path).exists();
} }
public String convertToAbsolutePathIfNeededButWithoutLeadingSlash(String path) {
path = convertToAbsolutePathIfNeeded(path);
return path.substring(1);
}
@Override @Override
public boolean isFile(String path) { public boolean isFile(String path) {
path = convertToAbsolutePathIfNeeded(path); path = convertToAbsolutePathIfNeededButWithoutLeadingSlash(path);
return !createEmbeddedLibGDXFileHandle(path).isDirectory(); return !createEmbeddedLibGDXFileHandle(path).isDirectory();
} }
@Override @Override
public boolean isDirectory(String path) { public boolean isDirectory(String path) {
path = convertToAbsolutePathIfNeeded(path); path = convertToAbsolutePathIfNeededButWithoutLeadingSlash(path);
return createEmbeddedLibGDXFileHandle(path).isDirectory(); return createEmbeddedLibGDXFileHandle(path).isDirectory();
} }
@ -148,13 +159,13 @@ public class AssetsLibGDXStorage implements Storage {
@Override @Override
public FileType type(String path) { public FileType type(String path) {
path = convertToAbsolutePathIfNeeded(path); path = convertToAbsolutePathIfNeededButWithoutLeadingSlash(path);
return isDirectory(path) ? FileType.DIRECTORY : FileType.FILE; return isDirectory(path) ? FileType.DIRECTORY : FileType.FILE;
} }
@Override @Override
public RegularFileType getRegularFileType(String path) { public RegularFileType getRegularFileType(String path) {
path = convertToAbsolutePathIfNeeded(path); path = convertToAbsolutePathIfNeededButWithoutLeadingSlash(path);
return isTextFile(createEmbeddedLibGDXFileHandle(path)) ? RegularFileType.TEXT : RegularFileType.BINARY; return isTextFile(createEmbeddedLibGDXFileHandle(path)) ? RegularFileType.TEXT : RegularFileType.BINARY;
} }