Skip to content

Commit

Permalink
Merge pull request #12 from QuestPackageManager/rust-gen
Browse files Browse the repository at this point in the history
Rust gen and fixes
  • Loading branch information
Fernthedev authored Dec 19, 2024
2 parents 7e7bfe4 + aefab2f commit 6addbae
Show file tree
Hide file tree
Showing 39 changed files with 4,277 additions and 418 deletions.
8 changes: 7 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,10 @@
global-metadata.dat
libil2cpp.so
codegen/
codegen.zip
codegen.zip
0.3.0/
extern/
codegen-rs/

stuff/
json_cordl/
44 changes: 43 additions & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,53 @@
"kind": "bin"
}
},
"args": [
"--metadata",
"./stuff/unity2022.3/global-metadata.dat",
"--libil2cpp",
"./stuff/unity2022.3/libil2cpp.so",
"cpp"
],
"cwd": "${workspaceFolder}"
},
{
"type": "lldb",
"request": "launch",
"name": "Debug executable 'cordl' json",
"cargo": {
"args": ["build", "--bin=cordl", "--package=cordl"],
"filter": {
"name": "cordl",
"kind": "bin"
}
},
"args": [
"--metadata",
"./stuff/bs1.34.2/global-metadata.dat",
"--libil2cpp",
"./stuff/bs1.34.2/libil2cpp.so"
"./stuff/bs1.34.2/libil2cpp.so",
"--multi-json",
"./json_cordl"
],
"cwd": "${workspaceFolder}"
},
{
"type": "lldb",
"request": "launch",
"name": "Debug executable 'cordl' rust",
"cargo": {
"args": ["build", "--bin=cordl", "--package=cordl"],
"filter": {
"name": "cordl",
"kind": "bin"
}
},
"args": [
"--metadata",
"./stuff/unity2022.3/global-metadata.dat",
"--libil2cpp",
"./stuff/unity2022.3/libil2cpp.so",
"rust"
],
"cwd": "${workspaceFolder}"
},
Expand Down
28 changes: 21 additions & 7 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 13 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,13 @@ edition = "2021"


[features]
default = ["il2cpp_v31"]
default = ["il2cpp_v31", "json", "rust", "cpp"]
il2cpp_v31 = ["brocolib_il2cpp_v31"]
il2cpp_v29 = ["brocolib_il2cpp_v29"]
json = ["dep:serde_json", "dep:serde"]
rust = ["dep:quote", "dep:prettyplease", "dep:syn", "dep:proc-macro2"]
cpp = []


# Alias a second version of the dependency with a different package name
[dependencies.brocolib_il2cpp_v31]
Expand Down Expand Up @@ -41,10 +45,16 @@ log = "0.4.20"
pretty_env_logger = "0.5.0"
rayon = "1.8"
filesize = "0.2.0"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
serde = { version = "1.0", features = ["derive"], optional = true }
serde_json = {version = "1.0", optional = true }
bitflags = "2.6.0"

# Rust syntax generation
quote = {version = "1", optional = true}
prettyplease = {version = "0.2", optional = true}
syn = { version = "2", optional = true }
proc-macro2 = { version = "1", optional = true }

[profiles.release]
opt-level = 3
lto = true
15 changes: 15 additions & 0 deletions cordl_internals_rs/Cargo_template.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[package]
name = "bs_cordl"
version = "0.1.0"
edition = "2021"

[dependencies]
quest_hook = { git = "https://github.com/Fernthedev/quest-hook-rs.git", features = ["il2cpp_v31"], branch = "cordl-fixes"}
# quest_hook = { path = "../../quest-hook-rs", features = ["il2cpp_v31"]}

[lib]

[features]
default = []

#cordl_features
2 changes: 1 addition & 1 deletion rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
[toolchain]
channel = "nightly-2023-12-28"
channel = "nightly-2024-12-18"
41 changes: 13 additions & 28 deletions src/data/name_components.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,38 +4,29 @@ pub struct NameComponents {
pub declaring_types: Option<Vec<String>>,
pub name: String,
pub generics: Option<Vec<String>>,
pub is_pointer: bool,
}

