Skip to content

Instantly share code, notes, and snippets.

@Skirmisher
Created Jun 25, 2020
Embed
What would you like to do?
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