Skip to content

Commit

Permalink
docs: added doc comments to all rust functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Tormak9970 committed May 4, 2023
1 parent e3f2c80 commit 226a687
Show file tree
Hide file tree
Showing 9 changed files with 94 additions and 8 deletions.
3 changes: 3 additions & 0 deletions src-tauri/src/appinfo_vdf_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use serde_json::{Value, Map};
use crate::reader::Reader;
use crate::vdf_reader::read_entry_map;

/// Opens the appinfo.vdf file and returns the values as JSON.
pub fn open_appinfo_vdf(path: &PathBuf) -> Map<String, Value> {
let mut file = fs::File::open(path).expect("Path should have existed.");

Expand All @@ -19,6 +20,7 @@ pub fn open_appinfo_vdf(path: &PathBuf) -> Map<String, Value> {
return read(&mut reader);
}

/// Reads the appinfo.vdf file and returns the values as JSON.
fn read(reader: &mut Reader) -> Map<String, Value> {
let magic = reader.read_uint32(true);
let _universe = reader.read_uint32(true); //always 1
Expand All @@ -39,6 +41,7 @@ fn read(reader: &mut Reader) -> Map<String, Value> {
return res;
}

/// Reads the appinfo.vdf app sections to a JSON array.
fn read_app_sections(reader: &mut Reader, skip: u8) -> Vec<Value> {
let mut entries: Vec<Value> = vec![];
let mut id: u32 = reader.read_uint32(true);
Expand Down
3 changes: 3 additions & 0 deletions src-tauri/src/logger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use std::fs::{
use tauri::AppHandle;
use chrono::prelude::*;

/// Gets the log file path for this app.
pub fn get_log_path(app_handle: &AppHandle) -> PathBuf {
let app_log_dir: PathBuf = app_handle.to_owned().path_resolver().app_log_dir().expect("Tried to resolve app log dir and failed.");

Expand All @@ -20,6 +21,7 @@ pub fn get_log_path(app_handle: &AppHandle) -> PathBuf {
}

#[tauri::command]
/// Logs a message to file with level 0 (info), 1 (warn), or 2 (err).
pub fn log_to_file(app_handle: AppHandle, message: &str, level: u8) {
let log_path: PathBuf = get_log_path(&app_handle);

Expand Down Expand Up @@ -48,6 +50,7 @@ pub fn log_to_file(app_handle: AppHandle, message: &str, level: u8) {
}

#[tauri::command]
/// Cleans the log file for a new launch of the app.
pub fn clean_out_log(app_handle: AppHandle) {
let log_path: PathBuf = get_log_path(&app_handle);

Expand Down
29 changes: 23 additions & 6 deletions src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,23 +46,29 @@ struct ChangedPath {
sourcePath: String
}

fn get_grid_filename(appid: &str, grid_type: &str, image_type: &str) -> String {
/// Gets a grid's file name based on its type.
fn get_grid_filename(app_handle: &AppHandle, appid: &str, grid_type: &str, image_type: &str) -> String {
match grid_type {
"Capsule" => return format!("{}p{}", appid, image_type),
"Wide Capsule" => return format!("{}{}", appid, image_type),
"Hero" => return format!("{}_hero{}", appid, image_type),
"Logo" => return format!("{}_logo{}", appid, image_type),
"Icon" => return format!("{}_icon{}", appid, image_type),
_ => panic!("Unexpected grid type {}", grid_type)
_ => {
logger::log_to_file(app_handle.to_owned(), format!("Unexpected grid type {}", grid_type).as_str(), 2);
panic!("Unexpected grid type {}", grid_type);
}
}
}

fn adjust_path(appid: &str, path: &str, grid_type: &str) -> String {
/// Adjusts the path of a grid based on its type.
fn adjust_path(app_handle: &AppHandle, appid: &str, path: &str, grid_type: &str) -> String {
let format_start_index = path.rfind(".").expect("Path should have had a file extension.");
let image_type = &path[format_start_index..];
return get_grid_filename(appid, grid_type, image_type);
return get_grid_filename(app_handle, appid, grid_type, image_type);
}

/// Filters the grid paths based on which have change.
fn filter_paths(app_handle: &AppHandle, steam_active_user_id: String, current_paths: &GridImageCache, original_paths: &GridImageCache) -> Vec<ChangedPath> {
let grids_dir = PathBuf::from(steam::get_grids_directory(app_handle.to_owned(), steam_active_user_id));
let mut res:Vec<ChangedPath> = Vec::new();
Expand All @@ -82,7 +88,7 @@ fn filter_paths(app_handle: &AppHandle, steam_active_user_id: String, current_pa
let target_path;

if source_path != "REMOVE" {
let adjusted_path = adjust_path(appid.as_str(), source_path_owned.as_str(), grid_type.as_str()).replace("\\", "/");
let adjusted_path = adjust_path(app_handle, appid.as_str(), source_path_owned.as_str(), grid_type.as_str()).replace("\\", "/");
target_path = String::from(grids_dir.join(adjusted_path).to_str().unwrap()).replace("\\", "/");
} else {
target_path = String::from("REMOVE");
Expand Down Expand Up @@ -112,6 +118,7 @@ fn filter_paths(app_handle: &AppHandle, steam_active_user_id: String, current_pa
return res;
}

/// Checks for shortcut grid changes.
fn check_for_shortcut_changes(changed_paths: Vec<ChangedPath>, shortcut_ids: Vec<String>, shortcut_icons: &Map<String, Value>, original_shortcut_icons: &Map<String, Value>) -> bool {
for changed_path in changed_paths.into_iter() {
let appid = changed_path.appId;
Expand All @@ -132,8 +139,8 @@ fn check_for_shortcut_changes(changed_paths: Vec<ChangedPath>, shortcut_ids: Vec
return false;
}

// Learn more about Tauri commands at https://tauri.app/v1/guides/features/command
#[tauri::command]
/// Exports the users grids to a Grids zip file.
async fn export_grids_to_zip(app_handle: AppHandle, steam_active_user_id: String, platform_id_map: Map<String, Value>, id_name_map: Map<String, Value>) -> bool {
let file_dialog = FileDialogBuilder::new()
.set_title("Save Grids Zip")
Expand Down Expand Up @@ -164,6 +171,7 @@ async fn export_grids_to_zip(app_handle: AppHandle, steam_active_user_id: String
}

#[tauri::command]
/// Sets the users grids from a Grids zip file.
async fn import_grids_from_zip(app_handle: AppHandle, steam_active_user_id: String, name_id_map: Map<String, Value>) -> (bool, Map<String, Value>) {
let file_dialog = FileDialogBuilder::new()
.set_title("Pick a Grids Zip")
Expand Down Expand Up @@ -193,13 +201,15 @@ async fn import_grids_from_zip(app_handle: AppHandle, steam_active_user_id: Stri
}

#[tauri::command]
/// Reads the user's appinfo.vdf file.
async fn read_appinfo_vdf(app_handle: AppHandle) -> String {
let appinfo_path: PathBuf = PathBuf::from(steam::get_appinfo_path(app_handle.to_owned()));
let appinfo_vdf: Map<String, Value> = open_appinfo_vdf(&appinfo_path);
return serde_json::to_string(&appinfo_vdf).expect("Should have been able to serialize AppInfo vdf to string.");
}

#[tauri::command]
/// Reads the user's shortcuts.vdf file.
async fn read_shortcuts_vdf(app_handle: AppHandle, steam_active_user_id: String) -> String {
let shortcuts_path = PathBuf::from(steam::get_shortcuts_path(app_handle.to_owned(), steam_active_user_id));

Expand All @@ -214,6 +224,7 @@ async fn read_shortcuts_vdf(app_handle: AppHandle, steam_active_user_id: String)
}

#[tauri::command]
/// Reads the user's localconfig.vdf file.
async fn read_localconfig_vdf(app_handle: AppHandle, steam_active_user_id: String) -> String {
let localconfig_path = PathBuf::from(steam::get_localconfig_path(app_handle.to_owned(), steam_active_user_id));

Expand Down Expand Up @@ -241,6 +252,7 @@ async fn read_localconfig_vdf(app_handle: AppHandle, steam_active_user_id: Strin
}

#[tauri::command]
/// Applies the changes the user has made.
async fn save_changes(app_handle: AppHandle, steam_active_user_id: String, current_art: String, original_art: String, shortcuts_str: String, shortcut_ids_str: String, shortcut_icons: Map<String, Value>, original_shortcut_icons: Map<String, Value>) -> String {
let shortcut_ids: Vec<String> = shortcut_ids_str.split(", ").map(| appid | {
return appid.to_owned();
Expand Down Expand Up @@ -312,6 +324,7 @@ async fn save_changes(app_handle: AppHandle, steam_active_user_id: String, curre
}

#[tauri::command]
/// Writes the user's shortcuts.vdf file.
async fn write_shortcuts(app_handle: AppHandle, steam_active_user_id: String, shortcuts_str: String) -> bool {
logger::log_to_file(app_handle.to_owned(), "Writing shortcuts.vdf...", 0);
let shortcuts_vdf_path: PathBuf = PathBuf::from(steam::get_shortcuts_path(app_handle.to_owned(), steam_active_user_id));
Expand All @@ -329,6 +342,7 @@ async fn write_shortcuts(app_handle: AppHandle, steam_active_user_id: String, sh
}

#[tauri::command]
/// Downloads a file from a url.
async fn download_grid(app_handle: AppHandle, grid_url: String, dest_path: String) -> bool {
logger::log_to_file(app_handle.to_owned(), format!("Downloading grid from {} to {}", grid_url, dest_path).as_str(), 0);

Expand All @@ -348,6 +362,7 @@ async fn download_grid(app_handle: AppHandle, grid_url: String, dest_path: Strin
}
}

/// Adds the user's steam directory to Tauri FS and Asset scope.
fn add_steam_to_scope(app_handle: &AppHandle) {
let steam_path = get_steam_root_dir();

Expand All @@ -372,6 +387,8 @@ fn add_steam_to_scope(app_handle: &AppHandle) {
}
}


/// This app's main function.
fn main() {
tauri::Builder::default()
.invoke_handler(tauri::generate_handler![
Expand Down
19 changes: 19 additions & 0 deletions src-tauri/src/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,17 @@ pub struct Reader<'a> {

#[allow(dead_code)]
impl Reader<'_> {
/// Gets the underlying data of the reader.
pub fn get_data(&self) -> &[u8] { return self.data; }
/// Gets the offset of the reader.
pub fn get_offset(&self) -> usize { return self.offset; }
/// Gets the length of the reader.
pub fn get_length(&self) -> u64 { return self.length; }

/// 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 }; }

/// 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 {
self.offset = offset;
Expand All @@ -117,8 +122,10 @@ impl Reader<'_> {
}
}

/// Gets the remaining length of the buffer.
pub fn remaining(&mut self) -> u64 { return self.length - (self.offset as u64); }

/// Data reading interface.
fn read_i<T: HasByteConvert>(&mut self, endianness: bool) -> T {
if endianness {
return T::from_le_bytes(self.data, self.offset);
Expand All @@ -127,63 +134,75 @@ impl Reader<'_> {
}
}

/// Reads the next char from the buffer.
pub fn read_char(&mut self, endianness: bool) -> char {
return self.read_uint8(endianness) as char;
}

/// Reads the next 8 bit unsigned int from the buffer.
pub fn read_uint8(&mut self, endianness: bool) -> u8 {
let res = self.read_i::<u8>(endianness);
self.offset += 1;
return res;
}
/// Reads the next 16 bit unsigned int from the buffer.
pub fn read_uint16(&mut self, endianness: bool) -> u16 {
let res = self.read_i::<u16>(endianness);
self.offset += 2;
return res;
}
/// Reads the next 32 bit unsigned int from the buffer.
pub fn read_uint32(&mut self, endianness: bool) -> u32 {
let res = self.read_i::<u32>(endianness);
self.offset += 4;
return res;
}
/// Reads the next 64 bit unsigned int from the buffer.
pub fn read_uint64(&mut self, endianness: bool) -> u64 {
let res = self.read_i::<u64>(endianness);
self.offset += 8;
return res;
}

/// Reads the next 8 bit signed int from the buffer.
pub fn read_int8(&mut self, endianness: bool) -> i8 {
let res = self.read_i::<i8>(endianness);
self.offset += 1;
return res;
}
/// Reads the next 16 bit signed int from the buffer.
pub fn read_int16(&mut self, endianness: bool) -> i16 {
let res = self.read_i::<i16>(endianness);
self.offset += 2;
return res;
}
/// Reads the next 32 bit signed int from the buffer.
pub fn read_int32(&mut self, endianness: bool) -> i32 {
let res = self.read_i::<i32>(endianness);
self.offset += 4;
return res;
}
/// Reads the next 64 bit signed int from the buffer.
pub fn read_int64(&mut self, endianness: bool) -> i64 {
let res = self.read_i::<i64>(endianness);
self.offset += 8;
return res;
}

/// Reads the next 32 bit float from the buffer.
pub fn read_float32(&mut self, endianness: bool) -> f32 {
let res = self.read_i::<f32>(endianness);
self.offset += 4;
return res;
}
/// Reads the next 64 bit float from the buffer.
pub fn read_float64(&mut self, endianness: bool) -> f64 {
let res = self.read_i::<f64>(endianness);
self.offset += 8;
return res;
}

/// Reads the next string from the buffer, using the provided length or reading till next 00 byte.
pub fn read_string(&mut self, length: Option<u32>) -> String {
let mut len: usize = 0;

Expand Down
5 changes: 5 additions & 0 deletions src-tauri/src/shortcuts_vdf_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use crate::reader::Reader;
use crate::vdf_reader::read_entry_map;
use crate::writer::Writer;

/// Opens the shortcuts.vdf file and returns the values as JSON.
pub fn open_shortcuts_vdf(path: &PathBuf) -> Value {
let mut file = fs::File::open(path).expect("Path should have existed.");

Expand All @@ -20,6 +21,7 @@ pub fn open_shortcuts_vdf(path: &PathBuf) -> Value {
return read(&mut reader);
}

/// Reads the shortcuts.vdf file and returns the values as JSON.
fn read(reader: &mut Reader) -> Value {
reader.seek(1, 0);

Expand All @@ -32,6 +34,7 @@ fn read(reader: &mut Reader) -> Value {
return Value::Object(read_entry_map(reader));
}

/// Writes the shortcuts.vdf file from JSON.
pub fn write_shortcuts_vdf(path: &PathBuf, data: Value) -> bool {
if data.is_object() {
let shortcuts = data.as_object().expect("Should have been able to convert to an object.");
Expand All @@ -57,6 +60,7 @@ pub fn write_shortcuts_vdf(path: &PathBuf, data: Value) -> bool {
}
}

/// Writes a shortcuts.vdf entry map from JSON.
fn write_entry_map(writer: &mut Writer, map: &Map<String, Value>) {
for (key, val) in map.into_iter() {
write_entry_field(writer, key, val);
Expand All @@ -65,6 +69,7 @@ fn write_entry_map(writer: &mut Writer, map: &Map<String, Value>) {
writer.write_uint8(0x08, true);
}

/// Writes a shortcuts.vdf entry field from JSON.
fn write_entry_field(writer: &mut Writer, key: &String, field: &Value) {
let key_owned: String = key.to_owned();

Expand Down
Loading

0 comments on commit 226a687

Please sign in to comment.