Storage was replaced by FileSystem
This commit is contained in:
parent
b07fcd2a47
commit
e49692818e
@ -22,7 +22,7 @@ package com.pixelgamelibrary.backend.libgdx;
|
||||
import com.pixelgamelibrary.api.audio.Music;
|
||||
import com.pixelgamelibrary.api.audio.Sound;
|
||||
import com.pixelgamelibrary.api.interfaces.Audio;
|
||||
import com.pixelgamelibrary.api.files.FileHandle;
|
||||
import com.pixelgamelibrary.api.files.File;
|
||||
|
||||
/**
|
||||
*
|
||||
@ -31,12 +31,12 @@ import com.pixelgamelibrary.api.files.FileHandle;
|
||||
public class AudioLibGDXImpl implements Audio {
|
||||
|
||||
@Override
|
||||
public Sound newSound(FileHandle fileHandle) {
|
||||
public Sound newSound(File fileHandle) {
|
||||
throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody
|
||||
}
|
||||
|
||||
@Override
|
||||
public Music newMusic(FileHandle fileHandle) {
|
||||
public Music newMusic(File fileHandle) {
|
||||
throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody
|
||||
}
|
||||
|
||||
|
@ -19,47 +19,47 @@
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
package com.pixelgamelibrary.backend.libgdx;
|
||||
|
||||
import com.pixelgamelibrary.backend.libgdx.files.StorageFactory;
|
||||
import com.pixelgamelibrary.api.files.Storage;
|
||||
import com.pixelgamelibrary.backend.libgdx.files.FileSystemFactory;
|
||||
import com.pixelgamelibrary.api.interfaces.Files;
|
||||
import com.pixelgamelibrary.backend.libgdx.files.AssetsLibGDXStorage;
|
||||
import com.pixelgamelibrary.backend.libgdx.files.AssetsLibGDXFileSystem;
|
||||
import com.pixelgamelibrary.api.files.FileSystem;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author robertvokac
|
||||
*/
|
||||
public class FilesLibGDXImpl implements Files {
|
||||
Storage assetsStorage = null;
|
||||
FileSystem assetsFileSystem = null;
|
||||
|
||||
@Override
|
||||
public Storage localStorage() {
|
||||
return StorageFactory.getStorage();
|
||||
public FileSystem localFileSystem() {
|
||||
return FileSystemFactory.getFileSystem();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Storage assetsStorage() {
|
||||
if(assetsStorage == null) {
|
||||
assetsStorage = new AssetsLibGDXStorage();
|
||||
public FileSystem assetsFileSystem() {
|
||||
if(assetsFileSystem == null) {
|
||||
assetsFileSystem = new AssetsLibGDXFileSystem();
|
||||
}
|
||||
return assetsStorage; }
|
||||
return assetsFileSystem; }
|
||||
|
||||
@Override
|
||||
public Storage externalStorage() {
|
||||
public FileSystem externalFileSystem() {
|
||||
throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody
|
||||
}
|
||||
|
||||
@Override
|
||||
public Storage relativeStorage() {
|
||||
public FileSystem relativeFileSystem() {
|
||||
throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody
|
||||
}
|
||||
|
||||
@Override
|
||||
public Storage absoluteStorage() {
|
||||
public FileSystem absoluteFileSystem() {
|
||||
throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody
|
||||
}
|
||||
|
||||
@Override
|
||||
public Storage tmpStorage() {
|
||||
public FileSystem tmpFileSystem() {
|
||||
throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody
|
||||
}
|
||||
|
||||
|
@ -25,10 +25,10 @@ import com.pixelgamelibrary.api.Platform;
|
||||
*
|
||||
* @author robertvokac
|
||||
*/
|
||||
public class AndroidStorage extends DesktopAndroidStorage {
|
||||
public class AndroidFileSystem extends DesktopAndroidFileSystem {
|
||||
|
||||
public AndroidStorage(String storageName) {
|
||||
super(storageName);
|
||||
public AndroidFileSystem(String FileSystemName) {
|
||||
super(FileSystemName);
|
||||
}
|
||||
|
||||
@Override
|
@ -7,24 +7,24 @@ import com.pixelgamelibrary.api.Pixel;
|
||||
import com.pixelgamelibrary.api.Platform;
|
||||
import com.pixelgamelibrary.api.files.FileType;
|
||||
import com.pixelgamelibrary.api.files.RegularFileType;
|
||||
import com.pixelgamelibrary.api.files.Storage;
|
||||
import com.pixelgamelibrary.api.files.StorageType;
|
||||
import com.pixelgamelibrary.api.files.FileSystemType;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
import lombok.Getter;
|
||||
import com.pixelgamelibrary.api.files.FileSystem;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author robertvokac
|
||||
*/
|
||||
public class AssetsLibGDXStorage implements Storage {
|
||||
public class AssetsLibGDXFileSystem implements FileSystem {
|
||||
|
||||
private boolean isProbablyTeaVM = false;
|
||||
@Getter
|
||||
private final AssetsTxt assets;
|
||||
private String workingDirectory = "/";
|
||||
|
||||
public AssetsLibGDXStorage() {
|
||||
public AssetsLibGDXFileSystem() {
|
||||
assets = new AssetsTxt(readAssetsTxt());
|
||||
}
|
||||
|
||||
@ -231,8 +231,8 @@ public class AssetsLibGDXStorage implements Storage {
|
||||
}
|
||||
|
||||
@Override
|
||||
public StorageType getStorageType() {
|
||||
return StorageType.ASSETS;
|
||||
public FileSystemType getFileSystemType() {
|
||||
return FileSystemType.ASSETS;
|
||||
}
|
||||
|
||||
}
|
@ -23,29 +23,29 @@ import com.badlogic.gdx.Gdx;
|
||||
import com.pixelgamelibrary.api.Pixel;
|
||||
import com.pixelgamelibrary.api.files.FileType;
|
||||
import com.pixelgamelibrary.api.files.RegularFileType;
|
||||
import com.pixelgamelibrary.api.files.Storage;
|
||||
import com.pixelgamelibrary.api.files.StorageException;
|
||||
import com.pixelgamelibrary.api.files.StorageType;
|
||||
import com.pixelgamelibrary.api.files.FileException;
|
||||
import com.pixelgamelibrary.api.files.FileSystemType;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
import com.pixelgamelibrary.api.files.FileSystem;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author robertvokac
|
||||
*/
|
||||
public abstract class DesktopAndroidStorage implements Storage {
|
||||
public abstract class DesktopAndroidFileSystem implements FileSystem {
|
||||
|
||||
private String workingDirectory = "/";
|
||||
private final String storageName;
|
||||
private final String fileSystemName;
|
||||
|
||||
public DesktopAndroidStorage(String storageName) {
|
||||
if (storageName == null || storageName.trim().isEmpty()) {
|
||||
var msg = "storageName == null || storageName.trim().isEmpty()";
|
||||
public DesktopAndroidFileSystem(String FileSystemName) {
|
||||
if (FileSystemName == null || FileSystemName.trim().isEmpty()) {
|
||||
var msg = "fileSystemName == null || fileSystemName.trim().isEmpty()";
|
||||
Pixel.app().error(msg);
|
||||
throw new StorageException(msg);
|
||||
throw new FileException(msg);
|
||||
}
|
||||
this.storageName = storageName;
|
||||
this.fileSystemName = FileSystemName;
|
||||
com.badlogic.gdx.files.FileHandle rootFileHandle = createLibGdxFileHandle("/");
|
||||
if (!rootFileHandle.exists()) {
|
||||
rootFileHandle.mkdirs();
|
||||
@ -67,9 +67,9 @@ public abstract class DesktopAndroidStorage implements Storage {
|
||||
|
||||
com.badlogic.gdx.files.FileHandle createLibGdxFileHandle(String path) {
|
||||
if (path.equals("/")) {
|
||||
return Gdx.files.local(storageName);
|
||||
return Gdx.files.local(fileSystemName);
|
||||
} else {
|
||||
return Gdx.files.local(storageName + "/" + path);
|
||||
return Gdx.files.local(fileSystemName + "/" + path);
|
||||
}
|
||||
|
||||
}
|
||||
@ -145,7 +145,7 @@ public abstract class DesktopAndroidStorage implements Storage {
|
||||
return fileHandle.readBytes();
|
||||
} catch (Exception ex) {
|
||||
Pixel.app().error(ex.getMessage());
|
||||
throw new StorageException(ex.getMessage());
|
||||
throw new FileException(ex.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@ -277,7 +277,7 @@ public abstract class DesktopAndroidStorage implements Storage {
|
||||
}
|
||||
|
||||
@Override
|
||||
public StorageType getStorageType() {
|
||||
public FileSystemType getFileSystemType() {
|
||||
throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody
|
||||
}
|
||||
|
@ -25,10 +25,10 @@ import com.pixelgamelibrary.api.Platform;
|
||||
*
|
||||
* @author robertvokac
|
||||
*/
|
||||
public class DesktopStorage extends DesktopAndroidStorage {
|
||||
public class DesktopFileSystem extends DesktopAndroidFileSystem {
|
||||
|
||||
public DesktopStorage(String storageName) {
|
||||
super(storageName);
|
||||
public DesktopFileSystem(String fileSystemName) {
|
||||
super(fileSystemName);
|
||||
}
|
||||
|
||||
@Override
|
@ -21,42 +21,42 @@ package com.pixelgamelibrary.backend.libgdx.files;
|
||||
|
||||
import com.pixelgamelibrary.api.Pixel;
|
||||
import com.pixelgamelibrary.api.Platform;
|
||||
import com.pixelgamelibrary.api.files.Storage;
|
||||
import com.pixelgamelibrary.api.files.StorageException;
|
||||
import com.pixelgamelibrary.api.files.FileException;
|
||||
import com.pixelgamelibrary.api.files.FileSystem;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author robertvokac
|
||||
*/
|
||||
public class StorageFactory {
|
||||
public class FileSystemFactory {
|
||||
|
||||
private StorageFactory() {
|
||||
private FileSystemFactory() {
|
||||
//Not meant to be instantiated.
|
||||
}
|
||||
private static Storage storage = null;
|
||||
private static FileSystem fs = null;
|
||||
|
||||
public static Storage getStorage() {
|
||||
public static FileSystem getFileSystem() {
|
||||
final Platform platform = Pixel.app().getPlatform();
|
||||
// if (storage == null) {
|
||||
// storage = new PreferencesStorage();
|
||||
// if (fs == null) {
|
||||
// fs = new PreferencesFileSystem();
|
||||
// }//todo fixme
|
||||
if (storage == null) {
|
||||
if (fs == null) {
|
||||
final String appName = Pixel.app().getAppName();
|
||||
|
||||
if (platform.isDesktop()) {
|
||||
storage = new DesktopStorage(appName);
|
||||
fs = new DesktopFileSystem(appName);
|
||||
}
|
||||
if (platform.isAndroid()) {
|
||||
storage = new AndroidStorage(appName);
|
||||
fs = new AndroidFileSystem(appName);
|
||||
}
|
||||
if (platform.isWeb()) {
|
||||
storage = new PreferencesStorage(appName);
|
||||
fs = new PreferencesFileSystem(appName);
|
||||
}
|
||||
}
|
||||
if (storage == null) {
|
||||
throw new StorageException("Platform is not supported: " + platform);
|
||||
if (fs == null) {
|
||||
throw new FileException("Platform is not supported: " + platform);
|
||||
}
|
||||
return storage;
|
||||
return fs;
|
||||
}
|
||||
|
||||
}
|
@ -22,28 +22,28 @@ package com.pixelgamelibrary.backend.libgdx.files;
|
||||
import com.badlogic.gdx.Gdx;
|
||||
import com.badlogic.gdx.Preferences;
|
||||
import com.pixelgamelibrary.api.Platform;
|
||||
import com.pixelgamelibrary.api.files.map.MapStorage;
|
||||
import com.pixelgamelibrary.api.files.map.MapFileSystem;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author robertvokac
|
||||
*/
|
||||
public class PreferencesStorage extends MapStorage {
|
||||
public class PreferencesFileSystem extends MapFileSystem {
|
||||
|
||||
|
||||
public Platform getPlatform() {
|
||||
return Platform.WEB;
|
||||
}
|
||||
|
||||
public PreferencesStorage() {
|
||||
this("com.pixelgamelibrary.backend.libgdx.storage.PreferencesStorage");
|
||||
public PreferencesFileSystem() {
|
||||
this("com.pixelgamelibrary.backend.libgdx.files.PreferencesFileSystem");
|
||||
}
|
||||
|
||||
public PreferencesStorage(String preferencesName) {
|
||||
public PreferencesFileSystem(String preferencesName) {
|
||||
this(Gdx.app.getPreferences(preferencesName));
|
||||
}
|
||||
|
||||
public PreferencesStorage(Preferences preferences) {
|
||||
public PreferencesFileSystem(Preferences preferences) {
|
||||
super(new SimpleLocalStorageMap(preferences));
|
||||
}
|
||||
|
@ -1,13 +1,12 @@
|
||||
package com.pixelgamelibrary.backend.libgdx.files;
|
||||
|
||||
import com.pixelgamelibrary.backend.libgdx.files.DesktopAndroidStorage;
|
||||
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.interfaces.PixelBackend;
|
||||
import com.pixelgamelibrary.api.files.RegularFileType;
|
||||
import com.pixelgamelibrary.api.files.StorageException;
|
||||
import com.pixelgamelibrary.api.files.FileException;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.Mockito;
|
||||
@ -33,9 +32,9 @@ import com.pixelgamelibrary.api.app.ClipBoard;
|
||||
import com.pixelgamelibrary.api.app.LogLevel;
|
||||
import com.pixelgamelibrary.api.app.Preferences;
|
||||
|
||||
class DesktopAndroidStorageTest {
|
||||
class DesktopAndroidFileSystemTest {
|
||||
|
||||
private DesktopAndroidStorage storage;
|
||||
private DesktopAndroidFileSystem fs;
|
||||
private FileHandle mockFileHandle;
|
||||
|
||||
//
|
||||
@ -217,7 +216,7 @@ class DesktopAndroidStorageTest {
|
||||
|
||||
//
|
||||
|
||||
storage = new DesktopAndroidStorage("testStorage") {
|
||||
fs = new DesktopAndroidFileSystem("testFileSystem") {
|
||||
protected FileHandle createLibGdxFileHandle(String path) {
|
||||
return mockFileHandle;
|
||||
}
|
||||
@ -230,8 +229,8 @@ class DesktopAndroidStorageTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
void testConstructor_NullStorageName_ShouldThrowException() {
|
||||
Exception exception = assertThrows(StorageException.class, () -> new DesktopAndroidStorage(null) {
|
||||
void testConstructor_NullFileSystemName_ShouldThrowException() {
|
||||
Exception exception = assertThrows(FileException.class, () -> new DesktopAndroidFileSystem(null) {
|
||||
|
||||
protected FileHandle createLibGdxFileHandle(String path) {
|
||||
return mock(FileHandle.class);
|
||||
@ -242,37 +241,37 @@ class DesktopAndroidStorageTest {
|
||||
throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody
|
||||
}
|
||||
});
|
||||
assertEquals("storageName == null || storageName.trim().isEmpty()", exception.getMessage());
|
||||
assertEquals("fileSystemName == null || fileSystemName.trim().isEmpty()", exception.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testChangeDirectory_NonExistingDirectory_ShouldReturnErrorMessage() {
|
||||
when(mockFileHandle.exists()).thenReturn(false);
|
||||
String result = storage.changeDirectory("nonExistingDir");
|
||||
String result = fs.changeDirectory("nonExistingDir");
|
||||
assertEquals("Directory does not exist: nonExistingDir", result);
|
||||
}
|
||||
|
||||
@Disabled @Test
|
||||
void testCreateDirectory_ExistingDirectory_ShouldReturnWarning() {
|
||||
when(storage.createLibGdxFileHandle(anyString())).thenReturn(mockFileHandle);
|
||||
when(fs.createLibGdxFileHandle(anyString())).thenReturn(mockFileHandle);
|
||||
when(mockFileHandle.exists()).thenReturn(true);
|
||||
|
||||
|
||||
String result = storage.createDirectory("existingDir");
|
||||
String result = fs.createDirectory("existingDir");
|
||||
assertEquals("Directory already exists: existingDir", result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testCreateDirectory_NewDirectory_ShouldCreateDirectory() {
|
||||
when(mockFileHandle.exists()).thenReturn(false);
|
||||
String result = storage.createDirectory("newDir");
|
||||
String result = fs.createDirectory("newDir");
|
||||
assertEquals("", result);
|
||||
verify(mockFileHandle, times(2)).mkdirs();
|
||||
}
|
||||
|
||||
@Test
|
||||
void testTouch_ShouldCreateFile() {
|
||||
String result = storage.touch("testFile.txt");
|
||||
String result = fs.touch("testFile.txt");
|
||||
assertEquals("", result);
|
||||
verify(mockFileHandle).writeString("", false);
|
||||
}
|
||||
@ -280,21 +279,21 @@ class DesktopAndroidStorageTest {
|
||||
@Test
|
||||
void testRemove_ShouldDeleteFile() {
|
||||
when(mockFileHandle.delete()).thenReturn(true);
|
||||
boolean result = storage.remove("testFile.txt");
|
||||
boolean result = fs.remove("testFile.txt");
|
||||
assertTrue(result);
|
||||
verify(mockFileHandle).delete();
|
||||
}
|
||||
|
||||
@Test
|
||||
void testMove_ShouldMoveFile() {
|
||||
storage.move("sourceFile.txt", "targetFile.txt");
|
||||
fs.move("sourceFile.txt", "targetFile.txt");
|
||||
verify(mockFileHandle).moveTo(mockFileHandle);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testReadString_ShouldReturnFileContent() {
|
||||
when(mockFileHandle.readString()).thenReturn("File content");
|
||||
String result = storage.readString("testFile.txt");
|
||||
String result = fs.readString("testFile.txt");
|
||||
assertEquals("File content", result);
|
||||
}
|
||||
|
||||
@ -304,13 +303,13 @@ class DesktopAndroidStorageTest {
|
||||
when(mockFileHandle.read()).thenReturn(mockInputStream);
|
||||
when(mockInputStream.readAllBytes()).thenReturn(new byte[]{1, 2, 3});
|
||||
|
||||
byte[] result = storage.readBytes("testFile.bin");
|
||||
byte[] result = fs.readBytes("testFile.bin");
|
||||
assertArrayEquals(new byte[]{1, 2, 3}, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testWriteString_ShouldWriteToFile() {
|
||||
String result = storage.writeString("testFile.txt", "Hello World");
|
||||
String result = fs.writeString("testFile.txt", "Hello World");
|
||||
assertEquals("", result);
|
||||
verify(mockFileHandle).writeString("Hello World", false);
|
||||
}
|
||||
@ -318,19 +317,19 @@ class DesktopAndroidStorageTest {
|
||||
@Test
|
||||
void testExists_ShouldReturnTrueIfFileExists() {
|
||||
when(mockFileHandle.exists()).thenReturn(true);
|
||||
assertTrue(storage.exists("testFile.txt"));
|
||||
assertTrue(fs.exists("testFile.txt"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testIsDirectory_ShouldReturnTrueForDirectory() {
|
||||
when(mockFileHandle.isDirectory()).thenReturn(true);
|
||||
assertTrue(storage.isDirectory("testDir"));
|
||||
assertTrue(fs.isDirectory("testDir"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testIsFile_ShouldReturnTrueForFile() {
|
||||
when(mockFileHandle.isDirectory()).thenReturn(false);
|
||||
assertTrue(storage.isFile("testFile.txt"));
|
||||
assertTrue(fs.isFile("testFile.txt"));
|
||||
}
|
||||
|
||||
@Disabled @Test
|
||||
@ -338,25 +337,25 @@ class DesktopAndroidStorageTest {
|
||||
when(mockFileHandle.isDirectory()).thenReturn(true);
|
||||
when(mockFileHandle.name()).thenReturn("root");
|
||||
|
||||
String result = storage.debug();
|
||||
String result = fs.debug();
|
||||
assertEquals("root\n", result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testRemoveDirectory_ShouldReturnTrueIfDirectoryDeleted() {
|
||||
when(mockFileHandle.deleteDirectory()).thenReturn(true);
|
||||
assertTrue(storage.removeDirectory("testDir"));
|
||||
assertTrue(fs.removeDirectory("testDir"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetRegularFileType_ShouldReturnTextForTextFile() {
|
||||
when(mockFileHandle.readString()).thenReturn("This is text");
|
||||
assertEquals(RegularFileType.TEXT, storage.getRegularFileType("testFile.txt"));
|
||||
assertEquals(RegularFileType.TEXT, fs.getRegularFileType("testFile.txt"));
|
||||
}
|
||||
|
||||
@Disabled @Test
|
||||
void testGetRegularFileType_ShouldReturnBinaryForBinaryFile() {
|
||||
when(mockFileHandle.readString()).thenThrow(new RuntimeException());
|
||||
assertEquals(RegularFileType.BINARY, storage.getRegularFileType("testFile.bin"));
|
||||
assertEquals(RegularFileType.BINARY, fs.getRegularFileType("testFile.bin"));
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user