Skip to content

Commit

Permalink
cache: fix archive ids being written out of order
Browse files Browse the repository at this point in the history
  • Loading branch information
Adam- committed Nov 6, 2024
1 parent 95d4208 commit db5bdc2
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 7 deletions.
47 changes: 41 additions & 6 deletions cache/src/main/java/net/runelite/cache/fs/Index.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
package net.runelite.cache.fs;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import net.runelite.cache.index.ArchiveData;
Expand Down Expand Up @@ -147,26 +148,60 @@ public void setCompression(int compression)

public List<Archive> getArchives()
{
return archives;
return Collections.unmodifiableList(archives);
}

public Archive addArchive(int id)
{
int idx = findArchiveIndex(id);
if (idx >= 0)
{
throw new IllegalArgumentException("archive " + id + " already exists");
}

idx = -idx - 1;
Archive archive = new Archive(this, id);
this.archives.add(archive);
this.archives.add(idx, archive);
return archive;
}

public Archive getArchive(int id)
{
for (Archive a : archives)
int idx = findArchiveIndex(id);
if (idx < 0)
{
if (a.getArchiveId() == id)
return null;
}

return archives.get(idx);
}

private int findArchiveIndex(int id)
{
int low = 0;
int high = archives.size() - 1;

while (low <= high)
{
int mid = (low + high) >>> 1;

Archive a = archives.get(mid);
int cmp = Integer.compare(a.getArchiveId(), id);
if (cmp < 0)
{
return a;
low = mid + 1;
}
else if (cmp > 0)
{
high = mid - 1;
}
else
{
return mid;
}
}
return null;

return -(low + 1);
}

public boolean removeArchive(Archive archive)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,6 @@ public void save(Store store) throws IOException
br.printf("crc=%d\n", idx.getCrc());
br.printf("named=%b\n", idx.isNamed());

idx.getArchives().sort(Comparator.comparingInt(Archive::getArchiveId));
for (Archive archive : idx.getArchives())
{
br.printf("id=%d\n", archive.getArchiveId());
Expand Down
2 changes: 2 additions & 0 deletions cache/src/main/java/net/runelite/cache/index/IndexData.java
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ public byte[] writeIndexData()
if (i != 0)
{
ArchiveData prev = this.archives[i - 1];
assert a.id > prev.id : "archive ids out of order";
archive -= prev.getId();
}

Expand Down Expand Up @@ -224,6 +225,7 @@ public byte[] writeIndexData()
if (j != 0)
{
FileData prev = a.getFiles()[j - 1];
assert file.id > prev.id : "file ids out of order";
offset -= prev.getId();
}

Expand Down
3 changes: 3 additions & 0 deletions cache/src/test/java/net/runelite/cache/fs/StoreTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -121,20 +121,23 @@ public void testMultipleArchives() throws IOException
{
FileData[] fileData = archive.getFileData();
FileData file = fileData[i] = new FileData();
file.setId(i);
file.setNameHash(random.nextInt(Integer.MAX_VALUE));
}

for (int i = 0; i < NUMBER_OF_FILES; ++i)
{
FileData[] fileData = archive2.getFileData();
FileData file = fileData[i] = new FileData();
file.setId(i);
file.setNameHash(random.nextInt(Integer.MAX_VALUE));
}

for (int i = 0; i < NUMBER_OF_FILES; ++i)
{
FileData[] fileData = archive3.getFileData();
FileData file = fileData[i] = new FileData();
file.setId(i);
file.setNameHash(random.nextInt(Integer.MAX_VALUE));
}

Expand Down

0 comments on commit db5bdc2

Please sign in to comment.