Working on the Pixel Game Library II

This commit is contained in:
Robert Vokac 2024-09-03 18:42:14 +02:00
parent 2c841d5c1b
commit e6051bbc01
No known key found for this signature in database
GPG Key ID: C459E1E4B4A986BB
16 changed files with 137 additions and 63 deletions

View File

@ -37,7 +37,7 @@ import com.openeggbert.core.mod.ModIdentification;
import com.openeggbert.core.screen.GameSpaceListScreen;
import com.openeggbert.core.screen.InitScreen;
import com.pixelgamelibrary.storage.Storage;
import com.pixelgamelibrary.storage.StorageImplementationLoader;
import com.pixelgamelibrary.backends.libgdx.storage.StorageFactory;
import com.openeggbert.core.configuration.OpenEggbertDisplayMode;
import com.pixelgamelibrary.Game;
import com.openeggbert.core.utils.OpenEggbertUtils;
@ -90,7 +90,7 @@ public class OpenEggbertGame extends Game {
public Storage getStorage() {
if(storage == null) {
this.storage = StorageImplementationLoader.getStorage();
this.storage = StorageFactory.getStorage();
}
return storage;
}

View File

@ -13,38 +13,19 @@
// 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
// 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.pixelgamelibrary.storage;
import com.badlogic.gdx.utils.Base64Coder;
package com.pixelgamelibrary.api;
/**
*
* @author robertvokac
*/
public class GdxStorageUtils {
public interface AppI {
void log(String msg);
void error(String msg);
void debug(String msg);
private GdxStorageUtils() {
//Not meant to be instantiated.
}
public static String decodeBase64AsString(String string) {
return new String(decodeBase64AsByteArray(string));
}
public static byte[] decodeBase64AsByteArray(String string) {
return Base64Coder.decode(string);
}
public static String encodeToBase64(String string) {
return encodeToBase64(string.getBytes());
}
public static String encodeToBase64(byte[] data) {
return String.valueOf(Base64Coder.encode(data));
}
}

View File

@ -27,8 +27,9 @@ import com.pixelgamelibrary.Platform;
*/
public interface PixelBackend {
void exit();
Platform getPlatform();
AppI app();
GraphicsI graphics();
AudioI audio();
InputI input();
@ -36,5 +37,6 @@ public interface PixelBackend {
AssetI asset();
StorageI storage();
UtilsI utils();
void exit();
}

View File

@ -19,10 +19,23 @@
///////////////////////////////////////////////////////////////////////////////////////////////
package com.pixelgamelibrary.api;
import com.badlogic.gdx.utils.Base64Coder;
/**
*
* @author robertvokac
*/
public interface UtilsI {
XmlElement parseXml(String xmlString);
default String decodeBase64AsString(String string) {
return new String(decodeBase64AsByteArray(string));
}
byte[] decodeBase64AsByteArray(String string);
default String encodeToBase64(String string) {
return encodeToBase64(string.getBytes());
}
String encodeToBase64(byte[] data);
}

View File

@ -0,0 +1,57 @@
///////////////////////////////////////////////////////////////////////////////////////////////
// Pixel: Game library.
// 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.pixelgamelibrary.backends.libgdx;
import com.badlogic.gdx.Application;
import com.badlogic.gdx.Gdx;
import com.pixelgamelibrary.api.AppI;
/**
*
* @author robertvokac
*/
public class AppLibGDXImpl implements AppI {
@Override
public void log(String msg) {
Application app = Gdx.app;
if (app != null) {
Gdx.app.log(getClass().getName(), msg);
}
}
@Override
public void error(String msg) {
Application app = Gdx.app;
if (app != null) {
Gdx.app.error(getClass().getName(), msg);
}
}
@Override
public void debug(String msg) {
Application app = Gdx.app;
if (app != null) {
Gdx.app.debug(getClass().getName(), msg);
}
}
}

View File

@ -24,6 +24,7 @@ import static com.badlogic.gdx.Application.ApplicationType.Desktop;
import com.badlogic.gdx.Gdx;
import com.pixelgamelibrary.PixelException;
import com.pixelgamelibrary.Platform;
import com.pixelgamelibrary.api.AppI;
import com.pixelgamelibrary.api.AssetI;
import com.pixelgamelibrary.api.AudioI;
import com.pixelgamelibrary.api.GraphicsI;
@ -39,6 +40,7 @@ import com.pixelgamelibrary.api.PixelBackend;
*/
public class PixelLibGDXBackend implements PixelBackend {
private AppI pixelAppLibGdxImpl = null;
private GraphicsI pixelGraphicsLibGdxImpl = null;
private AudioI pixelAudioLibGdxImpl = null;
private InputI pixelInputLibGdxImpl = null;
@ -128,4 +130,11 @@ public class PixelLibGDXBackend implements PixelBackend {
}
return pixelUtilsLibGdxImpl; }
@Override
public AppI app() {
if (pixelAppLibGdxImpl == null) {
pixelAppLibGdxImpl = new AppLibGDXImpl();
}
return pixelAppLibGdxImpl; }
}

View File

@ -19,6 +19,7 @@
///////////////////////////////////////////////////////////////////////////////////////////////
package com.pixelgamelibrary.backends.libgdx;
import com.badlogic.gdx.utils.Base64Coder;
import com.badlogic.gdx.utils.XmlReader;
import com.pixelgamelibrary.api.XmlElement;
import com.pixelgamelibrary.api.UtilsI;
@ -36,6 +37,13 @@ public class UtilsLibGDXImpl implements UtilsI {
}
public byte[] decodeBase64AsByteArray(String string) {
return Base64Coder.decode(string);
}
public String encodeToBase64(byte[] data) {
return String.valueOf(Base64Coder.encode(data));
}
}

View File

@ -17,7 +17,7 @@
// <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.storage.filesystem;
package com.pixelgamelibrary.backends.libgdx.storage;
import com.pixelgamelibrary.Platform;

View File

@ -17,7 +17,7 @@
// <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.storage.filesystem;
package com.pixelgamelibrary.backends.libgdx.storage;
import com.pixelgamelibrary.Platform;
import com.pixelgamelibrary.storage.Storage;

View File

@ -17,7 +17,7 @@
// <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.storage.filesystem;
package com.pixelgamelibrary.backends.libgdx.storage;
import com.pixelgamelibrary.Platform;

View File

@ -17,9 +17,10 @@
// <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.storage.map;
package com.pixelgamelibrary.backends.libgdx.storage;
import com.badlogic.gdx.Preferences;
import com.pixelgamelibrary.storage.map.SimpleMap;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;

View File

@ -17,22 +17,21 @@
// <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.storage;
package com.pixelgamelibrary.backends.libgdx.storage;
import com.pixelgamelibrary.storage.map.WebGLStorage;
import com.pixelgamelibrary.storage.map.MemoryStorage;
import com.pixelgamelibrary.storage.filesystem.AndroidStorage;
import com.pixelgamelibrary.storage.filesystem.DesktopStorage;
import com.pixelgamelibrary.Pixel;
import com.pixelgamelibrary.Platform;
import com.pixelgamelibrary.storage.Storage;
import com.pixelgamelibrary.storage.StorageException;
/**
*
* @author robertvokac
*/
public class StorageImplementationLoader {
public class StorageFactory {
private StorageImplementationLoader() {
private StorageFactory() {
//Not meant to be instantiated.
}
private static Storage storage = null;
@ -55,7 +54,7 @@ public class StorageImplementationLoader {
}
}
if (storage == null) {
throw new GdxStorageException("Platform is not supported: " + platform);
throw new StorageException("Platform is not supported: " + platform);
}
return storage;
}

View File

@ -17,27 +17,32 @@
// <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.storage.map;
package com.pixelgamelibrary.backends.libgdx.storage;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Preferences;
import com.pixelgamelibrary.Platform;
import com.pixelgamelibrary.storage.map.MapStorage;
/**
*
* @author robertvokac
*/
public class WebGLStorage extends MapStorage {
public Platform getPlatform() {
return Platform.WEB;
}
public WebGLStorage() {
this("open-eggbert.webGL.Local-Storage");
this("pixel.libgdx.webGL.Local-Storage");
}
public WebGLStorage(String preferencesName) {
this(Gdx.app.getPreferences(preferencesName));
}
public WebGLStorage(Preferences preferences) {
public WebGLStorage(Preferences preferences) {
super(new SimpleLocalStorageMap(preferences));
}

View File

@ -19,13 +19,15 @@
///////////////////////////////////////////////////////////////////////////////////////////////
package com.pixelgamelibrary.storage;
import com.pixelgamelibrary.PixelException;
/**
*
* @author robertvokac
*/
public class GdxStorageException extends RuntimeException {
public class StorageException extends PixelException {
public GdxStorageException(String string) {
public StorageException(String string) {
super(string);
}

View File

@ -19,7 +19,7 @@
///////////////////////////////////////////////////////////////////////////////////////////////
package com.pixelgamelibrary.storage.map;
import com.pixelgamelibrary.storage.GdxStorageException;
import com.pixelgamelibrary.storage.StorageException;
/**
@ -31,7 +31,7 @@ public enum MapFileType {
public static MapFileType ofKey(String key, SimpleMap map) {
if (!map.contains(key)) {
throw new GdxStorageException("Map does not contain key: " + key);
throw new StorageException("Map does not contain key: " + key);
}
String value = map.getString(key);
if (value.startsWith(FILE.name())) {
@ -40,7 +40,7 @@ public enum MapFileType {
if (value.startsWith(DIRECTORY.name())) {
return DIRECTORY;
}
throw new GdxStorageException("Unsupported MapFileType for key in the map: " + key);
throw new StorageException("Unsupported MapFileType for key in the map: " + key);
}

View File

@ -19,11 +19,10 @@
///////////////////////////////////////////////////////////////////////////////////////////////
package com.pixelgamelibrary.storage.map;
import com.badlogic.gdx.Application;
import com.badlogic.gdx.Gdx;
import com.pixelgamelibrary.Pixel;
import com.pixelgamelibrary.Platform;
import com.pixelgamelibrary.storage.GdxStorageException;
import com.pixelgamelibrary.storage.GdxStorageUtils;
import com.pixelgamelibrary.storage.StorageException;
import com.pixelgamelibrary.storage.Storage;
import java.util.List;
import java.util.stream.Collectors;
@ -83,13 +82,7 @@ public class MapStorage implements Storage {
workingDirectory = absolutePath;
return "";
}
private static final String SLASH = "/";
private void logError(String msg) {
Application app = Gdx.app;
if(app != null) Gdx.app.error(getClass().getName(), msg);
}
private static final String SLASH = "/";
@Override
public String mkdir(String path) {
@ -125,10 +118,10 @@ public class MapStorage implements Storage {
private static String getParentPath(String path) {
// System.out.println("getParentPath()");
if (path == null) {
throw new GdxStorageException("Path is null");
throw new StorageException("Path is null");
}
if (path.trim().isEmpty()) {
throw new GdxStorageException("Path is empty");
throw new StorageException("Path is empty");
}
if (path.equals("/")) {
@ -219,10 +212,10 @@ public class MapStorage implements Storage {
private String moveOrCp(String source, String target, boolean move, boolean cp) {
if (move && cp) {
throw new GdxStorageException("move == true && cp == true");
throw new StorageException("move == true && cp == true");
}
if (!move && !cp) {
throw new GdxStorageException("move != true && cp != true");
throw new StorageException("move != true && cp != true");
}
String absolutePathSource = convertToAbsolutePathIfNeeded(source);
String absolutePathTarget = convertToAbsolutePathIfNeeded(target);
@ -288,7 +281,7 @@ public class MapStorage implements Storage {
return null;
}
text = text.substring(BINARYFILE.length());
return GdxStorageUtils.decodeBase64AsByteArray(text);
return Pixel.get().utils().decodeBase64AsByteArray(text);
}
private static final String BINARYFILE = "BINARYFILE";
@ -299,7 +292,7 @@ public class MapStorage implements Storage {
@Override
public String savebin(String name, byte[] data) {
return savetext(name, BINARYFILE + GdxStorageUtils.encodeToBase64(data));
return savetext(name, BINARYFILE + Pixel.get().utils().encodeToBase64(data));
}
@Override
@ -344,4 +337,8 @@ public class MapStorage implements Storage {
throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody
}
private void logError(String msg) {
Pixel.get().app().error(msg);
}
}