Bug 17: Create the Asset LibGDX implementation of Storage api

This commit is contained in:
Robert Vokac 2024-09-14 17:28:50 +02:00
parent 7626a1d72c
commit a25a6ae63a
No known key found for this signature in database
GPG Key ID: C459E1E4B4A986BB
7 changed files with 54 additions and 276 deletions

View File

@ -21,6 +21,7 @@ package com.openeggbert.core.configuration;
import com.openeggbert.core.main.OpenEggbertException;
import com.openeggbert.core.utils.OpenEggbertUtils;
import com.pixelgamelibrary.api.Pixel;
import java.util.HashMap;
import java.util.Map;
import lombok.ToString;
@ -35,8 +36,7 @@ public class ConfigDef {
private Map<String,String> map = new HashMap<>();
public ConfigDef(String textContentofConfigDefFile) {
OpenEggbertUtils
.lines(textContentofConfigDefFile)
Pixel.utils().splitStringToLinesAsStream(textContentofConfigDefFile)
.filter(l -> !l.trim().isEmpty())
.filter(l -> !l.trim().startsWith(HASH_CHARACTER))
.filter(l -> l.contains(EQUALS_CHARACTER))

View File

@ -19,7 +19,6 @@
///////////////////////////////////////////////////////////////////////////////////////////////
package com.openeggbert.core.main;
import com.openeggbert.core.utils.AssetsTxt;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.graphics.Camera;
@ -35,11 +34,9 @@ import com.openeggbert.core.mod.Mod;
import com.openeggbert.core.mod.ModIdentification;
import com.openeggbert.core.screen.GameSpaceListScreen;
import com.openeggbert.core.screen.InitScreen;
import com.pixelgamelibrary.api.storage.Storage;
import com.openeggbert.core.configuration.OpenEggbertDisplayMode;
import com.pixelgamelibrary.api.Game;
import com.openeggbert.core.utils.OpenEggbertUtils;
import com.pixelgamelibrary.api.Pixel;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
@ -65,7 +62,6 @@ public class OpenEggbertGame extends Game {
private int widthInPixels = 640;
private Camera camera;
private Viewport viewport;
private AssetsTxt assets;
private ConfigDef configDef;
private OpenEggbertDisplayMode openEggbertDisplayMode = OpenEggbertDisplayMode.WINDOW;
@ -91,7 +87,6 @@ public class OpenEggbertGame extends Game {
//.setToOrtho(false,640,480);
assets = new AssetsTxt(Gdx.files.internal("assets.txt").readString());
System.out.println("Searching mods");
for(FileHandle f:Gdx.files.internal(".").list()) {
@ -100,31 +95,31 @@ public class OpenEggbertGame extends Game {
FileHandle embeddedModsDirectory = Gdx.files.internal("embedded_mods");
System.out.println("embeddedModsDirectory.exists=" + embeddedModsDirectory.exists());
System.out.println("embeddedModsDirectory.list().length=" + embeddedModsDirectory.list().length);
for (FileHandle embeddedModGroup : assets.list(embeddedModsDirectory)) {
if(embeddedModGroup.name().equals("README.md"))continue;
System.out.println("Found group " + embeddedModGroup.name());
for (FileHandle embeddedMod : assets.list(embeddedModGroup)) {
System.out.println("Found mod " + embeddedMod.name());
FileHandle modXml = null;
for(FileHandle file: assets.list(embeddedMod)) {
if(file.name().equals("mod.xml")) {
modXml = file;
}
}
if (modXml == null) {
continue;
}
System.out.println("Found mod: " + embeddedMod.name());
Mod mod = new Mod(modXml.readString());
embeddedMods.add(mod);
System.out.println("embeddedMods.size(): " + embeddedMods.size());
// for (int i = 0; i < 42; i++) embeddedMods.add(mod);//for testing purposes
}
}
// for (FileHandle embeddedModGroup : assets.list(embeddedModsDirectory)) {
// if(embeddedModGroup.name().equals("README.md"))continue;
// System.out.println("Found group " + embeddedModGroup.name());
// for (FileHandle embeddedMod : assets.list(embeddedModGroup)) {
// System.out.println("Found mod " + embeddedMod.name());
//
// FileHandle modXml = null;
// for(FileHandle file: assets.list(embeddedMod)) {
// if(file.name().equals("mod.xml")) {
// modXml = file;
// }
// }
//
// if (modXml == null) {
// continue;
// }
// System.out.println("Found mod: " + embeddedMod.name());
//
// Mod mod = new Mod(modXml.readString());
// embeddedMods.add(mod);
// System.out.println("embeddedMods.size(): " + embeddedMods.size());
//// for (int i = 0; i < 42; i++) embeddedMods.add(mod);//for testing purposes
// }
//
// }
////
batch = new SpriteBatch();
//batch.setProjectionMatrix(viewport.getCamera().combined);

View File

@ -27,10 +27,7 @@ import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.utils.ScreenUtils;
import com.openeggbert.core.configuration.ConfigDef;
import com.openeggbert.core.main.OpenEggbertGame;
import com.openeggbert.core.utils.EmbeddedFileHandleFactory;
import com.openeggbert.core.configuration.OpenEggbertDisplayMode;
import com.pixelgamelibrary.api.Pixel;
import java.util.Arrays;
import java.util.Optional;
/**
@ -51,14 +48,14 @@ public class InitScreen extends AbstractOpenEggbertScreen {
FileHandle configDefFileHandle = null;
String[] array = new String[]{"config.def", "Config.def", "CONFIG.DEF"};
if (game.getGameSpace().isEmbeddedAssets()) {
for (String a : array) {
configDefFileHandle = EmbeddedFileHandleFactory.create(game.getGameSpace().getDataDirectory() + "/" + a);
if (configDefFileHandle.exists()) {
break;
} else {
continue;
}
}
// for (String a : array) {
// configDefFileHandle = EmbeddedFileHandleFactory.create(game.getGameSpace().getDataDirectory() + "/" + a);
// if (configDefFileHandle.exists()) {
// break;
// } else {
// continue;
// }
// }
} else {
for (String a : array) {
configDefFileHandle = Gdx.files.absolute(game.getGameSpace().getDataDirectory() + "/" + a);

View File

@ -1,172 +0,0 @@
///////////////////////////////////////////////////////////////////////////////////////////////
// Open Eggbert: Free recreation of the computer game Speedy Eggbert.
// Copyright (C) 2024 the original author or authors.
//
// This program is free software: you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation, either version 3
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see
// <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.openeggbert.core.utils;
import com.badlogic.gdx.Application;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.files.FileHandle;
import com.openeggbert.core.main.OpenEggbertException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;
/**
*
* @author robertvokac
*/
public class AssetsTxt {
private final List<List<String>> filesLists = new ArrayList<>();
private final List<List<String>> directoriesLists = new ArrayList<>();
private final Set<String> directoriesSet = new HashSet<>();
public AssetsTxt(String readString) {
OpenEggbertUtils.lines(readString).forEach(line -> {
var lineArray = Arrays.asList(line.split("/"));
filesLists.add(lineArray);
if (lineArray.size() > 1) {
String fileName = lineArray.get(lineArray.size() - 1);
String directory = line.substring(0, line.length() - 1 - fileName.length());
if (!directoriesSet.contains(directory)) {
directoriesSet.add(directory);
directoriesLists.add(Arrays.asList(directory.split("/")));
}
}
});
//directories: without files, with only directories
Set<String> subDirectoriesTmpSet = new HashSet<>();
for (String dir : directoriesSet) {
List<String> list = Arrays.asList(dir.split("/"));
int depth = list.size();
while (depth > 1) {
depth = depth - 1;
String aSubdirectory = list.stream().limit(depth).collect(Collectors.joining("/"));
if (!directoriesSet.contains(aSubdirectory)) {
subDirectoriesTmpSet.add(aSubdirectory);
directoriesLists.add(Arrays.asList(aSubdirectory.split("/")));
}
}
}
directoriesSet.addAll(subDirectoriesTmpSet);
}
public void listDirectories() {
directoriesLists.forEach(l -> System.out.println(convertListStringToStringPath(l)));
}
public void listFiles() {
filesLists.forEach(l -> System.out.println(convertListStringToStringPath(l)));
}
public List<String> listRoot(boolean directoryType, boolean fileType) {
return AssetsTxt.this.list(".", directoryType, fileType);
}
public List<String> listRoot() {
return listRoot(true, true);
}
public List<String> list(String pathToDirectory) {
return AssetsTxt.this.list(pathToDirectory, true, true);
}
public List<String> listDirectories(String pathToDirectory) {
return AssetsTxt.this.list(pathToDirectory, true, false);
}
public List<String> listFiles(String pathToDirectory) {
return AssetsTxt.this.list(pathToDirectory, false, true);
}
public List<String> list(String pathToDirectory, boolean directoryType, boolean fileType) {
// System.out.println("Calling: AssetsTxt.list( " + pathToDirectory + " ...)");
if (!directoryType && !fileType) {
throw new OpenEggbertException("Invalid arguments, both arguments are false: directoryType, fileType");
}
if (pathToDirectory.equals(".")) {
List<String> files = fileType ? filesLists
.stream()
.filter(l -> l.size() == 1)
.map(l -> l.get(0))
.collect(Collectors.toList()) : new ArrayList<>();
List<String> directories = directoryType ? directoriesLists
.stream()
.filter(l -> l.size() == 1)
.map(l -> l.get(0))
.collect(Collectors.toList()) : new ArrayList<>();
List<String> result = new ArrayList<>();
result.addAll(files);
result.addAll(directories);
return result;
}
if (!directoriesSet.contains(pathToDirectory)) {
throw new OpenEggbertException("There is no such directory in assets: " + pathToDirectory);
}
var directoryArray = pathToDirectory.split("/");
int depth = directoryArray.length;
List<String> files = fileType ? filesLists
.stream()
.filter(l -> l.size() == depth + 1)
.filter(l -> convertListStringToStringPath(l).startsWith(pathToDirectory))
.map(l -> l.get(depth))
.collect(Collectors.toList()) : new ArrayList<>();
List<String> directories = directoryType ? directoriesLists
.stream()
.filter(l -> l.size() == depth + 1)
.filter(l -> convertListStringToStringPath(l).startsWith(pathToDirectory))
.map(l -> l.get(depth))
.distinct()
.collect(Collectors.toList()) : new ArrayList<>();
List<String> result = new ArrayList<>();
result.addAll(files);
result.addAll(directories);
return result;
}
public List<FileHandle> list(FileHandle fileHandle) {
String pathToDirectory = fileHandle.path();//((fileHandle.path().isEmpty() ? "" : (fileHandle.path() + "/"))) + fileHandle.name();
Function<String, FileHandle> 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))
.collect(Collectors.toList());
}
private static String convertListStringToStringPath(List<String> list) {
return list.stream().collect(Collectors.joining("/"));
}
}

View File

@ -1,47 +0,0 @@
///////////////////////////////////////////////////////////////////////////////////////////////
// Open Eggbert: Free recreation of the computer game Speedy Eggbert.
// Copyright (C) 2024 the original author or authors.
//
// This program is free software: you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation, either version 3
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see
// <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.openeggbert.core.utils;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.files.FileHandle;
import com.pixelgamelibrary.api.Pixel;
import com.pixelgamelibrary.api.Platform;
/**
*
* @author robertvokac
*/
public class EmbeddedFileHandleFactory {
private EmbeddedFileHandleFactory() {
//Not meant to be instantiated.
}
public static FileHandle create(String name) {
if (Pixel.app().isOneOfPlatforms(Platform.ANDROID, Platform.WEB)) {
return Gdx.files.internal(name);
} else {
return Gdx.files.classpath(name);
}
}
}

View File

@ -40,10 +40,6 @@ public class OpenEggbertUtils {
//Not meant to be instantiated.
}
public static Stream<String> lines(String string) {
return Arrays.asList(string.split("\\r?\\n")).stream();
}
public static <T> List<T> streamToList(Stream<T> stream) {
return stream.collect(Collectors.toList());
}

File diff suppressed because one or more lines are too long