Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

try one's hand at 小试牛刀 #157

Open
wants to merge 1 commit into
base: rust-rewrite
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
/cmake-build-debug/
/cmake-build-release/
/cmake-build-*/
target/

node_modules/

Expand Down
11 changes: 11 additions & 0 deletions Justfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
list:
just -l

cli-run:
cargo run --package jsxer_cli --bin jsxer_cli --help

cli-build:
cargo build --package jsxer_cli --bin jsxer_cli

cli-dev argument:
./target/debug/jsxer_cli.exe --help {{argument}}
105 changes: 104 additions & 1 deletion crates/jsxer_cli/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,108 @@

use clap::{Arg, Command};
use std::fs;
use std::path::Path;
use std::process;

fn main() {
println!("Hello, world!");
let matches = Command::new("JSXER - A fast and accurate JSXBIN decompiler.")
.version(env!("CARGO_PKG_VERSION"))
.author("Angelo DeLuca and contributors.")
.about("Decompiles JSXBIN files to JSX source code.")
.arg(
Arg::new("input")
.help("Name of file to read")
.required(true)
.index(1),
)
.arg(
Arg::new("output")
.short('o')
.long("output")
.value_name("FILE")
.help("Output path for the decompiled file"),
)
.arg(
Arg::new("unblind")
.short('b')
.long("unblind")
.help("Try renaming symbols which are obfuscated by 'JsxBlind' (experimental)"),
)
// Uncomment if you want to add a verbose flag
/*
.arg(
Arg::new("verbose")
.short('v')
.long("verbose")
.help("Display verbose log"),
)
*/
.get_matches();

let input = matches.get_one::<String>("input").expect("Input file is required");
let output = matches.get_one::<String>("output").map(|s| s.as_str());
let unblind = matches.get_flag("unblind");

let input_path = Path::new(input);
if !input_path.exists() || !input_path.is_file() {
eprintln!("Error: Input file does not exist or is not a file.");
process::exit(1);
}

let output_path = match output {
Some(output) => Path::new(output).to_owned(),
None => {
let parent = input_path.parent().unwrap_or_else(|| Path::new("."));
parent.join(format!("{}.jsx", input_path.file_stem().unwrap().to_string_lossy()))
}
};

if let Some(parent) = output_path.parent() {
if !parent.exists() {
if let Err(err) = fs::create_dir_all(parent) {
eprintln!("Error: Failed to create directory for output file: {}", err);
process::exit(1);
}
}
}

// Read in the JSXBIN file contents...
let contents = match read_file_contents(&input_path) {
Ok(contents) => contents,
Err(err) => {
eprintln!("Error: Failed to read input file: {}", err);
process::exit(1);
}
};

// Begin de-compilation...
println!("[i] Decompiling...");
let decompiled = match decompile_jsxbin(&contents, unblind) {
Ok(decompiled) => decompiled,
Err(err) => {
eprintln!("Error: Failed to decompile JSXBIN content: {}", err);
process::exit(1);
}
};
println!("[i] Finished.");

// Write the decompiled content to the output file...
if let Err(err) = write_file_contents(&output_path, &decompiled) {
eprintln!("Error: Failed to write output file: {}", err);
process::exit(1);
}
}

// Implement these functions as per your requirements
fn read_file_contents(path: &Path) -> Result<String, std::io::Error> {
fs::read_to_string(path)
}

fn decompile_jsxbin(contents: &str, unblind: bool) -> Result<String, &'static str> {
// Placeholder for actual decompilation logic
Ok(String::from("/* decompiled content */"))
}

fn write_file_contents(path: &Path, contents: &str) -> Result<(), std::io::Error> {
fs::write(path, contents)
}