various fixes

- only generate at most 256 variants for an enum
- only generate methods trait for classes, also depend on AsUObject trait
- declare func variable in methods as mut
- swap out params
This commit is contained in:
Janis 2023-04-22 01:01:00 +02:00
parent 3f16ac51c6
commit f2366991f1

View file

@ -1,4 +1,7 @@
use std::io::{BufWriter, Write}; use std::{
cell::UnsafeCell,
io::{BufWriter, Write},
};
use anyhow::Context; use anyhow::Context;
@ -137,7 +140,7 @@ pub fn generate_enum<W: Write>(enm: &Enum, _sdk: &Sdk, w: &mut W) -> anyhow::Res
)?; )?;
writeln!(w, "pub enum {name} {{")?; writeln!(w, "pub enum {name} {{")?;
for (i, value) in values.iter().enumerate() { for (i, value) in values.iter().enumerate().take(256) {
writeln!(w, "\t{value} = {i},")?; writeln!(w, "\t{value} = {i},")?;
} }
writeln!(w, "}}")?; writeln!(w, "}}")?;
@ -260,6 +263,7 @@ pub fn generate_class_impl<W: Write>(class: &Class, sdk: &Sdk, w: &mut W) -> any
super_class, super_class,
fields, fields,
methods, methods,
is_class,
.. ..
} = class; } = class;
@ -295,26 +299,30 @@ pub fn generate_class_impl<W: Write>(class: &Class, sdk: &Sdk, w: &mut W) -> any
writeln!(w, "}}")?; writeln!(w, "}}")?;
writeln!(w, "impl {name}Fields for {name} {{}}")?; writeln!(w, "impl {name}Fields for {name} {{}}")?;
for method in methods { if *is_class {
generate_method_params(class, method, sdk, w)?; for method in methods {
} generate_method_params(class, method, sdk, w)?;
}
writeln!(w, "pub trait {}Methods: AsPtr {{", name)?; writeln!(w, "pub trait {}Methods: AsUObject {{", name)?;
for method in methods { for method in methods {
generate_method(class, method, sdk, w)?; generate_method(class, method, sdk, w)?;
}
writeln!(w, "}}")?;
writeln!(w, "impl {name}Methods for {name} {{}}")?;
} }
writeln!(w, "}}")?;
writeln!(w, "impl {name}Methods for {name} {{}}")?;
if let Some(supr) = super_class { if let Some(supr) = super_class {
let iter = core::iter::once(*supr).chain(supr.iter_super_structs()); let iter = core::iter::once(*supr).chain(supr.iter_super_structs());
for parent in iter { for parent in iter {
if let Some(parent) = sdk.find_type(parent.as_uobject()) { if let Some(parent) = sdk.find_type(parent.as_uobject()) {
match parent { match parent {
Types::Class(class) => { Types::Class(super_class) => {
writeln!(w, "impl {}Methods for {name} {{}}", class.rust_name())?; writeln!(w, "impl {}Fields for {name} {{}}", super_class.rust_name())?;
writeln!(w, "impl {}Fields for {name} {{}}", class.rust_name())?; if *is_class {
writeln!(w, "impl {}Methods for {name} {{}}", super_class.rust_name())?;
}
} }
_ => {} _ => {}
} }
@ -381,7 +389,7 @@ pub fn generate_method<W: Write>(
writeln!(w, " {{")?; writeln!(w, " {{")?;
write!(w, "let func: UFunction =")?; write!(w, "let mut func: UFunction =")?;
generate_find_object(&method.full_name, w)?; generate_find_object(&method.full_name, w)?;
writeln!(w, ".expect(\"function '{}' not found\");", method.full_name)?; writeln!(w, ".expect(\"function '{}' not found\");", method.full_name)?;
@ -390,22 +398,26 @@ pub fn generate_method<W: Write>(
for param in &method.parameters { for param in &method.parameters {
match param { match param {
crate::sdk::ParameterKind::Default(_) | crate::sdk::ParameterKind::Out(_) => { crate::sdk::ParameterKind::Default(param) => {
let param = param.as_param();
writeln!(w, "params.{0} = {0};", param.name)?; writeln!(w, "params.{0} = {0};", param.name)?;
} }
crate::sdk::ParameterKind::Out(param) => {
// swap so we dont have to worry about Clone or Copy
writeln!(w, "core::mem::swap(&mut params.{0}, {0});", param.name)?;
}
_ => {} _ => {}
} }
} }
writeln!(w, "let flags = *func.get_function_flags();")?; writeln!(w, "let flags = *func.get_function_flags();")?;
writeln!(w, "process_event(self.as_uobject(), func, &mut params);")?; writeln!(w, "process_event(self.as_uobject(), func, &mut params);")?;
writeln!(w, "*func.get_function_flags_mut() = flags;")?; writeln!(w, "func.set_function_flags(flags);")?;
for param in &method.parameters { for param in &method.parameters {
match param { match param {
crate::sdk::ParameterKind::Out(param) => { crate::sdk::ParameterKind::Out(param) => {
writeln!(w, "*{0} = params.{0};", param.name)?; // swap back
writeln!(w, "core::mem::swap({0}, &mut params.{0});", param.name)?;
} }
_ => {} _ => {}
} }