From 22aaa9fb183282e29c4031c8a2c2902dcba996ad Mon Sep 17 00:00:00 2001 From: IQuant Date: Mon, 25 Nov 2024 19:57:18 +0300 Subject: [PATCH] Support for multiple return values --- ewext/noita_api/src/lua.rs | 110 +++++++++++++++++++++++++++++++ ewext/noita_api_macro/src/lib.rs | 14 ++-- ewext/src/lib.rs | 10 ++- 3 files changed, 126 insertions(+), 8 deletions(-) diff --git a/ewext/noita_api/src/lua.rs b/ewext/noita_api/src/lua.rs index c9e1bd82..d1e694db 100644 --- a/ewext/noita_api/src/lua.rs +++ b/ewext/noita_api/src/lua.rs @@ -336,3 +336,113 @@ impl LuaGetValue for Color { todo!() } } + +impl LuaGetValue for (T0, T1) { + fn get(lua: LuaState, index: i32) -> eyre::Result + where + Self: Sized, + { + Ok((T0::get(lua, index - T1::size())?, T1::get(lua, index)?)) + } + + fn size() -> i32 { + T0::size() + T1::size() + } +} + +impl LuaGetValue for (T0, T1, T2) { + fn get(lua: LuaState, index: i32) -> eyre::Result + where + Self: Sized, + { + Ok(( + T0::get(lua, index - T1::size() - T2::size())?, + T1::get(lua, index - T2::size())?, + T2::get(lua, index)?, + )) + } + + fn size() -> i32 { + T0::size() + T1::size() + T2::size() + } +} + +impl LuaGetValue + for (T0, T1, T2, T3) +{ + fn get(lua: LuaState, index: i32) -> eyre::Result + where + Self: Sized, + { + Ok(( + T0::get(lua, index - T1::size() - T2::size() - T3::size())?, + T1::get(lua, index - T2::size() - T3::size())?, + T2::get(lua, index - T3::size())?, + T3::get(lua, index)?, + )) + } + + fn size() -> i32 { + T0::size() + T1::size() + T2::size() + T3::size() + } +} + +impl + LuaGetValue for (T0, T1, T2, T3, T4) +{ + fn get(lua: LuaState, index: i32) -> eyre::Result + where + Self: Sized, + { + let prev = <(T0, T1, T2, T3)>::get(lua, index - T4::size())?; + Ok((prev.0, prev.1, prev.2, prev.3, T4::get(lua, index)?)) + } + + fn size() -> i32 { + <(T0, T1, T2, T3)>::size() + T4::size() + } +} + +impl< + T0: LuaGetValue, + T1: LuaGetValue, + T2: LuaGetValue, + T3: LuaGetValue, + T4: LuaGetValue, + T5: LuaGetValue, + > LuaGetValue for (T0, T1, T2, T3, T4, T5) +{ + fn get(lua: LuaState, index: i32) -> eyre::Result + where + Self: Sized, + { + let prev = <(T0, T1, T2, T3, T4)>::get(lua, index - T5::size())?; + Ok((prev.0, prev.1, prev.2, prev.3, prev.4, T5::get(lua, index)?)) + } + + fn size() -> i32 { + <(T0, T1, T2, T3, T4)>::size() + T5::size() + } +} + +impl LuaGetValue for (bool, bool, bool, f64, f64, f64, f64, f64, f64, f64, f64) { + fn get(lua: LuaState, index: i32) -> eyre::Result { + Ok(( + bool::get(lua, index - 10)?, + bool::get(lua, index - 9)?, + bool::get(lua, index - 8)?, + f64::get(lua, index - 7)?, + f64::get(lua, index - 6)?, + f64::get(lua, index - 5)?, + f64::get(lua, index - 4)?, + f64::get(lua, index - 3)?, + f64::get(lua, index - 2)?, + f64::get(lua, index - 1)?, + f64::get(lua, index)?, + )) + } + + fn size() -> i32 { + 11 + } +} diff --git a/ewext/noita_api_macro/src/lib.rs b/ewext/noita_api_macro/src/lib.rs index 5a65c503..d0e9cd75 100644 --- a/ewext/noita_api_macro/src/lib.rs +++ b/ewext/noita_api_macro/src/lib.rs @@ -226,13 +226,13 @@ fn generate_code_for_api_fn(api_fn: ApiFn) -> proc_macro2::TokenStream { let ret_type = if api_fn.rets.is_empty() { quote! { () } } else { - // TODO support for more than one return value. - // if api_fn.rets.len() == 1 { - let ret = api_fn.rets.first().unwrap(); - ret.typ.as_rust_type_return() - // } else { - // quote! { ( /* todo */) } - // } + if api_fn.rets.len() == 1 { + let ret = api_fn.rets.first().unwrap(); + ret.typ.as_rust_type_return() + } else { + let ret_types = api_fn.rets.iter().map(|ret| ret.typ.as_rust_type_return()); + quote! { ( #(#ret_types),* ) } + } }; let fn_name_c = name_to_c_literal(api_fn.fn_name); diff --git a/ewext/src/lib.rs b/ewext/src/lib.rs index ceb635c3..c4491976 100644 --- a/ewext/src/lib.rs +++ b/ewext/src/lib.rs @@ -122,8 +122,16 @@ fn test_fn(_lua: LuaState) -> eyre::Result<()> { ); let hp = damage_model.hp()?; + let transform = noita_api::raw::entity_get_transform(player)?; + noita_api::raw::game_print( - format!("Component: {:?}, Hp: {}", damage_model.0, hp * 25.0).into(), + format!( + "Component: {:?}, Hp: {}, tranform: {:?}", + damage_model.0, + hp * 25.0, + transform + ) + .into(), )?; // noita::api::raw::entity_set_transform(player, 0.0, 0.0, 0.0, 1.0, 1.0)?;