Skip to content

Commit

Permalink
RTTI: Improve data source reading
Browse files Browse the repository at this point in the history
  • Loading branch information
ShadelessFox committed Mar 27, 2023
1 parent e616f19 commit c81ef53
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.shade.util.NotNull;

import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;

Expand Down Expand Up @@ -58,12 +59,30 @@ public int getSize() {
@NotNull
@Override
public byte[] getData(@NotNull PackfileManager manager) throws IOException {
return getData(manager, getOffset(), getLength());
}

@NotNull
@Override
public byte[] getData(@NotNull PackfileManager manager, int offset, int length) throws IOException {
final String path = "%s.core.stream".formatted(location);
final Packfile packfile = manager.findFirst(path);

if (packfile == null) {
throw new IOException("Can't find packfile that contains " + path);
}
return packfile.extract(path);

try (InputStream is = packfile.newInputStream(path)) {
if (offset > 0) {
is.skipNBytes(offset);
}

if (length > 0) {
return is.readNBytes(length);
} else {
return is.readAllBytes();
}
}
}

@NotNull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.shade.util.NotNull;

import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;

Expand Down Expand Up @@ -49,16 +50,35 @@ public int getSize() {
@NotNull
@Override
public byte[] getData(@NotNull PackfileManager manager) throws IOException {
return getData(manager, getOffset(), getLength());
}

@NotNull
@Override
public byte[] getData(@NotNull PackfileManager manager, int offset, int length) throws IOException {
if (!location.startsWith("cache:")) {
throw new IOException("Data source points to a resource outside cache: " + location);
}

// TODO: Should prefix removal be handled by the manager itself?
final String path = location.substring(6);
final Packfile packfile = manager.findFirst(path);

if (packfile == null) {
throw new IOException("Can't find packfile that contains " + path);
}
return packfile.extract(path);

try (InputStream is = packfile.newInputStream(path)) {
if (offset > 0) {
is.skipNBytes(offset);
}

if (length > 0) {
return is.readNBytes(length);
} else {
return is.readAllBytes();
}
}
}

@NotNull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ public interface HwDataSource extends HwType {
@NotNull
byte[] getData(@NotNull PackfileManager manager) throws IOException;

@NotNull
byte[] getData(@NotNull PackfileManager manager, int offset, int length) throws IOException;

@NotNull
String getLocation();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ private ImageData getImageData(int mip, int slice) {
throw new UncheckedIOException(e);
}

mipBuffer = ByteBuffer.wrap(stream).slice(dataSource.getOffset(), dataSource.getLength());
mipBuffer = ByteBuffer.wrap(stream);
mipOffset = IntStream.range(0, mip + 1)
.map(x -> getTextureSize(reader, dimension, x) * (x == mip ? slice : getSliceCount(x)))
.sum();
Expand Down

0 comments on commit c81ef53

Please sign in to comment.