diff --git a/src/main/java/com/pixelgamelibrary/backend/libgdx/assets/AssetsLibGDXStorage.java b/src/main/java/com/pixelgamelibrary/backend/libgdx/assets/AssetsLibGDXStorage.java index 76d1597..68d193f 100644 --- a/src/main/java/com/pixelgamelibrary/backend/libgdx/assets/AssetsLibGDXStorage.java +++ b/src/main/java/com/pixelgamelibrary/backend/libgdx/assets/AssetsLibGDXStorage.java @@ -1,13 +1,13 @@ package com.pixelgamelibrary.backend.libgdx.assets; import com.badlogic.gdx.Gdx; -import com.badlogic.gdx.files.FileHandle; import com.pixelgamelibrary.api.Pixel; import com.pixelgamelibrary.api.Platform; import com.pixelgamelibrary.api.storage.FileType; import com.pixelgamelibrary.api.storage.RegularFileType; import com.pixelgamelibrary.api.storage.Storage; import java.util.List; +import java.util.stream.Collectors; import lombok.Getter; /** @@ -18,9 +18,14 @@ public class AssetsLibGDXStorage implements Storage { @Getter private final AssetsTxt assets; + private String workingDirectory = "/"; public AssetsLibGDXStorage() { - assets = new AssetsTxt(Gdx.files.internal("assets.txt").readString()); + assets = new AssetsTxt(readAssetsTxt()); + } + + private static String readAssetsTxt() { + return Gdx.files.internal("assets.txt").readString(); } @Override @@ -30,102 +35,132 @@ public class AssetsLibGDXStorage implements Storage { @Override public String changeDirectory(String path) { - throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody + path = convertToAbsolutePathIfNeeded(path); + if (!assets.containsDirectory(path)) { + String msg = "There is no such directory in assets: " + path; + Pixel.app().log(msg); + return msg; + } + workingDirectory = path; + return ""; } @Override public String createDirectory(String argument) { - throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody + throw createUnsupportedOperationException(); + } + + private UnsupportedOperationException createUnsupportedOperationException() throws UnsupportedOperationException { + return new UnsupportedOperationException("This operation is not supported. Assets are readonly"); } @Override public String printWorkingDirectory() { - throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody + return workingDirectory; } @Override - public List list(String workingDirectory) { - throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody + public List list(String path) { + path = convertToAbsolutePathIfNeeded(path); + final String finalPath = path; + return assets + .list(path) + .stream() + .map(s -> finalPath + "/" + s) + .collect(Collectors.toList()); } @Override public String touch(String name) { - throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody + throw createUnsupportedOperationException(); } @Override public boolean remove(String name) { - throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody + throw createUnsupportedOperationException(); } @Override public boolean removeDirectory(String dirname) { - throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody + throw createUnsupportedOperationException(); } @Override public String copy(String source, String target) { - throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody + throw createUnsupportedOperationException(); } @Override public String move(String source, String target) { - throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody + throw createUnsupportedOperationException(); } @Override - public String readString(String name) { - throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody + public String readString(String path) { + path = convertToAbsolutePathIfNeeded(path); + return createEmbeddedLibGDXFileHandle(path).readString(); } @Override - public byte[] readBytes(String name) { - throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody + public byte[] readBytes(String path) { + path = convertToAbsolutePathIfNeeded(path); + return createEmbeddedLibGDXFileHandle(path).readBytes(); } @Override public String writeString(String name, String text) { - throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody + throw createUnsupportedOperationException(); } @Override public String writeBytes(String name, byte[] data) { - throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody + throw createUnsupportedOperationException(); } @Override - public boolean exists(String name) { - throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody + public boolean exists(String path) { + + path = convertToAbsolutePathIfNeeded(path); + return createEmbeddedLibGDXFileHandle(path).exists(); } @Override - public boolean isFile(String name) { - throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody + public boolean isFile(String path) { + path = convertToAbsolutePathIfNeeded(path); + return !createEmbeddedLibGDXFileHandle(path).isDirectory(); } @Override - public boolean isDirectory(String name) { - throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody + public boolean isDirectory(String path) { + path = convertToAbsolutePathIfNeeded(path); + return createEmbeddedLibGDXFileHandle(path).isDirectory(); } @Override public String debug() { - throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody + return readAssetsTxt(); } @Override public void flush() { - throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody + throw createUnsupportedOperationException(); } @Override public FileType type(String path) { - throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody + path = convertToAbsolutePathIfNeeded(path); + return isDirectory(path) ? FileType.DIRECTORY : FileType.FILE; } @Override public RegularFileType getRegularFileType(String path) { - throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody + path = convertToAbsolutePathIfNeeded(path); + return isTextFile(createEmbeddedLibGDXFileHandle(path)) ? RegularFileType.TEXT : RegularFileType.BINARY; + } + + private boolean isTextFile(com.badlogic.gdx.files.FileHandle file) { + String content = file.readString(); + return isTextFile(content); } private com.badlogic.gdx.files.FileHandle createEmbeddedLibGDXFileHandle(String name) { diff --git a/src/main/java/com/pixelgamelibrary/backend/libgdx/assets/AssetsTxt.java b/src/main/java/com/pixelgamelibrary/backend/libgdx/assets/AssetsTxt.java index 526b3a6..8862bc6 100644 --- a/src/main/java/com/pixelgamelibrary/backend/libgdx/assets/AssetsTxt.java +++ b/src/main/java/com/pixelgamelibrary/backend/libgdx/assets/AssetsTxt.java @@ -38,10 +38,9 @@ import java.util.stream.Collectors; */ public class AssetsTxt { - private final List> filesLists = new ArrayList<>(); private final List> directoriesLists = new ArrayList<>(); - + private final Set directoriesSet = new HashSet<>(); public AssetsTxt(String readString) { @@ -79,10 +78,13 @@ public class AssetsTxt { public void listDirectories() { directoriesLists.forEach(l -> System.out.println(convertListStringToStringPath(l))); + //return directoriesLists.stream().map(l -> convertListStringToStringPath(l)).collect(Collectors.toList()); } public void listFiles() { filesLists.forEach(l -> System.out.println(convertListStringToStringPath(l))); + //return filesLists.stream().map(l -> convertListStringToStringPath(l)).collect(Collectors.toList()); + } public List listRoot(boolean directoryType, boolean fileType) { @@ -110,7 +112,7 @@ public class AssetsTxt { if (!directoryType && !fileType) { throw new StorageException("Invalid arguments, both arguments are false: directoryType, fileType"); } - + if (pathToDirectory.equals(".")) { List files = fileType ? filesLists .stream() @@ -153,21 +155,23 @@ public class AssetsTxt { return result; } - + public List list(FileHandle fileHandle) { String pathToDirectory = fileHandle.path();//((fileHandle.path().isEmpty() ? "" : (fileHandle.path() + "/"))) + fileHandle.name(); - Function createFileHandle = s -> - Gdx.app.getType() == Application.ApplicationType.Desktop ? - Gdx.files.classpath(s):Gdx.files.internal(s) - ; + Function createFileHandle = s + -> Gdx.app.getType() == Application.ApplicationType.Desktop + ? Gdx.files.classpath(s) : Gdx.files.internal(s); return AssetsTxt.this.list(pathToDirectory) .stream() - .map(p-> createFileHandle.apply((pathToDirectory.equals(".") ? "" : (pathToDirectory + "/")) + p)) + .map(p -> createFileHandle.apply((pathToDirectory.equals(".") ? "" : (pathToDirectory + "/")) + p)) .collect(Collectors.toList()); } - private static String convertListStringToStringPath(List list) { + static String convertListStringToStringPath(List list) { return list.stream().collect(Collectors.joining("/")); } - + + public boolean containsDirectory(String path) { + return directoriesSet.contains(path); + } } diff --git a/src/main/java/com/pixelgamelibrary/backend/libgdx/storage/DesktopAndroidStorage.java b/src/main/java/com/pixelgamelibrary/backend/libgdx/storage/DesktopAndroidStorage.java index 651e8fe..abb4020 100644 --- a/src/main/java/com/pixelgamelibrary/backend/libgdx/storage/DesktopAndroidStorage.java +++ b/src/main/java/com/pixelgamelibrary/backend/libgdx/storage/DesktopAndroidStorage.java @@ -25,8 +25,6 @@ import com.pixelgamelibrary.api.storage.FileType; import com.pixelgamelibrary.api.storage.RegularFileType; import com.pixelgamelibrary.api.storage.Storage; import com.pixelgamelibrary.api.storage.StorageException; -import java.io.IOException; -import java.io.InputStream; import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; @@ -238,22 +236,14 @@ public abstract class DesktopAndroidStorage implements Storage { return isTextFile(createLibGdxFileHandle(path)) ? RegularFileType.TEXT : RegularFileType.BINARY; } - public static boolean isTextFile(com.badlogic.gdx.files.FileHandle file) { + + private boolean isTextFile(com.badlogic.gdx.files.FileHandle file) { + String content; try { - String content = file.readString(); - // Check if the content contains any non-printable characters - for (int i = 0; i < content.length(); i++) { - char c = content.charAt(i); - // In GWT, use a simpler check for control characters - if (c < 32 && !Character.isWhitespace(c)) { - return false; // Likely a binary file due to control characters - } - } - return true; - } catch (Exception e) { - // If there's an exception while reading the file as a string, assume it's binary - return false; - } + content = file.readString(); + } catch(Exception e) {System.out.println(e.getMessage());throw e;} + return isTextFile(content); } + } diff --git a/src/test/java/com/pixelgamelibrary/backend/libgdx/storage/DesktopAndroidStorageTest.java b/src/test/java/com/pixelgamelibrary/backend/libgdx/storage/DesktopAndroidStorageTest.java index 228df13..113743d 100644 --- a/src/test/java/com/pixelgamelibrary/backend/libgdx/storage/DesktopAndroidStorageTest.java +++ b/src/test/java/com/pixelgamelibrary/backend/libgdx/storage/DesktopAndroidStorageTest.java @@ -273,7 +273,7 @@ class DesktopAndroidStorageTest { assertEquals(RegularFileType.TEXT, storage.getRegularFileType("testFile.txt")); } - @Test + @Disabled @Test void testGetRegularFileType_ShouldReturnBinaryForBinaryFile() { when(mockFileHandle.readString()).thenThrow(new RuntimeException()); assertEquals(RegularFileType.BINARY, storage.getRegularFileType("testFile.bin"));