Improve schema parsing

This commit is contained in:
a2x
2024-04-06 03:20:08 +10:00
parent efe4775dc0
commit ce0fb918ab
114 changed files with 79858 additions and 79527 deletions

View File

@@ -79,6 +79,7 @@ impl CodeGen for Vec<Button> {
fmt.block("pub mod buttons", false, |fmt| {
for button in self {
let mut name = button.name.clone();
if name == "use" {
name = format!("r#{}", name);
}

View File

@@ -42,16 +42,12 @@ impl<'a> Item<'a> {
}
trait CodeGen {
/// Converts an [`Item`] to formatted C# code.
fn to_cs(&self, results: &Results, indent_size: usize) -> Result<String>;
/// Converts an [`Item`] to formatted C++ code.
fn to_hpp(&self, results: &Results, indent_size: usize) -> Result<String>;
/// Converts an [`Item`] to formatted JSON.
fn to_json(&self, results: &Results, indent_size: usize) -> Result<String>;
/// Converts an [`Item`] to formatted Rust code.
fn to_rs(&self, results: &Results, indent_size: usize) -> Result<String>;
#[inline]
@@ -126,7 +122,7 @@ pub struct Results {
/// Map of offsets to dump.
pub offsets: OffsetMap,
/// Map of schema classes and enums to dump.
/// Map of schema classes/enums to dump.
pub schemas: SchemaMap,
}
@@ -152,10 +148,8 @@ impl Results {
out_dir: P,
indent_size: usize,
) -> Result<()> {
// TODO: Make this user-configurable.
const FILE_EXTS: &[&str] = &["cs", "hpp", "json", "rs"];
// Create the output directory if it doesn't exist.
fs::create_dir_all(&out_dir)?;
let items = [
@@ -164,18 +158,12 @@ impl Results {
("offsets", Item::Offsets(&self.offsets)),
];
self.dump_items(&items, out_dir.as_ref(), indent_size, FILE_EXTS)?;
// Write a new file for each module.
for (module_name, (classes, enums)) in &self.schemas {
let schemas = [(module_name.clone(), (classes.clone(), enums.clone()))].into();
let item = Item::Schemas(&schemas);
self.dump_item(&item, out_dir.as_ref(), indent_size, FILE_EXTS, module_name)?;
for (file_name, item) in &items {
self.dump_item(item, &out_dir, indent_size, FILE_EXTS, file_name)?;
}
self.dump_info_file(process, out_dir)?;
self.dump_schemas(&out_dir, indent_size, FILE_EXTS)?;
self.dump_info(process, &out_dir)?;
Ok(())
}
@@ -194,19 +182,6 @@ impl Results {
Ok(())
}
fn dump_info_file<P: AsRef<Path>>(
&self,
process: &mut IntoProcessInstanceArcBox<'_>,
out_dir: P,
) -> Result<()> {
let content = &serde_json::to_string_pretty(&json!({
"timestamp": self.timestamp.to_rfc3339(),
"build_number": self.read_build_number(process).unwrap_or(0),
}))?;
self.dump_file(out_dir.as_ref(), "info", "json", &content)
}
fn dump_item<P: AsRef<Path>>(
&self,
item: &Item,
@@ -218,21 +193,36 @@ impl Results {
for ext in file_exts {
let content = item.generate(self, indent_size, ext)?;
self.dump_file(out_dir.as_ref(), file_name, ext, &content)?;
self.dump_file(&out_dir, file_name, ext, &content)?;
}
Ok(())
}
fn dump_items<P: AsRef<Path>>(
fn dump_info<P: AsRef<Path>>(
&self,
process: &mut IntoProcessInstanceArcBox<'_>,
out_dir: P,
) -> Result<()> {
let content = &serde_json::to_string_pretty(&json!({
"timestamp": self.timestamp.to_rfc3339(),
"build_number": self.read_build_number(process).unwrap_or(0),
}))?;
self.dump_file(&out_dir, "info", "json", &content)
}
fn dump_schemas<P: AsRef<Path>>(
&self,
items: &[(&str, Item)],
out_dir: P,
indent_size: usize,
file_exts: &[&str],
) -> Result<()> {
for (file_name, item) in items {
self.dump_item(item, out_dir.as_ref(), indent_size, file_exts, file_name)?;
for (module_name, (classes, enums)) in &self.schemas {
let map = SchemaMap::from([(module_name.clone(), (classes.clone(), enums.clone()))]);
let item = Item::Schemas(&map);
self.dump_item(&item, &out_dir, indent_size, file_exts, &module_name)?;
}
Ok(())
@@ -242,10 +232,10 @@ impl Results {
self.offsets
.iter()
.find_map(|(module_name, offsets)| {
let offset = offsets.iter().find(|(name, _)| *name == "dwBuildNumber")?;
let (_name, value) = offsets.iter().find(|(name, _)| *name == "dwBuildNumber")?;
let module = process.module_by_name(module_name).ok()?;
process.read(module.base + offset.1).ok()
process.read(module.base + value).ok()
})
.ok_or(Error::Other("unable to read build number"))
}

View File

@@ -32,7 +32,7 @@ impl CodeGen for SchemaMap {
false,
|fmt| {
for enum_ in enums {
let ty = match enum_.alignment {
let type_name = match enum_.alignment {
1 => "byte",
2 => "ushort",
4 => "uint",
@@ -47,7 +47,7 @@ impl CodeGen for SchemaMap {
&format!(
"public enum {} : {}",
Self::sanitize_name(&enum_.name),
ty
type_name
),
false,
|fmt| {
@@ -90,7 +90,7 @@ impl CodeGen for SchemaMap {
writeln!(
fmt,
"public const nint {} = {:#X}; // {}",
field.name, field.offset, field.ty
field.name, field.offset, field.type_name
)?;
}
@@ -136,7 +136,7 @@ impl CodeGen for SchemaMap {
false,
|fmt| {
for enum_ in enums {
let ty = match enum_.alignment {
let type_name = match enum_.alignment {
1 => "uint8_t",
2 => "uint16_t",
4 => "uint32_t",
@@ -151,7 +151,7 @@ impl CodeGen for SchemaMap {
&format!(
"enum class {} : {}",
Self::sanitize_name(&enum_.name),
ty
type_name
),
true,
|fmt| {
@@ -191,7 +191,7 @@ impl CodeGen for SchemaMap {
writeln!(
fmt,
"constexpr std::ptrdiff_t {} = {:#X}; // {}",
field.name, field.offset, field.ty
field.name, field.offset, field.type_name
)?;
}
@@ -236,10 +236,10 @@ impl CodeGen for SchemaMap {
"type": "NetworkChangeCallback",
"name": name,
}),
ClassMetadata::NetworkVarNames { name, ty } => json!({
ClassMetadata::NetworkVarNames { name, type_name } => json!({
"type": "NetworkVarNames",
"name": name,
"ty": ty,
"type_name": type_name,
}),
ClassMetadata::Unknown { name } => json!({
"type": "Unknown",
@@ -268,7 +268,7 @@ impl CodeGen for SchemaMap {
.map(|member| (&member.name, member.value))
.collect();
let ty = match enum_.alignment {
let type_name = match enum_.alignment {
1 => "uint8",
2 => "uint16",
4 => "uint32",
@@ -280,7 +280,7 @@ impl CodeGen for SchemaMap {
Self::sanitize_name(&enum_.name),
json!({
"alignment": enum_.alignment,
"type": ty,
"type": type_name,
"members": members,
}),
)
@@ -324,7 +324,7 @@ impl CodeGen for SchemaMap {
false,
|fmt| {
for enum_ in enums {
let ty = match enum_.alignment {
let type_name = match enum_.alignment {
1 => "u8",
2 => "u16",
4 => "u32",
@@ -338,7 +338,7 @@ impl CodeGen for SchemaMap {
fmt.block(
&format!(
"#[repr({})]\npub enum {}",
ty,
type_name,
Self::sanitize_name(&enum_.name),
),
false,
@@ -381,7 +381,7 @@ impl CodeGen for SchemaMap {
writeln!(
fmt,
"pub const {}: usize = {:#X}; // {}",
field.name, field.offset, field.ty
field.name, field.offset, field.type_name
)?;
}
@@ -417,8 +417,8 @@ fn write_metadata(fmt: &mut Formatter<'_>, metadata: &[ClassMetadata]) -> fmt::R
ClassMetadata::NetworkChangeCallback { name } => {
writeln!(fmt, "// NetworkChangeCallback: {}", name)?;
}
ClassMetadata::NetworkVarNames { name, ty } => {
writeln!(fmt, "// NetworkVarNames: {} ({})", name, ty)?;
ClassMetadata::NetworkVarNames { name, type_name } => {
writeln!(fmt, "// NetworkVarNames: {} ({})", name, type_name)?;
}
ClassMetadata::Unknown { name } => {
writeln!(fmt, "// {}", name)?;