DefProvider

Struct DefProvider 

Source
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§

Source§

impl<T: DefParser> DefProvider<T>

Source

pub fn with(cache: &Arc<Mutex<Cache>>, index: u32) -> Self

Source

pub fn get_def( &mut self, archive: &dyn ContainerIdProvider, file: &dyn ContainerIdProvider, id: u32, ) -> &T

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.