diff --git a/unreal-sdk/Cargo.toml b/unreal-sdk/Cargo.toml index 9a63893..a49036d 100644 --- a/unreal-sdk/Cargo.toml +++ b/unreal-sdk/Cargo.toml @@ -17,4 +17,8 @@ lazy_static = "1.4.0" once_cell = "1.17.1" serde = { version = "1.0", features = ["derive"] } -serde_json = "1.0" \ No newline at end of file + +serde_json = "1.0" +serde_json_any_key = "2.0.0" + +ron = "0.8.0" \ No newline at end of file diff --git a/unreal-sdk/src/sdk/process.rs b/unreal-sdk/src/sdk/process.rs index 367f242..75797fa 100644 --- a/unreal-sdk/src/sdk/process.rs +++ b/unreal-sdk/src/sdk/process.rs @@ -1,8 +1,10 @@ use std::collections::{btree_map::Entry, BTreeMap}; +use std::path::Path; use anyhow::Context; use itertools::Itertools; use rayon::prelude::{IntoParallelIterator, IntoParallelRefIterator, ParallelIterator}; +use ron::ser::PrettyConfig; use crate::global_tables::objects::GOBJECTS; use crate::sdk::repr::*; @@ -38,10 +40,31 @@ impl Sdk { output }) + // remove empty packages + .filter(|pkg| { + pkg.as_ref() + .map(|(_, pkg)| !pkg.types.is_empty()) + .unwrap_or(true) + }) .collect::>>()?; Ok(Self { packages }) } + + pub fn to_json(&self) -> Result { + serde_json::to_string(self) + } + + pub fn to_ron(&self) -> Result { + ron::ser::to_string_pretty(self, PrettyConfig::new().compact_arrays(true)) + } + + pub fn save_to_path>(&self, path: P) -> Result<(), anyhow::Error> { + self.to_ron() + .context("failed to serialize") + .and_then(|text| std::fs::write(path, text).context("failed to write to file.")) + } + fn get_packages() -> anyhow::Result> { let objects = GOBJECTS .read() @@ -152,7 +175,7 @@ impl Class { } }); - let (fields, methods) = Self::process_children(&strct)?; + let (fields, methods) = Self::process_children(&strct); Ok(Self { obj_ref: strct.as_uobject().object_ref(), @@ -169,7 +192,7 @@ impl Class { } /// returns tuple of (fields, methods) - fn process_children(strct: &UStruct) -> anyhow::Result<(Vec, Vec)> { + fn process_children(strct: &UStruct) -> (Vec, Vec) { enum ClassChild { Field(ClassField), Method(ClassMethod), @@ -191,20 +214,25 @@ impl Class { Ok(child) }) - .fold_ok( + .filter_map(|prop| match prop { + Ok(field) => field, + Err(err) => { + log::warn!("skipping field because: {err}"); + None + } + }) + .fold( (Vec::new(), Vec::new()), |(mut fields, mut methods), child| { - if let Some(child) = child { - match child { - ClassChild::Field(field) => fields.push(field), - ClassChild::Method(method) => methods.push(method), - } + match child { + ClassChild::Field(field) => fields.push(field), + ClassChild::Method(method) => methods.push(method), } (fields, methods) }, - )?; + ); - Ok((fields, methods)) + (fields, methods) } pub fn get_dependent_types(&self) -> Vec { @@ -314,6 +342,7 @@ impl Package { let dependencies = types .iter() .flat_map(|(_, ty)| ty.get_dependent_types().into_iter().map(|obj| obj.package)) + .sorted() .dedup() .collect::>();