Bug 14: Refactor, improve and finish the Storage api

This commit is contained in:
Robert Vokac 2024-09-14 08:16:27 +02:00 committed by Robert Vokac
parent 60a94ab1ec
commit b12309056f
Signed by: robertvokac
GPG Key ID: FB9CE8E20AADA55F
5 changed files with 56 additions and 20 deletions

View File

@ -19,7 +19,9 @@
///////////////////////////////////////////////////////////////////////////////////////////////
package com.pixelgamelibrary.api.interfaces;
import com.badlogic.gdx.utils.Base64Coder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
*
@ -38,4 +40,10 @@ public interface UtilsI {
}
String encodeToBase64(byte[] data);
List<String> listSupportedCompressions();
byte[] compress(byte[] data, String compression, Map<String, String> arguments);
default byte[] compress(byte[] data, String compression) {
return compress(data, compression, new HashMap<>());
}
byte[] decompress(byte[] data, String compression);
}

View File

@ -47,6 +47,9 @@ public enum MapFileType {
}
// Retrieve the value associated with the key
String value = map.getString(key);
if(value == null) {
throw new StorageException("Value is null for key: " + key);
}
// Determine the MapFileType based on the value
if (value.startsWith(FILE.name())) {
return FILE;

View File

@ -17,10 +17,8 @@
// <https://www.gnu.org/licenses/> or write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
///////////////////////////////////////////////////////////////////////////////////////////////
package com.pixelgamelibrary.api.storage.map;
import com.badlogic.gdx.Gdx;
import com.pixelgamelibrary.api.Pixel;
import com.pixelgamelibrary.api.Platform;
import com.pixelgamelibrary.api.storage.StorageException;
@ -40,12 +38,13 @@ public class MapStorage implements Storage {
private final MapStorageCompression mapStorageCompression;
/**
* Constructs a MapStorage instance with the specified map and default compression.
* Constructs a MapStorage instance with the specified map and default
* compression.
*
* @param mapIn the map to be used for storage
*/
public MapStorage(SimpleMap mapIn) {
this(mapIn, MapStorageCompression.NONE);
this(mapIn, MapStorageCompression.LZMA);
}
/**
@ -57,6 +56,13 @@ public class MapStorage implements Storage {
public MapStorage(SimpleMap mapIn, MapStorageCompression mapStorageCompressionIn) {
this.map = mapIn;
this.mapStorageCompression = mapStorageCompressionIn;
if (map.contains("system.compression")) {
if (!map.getString("system.compression").equals(this.mapStorageCompression.name())) {
throw new StorageException("Fatal error, compression method passed to the constructor is different, than the compression method in the map (key system.compression).");
}
} else {
map.putString("system.compression", mapStorageCompression.name());
}
mkdir("/"); // Initialize the root directory
}
@ -108,6 +114,11 @@ public class MapStorage implements Storage {
@Override
public String mkdir(String path) {
if (path.equals("system")) {
String msg = "Creating directory system is not allowed";
logError(msg);
return msg;
}
// Create a new directory at the specified path
if (path.isEmpty()) {
String msg = "Missing argument";
@ -219,9 +230,15 @@ public class MapStorage implements Storage {
@Override
public boolean rm(String path) {
// Remove the file or directory at the specified path
String absolutePath = convertToAbsolutePathIfNeeded(path);
if (map.contains(absolutePath) && isdir(path)) {
logError("Removing directories is not yet supported");
return false;
}
// Remove the file or directory at the specified path
if (!map.contains(absolutePath)) {
logError("Cannot remove file, because it does not exist: " + absolutePath);
return false;
@ -321,7 +338,11 @@ public class MapStorage implements Storage {
return null;
}
text = text.substring(BINARYFILE.length());
return Pixel.utils().decodeBase64AsByteArray(text);
byte[] data = Pixel.utils().decodeBase64AsByteArray(text);
if (this.mapStorageCompression != MapStorageCompression.NONE) {
data = Pixel.utils().decompress(data, mapStorageCompression.name());
}
return data;
}
@Override
@ -331,6 +352,9 @@ public class MapStorage implements Storage {
@Override
public String savebin(String name, byte[] data) {
if (this.mapStorageCompression != MapStorageCompression.NONE) {
data = Pixel.utils().compress(data, mapStorageCompression.name());
}
return savetext(name, BINARYFILE + Pixel.utils().encodeToBase64(data));
}

View File

@ -24,5 +24,5 @@ package com.pixelgamelibrary.api.storage.map;
* @author robertvokac
*/
public enum MapStorageCompression {
NONE;
NONE, LZMA;
}

View File

@ -187,6 +187,7 @@ public class MapStorageTest {
@Test
public void testRmFile() {
when(mockMap.contains("/file.txt")).thenReturn(true);
when(mockMap.getString("/file.txt")).thenReturn("FILE::::::::Hello World");
boolean result = mapStorage.rm("/file.txt");