mirror of
https://github.com/collinsmith/riiablo.git
synced 2025-02-13 12:26:28 +07:00
Changed strategy to support FileHandleAdapter and AsyncAdapter
This commit is contained in:
parent
8837ff46b6
commit
9d7ee8450e
11
core/src/com/riiablo/assets/AdapterNotFound.java
Normal file
11
core/src/com/riiablo/assets/AdapterNotFound.java
Normal 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);
|
||||
}
|
||||
}
|
@ -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));
|
||||
}
|
||||
|
3
core/src/com/riiablo/assets/AsyncAdapter.java
Normal file
3
core/src/com/riiablo/assets/AsyncAdapter.java
Normal file
@ -0,0 +1,3 @@
|
||||
package com.riiablo.assets;
|
||||
|
||||
public interface AsyncAdapter {}
|
12
core/src/com/riiablo/assets/FileHandleAdapter.java
Normal file
12
core/src/com/riiablo/assets/FileHandleAdapter.java
Normal 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);
|
||||
}
|
23
core/src/com/riiablo/assets/GdxFileHandleAdapter.java
Normal file
23
core/src/com/riiablo/assets/GdxFileHandleAdapter.java
Normal 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();
|
||||
}
|
||||
}
|
23
core/src/com/riiablo/assets/MPQFileHandleAdapter.java
Normal file
23
core/src/com/riiablo/assets/MPQFileHandleAdapter.java
Normal 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();
|
||||
}
|
||||
}
|
@ -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);
|
||||
}
|
||||
}
|
@ -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();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user