|
|
|
@ -19,11 +19,17 @@
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
package com.pixelgamelibrary.backend.libgdx.storage;
|
|
|
|
|
|
|
|
|
|
import com.pixelgamelibrary.api.Platform;
|
|
|
|
|
import com.badlogic.gdx.Gdx;
|
|
|
|
|
import com.pixelgamelibrary.api.Pixel;
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
@ -31,97 +37,179 @@ import java.util.List;
|
|
|
|
|
*/
|
|
|
|
|
public abstract class DesktopAndroidStorage implements Storage {
|
|
|
|
|
|
|
|
|
|
public DesktopAndroidStorage() {
|
|
|
|
|
}
|
|
|
|
|
private String workingDirectory = "/";
|
|
|
|
|
private final String storageName;
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public Platform getPlatform() {
|
|
|
|
|
throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody
|
|
|
|
|
public DesktopAndroidStorage(String storageName) {
|
|
|
|
|
if (storageName == null || storageName.trim().isEmpty()) {
|
|
|
|
|
var msg = "storageName == null || storageName.trim().isEmpty()";
|
|
|
|
|
Pixel.app().error(msg);
|
|
|
|
|
throw new StorageException(msg);
|
|
|
|
|
}
|
|
|
|
|
this.storageName = storageName;
|
|
|
|
|
com.badlogic.gdx.files.FileHandle rootFileHandle = createLibGdxFileHandle("/");
|
|
|
|
|
if (!rootFileHandle.exists()) {
|
|
|
|
|
rootFileHandle.mkdirs();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public String changeDirectory(String path) {
|
|
|
|
|
throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody
|
|
|
|
|
com.badlogic.gdx.files.FileHandle fh = createLibGdxFileHandle(path);
|
|
|
|
|
if (!fh.exists()) {
|
|
|
|
|
var msg = "Directory does not exist: " + path;
|
|
|
|
|
Pixel.app().error(msg);
|
|
|
|
|
return msg;
|
|
|
|
|
}
|
|
|
|
|
workingDirectory = path;
|
|
|
|
|
return "";
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private com.badlogic.gdx.files.FileHandle createLibGdxFileHandle(String path) {
|
|
|
|
|
if (path.equals("/")) {
|
|
|
|
|
return Gdx.files.local(storageName);
|
|
|
|
|
} else {
|
|
|
|
|
return Gdx.files.local(storageName + "/" + path);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public String createDirectory(String name) {
|
|
|
|
|
throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody
|
|
|
|
|
public String createDirectory(String path) {
|
|
|
|
|
var dir = createLibGdxFileHandle(path);
|
|
|
|
|
if (dir.exists()) {
|
|
|
|
|
var msg = "Directory already exists: " + path;
|
|
|
|
|
Pixel.app().warn(msg);
|
|
|
|
|
return msg;
|
|
|
|
|
} else {
|
|
|
|
|
dir.mkdirs();
|
|
|
|
|
return "";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public String printWorkingDirectory() {
|
|
|
|
|
throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody
|
|
|
|
|
return workingDirectory;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public List<String> list(String path) {
|
|
|
|
|
throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody
|
|
|
|
|
path = convertToAbsolutePathIfNeeded(path);
|
|
|
|
|
return Arrays.asList(createLibGdxFileHandle(path).list()).stream().map(e -> e.path()).collect(Collectors.toList());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public int depth(String path) {
|
|
|
|
|
throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody
|
|
|
|
|
public String touch(String path) {
|
|
|
|
|
path = convertToAbsolutePathIfNeeded(path);
|
|
|
|
|
createLibGdxFileHandle(path).writeString("", false);
|
|
|
|
|
return "";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public String touch(String name) {
|
|
|
|
|
throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public boolean remove(String name) {
|
|
|
|
|
throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody
|
|
|
|
|
public boolean remove(String path) {
|
|
|
|
|
path = convertToAbsolutePathIfNeeded(path);
|
|
|
|
|
return createLibGdxFileHandle(path).delete();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public String copy(String source, String target) {
|
|
|
|
|
throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody
|
|
|
|
|
|
|
|
|
|
source = convertToAbsolutePathIfNeeded(source);
|
|
|
|
|
target = convertToAbsolutePathIfNeeded(target);
|
|
|
|
|
createLibGdxFileHandle(source).copyTo(createLibGdxFileHandle(target));
|
|
|
|
|
return "";
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public String move(String source, String target) {
|
|
|
|
|
throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody
|
|
|
|
|
source = convertToAbsolutePathIfNeeded(source);
|
|
|
|
|
target = convertToAbsolutePathIfNeeded(target);
|
|
|
|
|
createLibGdxFileHandle(source).moveTo(createLibGdxFileHandle(target));
|
|
|
|
|
return "";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@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 createLibGdxFileHandle(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) {
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
path = convertToAbsolutePathIfNeeded(path);
|
|
|
|
|
InputStream is = createLibGdxFileHandle(path).read();
|
|
|
|
|
return is.readAllBytes();
|
|
|
|
|
} catch (IOException ex) {
|
|
|
|
|
Pixel.app().error(ex.getMessage());
|
|
|
|
|
throw new StorageException(ex.getMessage());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public String writeString(String name, String text) {
|
|
|
|
|
throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody
|
|
|
|
|
public String writeString(String path, String text) {
|
|
|
|
|
|
|
|
|
|
path = convertToAbsolutePathIfNeeded(path);
|
|
|
|
|
createLibGdxFileHandle(path).writeString(text, false);
|
|
|
|
|
return "";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public String writeBytes(String name, byte[] data) {
|
|
|
|
|
throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody
|
|
|
|
|
public String writeBytes(String path, byte[] data) {
|
|
|
|
|
|
|
|
|
|
path = convertToAbsolutePathIfNeeded(path);
|
|
|
|
|
createLibGdxFileHandle(path).writeBytes(data, false);
|
|
|
|
|
return "";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@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 createLibGdxFileHandle(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 !createLibGdxFileHandle(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 createLibGdxFileHandle(path).isDirectory();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public String debug() {
|
|
|
|
|
throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody
|
|
|
|
|
|
|
|
|
|
return printFileTree(createLibGdxFileHandle(SLASH));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public String printFileTree(com.badlogic.gdx.files.FileHandle dir) {
|
|
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
|
printFileTree(dir, "", sb);
|
|
|
|
|
return sb.toString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void printFileTree(com.badlogic.gdx.files.FileHandle file, String indent, StringBuilder sb) {
|
|
|
|
|
// Add the current file or folder
|
|
|
|
|
sb.append(indent).append(file.name()).append("\n");
|
|
|
|
|
|
|
|
|
|
if (file.isDirectory()) {
|
|
|
|
|
// If it's a directory, get its contents and recursively process them
|
|
|
|
|
com.badlogic.gdx.files.FileHandle[] children = file.list();
|
|
|
|
|
for (com.badlogic.gdx.files.FileHandle child : children) {
|
|
|
|
|
printFileTree(child, indent + " ", sb); // Indentation for subfiles
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@ -130,16 +218,39 @@ public abstract class DesktopAndroidStorage implements Storage {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public boolean removeDirectory(String dirname) {
|
|
|
|
|
throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody
|
|
|
|
|
public boolean removeDirectory(String path) {
|
|
|
|
|
|
|
|
|
|
path = convertToAbsolutePathIfNeeded(path);
|
|
|
|
|
return createLibGdxFileHandle(path).deleteDirectory();
|
|
|
|
|
}
|
|
|
|
|
@Override
|
|
|
|
|
|
|
|
|
|
@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(createLibGdxFileHandle(path)) ? RegularFileType.TEXT : RegularFileType.BINARY;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static boolean isTextFile(com.badlogic.gdx.files.FileHandle file) {
|
|
|
|
|
try {
|
|
|
|
|
String content = file.readString();
|
|
|
|
|
// Check if the content contains non-printable characters (indicating binary)
|
|
|
|
|
for (char c : content.toCharArray()) {
|
|
|
|
|
if (Character.isISOControl(c) && !Character.isWhitespace(c)) {
|
|
|
|
|
return false; // It's likely binary if it contains control characters
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
return false; // Unable to read as text, so it's likely binary
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|