1package com.runehive.fs.cache;
3import com.google.common.base.Preconditions;
4import com.runehive.fs.cache.archive.Archive;
6import java.io.IOException;
7import java.nio.ByteBuffer;
8import java.nio.channels.SeekableByteChannel;
9import java.nio.file.Files;
10import java.nio.file.Path;
11import java.nio.file.Paths;
12import java.util.Objects;
13import java.util.zip.CRC32;
15import static java.nio.file.StandardOpenOption.READ;
16import static java.nio.file.StandardOpenOption.WRITE;
75 private static final String
DATA_PREFIX =
"main_file_cache.dat";
112 Path root = Paths.get(directory);
113 Preconditions.checkArgument(Files.isDirectory(root),
"Supplied path must be a directory! " + root);
116 Preconditions.checkArgument(Files.exists(data),
"No data file found in the specified path!");
118 SeekableByteChannel dataChannel = Files.newByteChannel(data, READ, WRITE);
123 for (
int index = 0; index <
caches.length; index++) {
125 if (Files.exists(path)) {
126 SeekableByteChannel indexChannel = Files.newByteChannel(path, READ, WRITE);
127 caches[index] =
new Cache(dataChannel, indexChannel, index);
132 for (
int id = 1;
id <
archives.length;
id++) {
149 Preconditions.checkElementIndex(
id,
archives.length);
150 return Objects.requireNonNull(
archives[
id]);
162 Preconditions.checkElementIndex(
id,
caches.length);
163 return Objects.requireNonNull(
caches[
id]);
175 public ByteBuffer
getFile(
int cacheId,
int indexId)
throws IOException {
177 synchronized (
cache) {
178 return cache.get(indexId);
190 synchronized (
this) {
198 CRC32 crc32 =
new CRC32();
199 for (
int file = 1; file < crcs.length; file++) {
203 crc32.update(buffer);
205 crcs[file] = (int) crc32.getValue();
208 ByteBuffer buffer = ByteBuffer.allocate((crcs.length + 1) * Integer.BYTES);
211 for (
int crc : crcs) {
212 hash = (hash << 1) + crc;
219 synchronized (
this) {
Represents a Sector and Index cache.
static final int MEDIA_ARCHIVE
Represents the id of the media and sprite archive.
static final int ANIMATION_INDEX
Represents the id of the animations cache.
static final int CONFIG_INDEX
Represents the id of the configurations cache.
static final int MIDI_INDEX
Represents the id of the sounds and music cache.
ByteBuffer getFile(int cacheId, int indexId)
Returns a ByteBuffer of file data for the specified index within the specified Cache.
Archive getArchive(int id)
Gets an Archive for the specified id, this method fails-fast if no archive can be found.
ByteBuffer archiveHashes
The cached archive hashes.
static final int MAXIMUM_INDICES
Represents the maximum amount of indices within this file system.
static final int MODEL_INDEX
Represents the id of the model cache.
Cache getCache(int id)
Gets a Cache for the specified id, this method fails-fast if no cache can be found.
static final int SOUND_ARCHIVE
Represents the id of the sound and music archive.
static final int WORD_ARCHIVE
Represents the id of the word archive - user for storing profane or illegal words not allowed to be s...
ByteBuffer getArchiveHashes()
Returns the cached archiveHashes if they exist, otherwise they are calculated and cached for future u...
FileSystem(Cache[] caches, Archive[] archives)
Constructs a new FileSystem with the specified Caches and Archives.
static final int MANIFEST_ARCHIVE
Represents the id of the manifest archive.
static final int TITLE_ARCHIVE
Represents the id of the title screen archive.
static final String INDEX_PREFIX
Represents the prefix of this FileSystems index files.
static final int MAXIMUM_ARCHIVES
Represents the maximum amount of archives within this file system.
static final int TEXTURES_ARCHIVE
Represents the id of the textures archive.
final Archive[] archives
All of the Archives within this FileSystem.
static final int CONFIG_ARCHIVE
Represents the id of the configurations archive.
static final int MAP_INDEX
Represents the id of the tool.mapviewer and landscape cache.
static final String DATA_PREFIX
Represents the prefix of this FileSystems main cache files.
final Cache[] caches
All of the Caches within this FileSystem.
static final int INTERFACE_ARCHIVE
Represents the id of the interface archive.
static FileSystem create(String directory)
Constructs and initializes a FileSystem from the specified directory.
Represents an archive within the Cache.
static Archive decode(ByteBuffer data)
Decodes the data within this Archive.