Disallow indexing (#320)

* Remove indexing

* Introduce replace_with_dosdevices

* Remove all direct indexing
This commit is contained in:
Philip Kristoffersen
2023-01-08 11:58:50 +01:00
committed by GitHub
parent 7b9966b4ff
commit 7dfbc01ed2
12 changed files with 117 additions and 98 deletions
+5
View File
@@ -68,3 +68,8 @@ sqlite = "^0.30.3"
[features]
# This feature is enabled when building for a flatpak environment
flatpak = []
#[profile.release]
#codegen-units = 1
#opt-level = "z" # Optimize for size.
#lto = true
+1
View File
@@ -1,6 +1,7 @@
#![deny(clippy::unwrap_in_result)]
#![deny(clippy::get_unwrap)]
#![deny(clippy::unwrap_used)]
#![deny(clippy::indexing_slicing)]
mod config;
mod migration;
+19 -19
View File
@@ -67,25 +67,10 @@ fn get_manifest_item(dir_entry: DirEntry, _path: Option<PathBuf>) -> Option<Mani
{
if let Ok(mut item) = serde_json::from_reader::<_, ManifestItem>(reader) {
if let Some(compat_folder) = _path {
//Strip off the c:\\
item.manifest_location = compat_folder
.join("pfx")
.join("dosdevices")
.join(item.manifest_location[0..2].to_lowercase())
.join(item.manifest_location[3..].replace('\\', "/"))
.to_string_lossy()
.to_string();
item.install_location = compat_folder
.join("pfx")
.join("dosdevices")
.join(item.install_location[0..2].to_lowercase())
.join(item.install_location[3..].replace('\\', "/"))
.to_string_lossy()
.to_string();
item.manifest_location =
replace_with_dosdevices(&compat_folder, &item.manifest_location);
item.install_location =
replace_with_dosdevices(&compat_folder, &item.install_location);
return Some(item);
}
}
@@ -99,6 +84,21 @@ fn get_manifest_item(dir_entry: DirEntry, _path: Option<PathBuf>) -> Option<Mani
None
}
fn replace_with_dosdevices(compat_folder: &Path, location: &str) -> String {
let drive = location.get(0..2).map(|drive| drive.to_lowercase());
let rest_path = location.get(3..).map(|rest| rest.replace('\\', "/"));
if let (Some(drive), Some(rest_path)) = (drive, rest_path) {
let path_buf = compat_folder
.join("pfx")
.join("dosdevices")
.join(drive)
.join(rest_path);
path_buf.to_string_lossy().to_string()
} else {
location.to_string()
}
}
//Commented out because it will change from machine to machine
// #[cfg(test)]
// pub mod test{
+1
View File
@@ -65,6 +65,7 @@ mod tests {
//Okay to unwrap in tests
#![allow(clippy::unwrap_in_result)]
#![allow(clippy::unwrap_used)]
#![allow(clippy::indexing_slicing)]
use super::*;
+2
View File
@@ -10,6 +10,8 @@ pub fn parse_lutris_games(input: &str) -> Vec<LutrisGame> {
#[cfg(test)]
mod tests {
#![allow(clippy::indexing_slicing)]
use super::*;
#[test]
+1 -1
View File
@@ -54,7 +54,7 @@ pub fn load_setting_sections() -> eyre::Result<HashMap<String, String>> {
for line in lines {
if line.starts_with('[') && line.ends_with(']') {
add_sections(&current_section_name, &current_section_lines, &mut result);
current_section_name = Some(line[1..line.len() - 1].to_string());
current_section_name = line.get(1..line.len() - 1).map(|s| s.to_string());
current_section_lines.clear();
} else {
current_section_lines.push(line.to_string());
+12 -10
View File
@@ -272,12 +272,9 @@ fn get_namespace_keys<S: AsRef<str>>(steamid: S, db: &mut DB) -> HashSet<String>
fn get_namespaces(db: &mut DB, key_bytes: &[u8]) -> Option<Vec<(i32, String)>> {
match db.get(key_bytes) {
Some(got) => {
let collection_bytes = got.as_slice();
let collectin_str = String::from_utf8_lossy(collection_bytes)[1..].to_string();
let collection = serde_json::from_str(&collectin_str).unwrap_or_default();
Some(collection)
}
Some(got) => String::from_utf8_lossy(got.as_slice())
.get(1..)
.and_then(|s| serde_json::from_str(s).ok()),
_ => None,
}
}
@@ -329,7 +326,7 @@ fn serialize_collection_value<S: AsRef<str>>(name: S, game_ids: &[usize]) -> Str
fn name_to_key<S: AsRef<str>>(name: S) -> String {
let base64 = base64::encode(name.as_ref());
let base64_no_end = if base64.ends_with("==") {
&base64[..base64.len() - 2]
base64.get(..base64.len() - 2).unwrap_or_default()
} else {
&base64
};
@@ -368,17 +365,22 @@ pub fn write_vdf_collection_to_string<S: AsRef<str>>(
let key = "\t\"user-collections\"\t\t";
if let Some(start_index) = input.find_substring(key) {
let start_index_plus_key = start_index + key.len();
if let Some(line_index) = input[start_index_plus_key..].find('\n') {
if let Some(line_index) = input.get(start_index_plus_key..).and_then(|i| i.find('\n')) {
let end_index_in_full = line_index + start_index_plus_key;
if let (Some(before), Some(after)) = (
input.get(..start_index_plus_key),
input.get(end_index_in_full..),
) {
let result = format!(
"{}{}{}",
&input[..start_index_plus_key],
before,
encoded_json,
&input[end_index_in_full..]
after
);
return Some(result);
}
}
}
None
}
+12 -12
View File
@@ -40,12 +40,13 @@ fn get_install_folders(settings: &SteamSettings) -> Vec<PathBuf> {
if let Ok(vdf_file) = std::fs::read_to_string(vdf_path) {
for line in vdf_file.lines() {
if line.contains("\"path\"") {
let path_string = &line[11..line.len() - 1];
if let Some(path_string) = line.get(11..line.len() - 1) {
result.push(Path::new(&path_string).join("steamapps").to_path_buf());
}
}
}
}
}
result
}
@@ -63,19 +64,18 @@ fn parse_manifest_file(path: &Path) -> Option<SteamGameInfo> {
fn parse_manifest_string<S: AsRef<str>>(string: S) -> Option<SteamGameInfo> {
let mut lines = string.as_ref().lines();
let app_id_line = lines.find(|l| l.contains("\"appid\""));
let name_line = lines.find(|l| l.contains("\"name\""));
match (app_id_line, name_line) {
(Some(app_id_line), Some(name_line)) => {
let appid = app_id_line[11..app_id_line.len() - 1].to_string().parse();
match appid {
Ok(appid) => Some(SteamGameInfo {
name: name_line[10..name_line.len() - 1].to_string(),
let appid: Option<u32> = lines
.find(|l| l.contains("\"appid\""))
.and_then(|line| line.get(11..line.len() - 1))
.and_then(|app_id_str| app_id_str.parse().ok());
let name_line = lines
.find(|l| l.contains("\"name\""))
.and_then(|line| line.get(10..line.len() - 1));
match (appid, name_line) {
(Some(appid), Some(name)) => Some(SteamGameInfo {
name: name.to_string(),
appid,
}),
Err(_) => None,
}
}
_ => None,
}
}
+11 -6
View File
@@ -30,7 +30,8 @@ fn enable_proton_games<S: AsRef<str>, B: AsRef<str>>(vdf_content: S, games: &[B]
};
let proton_replace_string = include_str!("proton_string.txt");
let section_str = &vdf_content[section_info.start..section_info.append_end];
let section_str = vdf_content.get(section_info.start..section_info.append_end);
if let Some(section_str) = section_str {
let games_strings_to_add = games
.iter()
.filter(|g| {
@@ -49,9 +50,12 @@ fn enable_proton_games<S: AsRef<str>, B: AsRef<str>>(vdf_content: S, games: &[B]
}
new_section.push_str(&section_info.end_key);
let before_section = &vdf_content[..section_info.start];
let after_section = &vdf_content[section_info.end..];
if let Some(before_section) = vdf_content.get(..section_info.start) {
if let Some(after_section) = vdf_content.get(section_info.end..) {
return format!("{}{}{}", before_section, new_section, after_section);
}
}
}
} else {
//TODO make this an error instead?
println!("Could not find proton section in steam, try to manually set proton on at least one game and then rerun");
@@ -72,14 +76,14 @@ fn find_indexes<S: AsRef<str>>(vdf_content: S) -> Option<SectionInfo> {
let vdf_content = vdf_content.as_ref();
if let Some(compat_index) = vdf_content.find_substring(compat_key) {
let compat_index = compat_index + compat_key.len();
let after_key = vdf_content[compat_index..].to_string();
if let Some(base_indentation) = after_key.find('{') {
let after_key = vdf_content.get(compat_index..);
if let Some(base_indentation) = after_key.and_then(|k| k.find('{')) {
let mut end_key = "\n".to_string();
for _i in 0..base_indentation {
end_key.push('\t');
}
end_key.push('}');
if let Some(end_index) = after_key.as_str().find_substring(&end_key) {
if let Some(end_index) = after_key.and_then(|a| a.find_substring(&end_key)) {
return Some(SectionInfo {
start: compat_index,
end: compat_index + end_index + end_key.len(),
@@ -100,6 +104,7 @@ mod tests {
//Okay to unwrap in tests
#![allow(clippy::unwrap_in_result)]
#![allow(clippy::unwrap_used)]
#![allow(clippy::indexing_slicing)]
use super::*;
#[test]
+7 -9
View File
@@ -46,15 +46,15 @@ impl<'a> CachedSearch<'a> {
}
println!("Searching for {}", query.as_ref());
let search = self.client.search(query.as_ref()).await?;
if search.is_empty() {
return Ok(None);
}
let first_item = &search[0];
let assumed_id = first_item.id;
let first_id = search.get(0).map(|f| f.id);
match first_id {
Some(assumed_id) => {
self.search_map.insert(app_id, (query.into(), assumed_id));
Ok(Some(assumed_id))
}
None => Ok(None),
}
}
}
fn get_search_map() -> SearchMap {
@@ -62,9 +62,7 @@ fn get_search_map() -> SearchMap {
if path.exists() {
std::fs::read_to_string(path)
.ok()
.and_then(|string| {
serde_json::from_str::<SearchMap>(&string).ok()
})
.and_then(|string| serde_json::from_str::<SearchMap>(&string).ok())
.unwrap_or_default()
} else {
SearchMap::new()
+10 -5
View File
@@ -242,10 +242,15 @@ async fn search_for_images_to_download<T: SearchSettings>(
.await;
match image_search_result {
Ok(images) => {
let images = images
.iter()
.enumerate()
.map(|(index, image)| (image, shortcuts[index], image_ids[index]));
let images = images.iter().enumerate().filter_map(|(index, image)| {
if let (Some(shortcut), Some(image_id)) =
(shortcuts.get(index), image_ids.get(index))
{
Some((image, shortcut, image_id))
} else {
None
}
});
let download_for_this_type = stream::iter(images)
.filter_map(|(image, shortcut, game_id)| {
let extension = image
@@ -257,7 +262,7 @@ async fn search_for_images_to_download<T: SearchSettings>(
async move {
let image_url = match image {
Ok(img) => Some(img.url.clone()),
Err(_) => get_steam_image_url(game_id, &image_type).await,
Err(_) => get_steam_image_url(*game_id, &image_type).await,
};
image_url.map(|url| ToDownload {
path,
+6 -6
View File
@@ -21,11 +21,11 @@ pub fn render_user_select<'a>(
}
let id_now = selected_user.user_id.clone();
if !id_before.eq(&id_now) {
return Some(selected_user);
}
} else if !steam_users.is_empty() {
return Some(&steam_users[0]);
}
Some(selected_user)
} else {
None
}
} else {
return steam_users.get(0);
}
}