move print to noita api crate and add some more functions

This commit is contained in:
bgkillas 2025-06-25 16:01:57 -04:00
parent bda32fe65a
commit aa1f244a2f
4 changed files with 27 additions and 22 deletions

View file

@ -709,6 +709,16 @@ impl ComponentID {
pub fn get_type(self) -> eyre::Result<Cow<'static, str>> {
raw::component_get_type_name(self)
}
pub fn is_enabled(self) -> eyre::Result<bool> {
raw::component_get_is_enabled(self)
}
pub fn get_tags(self) -> eyre::Result<Cow<'static, str>> {
match raw::component_get_tags(self) {
Ok(Some(s)) => Ok(s),
Ok(None) => Err(eyre!("no string found")),
Err(s) => Err(s),
}
}
}
impl StatusEffectDataComponent {
@ -729,6 +739,10 @@ pub fn game_print(value: impl AsRef<str>) {
let _ = raw::game_print(value.as_ref().into());
}
pub fn print(value: impl AsRef<str>) {
let _ = raw::print(value.as_ref());
}
pub mod raw {
use eyre::Context;
use eyre::eyre;
@ -744,6 +758,14 @@ pub mod raw {
noita_api_macro::generate_api!();
pub(crate) fn print(value: &str) -> eyre::Result<()> {
let lua = LuaState::current()?;
lua.get_global(c"print");
lua.push_string(value);
lua.call(1, 0)
.wrap_err("Failed to call ComponentGetValue2")?;
Ok(())
}
pub(crate) fn component_get_value<T>(component: ComponentID, field: &str) -> eyre::Result<T>
where
T: LuaGetValue,
@ -1192,7 +1214,7 @@ struct ComponentData {
}
impl ComponentData {
fn new(id: ComponentID, is_var: bool) -> Self {
let enabled = raw::component_get_is_enabled(id).unwrap_or_default();
let enabled = id.is_enabled().unwrap_or_default();
let name = if is_var {
VarName::from_str_non_const(
&VariableStorageComponent::from(id)
@ -1202,12 +1224,7 @@ impl ComponentData {
} else {
VarName::None
};
let ent_tags = format!(
",{},",
raw::component_get_tags(id)
.unwrap_or_default()
.unwrap_or_default(),
);
let ent_tags = format!(",{},", id.get_tags().unwrap_or_default(),);
let mut tags = [false; COMP_TAG_LEN];
macro_rules! push_tag {
($($e: expr),*) => {
@ -1439,10 +1456,10 @@ impl EntityManager {
for com in vec.into_iter() {
if tags == ComponentTag::None || com.tags[tags as usize] {
is_some = true;
raw::entity_remove_component(self.current_entity, com.id)?;
self.current_entity.remove_component(com.id)?;
} else {
self.current_data.components
[const { CachedComponent::from_component::<C>() as usize }].push(com)
[const { CachedComponent::from_component::<C>() as usize }].push(com);
}
}
Ok(is_some)

View file

@ -447,15 +447,6 @@ pub(crate) fn print_error(error: eyre::Report) -> eyre::Result<()> {
Ok(())
}
pub(crate) fn print(string: &str) -> eyre::Result<()> {
let lua = LuaState::current()?;
lua.get_global(c"EwextPrint");
lua.push_string(string);
lua.call(1, 0i32)
.wrap_err("Failed to call EwextPrintError")?;
Ok(())
}
/// # Safety
///
/// Only gets called by lua when loading a module.

View file

@ -828,7 +828,7 @@ impl Module for EntitySync {
}
if self.log_performance {
times.push(start.elapsed().as_micros() - times.iter().sum::<u128>());
crate::print(&format!("{:?}", times))?;
noita_api::print(format!("{:?}", times));
}
Ok(())
}

View file

@ -11,9 +11,6 @@ local module = {}
EwextSerialize = util.serialize_entity
EwextDeserialize = util.deserialize_entity
EwextPrintError = util.print_error
function EwextPrint(s)
print(s)
end
function EwextAddInitLuaComponent(entity, file)
return EntityAddComponent2(entity, "LuaComponent", {
script_source_file = file,