pub struct DefProvider<T> {
pub file_provider: FileProvider,
pub index: u32,
pub parser: Option<fn(DataBuffer) -> T>,
/* private fields */
}
Expand description
The DefProvider
is going to be what you’ll primarily use to implement definition decoders and things along those lines.
You will use the [DefProvider::with(cache, index)
] method to construct a definition provider, along with your definition type.
Let’s say, for example, we had the below definition:
ⓘ
#[derive(Default)]
struct DummyDefinition {
dummy_int: u32,
dummy_str: String
}
Which resides in index 1. You would implement your decoder:
ⓘ
impl DefParser for DummyDefinition {
fn parse_buff(buffer: DataBuffer) -> Self {
let mut def = DummyDefinition::default();
let opcode: u8;
loop {
opcode = buffer.read_u8();
match opcode {
0 -> break,
1 -> def.dummy_int = buffer.read_u32(),
2 -> def.dummy_str = buffer.read_ntstr()
}
}
return def;
}
}
You would then create a definition provider like so:
ⓘ
use idx::*;
use idx::util::*;
use std::sync::{Arc,Mutex};
let cache = Arc::from(Mutex::from(Cache::from_path("test_cache")));
let dummy_def_provider = DefProvider::<DummyDefinition>::with(cache, 1);
let definition = dummy_def_provider.get_def(&3, &1); //returns the parsed definition from file 1 of archive 3.
It is additionally recommended to make some additional trait that can turn, for example, and item ID into the appropriate archive and file IDs
I would also recommend using ContainerIdProvider
as the type to be passed for the ID, as it can accept both u32 and String. But this is up to you.
ⓘ
pub trait IdFetch {
type DefType;
fn for_id(id: u32) -> &Self::DefType;
}
impl IdFetch for DefProvider<DummyDefinition> {
type DefType = DummyDefinition;
fn for_id(id: u32) -> &DummyDefinition {
let archive = id >> 8;
let file = id & 0xff;
return self.get_def(&archive, &file, id);
}
}
Fields§
§file_provider: FileProvider
§index: u32
§parser: Option<fn(DataBuffer) -> T>
Implementations§
Auto Trait Implementations§
impl<T> Freeze for DefProvider<T>
impl<T> RefUnwindSafe for DefProvider<T>where
T: RefUnwindSafe,
impl<T> Send for DefProvider<T>where
T: Send,
impl<T> Sync for DefProvider<T>where
T: Sync,
impl<T> Unpin for DefProvider<T>where
T: Unpin,
impl<T> UnwindSafe for DefProvider<T>where
T: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more