a bit of organization and stuff

main
aprzn 2 years ago
parent b478c030dd
commit 98a582bdc5

@ -13,13 +13,19 @@ struct User {
enum Song { enum Song {
Official{id: i32 /*k8*/}, Official{id: i32 /*k8*/},
Newgrounds{id: i32 /*k45*/}, Newgrounds{id: i32 /*k45*/},
Unknown
} }
struct Level { struct Level {
name: String, // k2 outer: OuterLevel,
song: Song, song: Song,
} }
struct OuterLevel {
name: String, // k2
revision: i32, // k46
}
pub fn gd_path() -> PathBuf { pub fn gd_path() -> PathBuf {
let mut path_buf = home::home_dir().unwrap(); let mut path_buf = home::home_dir().unwrap();
#[cfg(unix)] #[cfg(unix)]
@ -35,54 +41,79 @@ pub fn gd_path() -> PathBuf {
struct LevelBuilder { struct LevelBuilder {
name: Option<String>, name: Option<String>,
song: Option<Song>, song: Option<Song>,
revision: Option<i32>,
} }
impl Default for LevelBuilder { impl Default for LevelBuilder {
fn default() -> Self { fn default() -> Self {
Self {name: None, song: None} Self {name: None, song: None, revision: None}
} }
} }
impl LevelBuilder { impl LevelBuilder {
fn new() -> Self {Self::default()}
fn with_name(&mut self, name: String) { fn with_name(&mut self, name: String) {
self.name = Some(name); self.name = Some(name);
} }
fn with_song(&mut self, song: Song) { fn with_song(&mut self, song: Song) {
self.song = Some(song); self.song = Some(song);
} }
fn with_revision(&mut self, revision: i32) {
self.revision = Some(revision);
}
fn make_level(self) -> Option<Level> { fn make_level(self) -> Option<Level> {
if let Some(name) = self.name { match self {
if let Some(song) = self.song { Self {
return Some(Level {name, song}); name: Some(name),
song: Some(song),
revision: Some(revision)
} => Some(Level{song, outer: OuterLevel {name, revision}}),
_ => None,
}
} }
fn make_outer_level(self) -> Option<OuterLevel> {
match self {
Self {
name: Some(name),
song,
revision: Some(revision)
} => Some(OuterLevel {name, revision}),
_ => None,
} }
None
} }
} }
fn get_levels() -> Vec<Level> { fn get_local_level_plist() -> String {
let raw_save_data = { let raw_save_data = {
let mut save_file = File::open(gd_path().join("CCLocalLevels.dat")).expect("No save file found!"); let mut save_file = File::open(gd_path().join("CCLocalLevels.dat")).expect("No save file found!");
let mut sd = Vec::new(); let mut sd = Vec::new();
save_file.read_to_end(&mut sd); save_file.read_to_end(&mut sd).unwrap();
sd sd
}; };
let data_post_xor: Vec<u8> = raw_save_data.iter().map(|b| b ^ 11).collect(); let data_post_xor: Vec<u8> = raw_save_data.iter().map(|b| b ^ 11).collect();
let data_post_b64 = URL_SAFE.decode(data_post_xor).unwrap(); let data_post_b64 = URL_SAFE.decode(data_post_xor).unwrap();
let plist = {
let mut decoder = GzDecoder::<&[u8]>::new(data_post_b64.as_ref()); let mut decoder = GzDecoder::<&[u8]>::new(data_post_b64.as_ref());
let mut plist = String::new(); let mut plist = String::new();
if let Err(_) = decoder.read_to_string(&mut plist) { if let Err(_) = decoder.read_to_string(&mut plist) {
println!("Warning: Game save likely corrupted (gzip decode failed)"); println!("Warning: Game save likely corrupted (gzip decode failed)");
} }
plist plist
}; }
fn get_outer_levels() -> Vec<Level> {
let plist = get_local_level_plist();
let mut reader = Reader::from_str(plist.as_ref()); let mut reader = Reader::from_str(plist.as_ref());
let mut out = vec![]; let mut out = vec![];
let builder = LevelBuilder::default();
loop { loop {
let token = reader.read_event().unwrap(); let token = reader.read_event().unwrap();
match token { match token {
_ => {}
} }
break out; break out;
} }

Loading…
Cancel
Save