impl NameComponents {
// TODO: Add setting for adding :: prefix
// however, this cannot be allowed in all cases
pub fn combine_all(&self) -> String {
let combined_declaring_types = self.declaring_types.as_ref().map(|d| d.join("::"));
let mut completed = self.name.clone();

// will be empty if no namespace or declaring types
let prefix = combined_declaring_types
.as_ref()
.or(self.namespace.as_ref())
.map(|s| {
if s.is_empty() {
"::".to_string()
} else {
format!("::{s}::")
}
})
.unwrap_or_default();
// add declaring types
if let Some(declaring_types) = self.declaring_types.as_ref() {
completed = format!("{}/{completed}", declaring_types.join("/"));
}

let mut completed = format!("{prefix}{}", self.name);
// add namespace
if let Some(namespace) = self.namespace.as_ref() {
completed = format!("{namespace}.{completed}");
}

// add generics
if let Some(generics) = &self.generics {
completed = format!("{completed}<{}>", generics.join(","));
}

if self.is_pointer {
completed = format!("{completed}*")
}

completed
}

Expand All @@ -55,16 +46,10 @@ impl NameComponents {
}
}

pub fn as_pointer(&self) -> Self {
pub fn remove_namespace(self) -> Self {
Self {
is_pointer: true,
..self.clone()
}
}
pub fn remove_pointer(&self) -> Self {
Self {
is_pointer: false,
..self.clone()
namespace: None,
..self
}
}

Expand Down
7 changes: 3 additions & 4 deletions src/data/type_resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ pub struct TypeResolver<'a, 'b> {
pub collection: &'a TypeContextCollection,
}

impl<'a, 'b> TypeResolver<'a, 'b> {
impl TypeResolver<'_, '_> {
pub fn resolve_type(
&self,
declaring_cs_type: &mut CsType,
Expand Down Expand Up @@ -103,6 +103,7 @@ impl<'a, 'b> TypeResolver<'a, 'b> {
| Il2CppTypeEnum::Void
| Il2CppTypeEnum::Boolean
| Il2CppTypeEnum::Char
| Il2CppTypeEnum::Object
| Il2CppTypeEnum::String => {
declaring_cs_type
.requirements
Expand All @@ -112,10 +113,8 @@ impl<'a, 'b> TypeResolver<'a, 'b> {
));
ResolvedTypeData::Primitive(to_resolve.ty)
}

Il2CppTypeEnum::Object
Il2CppTypeEnum::Class
| Il2CppTypeEnum::Valuetype
| Il2CppTypeEnum::Class
| Il2CppTypeEnum::Typedbyref
// ptr types
| Il2CppTypeEnum::I
Expand Down
24 changes: 5 additions & 19 deletions src/generate/cpp/cpp_context.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::cmp::Ordering;
use std::io::Write;
use std::io::{BufWriter, Write};
use std::{
collections::{HashMap, HashSet},
fs::{create_dir_all, remove_file, File},
Expand Down Expand Up @@ -74,20 +74,6 @@ pub struct CppContext {
///
/// - `write_il2cpp_arg_macros`: Writes IL2CPP argument macros for the given C++ type.
impl CppContext {
/// Retrieves a mutable reference to a C++ type based on the given root tag.
pub fn get_cpp_type_recursive_mut(&mut self, root_tag: CsTypeTag) -> Option<&mut CppType> {
let ty = self.typedef_types.get_mut(&root_tag);

ty
}

/// Retrieves an immutable reference to a C++ type based on the given root tag.
pub fn get_cpp_type_recursive(&self, root_tag: CsTypeTag) -> Option<&CppType> {
let ty = self.typedef_types.get(&root_tag);

ty
}

/// Returns the include path for the C++ type definitions.
pub fn get_include_path(&self) -> &PathBuf {
&self.typedef_path
Expand Down Expand Up @@ -150,7 +136,7 @@ impl CppContext {
let tdi = tag.get_tdi();

let mut cpp_ty = CppType::make_cpp_type(*tag, ty, config);
cpp_ty.nested_fixup(ty, metadata, config);
cpp_ty.nested_fixup(context_tag, ty, metadata, config);

if metadata.blacklisted_types.contains(&tdi) {
let result = match t.is_value_type() {
Expand Down Expand Up @@ -209,17 +195,17 @@ impl CppContext {

trace!("Writing {:?}", self.typedef_path.as_path());
let mut typedef_writer = Writer {
stream: File::create(self.typedef_path.as_path())?,
stream: BufWriter::new(File::create(self.typedef_path.as_path())?),
indent: 0,
newline: true,
};
let mut typeimpl_writer = Writer {
stream: File::create(self.type_impl_path.as_path())?,
stream: BufWriter::new(File::create(self.type_impl_path.as_path())?),
indent: 0,
newline: true,
};
let mut fundamental_writer = Writer {
stream: File::create(self.fundamental_path.as_path())?,
stream: BufWriter::new(File::create(self.fundamental_path.as_path())?),
indent: 0,
newline: true,
};
Expand Down
Loading

0 comments on commit 6addbae

Please sign in to comment.