Skip to content

Commit

Permalink
Small refactoring; extract asset stuff to a separate module
Browse files Browse the repository at this point in the history
  • Loading branch information
ShadelessFox committed Dec 7, 2024
1 parent 9cd7c3f commit 6a5463d
Show file tree
Hide file tree
Showing 13 changed files with 78 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ repositories {
dependencies {
api project(':decima-model')
api project(':decima-rtti')
api project(':decima-game')

compileOnlyApi project(':decima-rtti-generator')
annotationProcessor project(':decima-rtti-generator')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
import com.shade.decima.game.hrzr.rtti.HRZRTypeFactory;
import com.shade.decima.game.hrzr.storage.PackFileManager;
import com.shade.decima.game.hrzr.storage.PathResolver;
import com.shade.decima.game.hrzr.storage.api.Asset;
import com.shade.decima.game.hrzr.storage.api.AssetId;
import com.shade.decima.game.Asset;
import com.shade.decima.game.AssetId;
import com.shade.decima.rtti.factory.TypeNotFoundException;
import com.shade.util.NotNull;
import com.shade.util.io.BinaryReader;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package com.shade.decima.game.hrzr.storage;

import com.shade.decima.game.Asset;
import com.shade.decima.game.hrzr.DirectStorageReader;
import com.shade.decima.game.hrzr.storage.api.Archive;
import com.shade.decima.game.hrzr.storage.api.AssetId;
import com.shade.decima.game.Archive;
import com.shade.decima.game.AssetId;
import com.shade.util.NotNull;
import com.shade.util.io.BinaryReader;

Expand All @@ -15,13 +16,15 @@
import java.util.Map;

public class PackFileArchive implements Archive, Comparable<PackFileArchive> {
private final BinaryReader reader;
private final Path path;
private final String name;
private final BinaryReader reader;
private final Map<PackFileAssetId, PackFileAsset> assets;

PackFileArchive(@NotNull Path path, @NotNull PackFileManager.PackFileInfo info) throws IOException {
this.reader = DirectStorageReader.open(path);
this.path = path;
this.name = info.name();
this.reader = DirectStorageReader.open(path);
this.assets = new HashMap<>(info.assets().length);

for (PackFileManager.PackFileAssetInfo asset : info.assets()) {
Expand All @@ -32,22 +35,18 @@ public class PackFileArchive implements Archive, Comparable<PackFileArchive> {

@NotNull
@Override
public List<PackFileAsset> assets() {
return List.copyOf(assets.values());
}

@Override
public boolean contains(@NotNull AssetId id) {
return assets.containsKey((PackFileAssetId) id);
public PackFileAsset get(@NotNull AssetId id) throws IOException {
PackFileAsset asset = assets.get((PackFileAssetId) id);
if (asset == null) {
throw new IllegalArgumentException("Asset not found: " + id);
}
return asset;
}

@NotNull
@Override
public ByteBuffer load(@NotNull AssetId id) throws IOException {
PackFileAsset asset = assets.get((PackFileAssetId) id);
if (asset == null) {
throw new IllegalArgumentException("Asset not found: " + id);
}
PackFileAsset asset = get(id);
ByteBuffer buffer = ByteBuffer.allocate(asset.length()).order(ByteOrder.LITTLE_ENDIAN);
synchronized (reader) {
reader.position(asset.offset());
Expand All @@ -56,6 +55,29 @@ public ByteBuffer load(@NotNull AssetId id) throws IOException {
return buffer;
}

@Override
public boolean contains(@NotNull AssetId id) {
return assets.containsKey((PackFileAssetId) id);
}

@NotNull
@Override
public List<PackFileAsset> assets() {
return List.copyOf(assets.values());
}

@NotNull
@Override
public String name() {
return name;
}

@NotNull
@Override
public Path path() {
return path;
}

@Override
public void close() throws IOException {
reader.close();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.shade.decima.game.hrzr.storage;

import com.shade.decima.game.hrzr.storage.api.Asset;
import com.shade.decima.game.Asset;
import com.shade.util.NotNull;

public record PackFileAsset(@NotNull PackFileAssetId id, long offset, int length) implements Asset {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.shade.decima.game.hrzr.storage;

import com.shade.decima.game.hrzr.storage.api.AssetId;
import com.shade.decima.game.AssetId;
import com.shade.util.NotNull;
import com.shade.util.hash.Hashing;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package com.shade.decima.game.hrzr.storage;

import com.shade.decima.game.hrzr.storage.api.Archive;
import com.shade.decima.game.hrzr.storage.api.Asset;
import com.shade.decima.game.hrzr.storage.api.AssetId;
import com.shade.decima.game.Asset;
import com.shade.decima.game.AssetId;
import com.shade.util.NotNull;
import com.shade.util.Nullable;
import com.shade.util.io.BinaryReader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.Closeable;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.file.Files;
Expand All @@ -17,7 +17,7 @@
import java.util.NavigableSet;
import java.util.TreeSet;

public class PackFileManager implements Archive {
public class PackFileManager implements Closeable {
private static final Logger log = LoggerFactory.getLogger(PackFileManager.class);

private final NavigableSet<PackFileArchive> archives = new TreeSet<>();
Expand Down Expand Up @@ -45,7 +45,6 @@ private void mount(@NotNull Path path, @NotNull PackFileInfo info) throws IOExce
}

@NotNull
@Override
public List<? extends Asset> assets() {
return archives.stream()
.flatMap(archive -> archive.assets().stream())
Expand All @@ -54,26 +53,13 @@ public List<? extends Asset> assets() {
}

@NotNull
@Override
public ByteBuffer load(@NotNull AssetId id) throws IOException {
return archives.reversed().stream()
.filter(archive -> archive.contains(id))
.findFirst().orElseThrow()
.load(id);
}

@Nullable
public PackFileArchive find(@NotNull AssetId id) {
return archives.reversed().stream()
.filter(archive -> archive.contains(id))
.findFirst().orElse(null);
}

@Override
public boolean contains(@NotNull AssetId id) {
return archives.stream().anyMatch(archive -> archive.contains(id));
}

@Override
public void close() throws IOException {
for (PackFileArchive archive : archives) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
requires static decima.rtti.generator;

requires decima.rtti;
requires platform.util;
requires decima.game;

requires org.lz4.java;
requires org.slf4j;
Expand Down
11 changes: 11 additions & 0 deletions modules/decima-game/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
plugins {
id 'java-library'
}

repositories {
mavenCentral()
}

dependencies {
api project(':platform-util')
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,28 @@
package com.shade.decima.game.hrzr.storage.api;
package com.shade.decima.game;

import com.shade.util.NotNull;

import java.io.Closeable;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.file.Path;
import java.util.List;

public interface Archive extends Closeable {
@NotNull
List<? extends Asset> assets();
Asset get(@NotNull AssetId id) throws IOException;

@NotNull
ByteBuffer load(@NotNull AssetId id) throws IOException;

boolean contains(@NotNull AssetId id);

@NotNull
List<? extends Asset> assets();

@NotNull
String name();

@NotNull
Path path();
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.shade.decima.game.hrzr.storage.api;
package com.shade.decima.game;

import com.shade.util.NotNull;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.shade.decima.game.hrzr.storage.api;
package com.shade.decima.game;

public interface AssetId extends Comparable<AssetId> {
}
5 changes: 5 additions & 0 deletions modules/decima-game/src/main/java/module-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module decima.game {
requires platform.util;

exports com.shade.decima.game;
}
1 change: 1 addition & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,4 @@ importProject 'decima-ext-texture-viewer'
//importProject 'decima-game-horizon-zero-dawn'
importProject 'decima-game-horizon-zero-dawn-remastered'
importProject 'decima-game-until-dawn'
importProject 'decima-game'

0 comments on commit 6a5463d

Please sign in to comment.