Changed strategy to support FileHandleAdapter and AsyncAdapter

This commit is contained in:
Collin Smith 2020-09-29 00:55:40 -07:00
parent 8837ff46b6
commit 9d7ee8450e
8 changed files with 89 additions and 26 deletions

View File

@ -0,0 +1,11 @@
package com.riiablo.assets;
public class AdapterNotFound extends RuntimeException {
AdapterNotFound(Class type) {
this(type, null);
}
AdapterNotFound(Class type, Throwable cause) {
super("Adapter not found for " + type.getSimpleName(), cause);
}
}

View File

@ -34,7 +34,7 @@ public class AssetManager implements Disposable, LoadTask.Callback, AsyncReader.
final ArrayBlockingQueue<LoadTask> completedTasks = new ArrayBlockingQueue<>(256);
final ArrayList<LoadTask> drain = new ArrayList<>(256);
final Map<Class, SyncReader> readers = new ConcurrentHashMap<>();
final Map<Class, FileHandleAdapter> readers = new ConcurrentHashMap<>();
final ExecutorService io;
final ExecutorService async;
@ -94,21 +94,21 @@ public class AssetManager implements Disposable, LoadTask.Callback, AsyncReader.
loaders.put(type, loader);
}
public SyncReader getReader(Class type) {
public FileHandleAdapter getAdapter(Class type) {
return readers.get(type);
}
SyncReader findReader(Class type) {
final SyncReader reader = getReader(type);
if (reader == null) throw new ReaderNotFound(type);
FileHandleAdapter findAdapter(Class type) {
final FileHandleAdapter reader = getAdapter(type);
if (reader == null) throw new AdapterNotFound(type);
return reader;
}
public <F extends FileHandle, B> void setReader(Class<F> type, SyncReader<B> reader) {
public <F extends FileHandle> void setAdapter(Class<F> type, FileHandleAdapter<F> adapter) {
if (type == null) throw new IllegalArgumentException("type cannot be null");
if (reader == null) throw new IllegalArgumentException("reader cannot be null");
log.debug("Reader set {} -> {}", type.getSimpleName(), reader.getClass());
readers.put(type, reader);
if (adapter == null) throw new IllegalArgumentException("adapter cannot be null");
log.debug("Adapter set {} -> {}", type.getSimpleName(), adapter.getClass());
readers.put(type, adapter);
}
public void clear() {
@ -160,10 +160,13 @@ public class AssetManager implements Disposable, LoadTask.Callback, AsyncReader.
}
final AssetLoader loader = findLoader(asset.type);
if (loader instanceof AsyncAssetLoader) {
//...
}
final FileHandle handle = loader.resolver().resolve(asset);
final SyncReader reader = findReader(handle.getClass());
if (reader instanceof AsyncReader) {
io.submit(((AsyncReader) reader).readFuture(asset, this));
final FileHandleAdapter adapter = findAdapter(handle.getClass());
if (adapter instanceof AsyncAdapter) {
// io.submit(((AsyncReader) reader).readFuture(asset, this));
} else {
// io.submit(reader.create(asset));
}

View File

@ -0,0 +1,3 @@
package com.riiablo.assets;
public interface AsyncAdapter {}

View File

@ -0,0 +1,12 @@
package com.riiablo.assets;
import io.netty.buffer.ByteBuf;
import java.io.InputStream;
import com.badlogic.gdx.files.FileHandle;
public interface FileHandleAdapter<T extends FileHandle> {
byte[] readBytes(T handle);
InputStream read(T handle);
ByteBuf readByteBuf(T handle);
}

View File

@ -0,0 +1,23 @@
package com.riiablo.assets;
import io.netty.buffer.ByteBuf;
import java.io.InputStream;
import com.badlogic.gdx.files.FileHandle;
public class GdxFileHandleAdapter implements FileHandleAdapter<FileHandle> {
@Override
public byte[] readBytes(FileHandle handle) {
return handle.readBytes();
}
@Override
public InputStream read(FileHandle handle) {
return handle.read();
}
@Override
public ByteBuf readByteBuf(FileHandle handle) {
throw new UnsupportedOperationException();
}
}

View File

@ -0,0 +1,23 @@
package com.riiablo.assets;
import io.netty.buffer.ByteBuf;
import java.io.InputStream;
import com.riiablo.mpq_bytebuf.MPQFileHandle;
public class MPQFileHandleAdapter implements FileHandleAdapter<MPQFileHandle>, AsyncAdapter {
@Override
public byte[] readBytes(MPQFileHandle handle) {
throw new UnsupportedOperationException();
}
@Override
public InputStream read(MPQFileHandle handle) {
return handle.read();
}
@Override
public ByteBuf readByteBuf(MPQFileHandle handle) {
return handle.readByteBuf();
}
}

View File

@ -1,11 +0,0 @@
package com.riiablo.assets;
public class ReaderNotFound extends RuntimeException {
ReaderNotFound(Class type) {
this(type, null);
}
ReaderNotFound(Class type, Throwable cause) {
super("Reader not found for " + type.getSimpleName(), cause);
}
}

View File

@ -4,7 +4,6 @@ import org.junit.BeforeClass;
import org.junit.Test;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.assets.loaders.resolvers.InternalFileHandleResolver;
import com.badlogic.gdx.files.FileHandle;
import com.riiablo.RiiabloTest;
@ -23,8 +22,8 @@ public class AssetManagerTest extends RiiabloTest {
public void init() {
AssetManager assets = new AssetManager(1);
MPQFileHandleResolver resolver = new MPQFileHandleResolver(Gdx.files.absolute("C:\\diablo"));
assets.setReader(FileHandle.class, new FileHandleReader(new InternalFileHandleResolver()));
assets.setReader(MPQFileHandle.class, new MPQFileHandleReader(resolver));
assets.setAdapter(FileHandle.class, new GdxFileHandleAdapter());
assets.setAdapter(MPQFileHandle.class, new MPQFileHandleAdapter());
assets.dispose();
}
}