Skip to content

Commit

Permalink
Memory file improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
xpenatan committed Jun 15, 2024
1 parent daec145 commit 811899a
Show file tree
Hide file tree
Showing 12 changed files with 329 additions and 139 deletions.
2 changes: 2 additions & 0 deletions backends/backend-teavm/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ dependencies {
api("org.teavm:teavm-jso:${LibExt.teaVMVersion}")
api("org.teavm:teavm-jso-apis:${LibExt.teaVMVersion}")
api("org.teavm:teavm-jso-impl:${LibExt.teaVMVersion}")

testImplementation("com.google.truth:truth:${LibExt.truthVersion}")
}

publishing {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public ResolutionFileResolverEmu(FileHandleResolver baseResolver, ResolutionFile
@Override
public FileHandle resolve(String fileName) {
ResolutionFileResolver.Resolution bestDesc = choose(descriptors);
FileHandle originalHandle = new TeaFileHandle(fileName);
FileHandle originalHandle = Gdx.files.internal(fileName);
FileHandle handle = baseResolver.resolve(resolve(originalHandle, bestDesc.folder));
if(!handle.exists()) handle = baseResolver.resolve(fileName);
return handle;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ else if(agentInfo.isLinux())
AssetLoaderListener<Object> assetListener = new AssetLoaderListener();

input = new TeaInput(graphics.canvas);
files = new TeaFiles(preloader);
files = new TeaFiles(config, preloader);
net = new TeaNet();
logger = new TeaApplicationLogger();
clipboard = new TeaClipboard();
Expand Down Expand Up @@ -188,8 +188,6 @@ public void handleEvent(EventWrapper evt) {
});
}

// Init database
TeaFileHandle.initHandle(config);
preloader.preload(config, "assets.txt");

window.requestAnimationFrame(this);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
package com.github.xpenatan.gdx.backends.teavm;

import com.badlogic.gdx.Files.FileType;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.utils.GdxRuntimeException;
import com.badlogic.gdx.utils.StreamUtils;
import com.github.xpenatan.gdx.backends.teavm.filesystem.FileDB;
import com.github.xpenatan.gdx.backends.teavm.filesystem.types.InternalDBStorage;
import com.github.xpenatan.gdx.backends.teavm.filesystem.types.LocalDBStorage;
import com.github.xpenatan.gdx.backends.teavm.preloader.Preloader;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
Expand All @@ -28,43 +25,20 @@
* @author xpenatan
*/
public class TeaFileHandle extends FileHandle {
private static InternalDBStorage internalStorage;
private static LocalDBStorage localStorage;

public final Preloader preloader;
private final String file;
private final FileType type;

private FileDB fileDB;

public static void initHandle(TeaApplicationConfiguration config) {
if(config.useNewFileHandle) {
internalStorage = new InternalDBStorage();
localStorage = new LocalDBStorage();
}
}

public TeaFileHandle(Preloader preloader, String fileName, FileType type) {
public TeaFileHandle(Preloader preloader, FileDB fileDB, String fileName, FileType type) {
if((type != FileType.Internal) && (type != FileType.Classpath) && (type != FileType.Local)) {
throw new GdxRuntimeException("FileType '" + type + "' Not supported in web backend");
}
this.preloader = preloader;
this.file = fixSlashes(fileName);
this.type = type;
TeaApplicationConfiguration config = ((TeaApplication)Gdx.app).getConfig();
if(config.useNewFileHandle) {
if(type == FileType.Local) {
fileDB = localStorage;
}
else {
// Internal and Classpath share the same storage
fileDB = internalStorage;
}
}
}

public TeaFileHandle(String path) {
this(((TeaApplication)Gdx.app).getPreloader(), path, FileType.Internal);
this.fileDB = fileDB;
}

/** @return The full url to an asset, e.g. http://localhost:8080/assets/data/shotgun.ogg */
Expand Down Expand Up @@ -555,15 +529,15 @@ else if(type == FileType.Local) {
* doesn't exist.
*/
public FileHandle child(String name) {
return new TeaFileHandle(preloader, (file.isEmpty() ? "" : (file + (file.endsWith("/") ? "" : "/"))) + name,
return new TeaFileHandle(preloader, fileDB, (file.isEmpty() ? "" : (file + (file.endsWith("/") ? "" : "/"))) + name,
type);
}

public FileHandle parent() {
int index = file.lastIndexOf("/");
String dir = "";
if(index > 0) dir = file.substring(0, index);
return new TeaFileHandle(preloader, dir, type);
return new TeaFileHandle(preloader, fileDB, dir, type);
}

public FileHandle sibling(String name) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import com.badlogic.gdx.Files;
import com.badlogic.gdx.files.FileHandle;
import com.github.xpenatan.gdx.backends.teavm.filesystem.FileDB;
import com.github.xpenatan.gdx.backends.teavm.filesystem.types.InternalDBStorage;
import com.github.xpenatan.gdx.backends.teavm.filesystem.types.LocalDBStorage;
import com.github.xpenatan.gdx.backends.teavm.preloader.Preloader;

/**
Expand All @@ -12,38 +14,55 @@ public class TeaFiles implements Files {

final Preloader preloader;

public TeaFiles(Preloader preloader) {
public InternalDBStorage internalStorage;
public LocalDBStorage localStorage;

public TeaFiles(TeaApplicationConfiguration config, Preloader preloader) {
this.preloader = preloader;
if(config.useNewFileHandle) {
this.internalStorage = new InternalDBStorage();
this.localStorage = new LocalDBStorage();
}
}

@Override
public FileHandle getFileHandle(String path, FileType type) {
return new TeaFileHandle(preloader, path, type);
FileDB fileDB = null;
if(type == FileType.Internal) {
return internal(path);
}
else if(type == FileType.Classpath) {
return classpath(path);
}
else if(type == FileType.Local) {
return local(path);
}
return new TeaFileHandle(preloader, null, path, type);
}

@Override
public FileHandle classpath(String path) {
return new TeaFileHandle(preloader, path, FileType.Classpath);
return new TeaFileHandle(preloader, internalStorage, path, FileType.Classpath);
}

@Override
public FileHandle internal(String path) {
return new TeaFileHandle(preloader, path, FileType.Internal);
return new TeaFileHandle(preloader, internalStorage, path, FileType.Internal);
}

@Override
public FileHandle external(String path) {
return new TeaFileHandle(preloader, path, FileType.External);
return new TeaFileHandle(preloader, null, path, FileType.External);
}

@Override
public FileHandle absolute(String path) {
return new TeaFileHandle(preloader, path, FileType.Absolute);
return new TeaFileHandle(preloader, null, path, FileType.Absolute);
}

@Override
public FileHandle local(String path) {
return new TeaFileHandle(preloader, path, FileType.Local);
return new TeaFileHandle(preloader, localStorage, path, FileType.Local);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.badlogic.gdx.utils.GdxRuntimeException;
import com.github.xpenatan.gdx.backends.teavm.TeaApplication;
import com.github.xpenatan.gdx.backends.teavm.TeaFileHandle;
import com.github.xpenatan.gdx.backends.teavm.preloader.Preloader;
import java.io.ByteArrayOutputStream;
import java.io.FileFilter;
import java.io.FilenameFilter;
Expand Down Expand Up @@ -80,13 +81,18 @@ public void close() throws IOException {
public final FileHandle[] list(TeaFileHandle file) {
// convert paths to file handles
String[] paths = paths(file);
FileHandle[] files = new FileHandle[paths.length];
FileHandle[] files = new TeaFileHandle[paths.length];

Preloader preloader = null;
if(Gdx.app != null) {
preloader = ((TeaApplication)Gdx.app).getPreloader();
}
for(int i = 0; i < paths.length; i++) {
String path = paths[i];
if((path.length() > 0) && (path.charAt(path.length() - 1) == '/')) {
path = path.substring(0, path.length() - 1);
}
files[i] = new TeaFileHandle(((TeaApplication)Gdx.app).getPreloader(), path, Files.FileType.Local);
files[i] = new TeaFileHandle(preloader, this, path, file.type());
}
return files;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,26 @@ public class FileData {
public static final int TYPE_DIRECTORY = 1;
public static final int TYPE_FILE = 2;

private final String name;
private final String path;
private final byte[] bytes;
private final int type;

public FileData(String name) {
this(name, TYPE_DIRECTORY, null);
public FileData(String path) {
this(path, TYPE_DIRECTORY, null);
}

public FileData(String name, byte[] bytes) {
this(name, TYPE_FILE, bytes);
public FileData(String path, byte[] bytes) {
this(path, TYPE_FILE, bytes);
}

public FileData(String name, int type, byte[] bytes) {
this.name = name;
this.path = name;
this.bytes = bytes;
this.type = type;
}


public String getName() {
return name;
public String getPath() {
return path;
}

public byte[] getBytes() {
Expand All @@ -39,4 +38,8 @@ public boolean isDirectory() {
public int getType() {
return type;
}

public int getBytesSize() {
return bytes != null ? bytes.length : 0;
}
}
Loading

0 comments on commit 811899a

Please sign in to comment.