mirror of
https://github.com/a2x/cs2-dumper.git
synced 2026-04-17 19:49:58 +08:00
Refactor code writer and fix minor things
Also added the ability to specify which files should be generated based on their file type. E.g. `cs2-dumper.exe -f hpp,json`
This commit is contained in:
@@ -1,27 +1,47 @@
|
||||
use std::fmt::Write;
|
||||
use std::fmt::{self, Write};
|
||||
|
||||
use heck::{AsPascalCase, AsSnakeCase};
|
||||
|
||||
use super::{CodeGen, OffsetMap, Results};
|
||||
use super::{slugify, CodeWriter, Formatter, OffsetMap};
|
||||
|
||||
use crate::error::Result;
|
||||
impl CodeWriter for OffsetMap {
|
||||
fn write_cs(&self, fmt: &mut Formatter<'_>) -> fmt::Result {
|
||||
fmt.block("namespace CS2Dumper.Offsets", false, |fmt| {
|
||||
for (module_name, offsets) in self {
|
||||
writeln!(fmt, "// Module: {}", module_name)?;
|
||||
|
||||
impl CodeGen for OffsetMap {
|
||||
fn to_cs(&self, results: &Results, indent_size: usize) -> Result<String> {
|
||||
self.write_content(results, indent_size, |fmt| {
|
||||
fmt.block("namespace CS2Dumper.Offsets", false, |fmt| {
|
||||
fmt.block(
|
||||
&format!("public static class {}", AsPascalCase(slugify(module_name))),
|
||||
false,
|
||||
|fmt| {
|
||||
for (name, value) in offsets {
|
||||
writeln!(fmt, "public const nint {} = {:#X};", name, value)?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
},
|
||||
)?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
})
|
||||
}
|
||||
|
||||
fn write_hpp(&self, fmt: &mut Formatter<'_>) -> fmt::Result {
|
||||
writeln!(fmt, "#pragma once\n")?;
|
||||
writeln!(fmt, "#include <cstddef>\n")?;
|
||||
|
||||
fmt.block("namespace cs2_dumper", false, |fmt| {
|
||||
fmt.block("namespace offsets", false, |fmt| {
|
||||
for (module_name, offsets) in self {
|
||||
writeln!(fmt, "// Module: {}", module_name)?;
|
||||
|
||||
fmt.block(
|
||||
&format!(
|
||||
"public static class {}",
|
||||
AsPascalCase(Self::slugify(module_name))
|
||||
),
|
||||
&format!("namespace {}", AsSnakeCase(slugify(module_name))),
|
||||
false,
|
||||
|fmt| {
|
||||
for (name, value) in offsets {
|
||||
writeln!(fmt, "public const nint {} = {:#X};", name, value)?;
|
||||
writeln!(fmt, "constexpr std::ptrdiff_t {} = {:#X};", name, value)?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
@@ -30,81 +50,37 @@ impl CodeGen for OffsetMap {
|
||||
}
|
||||
|
||||
Ok(())
|
||||
})?;
|
||||
|
||||
Ok(())
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
fn to_hpp(&self, results: &Results, indent_size: usize) -> Result<String> {
|
||||
self.write_content(results, indent_size, |fmt| {
|
||||
writeln!(fmt, "#pragma once\n")?;
|
||||
writeln!(fmt, "#include <cstddef>\n")?;
|
||||
|
||||
fmt.block("namespace cs2_dumper", false, |fmt| {
|
||||
fmt.block("namespace offsets", false, |fmt| {
|
||||
for (module_name, offsets) in self {
|
||||
writeln!(fmt, "// Module: {}", module_name)?;
|
||||
|
||||
fmt.block(
|
||||
&format!("namespace {}", AsSnakeCase(Self::slugify(module_name))),
|
||||
false,
|
||||
|fmt| {
|
||||
for (name, value) in offsets {
|
||||
writeln!(
|
||||
fmt,
|
||||
"constexpr std::ptrdiff_t {} = {:#X};",
|
||||
name, value
|
||||
)?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
},
|
||||
)?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
})
|
||||
})?;
|
||||
|
||||
Ok(())
|
||||
})
|
||||
fn write_json(&self, fmt: &mut Formatter<'_>) -> fmt::Result {
|
||||
fmt.write_str(&serde_json::to_string_pretty(self).expect("failed to serialize"))
|
||||
}
|
||||
|
||||
fn to_json(&self, _results: &Results, _indent_size: usize) -> Result<String> {
|
||||
serde_json::to_string_pretty(self).map_err(Into::into)
|
||||
}
|
||||
fn write_rs(&self, fmt: &mut Formatter<'_>) -> fmt::Result {
|
||||
writeln!(fmt, "#![allow(non_upper_case_globals, unused)]\n")?;
|
||||
|
||||
fn to_rs(&self, results: &Results, indent_size: usize) -> Result<String> {
|
||||
self.write_content(results, indent_size, |fmt| {
|
||||
writeln!(
|
||||
fmt,
|
||||
"#![allow(non_upper_case_globals, non_camel_case_types, unused)]\n"
|
||||
)?;
|
||||
fmt.block("pub mod cs2_dumper", false, |fmt| {
|
||||
fmt.block("pub mod offsets", false, |fmt| {
|
||||
for (module_name, offsets) in self {
|
||||
writeln!(fmt, "// Module: {}", module_name)?;
|
||||
|
||||
fmt.block("pub mod cs2_dumper", false, |fmt| {
|
||||
fmt.block("pub mod offsets", false, |fmt| {
|
||||
for (module_name, offsets) in self {
|
||||
writeln!(fmt, "// Module: {}", module_name)?;
|
||||
fmt.block(
|
||||
&format!("pub mod {}", AsSnakeCase(slugify(module_name))),
|
||||
false,
|
||||
|fmt| {
|
||||
for (name, value) in offsets {
|
||||
writeln!(fmt, "pub const {}: usize = {:#X};", name, value)?;
|
||||
}
|
||||
|
||||
fmt.block(
|
||||
&format!("pub mod {}", AsSnakeCase(Self::slugify(module_name))),
|
||||
false,
|
||||
|fmt| {
|
||||
for (name, value) in offsets {
|
||||
writeln!(fmt, "pub const {}: usize = {:#X};", name, value)?;
|
||||
}
|
||||
Ok(())
|
||||
},
|
||||
)?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
},
|
||||
)?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
})
|
||||
})?;
|
||||
|
||||
Ok(())
|
||||
Ok(())
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user