Skip to content

Instantly share code, notes, and snippets.

@Skirmisher
Created June 25, 2020 09:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Skirmisher/568534bc917e45ea06eaf3b8b9d451f7 to your computer and use it in GitHub Desktop.
Save Skirmisher/568534bc917e45ea06eaf3b8b9d451f7 to your computer and use it in GitHub Desktop.
the script demands it
// SPDX-License-Identifier: CC0-1.0
// Be good to her.
use std::io::{self, prelude::*, BufReader, BufWriter};
use std::fs::File;
use std::env;
use std::string::String;
fn main() -> io::Result<()> {
let script_name = env::args().nth(1);
if script_name.is_none() || !script_name.clone().unwrap().contains(".rpy") {
return Err(io::Error::new(io::ErrorKind::Other, "Please provide a Ren'Py script (.rpy) as an argument"));
}
let script_name = script_name.unwrap();
let script_in = File::open(&script_name)?;
let script_in = BufReader::new(script_in);
let script_out = File::create(script_name.replacen(".rpy", ".rpy.decoded", 1))?;
let mut script_out = BufWriter::new(script_out);
for line in script_in.lines() {
let line = line?;
if let Some(pos) = line.find("#") {
let (code, comment) = line.split_at(pos + 1);
let comment: Vec<char> = comment.chars()
.filter(|c| *c != ' ')
.collect();
let result: Result<Vec<u8>, _> = comment
.chunks(2)
.map(|hex: &[char]| u8::from_str_radix(hex.iter().collect::<String>().as_str(), 16))
.collect(); // turns an iterator of Results into a Result of the collected type
match result.ok().and_then(|x| String::from_utf8(x).ok()) {
Some(comment) => {
writeln!(script_out, "{}[nari] {}", code, comment)?;
}
None => {
writeln!(script_out, "{}", line)?;
}
}
} else {
writeln!(script_out, "{}", line)?;
}
}
script_out.flush()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment