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
use std::marker::PhantomData;

use me3_framework::scripting::lua::{LuaSerdeExt, UserData};
use me3_game_support_fromsoft::sprj::{ParamFileDescriptor, SprjGame};
use serde::Serialize;

pub struct LuaParamAccessor<T: ParamFileDescriptor> {
    _phantom: PhantomData<T>,
    game: &'static dyn SprjGame,
}

impl<T: ParamFileDescriptor> LuaParamAccessor<T> {
    pub fn new(game: &'static dyn SprjGame) -> Self {
        Self {
            game,
            _phantom: PhantomData::default(),
        }
    }
}

impl<T: ParamFileDescriptor> UserData for LuaParamAccessor<T>
where
    T::Row: Clone + Serialize,
{
    fn add_methods<'lua, M: me3_framework::scripting::lua::UserDataMethods<'lua, Self>>(
        methods: &mut M,
    ) {
        methods.add_method("get_row", |lua, this, id: i32| {
            let params = this.game.param_repository();

            Ok(lua.to_value(&params.get_row::<T>(id).cloned()))
        });
    }
}