Skip to content

Commit

Permalink
feat: added chunking and parallelization to appinfo reading
Browse files Browse the repository at this point in the history
  • Loading branch information
Tormak9970 committed Oct 8, 2024
1 parent 8cff2f1 commit 2599a15
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 16 deletions.
47 changes: 31 additions & 16 deletions src-tauri/src/appinfo_vdf_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::{path::PathBuf, fs};
use std::io::Read;

use serde_json::{Value, Map};
use rayon::iter::{IntoParallelRefIterator, ParallelIterator};

use crate::reader::Reader;
use crate::vdf_reader::read_entry_map;
Expand Down Expand Up @@ -58,36 +59,51 @@ fn read(reader: &mut Reader) -> Map<String, Value> {
return res;
}

struct AppInfoChunk {
pub offset: usize,
pub length: usize,
}

/// Reads the appinfo.vdf app sections to a JSON array.
fn read_app_sections(reader: &mut Reader, string_table_offset: i64, magic: Option<u32>, strings: &Option<&mut Vec<String>>) -> Vec<Value> {
let mut entries: Vec<Value> = vec![];
let mut id: u32 = reader.read_uint32(true);

let eof: usize = (string_table_offset - 4).try_into().unwrap();

let mut chunks: Vec<AppInfoChunk> = vec![];

while id != 0x00000000 && reader.get_offset() < eof {
let size = reader.read_uint32(true);
let chunk_size = reader.read_uint32(true);
let offset = reader.get_offset();

let data_end: usize = size.try_into().unwrap();
let chunk_length: usize = chunk_size.try_into().unwrap();

// let _state = reader.read_uint32(true);
// let _last_updated = reader.read_uint32(true);
// let _access_token = reader.read_uint64(true);
// reader.seek(20, 1);
// let _version_number = reader.read_uint32(true);
// reader.seek(20, 1);
reader.seek(60, 1);
chunks.push(AppInfoChunk { offset, length: chunk_length });

reader.seek(offset + chunk_length, 0);
id = reader.read_uint32(true);
}

let mut entry: Map<String, Value> = read_entry_map(reader, magic, strings);
let entries: Vec<Value> = chunks.par_iter().filter_map(| chunk | {
let mut chunk_reader = reader.slice(chunk.offset, chunk.length);

// let _state = chunk_reader.read_uint32(true);
// let _last_updated = chunk_reader.read_uint32(true);
// let _access_token = chunk_reader.read_uint64(true);
// chunk_reader.seek(20, 1);
// let _version_number = chunk_reader.read_uint32(true);
// chunk_reader.seek(20, 1);
chunk_reader.seek(60, 1);

let mut entry: Map<String, Value> = read_entry_map(&mut chunk_reader, magic, strings);

if entry.contains_key("appinfo") {
let appinfo_val: &Value = entry.get("appinfo").expect("Should have been able to get \"appinfo\".");
let appinfo = appinfo_val.as_object().expect("appinfo should have been an object.");

entry = appinfo.clone();
}

if entry.contains_key("common") {
let common_val: &Value = entry.get("common").expect("Should have been able to get \"common\".");
let common = common_val.as_object().expect("Common should have been an object.");
Expand All @@ -96,13 +112,12 @@ fn read_app_sections(reader: &mut Reader, string_table_offset: i64, magic: Optio
let type_str: &str = type_val.as_str().expect("Should have been able to convert type to str");

if type_str == "Game" || type_str == "game" {
entries.push(Value::Object(entry));
return Some(Value::Object(entry));
}
}

reader.seek(offset + data_end, 0);
id = reader.read_uint32(true);
}
return None;
}).collect();

return entries;
}
6 changes: 6 additions & 0 deletions src-tauri/src/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,12 @@ impl Reader<'_> {
/// Creates a new Reader from the provided buffer.
pub fn new(buf: &[u8]) -> Reader { return Reader { data: buf, offset: 0, length: buf.len() as u64 }; }

/// Slices the Reader's buffer and returns a new Reader for the slice.
pub fn slice(&self, offset: usize, length: usize) -> Reader {
let sliced = &self.data[offset..(offset+length)];
return Reader { data: sliced, offset: 0, length: length as u64 };
}

/// Seek to a new offset, from 0 (start), 1 (current), or 2 (end) of the buffer.
pub fn seek(&mut self, offset: usize, position: u8) {
if position == 0 {
Expand Down

0 comments on commit 2599a15

Please sign in to comment.