1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
use std::collections::{
    hash_map::{self, Entry},
    HashMap,
};

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

use crate::{
    definition::osrs::{
        Definition, FetchDefinition, ItemDefinition, LocationDefinition, MapDefinition,
        NpcDefinition, ObjectDefinition,
    },
    Cache,
};

/// Loads all item definitions from the current cache.
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Clone, Eq, PartialEq, Debug, Default)]
pub struct ItemLoader(HashMap<u16, ItemDefinition>);

impl_osrs_loader!(ItemLoader, ItemDefinition, index_id: 2, archive_id: 10);

/// Loads all npc definitions from the current cache.
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Clone, Eq, PartialEq, Debug, Default)]
pub struct NpcLoader(HashMap<u16, NpcDefinition>);

impl_osrs_loader!(NpcLoader, NpcDefinition, index_id: 2, archive_id: 9);

/// Loads all object definitions from the current cache.
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Clone, Eq, PartialEq, Debug, Default)]
pub struct ObjectLoader(HashMap<u16, ObjectDefinition>);

impl_osrs_loader!(ObjectLoader, ObjectDefinition, index_id: 2, archive_id: 6);

/// Loads maps definitions lazily from the current cache.
#[derive(Debug)]
pub struct MapLoader<'cache> {
    cache: &'cache Cache,
    maps: HashMap<u16, MapDefinition>,
}

impl<'cache> MapLoader<'cache> {
    /// Make a new `MapLoader`.
    ///
    /// This takes a `Cache` by references with a `'cache` lifetime.
    /// All the map definitions are loaded lazily where the `&'cache Cache` is used
    /// to cache them internally on load.
    pub fn new(cache: &'cache Cache) -> Self {
        Self {
            cache,
            maps: HashMap::new(),
        }
    }

    pub fn load(&mut self, id: u16) -> crate::Result<&MapDefinition> {
        if let Entry::Vacant(entry) = self.maps.entry(id) {
            let x = id >> 8;
            let y = id & 0xFF;

            let map_archive = self.cache.archive_by_name(5, format!("m{}_{}", x, y))?;
            let buffer = self.cache.read_archive(map_archive)?.decode()?;

            entry.insert(MapDefinition::new(id, &buffer)?);
        }

        Ok(&self.maps[&id])
    }
}

/// Loads location definitions lazily from the current cache.
#[derive(Debug)]
pub struct LocationLoader<'cache> {
    cache: &'cache Cache,
    locations: HashMap<u16, LocationDefinition>,
}

impl<'cache> LocationLoader<'cache> {
    /// Make a new `LocationLoader`.
    ///
    /// This takes a `Cache` by references with a `'cache` lifetime.
    /// All the location definitions are loaded lazily where the `&'cache Cache` is used
    /// to cache them internally on load.
    pub fn new(cache: &'cache Cache) -> Self {
        Self {
            cache,
            locations: HashMap::new(),
        }
    }

    /// Loads the location data for a particular region.
    ///
    /// Also takes a `keys: [u32; 4]` because the location archive is encrypted
    /// with XTEA. The buffer is automatically decoded with the given keys.
    pub fn load(&mut self, id: u16, keys: &[u32; 4]) -> crate::Result<&LocationDefinition> {
        if let Entry::Vacant(entry) = self.locations.entry(id) {
            let x = id >> 8;
            let y = id & 0xFF;

            let loc_archive = self.cache.archive_by_name(5, format!("l{}_{}", x, y))?;
            let buffer = self
                .cache
                .read_archive(loc_archive)?
                .with_xtea_keys(*keys)
                .decode()?;

            entry.insert(LocationDefinition::new(id, &buffer)?);
        }

        Ok(&self.locations[&id])
    }
}

#[cfg(test)]
mod items {
    use super::ItemLoader;
    use crate::test_util;

    fn item_loader() -> crate::Result<ItemLoader> {
        ItemLoader::new(&test_util::osrs_cache()?)
    }

    #[test]
    fn blue_partyhat() -> crate::Result<()> {
        let item_loader = item_loader()?;
        let item = item_loader.load(1042).unwrap();

        assert_eq!(item.name, "Blue partyhat");
        assert!(!item.stackable);
        assert!(!item.members_only);

        Ok(())
    }

    #[test]
    fn magic_logs() -> crate::Result<()> {
        let item_loader = item_loader()?;
        let item = item_loader.load(1513).unwrap();

        assert_eq!(item.name, "Magic logs");
        assert!(!item.stackable);
        assert!(item.members_only);

        Ok(())
    }

