RuneHive-Tarnish
Neural OSRS Enhancement Framework
Loading...
Searching...
No Matches
Archive.java
1package com.osroyale.fs.cache.archive;
2
3import com.google.common.base.Preconditions;
4import com.osroyale.fs.cache.Cache;
5import com.osroyale.fs.util.ByteBufferUtil;
6import com.osroyale.fs.util.CompressionUtil;
7import com.osroyale.util.StringUtils;
8import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
9import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
10
11import java.io.IOException;
12import java.nio.ByteBuffer;
13import java.util.Map;
14import java.util.Optional;
15
16import static com.osroyale.fs.cache.Cache.INDEX_SIZE;
17
58
59public final class Archive {
60
62 private final Int2ObjectMap<ArchiveSector> sectors;
63
70 private Archive(Int2ObjectMap<ArchiveSector> sectors) {
71 this.sectors = sectors;
72 }
73
82 public static Archive decode(ByteBuffer data) throws IOException {
83 int length = ByteBufferUtil.getMedium(data);
84 int compressedLength = ByteBufferUtil.getMedium(data);
85
86 byte[] uncompressedData = data.array();
87
88 if (compressedLength != length) {
89 uncompressedData = CompressionUtil.unbzip2Headerless(data.array(), INDEX_SIZE, compressedLength);
90 data = ByteBuffer.wrap(uncompressedData);
91 }
92
93 int total = data.getShort() & 0xFF;
94 int offset = data.position() + total * 10;
95
96 Int2ObjectMap<ArchiveSector> sectors = new Int2ObjectOpenHashMap<>(total);
97 for (int i = 0; i < total; i++) {
98 int hash = data.getInt();
99 length = ByteBufferUtil.getMedium(data);
100 compressedLength = ByteBufferUtil.getMedium(data);
101
102 byte[] sectorData = new byte[length];
103
104 if (length != compressedLength) {
105 sectorData = CompressionUtil.unbzip2Headerless(uncompressedData, offset, compressedLength);
106 } else {
107 System.arraycopy(uncompressedData, offset, sectorData, 0, length);
108 }
109
110 sectors.put(hash, new ArchiveSector(ByteBuffer.wrap(sectorData), hash));
111 offset += compressedLength;
112 }
113
114 return new Archive(sectors);
115 }
116
123 private Optional<ArchiveSector> getSector(int hash) {
124 return Optional.ofNullable(sectors.get(hash));
125 }
126
133 private Optional<ArchiveSector> getSector(String name) {
134 int hash = StringUtils.hashArchive(name);
135 return getSector(hash);
136 }
137
147 public ByteBuffer getData(String name) {
148 Optional<ArchiveSector> optionalData = getSector(name);
149 Preconditions.checkArgument(optionalData.isPresent());
150 ArchiveSector dataSector = optionalData.get();
151 return dataSector.getData();
152 }
153
154}
static Archive decode(ByteBuffer data)
Definition Archive.java:82
ByteBuffer getData(String name)
Definition Archive.java:147
static byte[] unbzip2Headerless(byte[] data, int offset, int length)