#runescape #osrs #huffman-coding #filesystem #rs3

rs-cache

A read-only, high-level, virtual file API for the RuneScape cache

36 releases

0.8.6 Jul 11, 2023
0.8.5 Nov 7, 2022
0.8.4 Jun 12, 2022
0.8.2 Feb 5, 2022
0.1.1 Feb 28, 2020

#25 in Games

MIT license

120KB
2.5K SLoC

rs-cache

Build API Crate dependency status OSRS Version RS3 Version

A read-only, high-level, virtual file API for the RuneScape cache.

This crate provides high performant data reads into the Oldschool RuneScape and RuneScape 3 cache file systems. It can read the necessary data to synchronize the client's cache with the server. There are also some loaders that give access to definitions from the cache such as items or npcs.

For read-heavy workloads, a writer can be used to prevent continuous buffer allocations. By default every read will allocate a writer with the correct capacity.

RuneScape’s chat system uses Huffman encoding to compress messages; this library contains a huffman implementation to decompress these messages.

When a RuneScape client sends game packets the id's are encoded and can be decoded with the IsaacRand implementation. These id's are encoded by the client in a predictable random order which can be reversed if the server has its own IsaacRand with the same encoder/decoder keys. These keys are sent by the client on login and are user specific. It will only send encoded packet id's if the packets are game packets.

Note that this crate is still evolving; both OSRS & RS3 are not fully supported/implemented and will probably contain bugs or miss core features. If you require features or find bugs consider opening an issue.

Useful links:
        Releases
        Documentation
        Examples

Safety

In order to read bytes in a high performant way the cache uses memmap2. This can be unsafe because of its potential for Undefined Behaviour when the underlying file is subsequently modified, in or out of process. Using Mmap here is safe because the RuneScape cache is a read-only binary file system. The map will remain valid even after the File is dropped, it's completely independent of the File used to create it. Therefore, the use of unsafe is not propagated outwards. When the Cache is dropped memory will be subsequently unmapped.

Features

The cache's protocol defaults to OSRS. In order to use the RS3 protocol you can enable the rs3 feature flag. A lot of types derive serde's Serialize and Deserialize. The serde feature flag can be used to enable (de)serialization on any compatible types.

Quick Start

For an instance that stays local to this thread you can simply use:

use rscache::Cache;

let cache = Cache::new("./data/osrs_cache").unwrap();

let index_id = 2; // Config index.
let archive_id = 10; // Archive containing item definitions.

let buffer = cache.read(index_id, archive_id).unwrap();

If you want to share the instance over multiple threads you can do so by wrapping it in an Arc

use rscache::Cache;
use std::sync::Arc;

let cache = Arc::new(Cache::new("./data/osrs_cache").unwrap());

let c = Arc::clone(&cache);
std::thread::spawn(move || {
    c.read(0, 10).unwrap();
});

std::thread::spawn(move || {
    cache.read(0, 10).unwrap();
});

The recommended usage would be to wrap it using once_cell making it the easiest way to access cache data from anywhere and at any time. No need for an Arc or a Mutex because Cache will always be Send & Sync.

use rscache::Cache;
use once_cell::sync::Lazy;

static CACHE: Lazy<Cache> = Lazy::new(|| {
    Cache::new("./data/osrs_cache").unwrap()
});

std::thread::spawn(move || {
    CACHE.read(0, 10).unwrap();
});

std::thread::spawn(move || {
    CACHE.read(0, 10).unwrap();
});

Integration tests are running on Oldschool RuneScape version 180, which you can run at any time because the cache is included in the ./data/osrs_cache directory. RS3 Integration tests are running on version 904. The RS3 cache is too large to include on GitHub.

This crate is marked as experimental. I will implement additional features once I need them for my own project. If you require a certain feature feel free to open an issue.

Usage

Add this to your Cargo.toml:

[dependencies]
rs-cache = "0.8.6"

Examples can be found in the examples directory which include both update protocols.

Acknowledgements

The following sources aided with the development of this crate:
        OpenRS
        RuneLite
        OSRS Cache Parsing Blog
        RSMod
        Librsfs
        OSRSBox
        Jagex-Store-5
        Matrix 876

License

rs-cache is distributed under the terms of the MIT license.

See LICENSE for details.

Dependencies

~2.6–4MB
~71K SLoC