    #[test]
    fn noted() -> crate::Result<()> {
        let item_loader = item_loader()?;
        let item = item_loader.load(1512).unwrap();

        assert!(item.stackable);
        assert!(!item.members_only);

        Ok(())
    }

    #[test]
    fn non_existent() -> crate::Result<()> {
        let item_loader = item_loader()?;

        assert!(item_loader.load(65_535).is_none());

        Ok(())
    }
}

#[cfg(test)]
mod npcs {
    use super::NpcLoader;
    use crate::test_util;

    fn npc_loader() -> crate::Result<NpcLoader> {
        NpcLoader::new(&test_util::osrs_cache()?)
    }

    #[test]
    fn woodsman_tutor() -> crate::Result<()> {
        let npc_loader = npc_loader()?;
        let npc = npc_loader.load(3226).unwrap();
        
        assert_eq!(npc.name, "Woodsman tutor");
        assert!(npc.interactable);
        
        Ok(())
    }
    
    #[test]
    fn last_valid_npc() -> crate::Result<()> {
        let npc_loader = npc_loader()?;
        let npc = npc_loader.load(8691).unwrap();
        
        assert_eq!(npc.name, "Mosol Rei");
        assert!(npc.interactable);
        
        Ok(())
    }
    
    #[test]
    fn non_existent() -> crate::Result<()> {
        let npc_loader = npc_loader()?;
        
        assert!(npc_loader.load(65_535).is_none());
        
        Ok(())
    }
}

#[cfg(test)]
mod objects {
    use super::ObjectLoader;
    use crate::test_util;

    fn obj_loader() -> crate::Result<ObjectLoader> {
        ObjectLoader::new(&test_util::osrs_cache()?)
    }

    #[test]
    fn law_rift() -> crate::Result<()> {
        let obj_loader = obj_loader()?;
        let obj = obj_loader.load(25034).unwrap();
        
        assert_eq!(obj.name, "Law rift");
        assert_eq!(obj.animation_id, 2178);
        assert!(obj.solid);
        assert!(!obj.obstruct_ground);
        
        Ok(())
    }
    
    #[test]
    fn furnace() -> crate::Result<()> {
        let obj_loader = obj_loader()?;
        let obj = obj_loader.load(2030).unwrap();
        
        assert_eq!(obj.name, "Furnace");
        assert!(obj.solid);
        assert!(!obj.obstruct_ground);
        
        Ok(())
    }
    
    #[test]
    fn bank_table() -> crate::Result<()> {
        let obj_loader = obj_loader()?;
        let obj = obj_loader.load(590).unwrap();
        
        assert_eq!(obj.name, "Bank table");
        assert_eq!(obj.supports_items, Some(1));
        assert!(obj.solid);
        assert!(!obj.obstruct_ground);
        
        Ok(())
    }
    
    #[test]
    fn dungeon_door() -> crate::Result<()> {
        let obj_loader = obj_loader()?;
        let obj = obj_loader.load(1725).unwrap();
        
        assert_eq!(obj.name, "Dungeon door");
        assert_eq!(obj.wall_or_door, Some(1));
        assert_eq!(obj.supports_items, Some(0));
        assert!(obj.solid);
        assert!(!obj.obstruct_ground);
        
        Ok(())
    }
}

#[cfg(test)]
mod locations {
    use super::LocationLoader;
    use crate::test_util;

    #[test]
    fn lumbridge() -> crate::Result<()> {
        let cache = test_util::osrs_cache()?;
        
        let keys: [u32; 4] = [3030157619, 2364842415, 3297319647, 1973582566];
        
        let mut location_loader = LocationLoader::new(&cache);
        let location_def = location_loader.load(12850, &keys)?;
        
        assert_eq!(location_def.region_x, 50);
        assert_eq!(location_def.region_y, 50);
        assert_eq!(location_def.region_base_coords(), (3200, 3200));
        assert_eq!(location_def.data.len(), 4730);
        
        Ok(())
    }
}

#[cfg(test)]
mod maps {
    use super::MapLoader;
    use crate::test_util;

    #[test]
    fn lumbridge() -> crate::Result<()> {
        let cache = test_util::osrs_cache()?;

        let mut map_loader = MapLoader::new(&cache);
        let map_def = map_loader.load(12850).unwrap();

        assert_eq!(map_def.region_x, 50);
        assert_eq!(map_def.region_y, 50);
        assert_eq!(map_def.region_base_coords(), (3200, 3200));

        Ok(())
    }
}